Skip to content

Commit

Permalink
Merge pull request #275 from serkm/multiple-ops
Browse files Browse the repository at this point in the history
Multiple operations
  • Loading branch information
phischu authored Jul 26, 2023
2 parents cc3678a + 3cb3bf9 commit af47a43
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ define ${show(returnType)} ${globalName(name)}(${commaSeparated(parameters.map(s
"""
case Verbatim(content) => content

case GlobalVariableArray(name, IntegerType8(), ConstantArray(IntegerType8(), members)) =>
case GlobalConstant(name, ConstantArray(IntegerType8(), members)) =>
val bytes = members.map { ini => ini match {
case ConstantInteger8(b) => b
case _ => ???
}}
val escaped = bytes.map(b => "\\" + f"$b%02x").mkString;
s"@$name = private constant [${bytes.length} x i8] c\"$escaped\""

case GlobalVariableArray(name, typ, initializer) =>
C.abort(s"cannot compile non-i8 constant array: $name = [ x ${typ}] ${initializer}")
case GlobalConstant(name, initializer) =>
s"@$name = private constant ${show(initializer)}"
}

def show(basicBlock: BasicBlock)(using Context): LLVMString = basicBlock match {
Expand Down Expand Up @@ -119,7 +119,7 @@ ${indentedLines(instructions.map(show).mkString("\n"))}
case ConstantDouble(n) => s"double $n"
case ConstantAggregateZero(tpe) => s"${show(tpe)} zeroinitializer"
case ConstantNull(tpe) => s"${show(tpe)} null"
case ConstantArray(memberType, members) => s"[${members.length} x ${show(memberType)}]"
case ConstantArray(memberType, members) => s"[${members.length} x ${show(memberType)}] [${commaSeparated(members.map(show))}]"
case ConstantInteger8(b) => s"i8 $b"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,48 +124,47 @@ object Transformer {

Switch(LocalReference(IntegerType64(), tagName), defaultLabel, labels)

case machine.New(variable, List(clause), rest) =>
// TODO multiple methods (see case below)

val closureEnvironment = freeVariables(clause).toList;

val clauseName = freshName(variable.name);

defineFunction(clauseName, List(Parameter(objType, "obj"), Parameter(envType, "env"), Parameter(spType, "sp"))) {
consumeObject(LocalReference(objType, "obj"), closureEnvironment, freeVariables(clause));
loadEnvironment(initialEnvironmentPointer, clause.parameters);
eraseValues(clause.parameters, freeVariables(clause.body));
transform(clause.body);
case machine.New(variable, clauses, rest) =>
val closureEnvironment = freeVariables(clauses).toList;

val clauseNames = clauses.map { clause =>
val clauseName = freshName(variable.name);
defineFunction(clauseName, List(Parameter(objType, "obj"), Parameter(envType, "env"), Parameter(spType, "sp"))) {
consumeObject(LocalReference(objType, "obj"), closureEnvironment, freeVariables(clause));
loadEnvironment(initialEnvironmentPointer, clause.parameters);
eraseValues(clause.parameters, freeVariables(clause.body));
transform(clause.body);
}
ConstantGlobal(methodType, clauseName)
}

val arrayName = freshName(variable.name)
emit(GlobalConstant(arrayName, ConstantArray(methodType, clauseNames)))

val obj = produceObject(closureEnvironment, freeVariables(rest));
val tmpName = freshName("tmp");
emit(InsertValue(tmpName, ConstantAggregateZero(negativeType), ConstantGlobal(methodType, clauseName), 0));
emit(InsertValue(tmpName, ConstantAggregateZero(negativeType), ConstantGlobal(PointerType(), arrayName), 0));
emit(InsertValue(variable.name, LocalReference(negativeType, tmpName), obj, 1));

eraseValues(List(variable), freeVariables(rest));
transform(rest)

// TODO multiple methods (for one method see case above)
case machine.New(variable, clauses, rest) =>
???

case machine.Invoke(value, 0, values) =>
case machine.Invoke(value, tag, values) =>
shareValues(value :: values, Set());
storeEnvironment(initialEnvironmentPointer, values);

val functionName = freshName("fp");
val arrayName = freshName("arrayp");
val objName = freshName("obj");
val pointerName = freshName("fpp");
val functionName = freshName("fp");

emit(ExtractValue(functionName, transform(value), 0));
emit(ExtractValue(arrayName, transform(value), 0));
emit(ExtractValue(objName, transform(value), 1));
emit(GetElementPtr(pointerName, methodType, LocalReference(PointerType(), arrayName), List(tag)))
emit(Load(functionName, methodType, LocalReference(PointerType(), pointerName)))
emit(TailCall(LocalReference(methodType, functionName), List(LocalReference(objType, objName), initialEnvironmentPointer, getStackPointer())));
RetVoid()

// TODO What is `tag`'s meaning?
case machine.Invoke(value, tag, values) =>
???

case machine.Allocate(ref @ machine.Variable(name, machine.Type.Reference(tpe)), init, evidence, rest) =>
val idx = regionIndex(ref.tpe)

Expand Down Expand Up @@ -344,7 +343,7 @@ object Transformer {
transform(rest)

case machine.LiteralUTF8String(v@machine.Variable(bind, _), utf8, rest) =>
emit(GlobalVariableArray(s"$bind.lit", IntegerType8(), ConstantArray(IntegerType8(), utf8.map { b => ConstantInteger8(b) }.toList)))
emit(GlobalConstant(s"$bind.lit", ConstantArray(IntegerType8(), utf8.map { b => ConstantInteger8(b) }.toList)))

val res = positiveType
val args = List(ConstantInt(utf8.size), ConstantGlobal(PointerType(), s"$bind.lit"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ enum Definition {
case Function(returnType: Type, name: String, parameters: List[Parameter], basicBlocks: List[BasicBlock])
case VerbatimFunction(returnType: Type, name: String, parameters: List[Parameter], body: String)
case Verbatim(content: String)
case GlobalVariableArray(name: String, typ: Type, initializer: Operand) // initializer should be constant
case GlobalConstant(name: String, initializer: Operand) // initializer should be constant
}
export Definition.*

Expand Down

0 comments on commit af47a43

Please sign in to comment.