evaluate method

  1. @override
void evaluate(
  1. SymbolTable table
)
override

Evaluates the function declaration by generating the complete LLVM function definition.

This method performs the complete function compilation process:

  1. Registers the function in the global symbol table
  2. Generates the LLVM function signature with parameters
  3. Sets up parameter handling (alloca and store for non-arrays, direct for arrays)
  4. Evaluates the function body in a new scope
  5. Generates the return instruction with default value

Parameters:

  • table: The global symbol table for function registration

The generated LLVM IR includes proper parameter handling where each parameter gets its own stack allocation and store operation, allowing the function body to modify parameter values without affecting the caller's variables.

Implementation

@override
void evaluate(SymbolTable table) {
  final funcName = identifier.nodeValue;
  final returnType = this.returnType.nodeValue;
  final func = LangFunc(funcName, this);
  SymbolTable.createFunction(funcName, func);
  final String paramsStr = params.map((e) => "${e.type.nodeValue.irType} %${e.identifier.nodeValue} ").join(", ");
  Node.addIrLine("define ${returnType.irType} @$funcName($paramsStr) {");
  Node.addIrlLabel("entry");
  final newTable = SymbolTable();
  for (var param in params) {
    if (param.type.nodeValue is ArrayType) {
      newTable.create(param.identifier.nodeValue, LangVar("%${param.identifier.nodeValue}", param.type.nodeValue));
      continue;
    }

  final ptrName = "%ptr.${param.identifier.nodeValue}.$id";
    Node.addIrLine("$ptrName = alloca ${param.type.nodeValue.irType}");
    Node.addIrLine("store ${param.type.nodeValue.irType} %${param.identifier.nodeValue}, ptr $ptrName");
    newTable.create(param.identifier.nodeValue, LangVar(ptrName, param.type.nodeValue));
  }
  block.evaluate(newTable);
  Node.addIrLine("ret ${returnType.irType} ${returnType.primitiveType == PrimitiveTypes.int ? "0" : "0.0"}");
  Node.endIrLabel();
  Node.addIrLine("}");
}