コード例 #1
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public override bool EqualType(SSAType other)
 {
     if (other is FunctionType ft)
     {
         if (!returnType.EqualType(ft.returnType))
         {
             return(false);
         }
         if (argumentTypes.Count != ft.argumentTypes.Count)
         {
             return(false);
         }
         for (int i = 0; i < argumentTypes.Count; ++i)
         {
             if (!argumentTypes[i].EqualType(ft.argumentTypes[i]))
             {
                 return(false);
             }
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #2
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public ConstPtr(SSAType pointerType, ulong data)
     : base(Op.ConstPtr, pointerType)
 {
     Debug.Assert(pointerType.kind == TypeKind.Pointer);
     isConst   = true;
     this.data = data;
 }
コード例 #3
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public ConstInt(SSAType t, ulong data)
     : base(Op.ConstInt, t)
 {
     isConst = true;
     Debug.Assert(t.kind == TypeKind.Integer);
     this.data = data;
 }
コード例 #4
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
            public GetElementPtr(Value ptr, bool inBounds = false, params Value[] indices)
                : base(Op.GEP, null, ptr)
            {
                args.AddRange(indices);

                Debug.Assert(ptr.type.kind == TypeKind.Pointer);
                Debug.Assert(indices != null && indices.Length > 0);
                Debug.Assert(indices[0].type.kind == TypeKind.Integer);

                var pt = (PointerType)ptr.type;

                baseType = pt.elementType;
                var resultType = pt.elementType;

                for (int i = 1; i < indices.Length; ++i)
                {
                    var idx = indices[i];
                    Debug.Assert(idx.type.kind == TypeKind.Integer);
                    if (resultType.kind == TypeKind.Array)
                    {
                        resultType = ((ArrayType)resultType).elementType;
                    }
                    else if (resultType.kind == TypeKind.Struct)
                    {
                        Debug.Assert(idx.isConst);
                        var elementIdx = (int)(idx as ConstInt).data;
                        var st         = (StructType)resultType;
                        resultType = st.elementTypes[elementIdx];
                    }
                }

                this.type     = new PointerType(resultType);
                this.inBounds = inBounds;
            }
コード例 #5
0
            public Value BuildExtractValue(Value v, AST.Node contextNode, string name = null, params Value[] indices)
            {
                Debug.Assert(indices != null && indices.Length > 0);
                var result = new Value(Op.ExtractValue, null, v);

                result.args.AddRange(indices);

                Debug.Assert(indices != null && indices.Length > 0);
                Debug.Assert(indices[0].type.kind == TypeKind.Integer);
                SSAType resultType = v.type;

                for (int i = 0; i < indices.Length; ++i)
                {
                    var idx = indices[i];
                    Debug.Assert(idx.type.kind == TypeKind.Integer);
                    if (resultType.kind == TypeKind.Array)
                    {
                        resultType = ((ArrayType)resultType).elementType;
                    }
                    else if (resultType.kind == TypeKind.Struct)
                    {
                        Debug.Assert(idx.isConst);
                        var elementIdx = (int)(idx as ConstInt).data;
                        var st         = (StructType)resultType;
                        resultType = st.elementTypes[elementIdx];
                    }
                }
                result.type    = resultType;
                result.isConst = v.isConst;
                AddOp(result, name, contextNode: contextNode);
                return(result);
            }
コード例 #6
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public FunctionArgument(SSAType type)
     : base(Op.FunctionArgument, type)
 {
     if (type.kind == TypeKind.Pointer)
     {
         noalias = true;
     }
 }
コード例 #7
0
            public Value BuildPtrToInt(Value v, SSAType integerType, AST.Node contextNode, string name = null)
            {
                var result = new Value(Op.PtrToInt, integerType, v);

                result.isConst = v.isConst;
                AddOp(result, name, contextNode: contextNode);
                return(result);
            }
コード例 #8
0
            public Value BuildBitCast(Value v, SSAType dest, AST.Node contextNode, string name = null)
            {
                var result = new Value(Op.BitCast, dest, v);

                result.isConst = v.isConst;
                AddOp(result, name, contextNode: contextNode);
                return(result);
            }
コード例 #9
0
            public Value BuildCondBr(Value cond, Block ifTrue, Block ifFalse, AST.Node contextNode)
            {
                Debug.Assert(SSAType.IsBoolType(cond.type));
                var result = new Value(Op.Br, null, cond, ifTrue, ifFalse);

                AddOp(result, contextNode: contextNode);
                return(result);
            }
コード例 #10
0
            public Value BuildIntToPtr(Value v, SSAType pointerType, AST.Node contextNode, string name = null)
            {
                var result = new Value(Op.IntToPtr, pointerType, v);

                result.isConst = v.isConst;
                AddOp(result, name, contextNode: contextNode);
                return(result);
            }
コード例 #11
0
            public Value BuildSizeOf(SSAType t, AST.Node contextNode, string name = null)
            {
                var np     = new ConstPtr(new PointerType(t), 0);
                var size   = BuildGEP(np, contextNode, "size_of_trick", false, one_i32_v);
                var result = BuildPtrToInt(size, Const.mm_t, contextNode, name);

                Debug.Assert(result.isConst);
                return(result);
            }
コード例 #12
0
            public Value BuildArrayAlloca(SSAType t, Value size, AST.Node contextNode, string name = null)
            {
                var result = new Value(Op.Alloca, new PointerType(t), size);

                result.alignment = 16;
                Debug.Assert(size.type.kind == TypeKind.Integer);
                AddOp(result, name, contextNode: contextNode);
                return(result);
            }
コード例 #13
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public ConstReal(SSAType t, double data)
     : base(Op.ConstReal, t)
 {
     isConst = true;
     Debug.Assert(t.kind == TypeKind.Half ||
                  t.kind == TypeKind.Float ||
                  t.kind == TypeKind.Double);
     this.data = data;
 }
コード例 #14
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public Value(Op op, SSAType t, params Value[] args)
 {
     this.op = op;
     type    = t;
     if (args != null && args.Length > 0)
     {
         this.args = new List <Value>();
         this.args.AddRange(args);
     }
 }
コード例 #15
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public Alloca(SSAType t, int alignment = 0, Value size = null) :
     base(Op.Alloca, new PointerType(t))
 {
     if (size != null)
     {
         args = new List <Value>(1);
         args.Add(size);
     }
     this.alignment = alignment;
 }
コード例 #16
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public override bool EqualType(SSAType other)
 {
     if (other is FloatType ft)
     {
         return(width == ft.width);
     }
     else
     {
         return(false);
     }
 }
コード例 #17
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public override bool EqualType(SSAType other)
 {
     if (other is PointerType pt)
     {
         return(elementType.EqualType(pt.elementType));
     }
     else
     {
         return(false);
     }
 }
コード例 #18
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public override bool EqualType(SSAType other)
 {
     if (other is IntegerType it)
     {
         return(bitWidth == it.bitWidth);
     }
     else
     {
         return(false);
     }
 }
コード例 #19
0
            public Value BuildAlloca(SSAType t, AST.Node contextNode, string name = null, int align = 0)
            {
                if (align > 32)
                {
                    align = 32;
                }
                var result = new Value(Op.Alloca, new PointerType(t));

                result.alignment = align;
                AddOp(result, name, contextNode: contextNode);
                return(result);
            }
コード例 #20
0
            public GlobalVariable AddGlobal(SSAType t, AST.Node contextNode, string name = null, bool isConst = false, int align = 0)
            {
                if (align > 16)
                {
                    align = 16;
                }
                var result = new GlobalVariable(t, isConst);

                result.alignment = align;

                AddOpGlobal(result, name, contextNode: contextNode);
                return(result);
            }
コード例 #21
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
            public static bool IsBoolType(SSAType t)
            {
                var it = t as IntegerType;

                if (it != null)
                {
                    if (it.bitWidth == 1)
                    {
                        return(true);
                    }
                }
                return(false);
            }
コード例 #22
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
            public ConstArray(SSAType arrayType, List <Value> data)
                : base(Op.ConstArray, arrayType)
            {
                isConst   = true;
                this.data = data;
#if DEBUG
                Debug.Assert(arrayType.kind == TypeKind.Array);
                var at = arrayType as ArrayType;
                Debug.Assert(at.elementCount == data.Count);
                foreach (var d in data)
                {
                    Debug.Assert(d.isConst);
                    Debug.Assert(d.type.EqualType(at.elementType));
                }
#endif
            }
コード例 #23
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
            public ConstVec(SSAType vecType, List <Value> elements)
                : base(Op.ConstVec, vecType)
            {
                isConst       = true;
                this.elements = elements;
#if DEBUG
                Debug.Assert(vecType.kind == TypeKind.Vector);
                var vt = vecType as VectorType;
                Debug.Assert(vt.elementCount == elements.Count);
                for (int i = 0; i < elements.Count; ++i)
                {
                    var el = elements[i];
                    Debug.Assert(el.isConst);
                    Debug.Assert(el.type.EqualType(vt.elementType));
                }
#endif
            }
コード例 #24
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
            public ConstStruct(SSAType structType, List <Value> elements)
                : base(Op.ConstStruct, structType)
            {
                isConst       = true;
                this.elements = elements;
                packed        = ((StructType)structType).packed;
#if DEBUG
                Debug.Assert(structType.kind == TypeKind.Struct);
                var st = structType as StructType;
                Debug.Assert(st.elementTypes.Count == elements.Count);
                for (int i = 0; i < elements.Count; ++i)
                {
                    var el = elements[i];
                    Debug.Assert(el.isConst);
                    Debug.Assert(el.type.EqualType(st.elementTypes[i]));
                }
#endif
            }
コード例 #25
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public override bool EqualType(SSAType other)
 {
     if (other is ArrayType at)
     {
         if (elementCount != at.elementCount)
         {
             return(false);
         }
         if (!elementType.EqualType(at.elementType))
         {
             return(false);
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #26
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public override bool EqualType(SSAType other)
 {
     if (other is VectorType vt)
     {
         if (elementCount != vt.elementCount)
         {
             return(false);
         }
         if (!elementType.EqualType(vt.elementType))
         {
             return(false);
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #27
0
        int GetMinimumAlignmentForBackend(SSAType t)
        {
            switch (t.kind)
            {
            case TypeKind.Vector:
            {
                var vt = (VectorType)t;
                if (vt.elementType.kind == TypeKind.Float)
                {
                    if (vt.elementCount == 4)
                    {
                        return(16);
                    }
                    else if (vt.elementCount == 8)
                    {
                        return(32);
                    }
                    else
                    {
                        Debug.Assert(false, "vector type not supported!");
                        return(32);
                    }
                }
                return(16);
            }

            case TypeKind.Struct:
            {
                var st = (StructType)t;
                return(st.elementTypes.Select(et => GetMinimumAlignmentForBackend(et)).Max());
            }

            case TypeKind.Array:
                return(GetMinimumAlignmentForBackend(((ArrayType)t).elementType));

            default:
                return(0);
            }
        }
コード例 #28
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public override bool EqualType(SSAType other)
 {
     if (other is StructType st)
     {
         if (elementTypes.Count != st.elementTypes.Count)
         {
             return(false);
         }
         for (int i = 0; i < elementTypes.Count; ++i)
         {
             if (!elementTypes[i].EqualType(st.elementTypes[i]))
             {
                 return(false);
             }
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #29
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public static Value Undefined(SSAType type)
 {
     if (type.kind == TypeKind.Integer)
     {
         var result = new ConstInt(type, 0);
         result.flags |= SSAFlags.undef;
         return(result);
     }
     else if (type.kind == TypeKind.Float || type.kind == TypeKind.Double)
     {
         var result = new ConstReal(type, 0);
         result.flags |= SSAFlags.undef;
         return(result);
     }
     else if (type.kind == TypeKind.Pointer)
     {
         var result = new ConstPtr(type, 0);
         result.flags |= SSAFlags.undef;
         return(result);
     }
     Debug.Assert(false);
     return(null);
 }
コード例 #30
0
ファイル: SSA.cs プロジェクト: pragmascript/PragmaScript
 public Phi(SSAType t, string name = null, params (Value, Block)[] incoming)