Пример #1
0
 public override void ExitTerm1(ssuplParser.Term1Context context)
 {
     //term    :  term MULOP neg
     if (context.MULOP().GetText() == "*")
     {
         //Console.WriteLine("made it here *");
         code.Put(context,
                  code.Get(context.term()),
                  code.Get(context.neg()),
                  "pop rbx", //value from term
                  "pop rax", //value from neg
                  "imul rax,rbx",
                  "push rax"
                  );
     }
     else if (context.MULOP().GetText() == "/")
     {
         //Console.WriteLine("made it here /");
         code.Put(context,
                  code.Get(context.term()),
                  code.Get(context.neg()),
                  "pop rbx",   //value from term
                  "pop rax",   //value from neg
                  "mov rdx,0", //Must clear the divisor register
                  "idiv rbx",
                  "push rax"
                  );
     }
     else //Modulo
     {
         //Console.WriteLine("made it here /");
         code.Put(context,
                  code.Get(context.term()),
                  code.Get(context.neg()),
                  "pop rbx",   //value from term
                  "pop rax",   //value from neg
                  "mov rdx,0", //Must clear the divisor register
                  "idiv rbx",
                  "push rdx"
                  );
     }
 }
Пример #2
0
        public override void ExitTerm1(ssuplParser.Term1Context context)
        {
            //term    :  term MULOP neg
            if (type(context.term()) != type(context.neg()))
            {
                throw new Exception("Type mismatch");
            }
            typeAttr.Put(context, type(context.term()));
            switch (type(context.term()))
            {
            case VarType.INT:
                if (context.MULOP().GetText() == "*")
                {
                    //Console.WriteLine("made it here *");
                    code.Put(context,
                             code.Get(context.term()),
                             code.Get(context.neg()),
                             "pop rbx",     //value from term
                             "pop rax",     //value from neg
                             "imul rax,rbx",
                             "push rax"
                             );
                }
                else if (context.MULOP().GetText() == "/")
                {
                    //Console.WriteLine("made it here /");
                    code.Put(context,
                             code.Get(context.term()),
                             code.Get(context.neg()),
                             "pop rbx",     //value from term
                             "pop rax",     //value from neg
                             "mov rdx,0",   //Must clear the divisor register
                             "idiv rbx",
                             "push rax"
                             );
                }
                else     //Modulo
                {
                    //Console.WriteLine("made it here /");
                    code.Put(context,
                             code.Get(context.term()),
                             code.Get(context.neg()),
                             "pop rbx",     //value from term
                             "pop rax",     //value from neg
                             "mov rdx,0",   //Must clear the divisor register
                             "idiv rbx",
                             "push rdx"
                             );
                }
                break;

            case VarType.DOUBLE:
                if (context.MULOP().GetText() == "*")
                {
                    //Console.WriteLine("made it here *");
                    code.Put(context,
                             code.Get(context.term()),
                             code.Get(context.neg()),
                             "movq xmm0, [rsp]",     //pop double from stack from term
                             "add rsp,8",
                             "movq xmm1, [rsp]",     //pop double from stack from neg
                             "add rsp,8",
                             "mulsd xmm0,xmm1",
                             "sub rsp,8",
                             "movq [rsp], xmm0" //push back on to stack
                             );
                }
                else if (context.MULOP().GetText() == "/")
                {
                    //Console.WriteLine("made it here /");
                    code.Put(context,
                             code.Get(context.term()),
                             code.Get(context.neg()),
                             "movq xmm0, [rsp]",     //pop double from stack from term
                             "add rsp,8",
                             "movq xmm1, [rsp]",     //pop double from stack from neg
                             "add rsp,8",
                             "divsd xmm1,xmm0",
                             "sub rsp,8",
                             "movq [rsp], xmm1" //push back on to stack
                             );
                }
                else     //Modulo
                {
                    throw new Exception("Modulo not possible on double type");
                }
                break;

            case VarType.VEC4:
                if (context.MULOP().GetText() == "*")
                {
                    //Console.WriteLine("made it here *");
                    code.Put(context,
                             code.Get(context.term()),
                             code.Get(context.neg()),
                             "movq xmm4, [rsp]",    //term.x
                             "movq xmm0, [rsp+32]", //sum.x
                             "mulsd xmm0,xmm4",     //result.x
                             "movq xmm4, [rsp+8]",  //term.y
                             "movq xmm1, [rsp+40]", //sum.y
                             "mulsd xmm1,xmm4",     //result.y
                             "movq xmm4, [rsp+16]", //term.z
                             "movq xmm2, [rsp+48]", //sum.z
                             "mulsd xmm2,xmm4",     //result.z
                             "movq xmm4, [rsp+24]", //term.w
                             "movq xmm3, [rsp+56]", //sum.w
                             "mulsd xmm3,xmm4",     //result.w
                             "add rsp,32",          //pop 64 bytes, push 32
                             "movq [rsp], xmm0",    //result.x
                             "movq [rsp+8], xmm1",  //result.y
                             "movq [rsp+16], xmm2", //result.z
                             "movq [rsp+24], xmm3"  //result.w
                             );
                }
                else if (context.MULOP().GetText() == "/")
                {
                    //Console.WriteLine("made it here /");
                    code.Put(context,
                             code.Get(context.term()),
                             code.Get(context.neg()),
                             "movq xmm4, [rsp]",    //term.x
                             "movq xmm0, [rsp+32]", //sum.x
                             "divsd xmm0,xmm4",     //result.x
                             "movq xmm4, [rsp+8]",  //term.y
                             "movq xmm1, [rsp+40]", //sum.y
                             "divsd xmm1,xmm4",     //result.y
                             "movq xmm4, [rsp+16]", //term.z
                             "movq xmm2, [rsp+48]", //sum.z
                             "divsd xmm2,xmm4",     //result.z
                             "movq xmm4, [rsp+24]", //term.w
                             "movq xmm3, [rsp+56]", //sum.w
                             "divsd xmm3,xmm4",     //result.w
                             "add rsp,32",          //pop 64 bytes, push 32
                             "movq [rsp], xmm0",    //result.x
                             "movq [rsp+8], xmm1",  //result.y
                             "movq [rsp+16], xmm2", //result.z
                             "movq [rsp+24], xmm3"  //result.w
                             );
                }
                else     //Modulo
                {
                    throw new Exception("Modulo not possible on vec4 type");
                }
                break;
            }
        }