ScEpTIC.AST.builtins package

Submodules

ScEpTIC.AST.builtins.builtin module

class ScEpTIC.AST.builtins.builtin.Builtin

Bases: ScEpTIC.AST.elements.instruction.Instruction

Base object to be extended for builtin functions. HOW TO:

  1. create the new instruction to be executed

  2. Implement the get_val() method, returning the result of the instruction.

    Arguments can be obatined using the list self.args (index = index of arg), their values will be automatically resolved on runtime.

  3. Call the class method define_builtin on the class

  4. All the builtins will be loaded by calling link()

Example:

class LenTestInstruction(Builtin):
def get_val(self):

return len(self.args)

LenTestInstruction.define_builtin(‘let_test_instruction’, ‘i32, i32, double, float, i8’, ‘i64’)

property args

Returns the resolved value of the arguments.

builtins = []
classmethod define_builtin(name, arguments, return_type)

Define builtins, which will be loaded by the link() function.

get_uses()

Returns the registers used by this instruction as a list of strings. (used by register allocation)

get_val()

NB: must be implemented by each builtin!

Links the builtin Instructions creating a function.

replace_reg_name(old_reg_name, new_reg_name)

Replaces the name of a register used by the instruction with a new one. (used by register allocation)

set_args(args)

Sets the arguments of the builtin.

tick_count = 1

ScEpTIC.AST.builtins.linker module

class ScEpTIC.AST.builtins.linker.BuiltinLinker(name, arguments, return_type, instruction)

Bases: object

This class generates the code of a builtin function. Builtin functions are implementation of library functions (e.g. sqrt, pow) that performs some architecture-dependent operations, which doesn’t modify the control flow nor the memory (SRAM, NVM).

In case control-flow modification or memory accesses are needed, it is better to directly create some C code for them and include it in the final code to be analyzed.

Builtins are just used to not include in the final code library operations, and should not be used to emulate memory accesses, global variables accesses, direct-memory writes and other major operations, which can be easily implemented in the program code.

An example of a builtins are mathematical functions and printing functions.

The core of a builtin is an extension of the Instruction operation. This is needed to be able to analyze the code.

Normally LLVM will consider the arguments to be from virtual register %0 to %(#args - 1) and the %(#args) is the ID of the first basic block. Since the builtins should not have labels and control-flow operations, it is omitted.

Arguments are passed from %0 to %(len(args) - 1) Arguments addresses in stack are stored from %(len(args)) to %(len(args)*2 - 1) Arguments values are loaded from %(len(args) * 2) to %(len(args) * 3 - 1) Return value will be on %(len(args) * 3)

prefix = None

Module contents

Links the builtins (both library-defined and user-defined)

ScEpTIC.AST.builtins.load_libraries()

Loads the libraries of builtins