summaryrefslogtreecommitdiff
path: root/compiler_and_linker/FrontEnd/Common/CIRTransform.c
blob: b91f6af3a903f4700be4f5b8e321b6a761394be4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include "compiler/CIRTransform.h"
#include "compiler/CError.h"
#include "compiler/CExpr.h"
#include "compiler/CFunc.h"
#include "compiler/CInit.h"
#include "compiler/CMachine.h"
#include "compiler/CParser.h"
#include "compiler/objects.h"
#include "compiler/types.h"
#include "compiler/CompilerTools.h"
#include "compiler/CDecl.h"

#ifdef __MWERKS__
#pragma options align=mac68k
#endif
typedef struct CIRTransTemp {
    struct CIRTransTemp *next;
    Object *object;
    Boolean flag;
} CIRTransTemp;

typedef struct MultiAccessOperand {
    Object *object;
    Object *tempobj;
    ENode *ass;
    Type *type;
    Type *bitfieldType;
} MultiAccessOperand;

// no idea what this is for...
typedef struct StrangeRuntimeFunction {
    Object *object;
    short unk;
    char name[1];
} StrangeRuntimeFunction;
#ifdef __MWERKS__
#pragma options align=reset
#endif

static TypeFunc cirtrans_rtfunc8 = {
    TYPEFUNC, 0, NULL, NULL, TYPE(&void_ptr), 0, 0
};
static TypeFunc cirtrans_rtfunc4 = {
    TYPEFUNC, 0, NULL, NULL, TYPE(&stunsignedlong), 0, 0
};
static TypeFunc cirtrans_rtfunc2 = {
    TYPEFUNC, 0, NULL, NULL, TYPE(&stsignedshort), 0, 0
};

static CIRTransTemp *cirtrans_temps;
Boolean modulo_generated;
Boolean bigswitch_generated;
Boolean alloca_called;

// forward decls
static ENode *CIRTrans_TransExpr(ENode *expr, Boolean flag);

void CIRTrans_Setup(void) {
}

void CIRTrans_Cleanup(void) {
}

static Object *CIRTrans_GetRuntimeFunction(StrangeRuntimeFunction *rtfunc, Type *type) {
    Object *object;

    object = rtfunc->object;
    if (!object) {
        object = CParser_NewFunctionObject(NULL);
        rtfunc->object = object;

        object->nspace = cscope_root;
        object->name = GetHashNameNodeExport(rtfunc->name);
        object->flags = OBJECT_INTERNAL;

        if (type) {
            switch (type->size) {
                case 2:
                    object->type = TYPE(&cirtrans_rtfunc2);
                    break;
                case 4:
                    object->type = TYPE(&cirtrans_rtfunc4);
                    break;
                case 8:
                    object->type = TYPE(&cirtrans_rtfunc8);
                    break;
                default:
                    CError_FATAL(427);
            }
        } else {
            object->type = TYPE(&cirtrans_rtfunc8);
        }
    }

    return object;
}

static Object *CIRTrans_GetTemporary(Type *type) {
    CIRTransTemp *temp;

    for (temp = cirtrans_temps; temp; temp = temp->next) {
        if (temp->object->type->size == type->size && !temp->flag) {
            temp->flag = 1;
            return temp->object;
        }
    }

    temp = oalloc(sizeof(CIRTransTemp));
    temp->next = cirtrans_temps;
    cirtrans_temps = temp;

    temp->object = create_temp_object(type);
    temp->flag = 1;
    return temp->object;
}

static ENode *CIRTrans_CheckRuntimeAssign(ENode *expr) {
    ENode *inner;

    if (ENODE_IS(expr->data.diadic.right, EINDIRECT)) {
        inner = expr->data.diadic.right->data.monadic;
        if (
            ENODE_IS(inner, EFUNCCALL) &&
            (inner->flags & ENODE_FLAG_80) &&
            inner->data.funccall.args &&
            ENODE_IS(inner->data.funccall.args->node, EOBJREF)
            )
        {
            CError_ASSERT(502, ENODE_IS(expr->data.diadic.left, EINDIRECT));
            inner->data.funccall.args->node = expr->data.diadic.left->data.monadic;
            inner->flags &= ~ENODE_FLAG_80;
            return expr->data.diadic.right;
        }
    }

    return expr;
}

