From db60a5e2a4b1299813dbcb4051ec484c35e39afd Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Thu, 31 Oct 2019 22:48:08 +0100 Subject: [PATCH] Moved constant value to SymbolVariable. --- .../kickc/model/DirectiveParserContext.java | 4 +-- .../kickc/model/symbols/ConstantVar.java | 30 +------------------ .../kickc/model/symbols/SymbolVariable.java | 17 +++++++---- .../dk/camelot64/kickc/parser/KickCParser.g4 | 11 ++++--- src/test/ref/complex/tetris/test-sprites.asm | 2 +- src/test/ref/complex/tetris/test-sprites.log | 12 ++++---- 6 files changed, 27 insertions(+), 49 deletions(-) diff --git a/src/main/java/dk/camelot64/kickc/model/DirectiveParserContext.java b/src/main/java/dk/camelot64/kickc/model/DirectiveParserContext.java index e87293901..b6db1c880 100644 --- a/src/main/java/dk/camelot64/kickc/model/DirectiveParserContext.java +++ b/src/main/java/dk/camelot64/kickc/model/DirectiveParserContext.java @@ -29,7 +29,7 @@ public class DirectiveParserContext { /** The different scopes deciding directive defaults. */ public enum DirectiveScope { - GLOBAL, LOCAL, PARAMETER, STRUCT_MEMBER; + GLOBAL, LOCAL, PARAMETER, MEMBER; public static DirectiveScope getFor(SymbolVariable lValue, boolean isParameter) { if(isParameter) { @@ -45,7 +45,7 @@ public class DirectiveParserContext { } else if(scope instanceof Procedure) { return LOCAL; } else if(scope instanceof StructDefinition) { - return STRUCT_MEMBER; + return MEMBER; } else if(scope instanceof BlockScope) { return getFor(scope.getScope()); } else { diff --git a/src/main/java/dk/camelot64/kickc/model/symbols/ConstantVar.java b/src/main/java/dk/camelot64/kickc/model/symbols/ConstantVar.java index 83e688985..c7d3d38b5 100644 --- a/src/main/java/dk/camelot64/kickc/model/symbols/ConstantVar.java +++ b/src/main/java/dk/camelot64/kickc/model/symbols/ConstantVar.java @@ -8,20 +8,9 @@ import dk.camelot64.kickc.model.values.ConstantValue; /** A named constant or a variable that has been inferred to be constant in the symbol table */ public class ConstantVar extends SymbolVariable { - /** The constant value. */ - private ConstantValue value; - public ConstantVar(String name, Scope scope, SymbolType type, ConstantValue value, String dataSegment) { super(name, scope, type, StorageStrategy.CONSTANT, MemoryArea.MAIN_MEMORY, dataSegment); - this.value = value; - } - - public ConstantValue getValue() { - return value; - } - - public void setValue(ConstantValue value) { - this.value = value; + setValue(value); } @Override @@ -45,21 +34,4 @@ public class ConstantVar extends SymbolVariable { return toString(null); } - @Override - public boolean equals(Object o) { - if(this == o) return true; - if(o == null || getClass() != o.getClass()) return false; - if(!super.equals(o)) return false; - - ConstantVar that = (ConstantVar) o; - - return value != null ? value.equals(that.value) : that.value == null; - } - - @Override - public int hashCode() { - int result = super.hashCode(); - result = 31 * result + (value != null ? value.hashCode() : 0); - return result; - } } diff --git a/src/main/java/dk/camelot64/kickc/model/symbols/SymbolVariable.java b/src/main/java/dk/camelot64/kickc/model/symbols/SymbolVariable.java index 4916730ea..1177c99a3 100644 --- a/src/main/java/dk/camelot64/kickc/model/symbols/SymbolVariable.java +++ b/src/main/java/dk/camelot64/kickc/model/symbols/SymbolVariable.java @@ -4,6 +4,7 @@ import dk.camelot64.kickc.model.Comment; import dk.camelot64.kickc.model.Program; import dk.camelot64.kickc.model.Registers; import dk.camelot64.kickc.model.types.SymbolType; +import dk.camelot64.kickc.model.values.ConstantValue; import java.util.ArrayList; import java.util.List; @@ -83,6 +84,9 @@ public abstract class SymbolVariable implements Symbol { /** The data segment to put the variable into (if it is allocated in memory). */ private String dataSegment; + /** The constant value if the variable is a constant. Null otherwise. */ + private ConstantValue value; + public SymbolVariable(String name, Scope scope, SymbolType type, StorageStrategy storageStrategy, MemoryArea memoryArea, String dataSegment) { this.name = name; this.scope = scope; @@ -101,6 +105,14 @@ public abstract class SymbolVariable implements Symbol { fullName = (scopeName.length() > 0) ? scopeName + "::" + name : name; } + public ConstantValue getValue() { + return value; + } + + public void setValue(ConstantValue value) { + this.value = value; + } + public String getDataSegment() { return dataSegment; } @@ -325,10 +337,6 @@ public abstract class SymbolVariable implements Symbol { } SymbolVariable variable = (SymbolVariable) o; - - if(inferredType != variable.inferredType) { - return false; - } if(name != null ? !name.equals(variable.name) : variable.name != null) { return false; } @@ -346,7 +354,6 @@ public abstract class SymbolVariable implements Symbol { int result = name != null ? name.hashCode() : 0; result = 31 * result + (scope != null ? scope.hashCode() : 0); result = 31 * result + (type != null ? type.hashCode() : 0); - result = 31 * result + (inferredType ? 1 : 0); result = 31 * result + (asmName != null ? asmName.hashCode() : 0); return result; } diff --git a/src/main/java/dk/camelot64/kickc/parser/KickCParser.g4 b/src/main/java/dk/camelot64/kickc/parser/KickCParser.g4 index 289e7285c..8612fa8af 100644 --- a/src/main/java/dk/camelot64/kickc/parser/KickCParser.g4 +++ b/src/main/java/dk/camelot64/kickc/parser/KickCParser.g4 @@ -98,19 +98,18 @@ directive : CONST #directiveConst | NOTCONST #directiveNotConst | MAYBECONST #directiveMaybeConst - | EXTERN #directiveExtern - | EXPORT #directiveExport | ALIGN PAR_BEGIN NUMBER PAR_END #directiveAlign | REGISTER ( PAR_BEGIN ( NAME ) PAR_END)? #directiveRegister | ADDRESS_ZEROPAGE #directiveMemoryAreaZp | ADDRESS_MAINMEM #directiveMemoryAreaMain | ADDRESS PAR_BEGIN ( NUMBER ) PAR_END #directiveMemoryAreaAddress - | FORM_SSA #directiveFormSsa - | FORM_NOTSSA #directiveFormNotSsa - | ADDRESS PAR_BEGIN ( NUMBER ) PAR_END #directiveMemoryAreaAddress - | INLINE #directiveInline | VOLATILE #directiveVolatile | NOTVOLATILE #directiveNotVolatile + | FORM_SSA #directiveFormSsa + | FORM_NOTSSA #directiveFormNotSsa + | EXTERN #directiveExtern + | EXPORT #directiveExport + | INLINE #directiveInline | INTERRUPT ( PAR_BEGIN NAME PAR_END )? #directiveInterrupt | RESERVE PAR_BEGIN NUMBER ( COMMA NUMBER )* PAR_END #directiveReserveZp | CALLINGCONVENTION #directiveCallingConvention diff --git a/src/test/ref/complex/tetris/test-sprites.asm b/src/test/ref/complex/tetris/test-sprites.asm index 7c1728391..17fb88feb 100644 --- a/src/test/ref/complex/tetris/test-sprites.asm +++ b/src/test/ref/complex/tetris/test-sprites.asm @@ -64,7 +64,7 @@ .label irq_cnt = 9 .label sin_idx = 3 __b1: - // The screen currently being showed to the user. $00 for screen 1 / $20 for screen 2. + // The screen currently being showed to the user. 0x00 for screen 1 / 0x20 for screen 2. lda #0 sta.z render_screen_showing // The raster line of the next IRQ diff --git a/src/test/ref/complex/tetris/test-sprites.log b/src/test/ref/complex/tetris/test-sprites.log index 60888355d..241486111 100644 --- a/src/test/ref/complex/tetris/test-sprites.log +++ b/src/test/ref/complex/tetris/test-sprites.log @@ -2100,7 +2100,7 @@ __bbegin: // @1 __b1: // [1] (byte) render_screen_showing#0 ← (byte) 0 -- vbuz1=vbuc1 - // The screen currently being showed to the user. $00 for screen 1 / $20 for screen 2. + // The screen currently being showed to the user. 0x00 for screen 1 / 0x20 for screen 2. lda #0 sta.z render_screen_showing // kickasm(location (const byte*) PLAYFIELD_SPRITES) {{ .var sprites = LoadPicture("playfield-sprites.png", List().add($010101, $000000)) // Put the sprites into memory .for(var sy=0;sy<10;sy++) { .var sprite_gfx_y = sy*20 .for(var sx=0;sx<3;sx++) { .for (var y=0;y<21; y++) { .var gfx_y = sprite_gfx_y + mod(2100+y-sprite_gfx_y,21) .for (var c=0; c<3; c++) { .byte sprites.getSinglecolorByte(sx*3+c,gfx_y) } } .byte 0 } } }} @@ -2978,7 +2978,7 @@ __bbegin: // @1 __b1: // [1] (byte) render_screen_showing#0 ← (byte) 0 -- vbuz1=vbuc1 - // The screen currently being showed to the user. $00 for screen 1 / $20 for screen 2. + // The screen currently being showed to the user. 0x00 for screen 1 / 0x20 for screen 2. lda #0 sta.z render_screen_showing // kickasm(location (const byte*) PLAYFIELD_SPRITES) {{ .var sprites = LoadPicture("playfield-sprites.png", List().add($010101, $000000)) // Put the sprites into memory .for(var sy=0;sy<10;sy++) { .var sprite_gfx_y = sy*20 .for(var sx=0;sx<3;sx++) { .for (var y=0;y<21; y++) { .var gfx_y = sprite_gfx_y + mod(2100+y-sprite_gfx_y,21) .for (var c=0; c<3; c++) { .byte sprites.getSinglecolorByte(sx*3+c,gfx_y) } } .byte 0 } } }} @@ -3903,7 +3903,7 @@ Score: 11662 __b1: // render_screen_showing = 0 // [1] (byte) render_screen_showing#0 ← (byte) 0 -- vbuz1=vbuc1 - // The screen currently being showed to the user. $00 for screen 1 / $20 for screen 2. + // The screen currently being showed to the user. 0x00 for screen 1 / 0x20 for screen 2. lda #0 sta.z render_screen_showing // kickasm @@ -3973,7 +3973,7 @@ main: { // [18] call sprites_init jsr sprites_init // main::@5 - // *SPRITES_ENABLE = $ff + // *SPRITES_ENABLE = 0xff // [19] *((const byte*) SPRITES_ENABLE) ← (byte) $ff -- _deref_pbuc1=vbuc2 lda #$ff sta SPRITES_ENABLE @@ -4062,7 +4062,7 @@ loop: { // loop::@1 // loop::@2 __b2: - // while (*RASTER!=$ff) + // while (*RASTER!=0xff) // [39] if(*((const byte*) RASTER)!=(byte) $ff) goto loop::@2 -- _deref_pbuc1_neq_vbuc2_then_la1 lda #$ff cmp RASTER @@ -4137,7 +4137,7 @@ sprites_irq_init: { // Disable CIA 1 Timer IRQ lda #CIA_INTERRUPT_CLEAR sta CIA1_INTERRUPT - // *VIC_CONTROL &=$7f + // *VIC_CONTROL &=0x7f // [54] *((const byte*) VIC_CONTROL) ← *((const byte*) VIC_CONTROL) & (byte) $7f -- _deref_pbuc1=_deref_pbuc1_band_vbuc2 // Set raster line lda #$7f