Next: , Previous: , Up: Project Layout   [Contents][Index]

3.2.28 The src/llvmtranslate Directory

Namespace llvmtranslate, delivered for TC-5. Translate the AST to LLVM intermediate code using the LLVM libraries.

File: escapes-collector.* (src/llvmtranslate/)

The FrameBuilder and the EscapesCollector.

LLVM IR doesn’t support static link and nested functions. In order to translate those functions to LLVM IR, we use Lambda Lifting, which consists in passing a pointer to the escaped variables to the nested function using that variable.

In order to do that, we need a visitor to collect these kind of variables and associate them to each function.

This visitor is the EscapesCollector.

In order for the EscapesCollector to work properly, the variables located in the function’s frame have to be excluded. The FrameBuilder is building a frame for the EscapesCollector to use.

File: libllvmtranslate.* (src/llvmtranslate/)

The interface.

File: llvm-type-visitor.* (src/llvmtranslate/)

The LLVM IR is a typed language. In order to ensure type safety, the Tiger types (type::Type) have to be translated to LLVM types (llvm::Type). In order to do that, this visitor defined in src/llvmtranslate is used to traverse the type hierarcy and translate it to LLVM types.

File: translator.hh (src/llvmtranslate/)

Implements the class ‘Translator’ which performs the LLVM IR generation using the LLVM API.

For instance, here is the translation of a ‘ast::SimpleVar’:

virtual void operator()(const SimpleVar& e)
  value_ = builder_.CreateLoad(access_var(e), e.name_get().get());
File: tiger-runtime.c (src/llvmtranslate/)

This is the specific runtime for TC-L. It is based on the original runtime, with some adaptations for LLVM.

It is compiled to LLVM IR in $(build_dir)/src/llvmtranslate/runtime.ll, then a function llvmtranslate::runtime_string() is generated in $(build_dir)/src/llvmtranslate/

This function is used by the task --llvm-runtime-display to print the runtime along the LLVM IR.


Strings are implemented as char* 0-terminated buffers, like C strings.


Most of the built-ins are just calls to the C standard library functions.


Since the type char doesn’t exist in TC, a char is nothing more than a string of length 1.

In order to avoid allocations every time a character is asked for, an array containing all the characters followed by a \0 is initialized at the beginning of the program.


The runtime initializes the one-character strings, then calls tc_main, which is the main that your compiler should have provided.

Next: , Previous: , Up: Project Layout   [Contents][Index]