Beispiel #1
0
 public override IR subscript(IR i, IR index, IRList list)
 {
     if (index.dType != Lookup.I32)
     {
         throw Jolly.addError(new SourceLocation(), "Only int can be used as subscript");
     }
     return(list.Add(IR.operation <IR_Substript>(i, index, null)));
 }
Beispiel #2
0
 public override IR getMember(IR i, int index, IRList list)
 {
     if (index >= members.Length)
     {
         throw Jolly.addError(new SourceLocation(), "Out of index bounds");
     }
     return(list.Add(IR.getMember(i, members[index], index)));
 }
Beispiel #3
0
        static IR packTuple(AST_Tuple tuple, DataType_Tuple tupleType)
        {
            IR_Allocate alloc = new IR_Allocate {
                dType = tupleType
            };

            tuple.values.forEach((val, i) => {
                IR member = instructions.Add(IR.getMember(alloc, tupleType.members[i], i));
                instructions.Add(IR.operation <IR_Assign>(member, val.result, null));
            });
            return(alloc);
        }
Beispiel #4
0
 static void implicitCast(ref IR ir, DataType to)
 {
     if (ir.dType != to)
     {
         Cast cast;
         if (!Lookup.implicitCast.get(ir.dType, to, out cast))
         {
             throw Jolly.addError(new SourceLocation(), "Cannot implicitly cast {0} to {1}".fill(ir.dType, to));
         }
         ir = cast(ir, to);
     }
 }
Beispiel #5
0
        static void dereference(AST_Node node)
        {
            IR  refIR     = (node as AST_Operation)?.a.result ?? node.result;
            var reference = refIR.dType as DataType_Reference;

            if (isStatic(refIR.dKind) || reference == null)
            {
                throw Jolly.addError((node as AST_Operation ?? node).location, "Cannot dereference this");
            }
            node.result = instructions.Add(new IR_Dereference {
                target = refIR, dType = reference.referenced
            });
        }
Beispiel #6
0
 public static IR cast <T>(IR from, DataType to, StaticExec exec) where T : IR_Cast, new()
 {
     if (exec != null &&
         @from.dKind == ValueKind.STATIC_VALUE)
     {
         return(new IR_Literal {
             dType = to, data = exec(((IR_Literal)@from).data, 0)
         });
     }
     return(new T {
         @from = @from, dType = to
     });
 }
Beispiel #7
0
 public static IR operation <T>(IR a, IR b, StaticExec exec) where T : IR_Operation, new()
 {
     if (exec != null &&
         a.dKind == ValueKind.STATIC_VALUE &&
         b.dKind == ValueKind.STATIC_VALUE)
     {
         return(new IR_Literal {
             dType = a.dType, data = exec(((IR_Literal)a).data, ((IR_Literal)a).data)
         });
     }
     return(new T {
         a = a, b = b, dType = a.dType
     });
 }
Beispiel #8
0
        public override IR implicitCast(IR i, DataType to, IRList list)
        {
            if (!(to is DataType_Reference))
            {
                return(null);
            }
            DataType_Struct iterator = this;

            while (iterator != null)
            {
                if (iterator == to)
                {
                    return(list.Add(IR.cast <IR_Reinterpret>(i, to, null)));
                }
            }
            return(null);
        }
Beispiel #9
0
        static void assign(AST_Node node)
        {
            var op = (AST_Operation)node;

            if ((op.a.result.dKind & ~ValueKind.ADDRES) != 0)
            {
                throw Jolly.addError(op.location, "Cannot assign to this");
            }
            load(op.b);
            implicitCast(ref op.b.result, op.a.result.dType);

            //TODO: Assign to tuple containing names: someStruct.(a, b) = (0, 1);

            if (op.a.result.irType == NT.ALLOCATE)
            {
                ((IR_Allocate)op.a.result).initialized = true;
            }
            op.result = instructions.Add(IR.operation <IR_Assign>(op.a.result, op.b.result, null));
        }
Beispiel #10
0
        public override IR getMember(IR i, string name, IRList list)
        {
            int             index;
            DataType_Struct iterator = this;

            while (iterator != null)
            {
                if (iterator.memberMap.TryGetValue(name, out index))
                {
                    IR _struct = i;
                    if (iterator != this)
                    {
                        _struct = list.Add(IR.cast <IR_Reinterpret>(i, iterator, null));
                    }
                    return(list.Add(IR.getMember(_struct, iterator.members[index], index + (iterator.inherits == null ? 0 : 1))));
                }
                iterator = iterator.inherits;
            }
            return(null);
        }
Beispiel #11
0
        static void basicOperator <T>(AST_Node node, Func <object, object, object> staticExec) where T : IR_Operation, new()
        {
            var op = (AST_Operation)node;

            load(op.a); load(op.b);

            IR aIR = op.a.result, bIR = op.b.result;

            if (aIR.dType != bIR.dType)
            {
                // TODO: This always tries to cast's the left type to the right type then right to left,
                // maybe this should be decided by the operator's left to right'ness.
                Cast cast;
                if (Lookup.implicitCast.get(aIR.dType, bIR.dType, out cast))
                {
                    aIR = cast(aIR, bIR.dType);
                }
                else if (Lookup.implicitCast.get(aIR.dType, bIR.dType, out cast))
                {
                    bIR = cast(bIR, aIR.dType);
                }
                else
                {
                    throw Jolly.addError(op.location, "Cannot use operator on {0} and {1}".fill(aIR.dType, bIR.dType));
                }
            }

            if ((aIR.dKind & bIR.dKind & ValueKind.STATIC_VALUE) != 0 && staticExec != null)
            {
                op.result = new IR_Literal {
                    dType = aIR.dType, data = staticExec(((IR_Literal)aIR).data, ((IR_Literal)bIR).data)
                };
                return;
            }
            op.result = instructions.Add(new T {
                a = aIR, b = bIR, dType = aIR.dType
            });
        }
Beispiel #12
0
 public virtual IR getMember(IR i, int index, IRList list) => null;
Beispiel #13
0
 public virtual IR getMember(IR i, string name, IRList list) => null;
Beispiel #14
0
 public static IR getMember(IR _struct, DataType result, int index)
 {
     return(new IR_GetMember {
         _struct = _struct, index = index, dType = result
     });
 }
Beispiel #15
0
 public virtual IR operator_assign(IR other) => null;
Beispiel #16
0
 public virtual IR subscript(IR i, IR subscript, IRList list) => null;
Beispiel #17
0
 public virtual IR implicitCast(IR i, DataType to, IRList list) => null;