Example #1
0
 public void print_llvm()
 {
     if (this.name.Equals("Operation"))
     {
         Console.WriteLine(LLVM_Translator.translate_operation(this));
     }
     else
     {
         foreach (AST_Node child in this.children)
         {
             if (child != null)
             {
                 child.print_llvm();
             }
         }
     }
 }
Example #2
0
 public string translate_tree()
 {
     if (this.name.Equals("Operation"))
     {
         return(LLVM_Translator.translate_operation(this));
     }
     else
     {
         Console.WriteLine("--- [{0}]", this.name);
         foreach (AST_Node child in this.children)
         {
             if (child != null)
             {
                 Console.WriteLine(child.name);
                 return(child.translate_tree());
             }
         }
         return("");
     }
 }
Example #3
0
        public static string translate_operation(AST_Node node)
        {
            if (node.children.Length == 3)
            {
                string operstring = node.children[1].identifier_string;

                string r_type = "";
                if (node.children[0].return_type != node.children[2].return_type)
                {
                    throw new InvalidProgramException("Types mismatch");
                }
                if (node.children[0].return_type == "integer")
                {
                    r_type = "int32";
                }
                if (node.children[0].return_type == "real")
                {
                    r_type = "double";
                }
                if (node.children[0].return_type == "bool")
                {
                    r_type = "i1";
                }

                string r1 = null;
                string v1 = null;
                string r2 = null;
                string v2 = null;
                Console.WriteLine(node.name);
                if (!node.children[0].is_token && !node.children[2].is_token)
                {
                    r1 = "%" + counter.ToString();
                    v1 = "%" + counter.ToString() + " = " + LLVM_Translator.translate_operation(node.children[0]);
                    counter++;
                    r2 = "%" + counter.ToString();
                    v2 = "%" + counter.ToString() + " = " + LLVM_Translator.translate_operation(node.children[2]);
                    counter++;
                }
                if (node.children[0].is_token && !node.children[2].is_token)
                {
                    r1 = "%" + counter.ToString();
                    v1 = null;
                    if (node.children[0].return_type.Equals("integer"))
                    {
                        v1 = "%" + counter.ToString() + " = add int32 " + node.children[0].ival + " 0\n";
                    }
                    if (node.children[0].return_type.Equals("integer"))
                    {
                        v1 = "%" + counter.ToString() + " = add double " + node.children[0].dval + " 0\n";
                    }
                    if (node.children[0].identifier_string == "true")
                    {
                        v1 = "%" + counter.ToString() + " = i1 true\n";
                    }
                    if (node.children[0].identifier_string == "false")
                    {
                        v1 = "%" + counter.ToString() + " = i1 false\n";
                    }

                    counter++;
                    r2 = "%" + counter.ToString();
                    v2 = "%" + counter.ToString() + LLVM_Translator.translate_operation(node.children[2]);
                    counter++;
                }

                if (node.children[0].is_token && !node.children[2].is_token)
                {
                    r1 = "%" + counter.ToString();
                    v1 = "%" + counter.ToString() + LLVM_Translator.translate_operation(node.children[0]);
                    counter++;

                    r2 = "%" + counter.ToString();
                    v2 = null;
                    if (node.children[2].return_type == "integer")
                    {
                        v2 = "%" + counter.ToString() + " = add int32 " + node.children[2].ival + " 0\n";
                    }
                    if (node.children[2].return_type == "real")
                    {
                        v2 = "%" + counter.ToString() + " = add double " + node.children[2].dval + " 0\n";
                    }
                    if (node.children[2].identifier_string == "true")
                    {
                        v2 = "%" + counter.ToString() + " = i1 true\n";
                    }
                    if (node.children[2].identifier_string == "false")
                    {
                        v2 = "%" + counter.ToString() + " = i1 false\n";
                    }
                }
                if (node.children[0].is_token && !node.children[2].is_token)
                {
                    r1 = "%" + counter.ToString();
                    v1 = null;
                    if (node.children[0].return_type == "integer")
                    {
                        v1 = "%" + counter.ToString() + " = add int32 " + node.children[0].ival + " 0\n";
                    }
                    if (node.children[0].return_type == "real")
                    {
                        v1 = "%" + counter.ToString() + " = add double " + node.children[0].dval + " 0\n";
                    }
                    if (node.children[0].identifier_string == "true")
                    {
                        v1 = "%" + counter.ToString() + " = i1 true\n";
                    }
                    if (node.children[0].identifier_string == "false")
                    {
                        v1 = "%" + counter.ToString() + " = i1 false\n";
                    }

                    r2 = "%" + counter.ToString();
                    v2 = null;
                    if (node.children[2].return_type == "integer")
                    {
                        v2 = "%" + counter.ToString() + " = add int32 " + node.children[2].ival + " 0\n";
                    }
                    if (node.children[2].return_type == "real")
                    {
                        v2 = "%" + counter.ToString() + " = add double " + node.children[2].dval + " 0\n";
                    }
                    if (node.children[2].identifier_string == "true")
                    {
                        v2 = "%" + counter.ToString() + " = i1 true\n";
                    }
                    if (node.children[2].identifier_string == "false")
                    {
                        v2 = "%" + counter.ToString() + " = i1 false\n";
                    }
                }

                switch (operstring)
                {
                case "+": {
                    operstring = "add";
                    r_type     = "i1";
                    return(v1 + v2 + "%" + counter.ToString() + " = add " + r_type + " " + r1 + ", " + r2 + "\n");
                }

                case "-": {
                    operstring = "sub";
                    r_type     = "i1";
                    return(v1 + v2 + "%" + counter.ToString() + " = add " + r_type + " " + r1 + ", " + r2 + "\n");
                }

                case "*": {
                    operstring = "mult";
                    r_type     = "i1";
                    return(v1 + v2 + "%" + counter.ToString() + " = add " + r_type + " " + r1 + ", " + r2 + "\n");
                }

                case "%": {
                    operstring = "mod";
                    r_type     = "i1";
                    return(v1 + v2 + "%" + counter.ToString() + " = add " + r_type + " " + r1 + ", " + r2 + "\n");
                };

                case "=": {
                    operstring = "eq";
                    r_type     = "i1";
                    return(v1 + v2 + "\n%" + counter.ToString() + " = icmp " + operstring + " " + r_type + " " + r1 + "," + r2 + "\n");
                }

                case ">": {
                    operstring = "ugt";
                    r_type     = "i1";
                    return(v1 + v2 + "\n%" + counter.ToString() + " = icmp " + operstring + " " + r_type + " " + r1 + "," + r2 + "\n");
                }

                case "<":
                {
                    operstring = "ult";
                    r_type     = "i1";
                    return(v1 + v2 + "%" + counter.ToString() + " = icmp " + operstring + " " + r_type + " " + r1 + "," + r2 + "\n");
                }

                case ">=": {
                    operstring = "ule";
                    r_type     = "i1";
                    return(v1 + v2 + "%" + counter.ToString() + " = icmp " + operstring + " " + r_type + " " + r1 + "," + r2 + "\n");
                }

                case "<=": {
                    operstring = "uge";
                    r_type     = "i1";
                    return(v1 + v2 + "%" + counter.ToString() + " = icmp " + operstring + " " + r_type + " " + r1 + "," + r2 + "\n");
                }

                case "/=": {
                    operstring = "ne";
                    r_type     = "i1";
                    return(v1 + v2 + "%" + counter.ToString() + " = icmp " + operstring + " " + r_type + " " + r1 + "," + r2 + "\n");
                }

                default: {
                    operstring = "ERROR";
                    r_type     = "i1";
                    return(v1 + v2 + "%" + counter.ToString() + " = icmp " + operstring + " " + r_type + " " + r1 + "," + r2 + "\n");
                }
                }
            }
            else
            {
                return("");
            }
        }