static void CIRTrans_SetupMultiAccessOperand(MultiAccessOperand *mop, ENode *expr) {
    memclrw(mop, sizeof(MultiAccessOperand));

    mop->type = expr->rtype;

    CError_ASSERT(522, ENODE_IS(expr, EINDIRECT));
    expr = expr->data.monadic;

    if (ENODE_IS(expr, EOBJREF)) {
        mop->object = expr->data.objref;
    } else {
        if (ENODE_IS(expr, EBITFIELD)) {
            mop->bitfieldType = expr->rtype;
            expr = expr->data.monadic;
        }
        expr->rtype = CDecl_NewPointerType(mop->type);
        mop->tempobj = create_temp_object(expr->rtype);
        mop->ass = makediadicnode(create_objectnode(mop->tempobj), expr, EASS);
    }
}

static ENode *CIRTrans_GetMultiAccessOperand(MultiAccessOperand *mop) {
    ENode *expr;

    if (mop->object == NULL) {
        expr = create_objectnode(mop->tempobj);
        if (mop->bitfieldType) {
            expr = makemonadicnode(expr, EBITFIELD);
            expr->rtype = mop->bitfieldType;
        }
        expr = makemonadicnode(expr, EINDIRECT);
    } else {
        expr = create_objectnode(mop->object);
    }

    expr->rtype = mop->type;
    return expr;
}

static ENode *CIRTrans_InitMultiAccessExpression(MultiAccessOperand *mop, ENode *expr) {
    if (mop->ass) {
        expr = makediadicnode(mop->ass, expr, ECOMMA);
        expr->rtype = expr->data.diadic.right->rtype;
    }
    return expr;
}

ENode *CIRTrans_TransformOpAss(ENode *expr) {
    ENodeType nt;
    ENode *expr2;
    MultiAccessOperand mop;

    if (!ENODE_IS(expr->data.diadic.left, EINDIRECT)) {
        CError_Error(CErrorStr142);
        return nullnode();
    }

    CIRTrans_SetupMultiAccessOperand(&mop, expr->data.diadic.left);

    switch (expr->type) {
        case EMULASS:
            nt = EMUL;
            break;
        case EDIVASS:
            nt = EDIV;
            break;
        case EMODASS:
            nt = EMODULO;
            break;
        case EADDASS:
            nt = EADD;
            break;
        case ESUBASS:
            nt = ESUB;
            break;
        case ESHLASS:
            nt = ESHL;
            break;
        case ESHRASS:
            nt = ESHR;
            break;
        case EANDASS:
            nt = EAND;
            break;
        case EXORASS:
            nt = EXOR;
            break;
        case EORASS:
            nt = EOR;
            break;
        default:
            CError_FATAL(622);
    }

    expr2 = CIRTrans_GetMultiAccessOperand(&mop);

    if (!IS_TYPE_POINTER_ONLY(expr2->rtype)) {
        expr2 = CExpr_NewDyadicNode(expr2, nt, expr->data.diadic.right);
        if (expr2->rtype != expr->data.diadic.left->rtype) {
            expr2 = makemonadicnode(expr2, ETYPCON);
            expr2->rtype = expr->data.diadic.left->rtype;
        }
    } else {
        expr2 = makediadicnode(expr2, expr->data.diadic.right, nt);
    }

    if (IS_TYPE_FLOAT(expr2->rtype))
        expr2 = CExpr_BinaryFloatExpression(expr2);

    expr2 = makediadicnode(CIRTrans_GetMultiAccessOperand(&mop), expr2, EASS);
    return CIRTrans_InitMultiAccessExpression(&mop, expr2);
}

static void CIRTrans_TransIncDec() {
    // empty, never called
}

