Esempio n. 1
0
        public override void OnCodeGenVisit(AssemblyGenerator assembler)
        {
            Right.OnCodeGenVisit(assembler);
            //assembler.EmitCode($"\tmovl %eax,%edx");
            assembler.EmitCode($"\tpushl %eax");
            Left.OnCodeGenVisit(assembler);
            assembler.EmitCode($"\tpopl %edx");
            ReturnType leftType = Left.GetReturnType(), rightType = Right.GetReturnType();
            string     postfix = "", regA = "", regB = "";

            switch (Cast(leftType, rightType))
            {
            case ReturnType.Char:
                postfix = "b";
                regA    = "%al";
                regB    = "%dl";
                Return  = ReturnType.Char;
                break;

            case ReturnType.Float:
                postfix = "l";
                regA    = "%eax";
                regB    = "%edx";
                Return  = ReturnType.Float;
                break;

            case ReturnType.Int:
                postfix = "l";
                regA    = "%eax";
                regB    = "%edx";
                Return  = ReturnType.Int;
                break;
            }
            switch (this.Operator)
            {
            case BinaryOperator.Plus:
                assembler.EmitCode($"\tadd{postfix} {regB},{regA}");
                break;

            case BinaryOperator.Minus:
                assembler.EmitCode($"\tsub{postfix} {regB},{regA}");
                break;

            case BinaryOperator.Multiply:
                assembler.EmitCode($"\timul{postfix} {regB},{regA}");
                break;

            case BinaryOperator.Divide:
                assembler.EmitCode($"\tpushl %edx");
                assembler.EmitCode($"\tcltd");
                assembler.EmitCode($"\tidivl (%esp)");
                assembler.EmitCode($"\taddl $4,%esp");
                break;

            case BinaryOperator.And:
                assembler.EmitCode($"\tcmp{postfix} $0,{regB}");
                assembler.EmitCode($"\tjz LGF{LogicCount}");
                assembler.EmitCode($"\tcmp{postfix} $0,{regA}");
                assembler.EmitCode($"\tjz LGF{LogicCount}");
                assembler.EmitCode($"\tmovl $1,%eax");
                assembler.EmitCode($"\tjmp LGE{LogicCount}");
                assembler.EmitCode($"LGF{LogicCount}:");
                assembler.EmitCode($"\tmovl $0,%eax");
                assembler.EmitCode($"LGE{LogicCount}:");
                LogicCount++;
                break;

            case BinaryOperator.Or:
                assembler.EmitCode($"\tcmp{postfix} $0,{regB}");
                assembler.EmitCode($"\tjnz LGT{LogicCount}");
                assembler.EmitCode($"\tcmp{postfix} $0,{regA}");
                assembler.EmitCode($"\tjnz LGT{LogicCount}");
                assembler.EmitCode($"\tmovl $0,%eax");
                assembler.EmitCode($"\tjmp LGE{LogicCount}");
                assembler.EmitCode($"LGT{LogicCount}:");
                assembler.EmitCode($"\tmovl $1,%eax");
                assembler.EmitCode($"LGE{LogicCount}:");
                LogicCount++;
                break;

            case BinaryOperator.Equal:
                assembler.EmitCode($"\tcmp{postfix} {regB},{regA}");
                assembler.EmitCode($"\tsete %al");
                assembler.EmitCode($"\tmovzbl %al,%eax");
                break;

            case BinaryOperator.GreaterEqual:
                assembler.EmitCode($"\tcmp{postfix} {regB},{regA}");
                assembler.EmitCode($"\tsetge %al");
                if (postfix == "l")
                {
                    assembler.EmitCode($"\tmovzbl %al,%eax");
                }
                break;

            case BinaryOperator.GreaterThan:
                assembler.EmitCode($"\tcmp{postfix} {regB},{regA}");
                assembler.EmitCode($"\tsetg %al");
                if (postfix == "l")
                {
                    assembler.EmitCode($"\tmovzbl %al,%eax");
                }
                break;

            case BinaryOperator.LessEqual:
                assembler.EmitCode($"\tcmp{postfix} {regB},{regA}");
                assembler.EmitCode($"\tsetle %al");
                if (postfix == "l")
                {
                    assembler.EmitCode($"\tmovzbl %al,%eax");
                }
                break;

            case BinaryOperator.LessThan:
                assembler.EmitCode($"\tcmp{postfix} {regB},{regA}");
                assembler.EmitCode($"\tsetl %al");
                if (postfix == "l")
                {
                    assembler.EmitCode($"\tmovzbl %al,%eax");
                }
                break;

            case BinaryOperator.NotEqual:
                assembler.EmitCode($"\tcmp{postfix} {regB},{regA}");
                assembler.EmitCode($"\tsetne %al");
                if (postfix == "l")
                {
                    assembler.EmitCode($"\tmovzbl %al,%eax");
                }
                break;
            }
        }
Esempio n. 2
0
 public override void OnCodeGenVisit(AssemblyGenerator assembler)
 {
     // This will not be visited
 }
Esempio n. 3
0
 public override void OnCodeGenVisit(AssemblyGenerator assembler)
 {
     Expression.OnCodeGenVisit(assembler);
 }
Esempio n. 4
0
 public override void OnCodeGenVisit(AssemblyGenerator assembler)
 {
     throw new NotImplementedException();
 }