File tree 3 files changed +20
-6
lines changed
src/com/plasstech/lang/c/codegen/tacky
3 files changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -375,17 +375,21 @@ public TackyVal visit(FunctionCall n) {
375
375
public TackyVal visit (Cast n ) {
376
376
// p 260
377
377
TackyVal result = n .exp ().accept (this );
378
+ Type innerType = n .exp ().type ();
378
379
Type targetType = n .targetType ();
379
- if (targetType .equals (n . exp (). type () )) {
380
+ if (targetType .equals (innerType )) {
380
381
return result ;
381
382
}
382
- TackyVar dst = makeTackyVariable ("cast_to_" + n .targetType ().name (), n .type ());
383
- if (targetType .equals (Type .LONG )) {
384
- emit (new TackySignExtend (result , dst ));
385
- } else if (targetType .equals (Type .INT )) {
383
+ // page 282
384
+ TackyVar dst = makeTackyVariable ("cast_to_" + targetType .name (), targetType );
385
+ if (targetType .size () == innerType .size ()) {
386
+ emit (new TackyCopy (result , dst ));
387
+ } else if (targetType .size () < innerType .size ()) {
386
388
emit (new TackyTruncate (result , dst ));
389
+ } else if (innerType .signed ()) {
390
+ emit (new TackySignExtend (result , dst ));
387
391
} else {
388
- throw new UnsupportedOperationException ( "Cannot generate cast" );
392
+ emit ( new TackyZeroExtend ( result , dst ) );
389
393
}
390
394
return dst ;
391
395
}
Original file line number Diff line number Diff line change @@ -26,6 +26,8 @@ interface Visitor<R> {
26
26
R visit (TackySignExtend op );
27
27
28
28
R visit (TackyTruncate op );
29
+
30
+ R visit (TackyZeroExtend op );
29
31
}
30
32
31
33
<R > R accept (Visitor <R > visitor );
Original file line number Diff line number Diff line change
1
+ package com .plasstech .lang .c .codegen .tacky ;
2
+
3
+ public record TackyZeroExtend (TackyVal src , TackyVal dst ) implements TackyInstruction {
4
+ @ Override
5
+ public <R > R accept (Visitor <R > visitor ) {
6
+ return visitor .visit (this );
7
+ }
8
+ }
You can’t perform that action at this time.
0 commit comments