static ENode *CIRTrans_TransIntConst(ENode *expr) {
    Object *obj;
    UInt8 data[16];

    CMach_InitIntMem(expr->rtype, expr->data.intval, data);

    obj = CParser_NewGlobalDataObject(NULL);
    obj->name = CParser_GetUniqueName();
    obj->type = expr->rtype;
    obj->sclass = TK_STATIC;
    obj->datatype = DDATA;
    CScope_AddGlobalObject(obj);
    CInit_DeclareData(obj, data, NULL, obj->type->size);
    return create_objectnode(obj);
}

static ENode *CIRTrans_TransFloatConst(ENode *expr) {
    Object *obj;
    UInt8 data[16];

    CMach_InitFloatMem(expr->rtype, expr->data.floatval, data);

    obj = CParser_NewGlobalDataObject(NULL);
    obj->name = CParser_GetUniqueName();
    obj->type = expr->rtype;
    obj->sclass = TK_STATIC;
    obj->datatype = DDATA;
    CScope_AddGlobalObject(obj);
    CInit_DeclareData(obj, data, NULL, obj->type->size);
    return create_objectnode(obj);
}

static ENode *CIRTrans_TransUnary(ENode *expr, Type *type, StrangeRuntimeFunction *rtfunc) {
    if (type->size > 4) {
        expr = funccallexpr(
            CIRTrans_GetRuntimeFunction(rtfunc, type),
            create_objectrefnode(CIRTrans_GetTemporary(type)),
            expr,
            NULL,
            NULL);
        expr->flags |= ENODE_FLAG_80;
        expr = makemonadicnode(expr, EINDIRECT);
        expr->rtype = type;
        return expr;
    } else {
        expr = funccallexpr(
            CIRTrans_GetRuntimeFunction(rtfunc, type),
            expr,
            NULL,
            NULL,
            NULL);
        expr->rtype = type;
        return expr;
    }
}

static ENode *CIRTrans_TransBinary(ENode *expr, StrangeRuntimeFunction *rtfunc) {
    ENode *expr2;

    if (expr->rtype->size > 4) {
        expr2 = funccallexpr(
            CIRTrans_GetRuntimeFunction(rtfunc, expr->rtype),
            create_objectrefnode(CIRTrans_GetTemporary(expr->rtype)),
            expr->data.diadic.left,
            expr->data.diadic.right,
            NULL);
        expr2->flags |= ENODE_FLAG_80;
        expr2 = makemonadicnode(expr2, EINDIRECT);
        expr2->rtype = expr->rtype;
        return expr2;
    } else {
        expr2 = funccallexpr(
            CIRTrans_GetRuntimeFunction(rtfunc, expr->rtype),
            expr->data.diadic.left,
            expr->data.diadic.right,
            NULL,
            NULL);
        expr2->rtype = expr->rtype;
        return expr2;
    }
}

static ENodeList *CIRTrans_TransExprList(ENodeList *list) {
    ENodeList *scan;

    for (scan = list; scan; scan = scan->next)
        scan->node = CIRTrans_TransExpr(scan->node, 1);

    return list;
}

