Exemplo n.º 1
0
        public List <Instruction> CodeGen(int truejump, int falsejump)
        {
            var b = new List <Instruction>();

            var f1 = S1.AsDirectField() ?? (S1.IsConstant() ? FieldSRef.Imm1() : null);

            if (f1 == null)
            {
                f1 = FieldSRef.ScratchInt();
                b.AddRange(S1.FetchToField(f1));
            }

            var f2 = S2.AsDirectField() ?? (S2.IsConstant() ? FieldSRef.Imm2() : null);

            if (f2 == null)
            {
                f2 = FieldSRef.ScratchInt();
                b.AddRange(S2.FetchToField(f2));
            }

            b.Add(new Instruction
            {
                opcode = Opcode.Branch,
                op1    = f1,
                imm1   = S1.IsConstant() ? new IntSExpr(S1.Evaluate()) : null,
                op2    = f2,
                imm2   = S2.IsConstant() ? new IntSExpr(S2.Evaluate()) : null,
                rjmpeq = this.Op.HasFlag(CompSpec.Equal) ? truejump : falsejump,
                rjmplt = this.Op.HasFlag(CompSpec.Less) ? truejump : falsejump,
                rjmpgt = this.Op.HasFlag(CompSpec.Greater) ? truejump : falsejump,
            });

            return(b);
        }
Exemplo n.º 2
0
        public List <Instruction> CodeGen(int truejump, int falsejump)
        {
            if (Op == CompSpec.Equal)
            {
                var b = new List <Instruction>();

                b.AddRange(V1.FetchToReg(RegVRef.rScratchTab));

                var r2 = V2.AsReg();
                if (r2 == null)
                {
                    // if V2 isn't already a register, borrow rScratchInts temporarily to fetch it
                    r2 = RegVRef.rScratchInts;
                    b.Add(new Push(RegVRef.rScratchInts));
                    b.AddRange(V2.FetchToReg(r2));
                }

                b.Add(new Instruction
                {
                    opcode = Opcode.EachMulV,
                    acc    = true,
                    dest   = RegVRef.rScratchTab,
                    op1    = r2,
                    op2    = FieldSRef.Imm2(),
                    imm2   = new IntSExpr(-1),
                });

                // restore rScratchInts if needed
                if (r2 == RegVRef.rScratchInts)
                {
                    b.Add(new Pop(RegVRef.rScratchInts));
                }
                var flag = FieldSRef.ScratchInt();

                // ensure flag is clear, in case this branch is used in a loop and is coming around again to reuse it
                b.Add(new Clear(flag));

                // set flag if every=0
                b.Add(new Instruction
                {
                    opcode = Opcode.EveryEqS2S1,
                    op1    = RegVRef.rScratchTab,
                    dest   = flag,
                    acc    = true,
                });


                b.Add(new Instruction
                {
                    opcode = Opcode.Branch,
                    op1    = flag,
                    rjmpeq = falsejump,
                    rjmplt = falsejump,                     // lt shouldn't happen at all, but...
                    rjmpgt = truejump,
                });

                return(b);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Exemplo n.º 3
0
        public List <Instruction> FetchToField(FieldSRef dest)
        {
            var code = new List <Instruction>();

            var f1 = S1.AsDirectField();

            if (f1 == null)
            {
                if (S1.IsConstant())
                {
                    f1 = FieldSRef.Imm1();
                }
                else
                {
                    f1 = FieldSRef.ScratchInt();
                    code.AddRange(S1.FetchToField(f1));
                }
            }

            var f2 = S2.AsDirectField();

            if (f2 == null)
            {
                if (S2.IsConstant())
                {
                    f2 = FieldSRef.Imm2();
                }
                else
                {
                    f2 = FieldSRef.ScratchInt();
                    code.AddRange(S2.FetchToField(f2));
                }
            }

            Opcode op;

            switch (Op)
            {
            case ArithSpec.Add:
                op = Opcode.Add;
                break;

            case ArithSpec.Subtract:
                op = Opcode.Sub;
                break;

            case ArithSpec.Multiply:
                op = Opcode.Mul;
                break;

            case ArithSpec.Divide:
                op = Opcode.Div;
                break;

            default:
                throw new InvalidOperationException();
            }

            var tmp = FieldSRef.ScratchInt();

            code.Add(new Instruction
            {
                opcode = op,
                op1    = f1,
                imm1   = S1.IsConstant() ? S1 : null,
                op2    = f2,
                imm2   = S2.IsConstant() ? S2 : null,
                dest   = tmp,
                acc    = true
            });

            //TODO: optimize this one day?
            code.AddRange(dest.PutFromField(tmp));

            return(code);
        }
Exemplo n.º 4
0
        public List <Instruction> FetchToReg(RegVRef dest)
        {
            var     code = new List <Instruction>();
            RegVRef other;

            if (dest == V1.AsReg())
            {
                other = V2.AsReg();
                if (other == null)
                {
                    other = RegVRef.rScratchTab;
                    code.AddRange(V2.FetchToReg(other));
                }
            }
            else if (dest == V2.AsReg())
            {
                other = V1.AsReg();
                if (other == null)
                {
                    other = RegVRef.rScratchTab;
                    code.AddRange(V1.FetchToReg(other));
                }
            }
            else
            {
                code.AddRange(V1.FetchToReg(dest));
                other = V2.AsReg();
                if (other == null)
                {
                    other = RegVRef.rScratchTab;
                    code.AddRange(V2.FetchToReg(other));
                }
            }

            switch (Op)
            {
            case ArithSpec.Add:
                code.Add(new Instruction {
                    opcode = Opcode.EachAddV, acc = true, op1 = other, dest = dest
                });
                break;

            case ArithSpec.Subtract:
                code.Add(new Instruction {
                    opcode = Opcode.EachMulV, acc = true, op1 = other, op2 = FieldSRef.Imm2(), imm2 = new IntSExpr(-1), dest = dest
                });
                break;

            case ArithSpec.Multiply:
                code.Add(new Instruction {
                    opcode = Opcode.VMul, op1 = dest, op2 = other, dest = dest
                });
                break;

            case ArithSpec.Divide:
                code.Add(new Instruction {
                    opcode = Opcode.VDiv, op1 = dest, op2 = other, dest = dest
                });
                break;

            default:
                throw new NotImplementedException();
            }

            return(code);
        }