Exemplo n.º 1
0
        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(" ");
                }
            }
        }
Exemplo n.º 2
0
 public CEnv(CEnv env)
 {
     this.globals   = env.globals;
     this.functions = env.functions;
     locals         = new Stack <LocalsLocation>();
     nextOffset     = 0;
 }
Exemplo n.º 3
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));
 }
Exemplo n.º 4
0
 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();
 }
Exemplo n.º 5
0
        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);
            }
        }
Exemplo n.º 6
0
 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();
 }
Exemplo n.º 7
0
        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));
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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);
            }
        }
Exemplo n.º 11
0
 public override void Compile(CEnv env, Generator gen)
 {
     type.CompileAllocation(gen); // Generate code
     env.DeclareLocal(this);      // Add to compilation environment
 }
Exemplo n.º 12
0
 // 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);
 }
Exemplo n.º 13
0
 // Compiling *e as lvalue is the same as compiling e as rvalue:
 public override void CompileLvalue(CEnv env, Generator gen)
 {
     e.Compile(env, gen);
 }
Exemplo n.º 14
0
 public override void CompileLvalue(CEnv env, Generator gen)
 {
     env.CompileVariableAccess(gen, name);
 }
Exemplo n.º 15
0
 public override void Compile(CEnv env, Generator gen)
 {
     CompileLvalue(env, gen);
     gen.Emit(Instruction.LDI);
 }
Exemplo n.º 16
0
 public abstract void CompileLvalue(CEnv env, Generator gen);
Exemplo n.º 17
0
 public override void Compile(CEnv env, Generator gen)
 {
     gen.Emit(new CSTI(value));
 }
Exemplo n.º 18
0
 public override void Compile(CEnv env, Generator gen)
 {
     lhs.Compile(env, gen);
     rhs.Compile(env, gen);
     gen.Emit(Instruction.STI);
 }
Exemplo n.º 19
0
 public override void Compile(CEnv env, Generator gen)
 {
     e.Compile(env, gen);
     gen.Emit(new INCSP(-1));
 }
Exemplo n.º 20
0
 abstract public void Compile(CEnv env, Generator gen);