José Romildo Malaquias
2012-Nov-14 21:47 UTC
[LLVMdev] Generating code for Sequence expression
Hello. In order to practice compiler writing, I am implementing a simple compiler for the Tiger language. I am using llvm IR for the intermediate representation. Regarding llvm, I have started by reading the OCaml version of the LLVM Tutorial, where a compiler for the kaleidoscope language is built. The language I am implementing has a form of expression called "sequence expression". It is made up of a list of expressions: ( e1; e2; ... ; en ) To evaluate the sequence expression, each expression ei in the list is evaluated in the order they appear, and the last one en gives the final result. How can the sequence expression be translated to the llvm IR? After translating each expression ei from the sequence, how would the resulting values be combined to evaluate in sequence? If a more realistic scenario is needed, consider it as a new construction from the kaleidoscope language used in the tutorial. Romildo
Hi José,> ( e1; e2; ... ; en ) > > To evaluate the sequence expression, each expression ei in the list is > evaluated in the order they appear, and the last one en gives the final > result. > > How can the sequence expression be translated to the llvm IR?Your language probably only cares about the visible effects of the evaluation order. For example, ordering of volatile loads/stores and the sequence of function calls involved (and even that, only to a certain extent). If so, you just need to generate code for the expressions one after the other and let LLVM take care of which optimisations it can perform that preserve the semantics.> After translating each expression ei from the sequence, how would the > resulting values be combined to evaluate in sequence?The "combination" would be not even bothering to record the value of the first expressions (e1, ... e_{n-1}), but returning the value of the last expression as the final result. Tim.
Krzysztof Parzyszek
2012-Nov-14 22:36 UTC
[LLVMdev] Generating code for Sequence expression
On 11/14/2012 3:47 PM, José Romildo Malaquias wrote:> > The language I am implementing has a form of expression called "sequence > expression". It is made up of a list of expressions: > > ( e1; e2; ... ; en ) > > To evaluate the sequence expression, each expression ei in the list is > evaluated in the order they appear, and the last one en gives the final > result.By the way, that is what the , (comma) operator in C/C++ does. -Krzysztof -- Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation