How to generate implicit runtime call in compiler

Hey everyone,

I am currently trying to add a runtime call to a library I have created. I am working on a system that will add some type soundness and need to check reference count variables I am keeping track of. The issue I am having is that I need the runtime call inserted before every variable assignment. Looking into the compiler I see three possible areas that I could change to make this work:

  1. Adding new AST node before the Assignment AST nodes (OP=OAS)
  2. Creating SSA value for my runtime call when generating SSA for assignments
  3. When finally generating machine code for assignments, throw in my runtime call

Ideally, I think option 1 would be best. Earlier in the chain seems like the right move. However, I have no idea how to progress in either of these options. If anyone has any input I would like to hear it. I have mainly attempted option 2 as it seemed easiest calling something along the lines of “s.newValue1A(ssa.OpStaticCall, types.TypeMem, myFunction, s.mem())” with my runtime call information but it just breaks the compiler in different ways and I don’t understand why. Specifically I’ll ask this:

  1. How to create an extra AST node and correctly insert it in the tree at that stage? or
  2. How to generate a SSA value for my runtime call?

I am new to this forum so please let me know if this is too much or if I should break it down a little more. Thanks!

go have some standard packages for analyzing Go programs

https://github.com/golang/example/tree/master/gotypes

Insert ast node to tree easy

  1. parse file with go/parser

  2. find target parent node

  3. insert node and rewrite file

here is ast parser example

https://golang.org/src/go/ast/example_test.go

With this method, the original go file will be rewritten to include the extra calls, then will need to be recompiled, correct? Is there a way to change the compiler directly, to inject this runtime call implicitly so the user does not need to know its there / do any extra steps?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.