// * 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 (scs) { case SCS.AUTO: state.GLOBL(name); break; case SCS.EXTERN: throw new InvalidProgramException(); case SCS.STATIC: break; case SCS.TYPEDEF: // Ignore. return; default: throw new InvalidProgramException(); } state.DATA(); state.ALIGN(ExprType.ALIGN_LONG); state.CGenLabel(name); Int32 last = 0; initr.Iterate(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 ExprType.Kind.CHAR: case ExprType.Kind.UCHAR: case ExprType.Kind.SHORT: case ExprType.Kind.USHORT: throw new NotImplementedException(); case ExprType.Kind.LONG: state.LONG(((ConstLong)expr).value); break; case ExprType.Kind.ULONG: state.LONG((Int32)((ConstULong)expr).value); break; case ExprType.Kind.POINTER: state.LONG((Int32)((ConstPtr)expr).value); break; case ExprType.Kind.FLOAT: byte[] float_bytes = BitConverter.GetBytes(((ConstFloat)expr).value); Int32 intval = BitConverter.ToInt32(float_bytes, 0); state.LONG(intval); break; case ExprType.Kind.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 (scs) { case SCS.AUTO: // .comm name,size,align break; case SCS.EXTERN: break; case SCS.STATIC: // .local name // .comm name,size,align state.LOCAL(name); break; case SCS.TYPEDEF: // Ignore. return; default: throw new InvalidProgramException(); } if (type.kind != ExprType.Kind.FUNCTION) { state.COMM(name, 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(name).Value.offset; if (this.initr.IsNone) { return; } Initr initr = this.initr.Value; initr.Iterate(type, (Int32 offset, Expr expr) => { Reg ret = expr.CGenValue(env, state); switch (expr.type.kind) { case ExprType.Kind.CHAR: case ExprType.Kind.UCHAR: state.MOVB(Reg.EAX, pos + offset, Reg.EBP); break; case ExprType.Kind.SHORT: case ExprType.Kind.USHORT: state.MOVW(Reg.EAX, pos + offset, Reg.EBP); break; case ExprType.Kind.DOUBLE: state.FSTPL(pos + offset, Reg.EBP); break; case ExprType.Kind.FLOAT: state.FSTPS(pos + offset, Reg.EBP); break; case ExprType.Kind.LONG: case ExprType.Kind.ULONG: case ExprType.Kind.POINTER: state.MOVL(Reg.EAX, pos + offset, Reg.EBP); break; case ExprType.Kind.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 ExprType.Kind.ARRAY: case ExprType.Kind.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 }