static ENode *CIRTrans_TransExpr(ENode *expr, Boolean flag) {
    switch (expr->type) {
        case EINDIRECT:
        case EFORCELOAD:
        case EBITFIELD:
            expr->data.monadic = CIRTrans_TransExpr(expr->data.monadic, flag);
            break;
        case EPOSTINC:
            expr->data.monadic = CIRTrans_TransExpr(expr->data.monadic, 1);
            break;
        case EPOSTDEC:
            expr->data.monadic = CIRTrans_TransExpr(expr->data.monadic, 1);
            break;
        case EPREINC:
            expr->data.monadic = CIRTrans_TransExpr(expr->data.monadic, 1);
            break;
        case EPREDEC:
            expr->data.monadic = CIRTrans_TransExpr(expr->data.monadic, 1);
            break;
        case ETYPCON:
            expr->data.monadic = CIRTrans_TransExpr(expr->data.monadic, flag);
            if (!flag)
                return expr->data.monadic;
            break;
        case EBINNOT:
            expr->data.monadic = CIRTrans_TransExpr(expr->data.monadic, flag);
            break;
        case ELOGNOT:
            expr->data.monadic = CIRTrans_TransExpr(expr->data.monadic, flag);
            break;
        case EMONMIN:
            expr->data.monadic = CIRTrans_TransExpr(expr->data.monadic, flag);
            break;
        case EADD:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case ESUB:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case EMUL:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
        case EDIV:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case EMODULO:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case ESHL:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case ESHR:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case EROTL:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case EROTR:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case EAND:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case EXOR:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case EOR:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case ELESS:
        case EGREATER:
        case ELESSEQU:
        case EGREATEREQU:
        case EEQU:
        case ENOTEQU:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            if (!flag) {
                expr->type = ECOMMA;
                expr->rtype = expr->data.diadic.right->rtype;
                return expr;
            }
            break;
        case ELAND:
        case ELOR:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, 1);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case EMULV:
        case EADDV:
        case ESUBV:
        case EPMODULO:
        case EBCLR:
        case EBTST:
        case EBSET:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, flag);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case EASS:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, 1);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, 1);
            break;
        case EMULASS:
        case EDIVASS:
        case EADDASS:
        case ESUBASS:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, 1);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, 1);
            break;
        case EMODASS:
        case ESHLASS:
        case ESHRASS:
        case EANDASS:
        case EXORASS:
        case EORASS:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, 1);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, 1);
            break;
        case ECOMMA:
            expr->data.diadic.left = CIRTrans_TransExpr(expr->data.diadic.left, 0);
            expr->data.diadic.right = CIRTrans_TransExpr(expr->data.diadic.right, flag);
            break;
        case ECOND:
            expr->data.cond.cond = CIRTrans_TransExpr(expr->data.cond.cond, 1);
            expr->data.cond.expr1 = CIRTrans_TransExpr(expr->data.cond.expr1, 1);
            expr->data.cond.expr2 = CIRTrans_TransExpr(expr->data.cond.expr2, 1);
            break;
        case EMFPOINTER:
            expr->data.mfpointer.accessnode = CIRTrans_TransExpr(expr->data.mfpointer.accessnode, flag);
            expr->data.mfpointer.mfpointer = CIRTrans_TransExpr(expr->data.mfpointer.mfpointer, flag);
            break;
        case EFUNCCALL:
        case EFUNCCALLP:
            if (
                ENODE_IS(expr->data.funccall.funcref, EOBJREF) &&
                !strcmp(expr->data.funccall.funcref->data.objref->name->name, "__alloca")
                )
                alloca_called = 1;
            expr->data.funccall.funcref = CIRTrans_TransExpr(expr->data.funccall.funcref, 1);
            expr->data.funccall.args = CIRTrans_TransExprList(expr->data.funccall.args);
            break;
        case ENULLCHECK:
            expr->data.nullcheck.nullcheckexpr = CIRTrans_TransExpr(expr->data.nullcheck.nullcheckexpr, 1);
            expr->data.nullcheck.condexpr = CIRTrans_TransExpr(expr->data.nullcheck.condexpr, 1);
            break;
        case ENEWEXCEPTION:
        case ENEWEXCEPTIONARRAY:
            expr->data.newexception.initexpr = CIRTrans_TransExpr(expr->data.newexception.initexpr, 1);
            expr->data.newexception.tryexpr = CIRTrans_TransExpr(expr->data.newexception.tryexpr, 1);
            break;
        case EINITTRYCATCH:
            expr->data.itc.initexpr = CIRTrans_TransExpr(expr->data.itc.initexpr, 1);
            expr->data.itc.tryexpr = CIRTrans_TransExpr(expr->data.itc.tryexpr, 1);
            expr->data.itc.catchexpr = CIRTrans_TransExpr(expr->data.itc.catchexpr, 1);
            expr->data.itc.result = CIRTrans_TransExpr(expr->data.itc.result, 1);
            break;
        case EINTCONST:
        case EFLOATCONST:
        case ESTRINGCONST:
        case EOBJREF:
        case EPRECOMP:
        case ETEMP:
        case ELABEL:
        case EMEMBER:
        case EINSTRUCTION:
        case EVECTOR128CONST:
            break;
        default:
            CError_FATAL(1947);
    }

    return expr;
}

void CIRTrans_Transform(void) {
    cirtrans_temps = NULL;
}