public void Compile() { CEnv env = new CEnv(globals, functions); Generator gen = new Generator(); String mainLabel = env.GetFun("main"); foreach (VarDecl varDecl in globals.Values) { env.CompileAndDeclare(gen, varDecl); } gen.Emit(new CALL(0, mainLabel)); gen.Emit(Instruction.STOP); foreach (FunDecl funDecl in functions.Values) { funDecl.Compile(new CEnv(env), gen); } gen.PrintCode(); int[] bytecode = gen.ToBytecode(); using (TextWriter wr = new StreamWriter("a.out")) { foreach (int b in bytecode) { wr.Write(b); wr.Write(" "); } } }
public CEnv(CEnv env) { this.globals = env.globals; this.functions = env.functions; locals = new Stack <LocalsLocation>(); nextOffset = 0; }
public override void Compile(CEnv env, Generator gen) { e.Compile(env, gen); gen.Emit(Instruction.READ); gen.Emit(Instruction.STI); gen.Emit(new INCSP(-1)); }
public override void Compile(CEnv env, Generator gen) { env.PushEnv(); foreach (Statement stmt in statements) { stmt.Compile(env, gen); } gen.Emit(new INCSP(-env.MostLocalSize)); env.PopEnv(); }
public override void Compile(CEnv env, Generator gen) { e1.Compile(env, gen); e2.Compile(env, gen); switch (op) { case Operator.Add: gen.Emit(Instruction.ADD); break; case Operator.Div: gen.Emit(Instruction.DIV); break; case Operator.Mul: gen.Emit(Instruction.MUL); break; case Operator.Sub: gen.Emit(Instruction.SUB); break; case Operator.Eq: gen.Emit(Instruction.EQ); break; case Operator.Ne: gen.Emit(Instruction.EQ); gen.Emit(Instruction.NOT); break; case Operator.Ge: gen.Emit(Instruction.LT); gen.Emit(Instruction.NOT); break; case Operator.Gt: gen.Emit(Instruction.SWAP); gen.Emit(Instruction.LT); break; case Operator.Le: gen.Emit(Instruction.SWAP); gen.Emit(Instruction.LT); gen.Emit(Instruction.NOT); break; case Operator.Lt: gen.Emit(Instruction.LT); break; default: throw new Exception("Unknown binary operator: " + op); } }
public void Compile(CEnv env, Generator gen) { env.PushEnv(); foreach (VarDecl parameter in parameters) { env.DeclareLocal(parameter); } gen.Label(env.GetFun(name)); body.Compile(env, gen); gen.Emit(new RET(parameters.Length - 1)); env.PopEnv(); }
public override void Compile(CEnv env, Generator gen) { int n = arguments.Length; for (int i = 0; i < n; i++) { arguments[i].Compile(env, gen); } String funLabel = env.GetFun(funName); gen.Emit(new CALL(n, funLabel)); }
public override void Compile(CEnv env, Generator gen) { // startLab: <condition>; IFZERO endLab; <body>; GOTO startLab; endLab: String startLab = Label.Fresh(); String endLab = Label.Fresh(); gen.Label(startLab); condition.Compile(env, gen); gen.Emit(new IFZERO(endLab)); body.Compile(env, gen); gen.Emit(new GOTO(startLab)); gen.Label(endLab); }
public override void Compile(CEnv env, Generator gen) { // <condition>; IFZERO falseLab; <thenStmt>; GOTO endLab; // falseLab: <elseStmt>; endLab: String falseLabel = Label.Fresh(); String endLabel = Label.Fresh(); condition.Compile(env, gen); gen.Emit(new IFZERO(falseLabel)); thenStmt.Compile(env, gen); gen.Emit(new GOTO(endLabel)); gen.Label(falseLabel); elseStmt.Compile(env, gen); gen.Label(endLabel); }
public override void Compile(CEnv env, Generator gen) { e1.Compile(env, gen); switch (op) { case Operator.Neg: gen.Emit(new CSTI(0)); gen.Emit(Instruction.SWAP); gen.Emit(Instruction.SUB); break; case Operator.Not: gen.Emit(Instruction.NOT); break; case Operator.WriteI: gen.Emit(Instruction.PRINTI); break; default: throw new Exception("Unknown unary operator: " + op); } }
public override void Compile(CEnv env, Generator gen) { type.CompileAllocation(gen); // Generate code env.DeclareLocal(this); // Add to compilation environment }
// Compiling e1[e2] is done by compiling e1 and e2 as rvalue and adding them: public override void CompileLvalue(CEnv env, Generator gen) { e1.Compile(env, gen); e2.Compile(env, gen); gen.Emit(Instruction.ADD); }
// Compiling *e as lvalue is the same as compiling e as rvalue: public override void CompileLvalue(CEnv env, Generator gen) { e.Compile(env, gen); }
public override void CompileLvalue(CEnv env, Generator gen) { env.CompileVariableAccess(gen, name); }
public override void Compile(CEnv env, Generator gen) { CompileLvalue(env, gen); gen.Emit(Instruction.LDI); }
public abstract void CompileLvalue(CEnv env, Generator gen);
public override void Compile(CEnv env, Generator gen) { gen.Emit(new CSTI(value)); }
public override void Compile(CEnv env, Generator gen) { lhs.Compile(env, gen); rhs.Compile(env, gen); gen.Emit(Instruction.STI); }
public override void Compile(CEnv env, Generator gen) { e.Compile(env, gen); gen.Emit(new INCSP(-1)); }
abstract public void Compile(CEnv env, Generator gen);