// * function; // * extern function; // * static function; // * obj; // * obj = Init; // * static obj; // * static obj = Init; // * extern obj; // * extern obj = Init; public void CGenDecln(Env env, CGenState state) { if (env.IsGlobal()) { if (this.initr.IsSome) { Initr initr = this.initr.Value; switch (this.scs) { case StorageClass.AUTO: state.GLOBL(this.name); break; case StorageClass.EXTERN: throw new InvalidProgramException(); case StorageClass.STATIC: break; case StorageClass.TYPEDEF: // Ignore. return; default: throw new InvalidProgramException(); } state.DATA(); state.ALIGN(ExprType.ALIGN_LONG); state.CGenLabel(this.name); Int32 last = 0; initr.Iterate(this.type, (Int32 offset, Expr expr) => { if (offset > last) { state.ZERO(offset - last); } if (!expr.IsConstExpr) { throw new InvalidOperationException("Cannot initialize with non-const expression."); } switch (expr.Type.Kind) { // TODO: without const char/short, how do I initialize? case ExprTypeKind.CHAR: case ExprTypeKind.UCHAR: case ExprTypeKind.SHORT: case ExprTypeKind.USHORT: throw new NotImplementedException(); case ExprTypeKind.LONG: state.LONG(((ConstLong)expr).Value); break; case ExprTypeKind.ULONG: state.LONG((Int32)((ConstULong)expr).Value); break; case ExprTypeKind.POINTER: state.LONG((Int32)((ConstPtr)expr).Value); break; case ExprTypeKind.FLOAT: byte[] float_bytes = BitConverter.GetBytes(((ConstFloat)expr).Value); Int32 intval = BitConverter.ToInt32(float_bytes, 0); state.LONG(intval); break; case ExprTypeKind.DOUBLE: byte[] double_bytes = BitConverter.GetBytes(((ConstDouble)expr).Value); Int32 first_int = BitConverter.ToInt32(double_bytes, 0); Int32 second_int = BitConverter.ToInt32(double_bytes, 4); state.LONG(first_int); state.LONG(second_int); break; default: throw new InvalidProgramException(); } last = offset + expr.Type.SizeOf; }); } else { // Global without initialization. switch (this.scs) { case StorageClass.AUTO: // .comm name,size,align break; case StorageClass.EXTERN: break; case StorageClass.STATIC: // .local name // .comm name,size,align state.LOCAL(this.name); break; case StorageClass.TYPEDEF: // Ignore. return; default: throw new InvalidProgramException(); } if (this.type.Kind != ExprTypeKind.FUNCTION) { state.COMM(this.name, this.type.SizeOf, ExprType.ALIGN_LONG); } } state.NEWLINE(); } else { // stack object state.CGenExpandStackTo(env.StackSize, ToString()); Int32 stack_size = env.StackSize; // pos should be equal to stack_size, but whatever... Int32 pos = env.Find(this.name).Value.Offset; if (this.initr.IsNone) { return; } Initr initr = this.initr.Value; initr.Iterate(this.type, (Int32 offset, Expr expr) => { Reg ret = expr.CGenValue(state); switch (expr.Type.Kind) { case ExprTypeKind.CHAR: case ExprTypeKind.UCHAR: state.MOVB(Reg.EAX, pos + offset, Reg.EBP); break; case ExprTypeKind.SHORT: case ExprTypeKind.USHORT: state.MOVW(Reg.EAX, pos + offset, Reg.EBP); break; case ExprTypeKind.DOUBLE: state.FSTPL(pos + offset, Reg.EBP); break; case ExprTypeKind.FLOAT: state.FSTPS(pos + offset, Reg.EBP); break; case ExprTypeKind.LONG: case ExprTypeKind.ULONG: case ExprTypeKind.POINTER: state.MOVL(Reg.EAX, pos + offset, Reg.EBP); break; case ExprTypeKind.STRUCT_OR_UNION: state.MOVL(Reg.EAX, Reg.ESI); state.LEA(pos + offset, Reg.EBP, Reg.EDI); state.MOVL(expr.Type.SizeOf, Reg.ECX); state.CGenMemCpy(); break; case ExprTypeKind.ARRAY: case ExprTypeKind.FUNCTION: throw new InvalidProgramException($"How could a {expr.Type.Kind} be in a init list?"); default: throw new InvalidProgramException(); } state.CGenForceStackSizeTo(stack_size); }); } // stack object }
public override void CalcAndSaveByte(CGenState state) { state.ADDL(1, Reg.EBX); state.MOVB(Reg.BL, 0, Reg.ECX); }
public override void CalcAndSaveByte(CGenState state) { state.SUBL(1, Reg.EAX); state.MOVB(Reg.AL, 0, Reg.ECX); }
public override Reg CGenValue(CGenState state) { // 1. %eax = &left this.Left.CGenAddress(state); // 2. push %eax Int32 pos = state.CGenPushLong(Reg.EAX); Reg ret = this.Right.CGenValue(state); switch (this.Left.Type.Kind) { case ExprTypeKind.CHAR: case ExprTypeKind.UCHAR: // pop %ebx // now %ebx = %Left state.CGenPopLong(pos, Reg.EBX); // *%ebx = %al state.MOVB(Reg.AL, 0, Reg.EBX); return Reg.EAX; case ExprTypeKind.SHORT: case ExprTypeKind.USHORT: // pop %ebx // now %ebx = %Left state.CGenPopLong(pos, Reg.EBX); // *%ebx = %al state.MOVW(Reg.AX, 0, Reg.EBX); return Reg.EAX; case ExprTypeKind.LONG: case ExprTypeKind.ULONG: case ExprTypeKind.POINTER: // pop %ebx // now %ebx = &Left state.CGenPopLong(pos, Reg.EBX); // *%ebx = %al state.MOVL(Reg.EAX, 0, Reg.EBX); return Reg.EAX; case ExprTypeKind.FLOAT: // pop %ebx // now %ebx = &Left state.CGenPopLong(pos, Reg.EBX); // *%ebx = %st(0) state.FSTS(0, Reg.EBX); return Reg.ST0; case ExprTypeKind.DOUBLE: // pop %ebx // now %ebx = &Left state.CGenPopLong(pos, Reg.EBX); // *%ebx = %st(0) state.FSTL(0, Reg.EBX); return Reg.ST0; case ExprTypeKind.STRUCT_OR_UNION: // pop %edi // now %edi = &Left state.CGenPopLong(pos, Reg.EDI); // %esi = &Right state.MOVL(Reg.EAX, Reg.ESI); // %ecx = nbytes state.MOVL(this.Left.Type.SizeOf, Reg.ECX); state.CGenMemCpy(); // %eax = &Left state.MOVL(Reg.EDI, Reg.EAX); return Reg.EAX; case ExprTypeKind.FUNCTION: case ExprTypeKind.VOID: case ExprTypeKind.ARRAY: case ExprTypeKind.INCOMPLETE_ARRAY: default: throw new InvalidProgramException("cannot assign to a " + this.Type.Kind); } }