Пример #1
0
        public Reko.Core.Types.FunctionType TranslateSignature(
            LLVMType retType,
            List <LLVMParameter> parameters,
            ProcedureBuilder m)
        {
            var rt            = TranslateType(retType);
            var sigRet        = m.Frame.CreateTemporary(rt);
            var sigParameters = new List <Identifier>();

            foreach (var param in parameters)
            {
                if (param.name == "...")
                {
                    throw new NotImplementedException("Varargs");
                }
                else
                {
                    var prefix = "arg";
                    var pt     = TranslateType(param.Type);
                    var id     = m.CreateLocalId(prefix, pt);
                    sigParameters.Add(id);
                }
            }
            return(new FunctionType(sigRet, sigParameters.ToArray()));
        }
Пример #2
0
        private Instruction ParseAlloca(LocalId result)
        {
            Expect(TokenType.alloca);
            LLVMType type      = ParseType();
            LLVMType?elcType   = null;
            Value?   elc       = null;
            int      alignment = 0;

            while (PeekAndDiscard(TokenType.COMMA))
            {
                if (Type_FIRST.Contains(Peek().Type))
                {
                    elcType = ParseType();
                    elc     = ParseValue();
                }
                else if (PeekAndDiscard(TokenType.align))
                {
                    alignment = int.Parse(Expect(TokenType.Integer));
                }
            }
            return(new Alloca
            {
                Result = result,
                Type = type,
                ElCountType = elcType,
                ElementCount = elc,
                Alignment = alignment,
            });
        }
Пример #3
0
        public LLVMType ParseType()
        {
            LLVMType type = ParseTypeCore();

            for (;;)
            {
                if (PeekAndDiscard(TokenType.STAR))
                {
                    type = new LLVMPointer(type);
                }
                else if (PeekAndDiscard(TokenType.LPAREN))
                {
                    var args = new List <LLVMParameter>();
                    if (Peek().Type != TokenType.RPAREN)
                    {
                        var argType = ParseType();
                        args.Add(new LLVMParameter {
                            Type = argType
                        });
                        while (PeekAndDiscard(TokenType.COMMA))
                        {
                            if (PeekAndDiscard(TokenType.ELLIPSIS))
                            {
                                args.Add(new LLVMParameter {
                                    name = "..."
                                });
                                break;
                            }
                            argType = ParseType();
                            args.Add(new LLVMParameter {
                                Type = argType
                            });
                        }
                    }
                    Expect(TokenType.RPAREN);
                    var fnType = new LLVMFunctionType
                    {
                        ReturnType = type,
                        Parameters = args,
                    };
                    type = fnType;
                }
                else
                {
                    return(type);
                }
            }
        }
Пример #4
0
        private Expression GetElementPtr(
            LLVMType ptrType,
            Value ptrValue,
            List <Tuple <LLVMType, Value> > indices)
        {
            var type = builder.TranslateType(ptrType);
            var e    = MakeValueExpression(ptrValue, type);

            foreach (var index in indices)
            {
                long?idx = null;
                var  dt  = builder.TranslateType(index.Item1);
                var  i   = MakeValueExpression(index.Item2, dt);
                var  con = i as IrConstant;
                if (con != null)
                {
                    idx = Convert.ToInt64(con.ToInt64());
                }
                var ptr = type as Pointer;
                if (ptr != null)
                {
                    if (idx.HasValue && idx.Value == 0)
                    {
                        e = m.Deref(e);
                    }
                    else
                    {
                        e = m.Array(ptr.Pointee, e, i);
                    }
                    e.DataType = ptr.Pointee;
                    type       = ptr.Pointee;
                    continue;
                }
                var a = type as ArrayType;
                if (a != null)
                {
                    e          = m.Array(a.ElementType, e, i);
                    e.DataType = a.ElementType;
                    type       = a.ElementType;
                    continue;
                }
                throw new NotImplementedException(index.Item1.ToString());
            }
            return(e);
        }
Пример #5
0
        private Argument ParseArgument()
        {
            LLVMType type  = ParseType();
            Value    val   = null;
            var      attrs = ParseParameterAttributes();

            if (Value_FIRST.Contains(Peek().Type))
            {
                val = ParseValue();
            }
            var arg = new Argument
            {
                Type       = type,
                Value      = val,
                Attributes = attrs
            };

            return(arg);
        }
Пример #6
0
 private DataType TranslateFn(LLVMType retType, List<LLVMParameter> parameters, IStorageBinder binder)
 {
     var rt = retType.Accept(this);
     var sigRet = binder.CreateTemporary("", rt);
     var sigParameters = new List<Identifier>();
     foreach (var param in parameters)
     {
         if (param.name == "...")
         {
             var dt = new UnknownType();
             var id = binder.CreateTemporary("...", dt);
             sigParameters.Add(id);
         }
         else
         {
             var pt = param.Type.Accept(this);
             var id = binder.CreateTemporary(pt);
             sigParameters.Add(id);
         }
     }
     return new FunctionType(sigRet, sigParameters.ToArray());
 }
Пример #7
0
        private LLVMType ParseTypeCore()
        {
            var tok = Peek();

            switch (tok.Type)
            {
            case TokenType.@void:
                Expect(TokenType.@void);
                return(LLVMType.Void);

            case TokenType.IntType:
                return(LLVMType.GetBaseType(Get().Value));

            case TokenType.@double:
                Expect(TokenType.@double);
                return(LLVMType.Double);

            case TokenType.x86_fp80:
                Expect(TokenType.x86_fp80);
                return(LLVMType.X86_fp80);

            case TokenType.LocalId:
                var typeName = ParseLocalId();
                return(new TypeReference(typeName));

            case TokenType.LBRACE:
                return(ParseStruct());

            case TokenType.LBRACKET:
                return(ParseArray());

            default:
                Unexpected(tok);
                return(null);
            }
        }
Пример #8
0
 static LLVMType()
 {
     Void     = new LLVMBaseType("void", Domain.Void, 0);
     Double   = new LLVMBaseType("double", Domain.Real, 64);
     X86_fp80 = new LLVMBaseType("x86_fp80", Domain.Real, 80);
 }
Пример #9
0
 public LLVMArrayType(int length, LLVMType elType)
 {
     this.Length      = length;
     this.ElementType = elType;
 }
Пример #10
0
 public LLVMPointer(LLVMType pointee)
 {
     this.Pointee = pointee;
 }
Пример #11
0
        public DataType TranslateType(LLVMType type)
        {
            var xlat = new TypeTranslator(program.Platform.PointerType.BitSize);

            return(type.Accept(xlat));
        }
Пример #12
0
 private Expression GetElementPtr(
     LLVMType ptrType,
     Value ptrValue,
     List <(LLVMType, Value?)> indices)