abstract syntax tree - How to go from AST to backend code? -
i'm working on compiler programming language , have representation of source language in form of abstract syntax tree (ast). tried generating backend code directly traversing ast, that's not working well. in general, think of mapping ast c-like language down assembly-like language. think there phase i'm missing go ast backend code. problem going ast next representation (say, 3-address code) rought. feels there step in between i'm missing.
[source lang] -> [lex/parse]-> [ast] -> [semantic analysis] -> [?] -> [backend code]
this i've come while thinking it:
1) transform ast of source language ast represents backend. means need have 2 different asts. then, output backend transformed ast.
2) transform ast different type of data structure, , generated backend code based on other structure. i'm not sure other struct be.
3) traverse ast in different way (from way used pretty print it) generate backend code. how tried doing first doesn't seem right; feels hackish way go it.
what options go ast backend code?
note i'm not asking kinds of representations ast turned into, such 3-address code. example, understand c addition so:
x = + b + c
could turned 3-address so:
t1 = add a, b t2 = add t1, c set x, t2
i asking techniques/guidance/experience on how it.
to give example of type of answer i'm looking be:
question: steps can take perform semantic analysis on source lang?
answer: parse language ast , traverse ast perform semantic checks.
one can represent program way 1 likes; can build compilers using trees. purpose of representation make easy collect kinds of facts; representation serves make collection/processing of specific facts easier. 1 expects representation of program change @ different stages of compilation, depending on compiler trying achieve in stage.
one typical scheme translate programs through these representations produce final code:
* asts * triples * abstract machine instructions * concrete machine instructions
the fact don't seem know this, means haven't done homework. pretty described in compiler books. go read one.
Comments
Post a Comment