1
0
mirror of https://github.com/dschmenk/PLASMA.git synced 2024-08-25 18:29:11 +00:00

Fix optimization fences and BROR/BRAND/DUP/DLW

This commit is contained in:
David Schmenk 2018-03-23 11:26:25 -07:00
parent 56f6d783e2
commit 72a7996871

View File

@ -82,10 +82,18 @@ def compiler(defptr)#0
if not isdata->[i]
when (^(bytecode+i) & $FE)
//
// Multi-byte operands
//
is $2E // CS
i = i + ^(bytecode+i+1) + 1
break
//
// Double byte operands
//
is $26 // LA
is $2C // CW
is $54 // CALL
is $58 // ENTER
is $68 // LAB
is $6A // LAW
is $78 // SAB
@ -106,6 +114,7 @@ def compiler(defptr)#0
is $3A // SUBI
is $3C // ANDI
is $3E // ORI
is $5A // LEAVE
is $5E // CFFB
is $64 // LLB
is $66 // LLW
@ -120,16 +129,9 @@ def compiler(defptr)#0
i++
break
//
// Multi-byte operands
//
is $2E // CS
i = i + ^(bytecode+i+1) + 1
break
//
// Branches
//
is $50 // BRNCH
^(bytecode+i) = ^(bytecode+i) | 1
is $22 // BREQ
is $24 // BENE
is $4C // BRFLS
@ -151,29 +153,6 @@ def compiler(defptr)#0
i++
break
//
// VM calls
//
is $54 // CALL
is $58 // ENTER, two byte operands
^(bytecode+i) = ^(bytecode+i) | 1
i = i + 2
break
is $5A // LEAVE, one byte operand
^(bytecode+i) = ^(bytecode+i) | 1
i++
break
is $86 // MULL
is $88 // DIV
is $8A // MOD
is $9A // SHL
is $9C // SHR
//
// External control
//
is $5C // RET
^(bytecode+i) = ^(bytecode+i) | 1
break
//
// SELect/caseblock
//
is $52 // SEL
@ -205,17 +184,44 @@ def compiler(defptr)#0
//
// Compile the bytecodes
//
codeptr = *jitcodeptr
VX = 0 // Virtual X register
i = 0
codeptr = *jitcodeptr
VX = 0 // Virtual X register
A_IS_TOSL = FALSE
i = 0
if ^bytecode == $58
//puts("ENTER "); puti(^(bytecode+1)); //putc(',');puti(^(bytecode+2))
//
// Call into VM
//
^codeptr = $20; codeptr++ // JSR INTERP
*codeptr = $3D0; codeptr = codeptr + 2
^codeptr = $58; codeptr++ // ENTER CODE
^codeptr = ^(bytecode+1); codeptr++ // ENTER FRAME SIZE
^codeptr = ^(bytecode+2); codeptr++ // ENTER ARG COUNT
^codeptr = $C0; codeptr++ // NATV CODE
i = 3
fin
//
// First optimization is to keep zero in Y register at all times
//
if ^bytecode & $FE <> $58 // ENTER will set Y to zero
^codeptr = $A0; codeptr++ // LDY #imm
^codeptr = $00; codeptr++ // $00
fin
^codeptr = $A0; codeptr++ // LDY #imm
^codeptr = $00; codeptr++ // $00
while isule(codeptr, codemax)
//putc('$'); puth(codeptr); //putc(':')
//putc('['); puti(i); //puts("] ")
opcode = ^(bytecode+i)
if opcode & 1
//
// Optimization fence
//
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
A_IS_TOSL = FALSE
fin
codeptr, VX = resolveX(codeptr, VX)
opcode = opcode & $FE
fin
//
// Update bytecode->native code address translation
//
@ -231,21 +237,6 @@ def compiler(defptr)#0
until not case
fin
addrxlate=>[i] = codeptr
//putc('$'); puth(codeptr); putc(':')
//putc('['); puti(i); //puts("] ")
opcode = ^(bytecode+i)
if opcode & 1
//
// Optimization fence
//
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
A_IS_TOSL = FALSE
fin
codeptr, VX = resolveX(codeptr, VX)
opcode = opcode & $FE
fin
if opcode < $20
// CN,CN,CN,CN,CN,CN,CN,CN ; 00 02 04 06 08 0A 0C 0E
// CN,CN,CN,CN,CN,CN,CN,CN ; 10 12 14 16 18 1A 1C 1E
@ -479,14 +470,19 @@ def compiler(defptr)#0
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
else
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
fin
VX-- //^codeptr = $CA; codeptr++ // DEX
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $B4; codeptr++ // LDY zp,X
^codeptr = $C0+1+VX; codeptr++ // ESTKH+1
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $94; codeptr++ // STY zp,X
^codeptr = $C0+VX; codeptr++ // ESTKH
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $D0+1+VX; codeptr++ // ESTKL+1
^codeptr = $A0; codeptr++ // LDY #imm
^codeptr = $00; codeptr++ // $00
//^codeptr = $B5; codeptr++ // LDA zp,X
//^codeptr = $D0+1+VX; codeptr++ // ESTKL+1
//^codeptr = $95; codeptr++ // STA zp,X
//^codeptr = $D0+VX; codeptr++ // ESTKL
A_IS_TOSL = TRUE
@ -788,6 +784,11 @@ def compiler(defptr)#0
dest = i + *(bytecode+i)
i++
//puts("BRNCH "); puti(dest)
codeptr, VX = resolveX(codeptr, VX)
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
fin
^codeptr = $4C; codeptr++ // JMP abs
*codeptr = addrxlate=>[dest]
if not (*codeptr & $8000) // Unresolved address list
@ -859,6 +860,11 @@ def compiler(defptr)#0
//
// Call address
//
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
fin
codeptr, VX = resolveX(codeptr, VX)
^codeptr = $20; codeptr++ // JSR abs
*codeptr = *(bytecode+i); codeptr = codeptr + 2
^codeptr = $A0; codeptr++ // LDY #imm
@ -893,29 +899,17 @@ def compiler(defptr)#0
^codeptr = $00; codeptr++ // $00
A_IS_TOSL = FALSE
break
is $58
i++
//puts("ENTER "); puti(^(bytecode+i)); putc(',');puti(^(bytecode+i+1))
//
// Call into VM
//
^codeptr = $20; codeptr++ // JSR INTERP
*codeptr = $3D0; codeptr = codeptr + 2
^codeptr = $58; codeptr++ // ENTER CODE
^codeptr = ^(bytecode+i); codeptr++ // ENTER FRAME SIZE
i++
^codeptr = ^(bytecode+i); codeptr++ // ENTER ARG COUNT
^codeptr = $C0; codeptr++ // NATV CODE
^codeptr = $A0; codeptr++ // LDY #imm
^codeptr = $00; codeptr++ // $00
A_IS_TOSL = FALSE
break
is $5A
i++
//puts("LEAVE "); puti(^(bytecode+i))
//
// Call into VM
//
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
fin
codeptr, VX = resolveX(codeptr, VX)
^codeptr = $20; codeptr++ // JSR INTERP
*codeptr = $3D0; codeptr = codeptr + 2
^codeptr = $5A; codeptr++ // LEAVE CODE
@ -924,6 +918,11 @@ def compiler(defptr)#0
break
is $5C
//puts("RET")
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
fin
codeptr, VX = resolveX(codeptr, VX)
^codeptr = $60; codeptr++ // RTS
A_IS_TOSL = FALSE
break
@ -1092,33 +1091,35 @@ def compiler(defptr)#0
^codeptr = $A0; codeptr++ // LDY #imm
^codeptr = $00; codeptr++ // $00
fin
A_IS_TOSL = FALSE
A_IS_TOSL = TRUE
break
is $6E
i++
//puts("DLW "); puti(^(bytecode+i))
if not A_IS_TOSL
^codeptr = $B5; codeptr++ // LDA zp,X
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
fin
if ^(bytecode+i) <> 0
^codeptr = $A0; codeptr++ // LDY #imm
^codeptr = ^(bytecode+i); codeptr++
^codeptr = ^(bytecode+i)+1; codeptr++
else
^codeptr = $C8; codeptr++ // INY
fin
^codeptr = $91; codeptr++ // STA (zp),Y
^codeptr = $E0; codeptr++ // IFP
^codeptr = $C8; codeptr++ // INY
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $C0+VX; codeptr++ // ESTKH
^codeptr = $91; codeptr++ // STA (zp),Y
^codeptr = $E0; codeptr++ // IFP
^codeptr = $88; codeptr++ // DEY
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
^codeptr = $91; codeptr++ // STA (zp),Y
^codeptr = $E0; codeptr++ // IFP
if ^(bytecode+i) <> 0
^codeptr = $A0; codeptr++ // LDY #imm
^codeptr = $00; codeptr++ // $00
else
^codeptr = $88; codeptr++ // DEY
fin
A_IS_TOSL = FALSE
A_IS_TOSL = TRUE
break
// SB,SW,SLB,SLW,SAB,SAW,DAB,DAW ; 70 72 74 76 78 7A 7C 7E
is $70
@ -1257,18 +1258,18 @@ def compiler(defptr)#0
is $7E
i++
//puts("DAW $"); puth(*(bytecode+i))
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
if not A_IS_TOSL
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
fin
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $C0+VX; codeptr++ // ESTKH
^codeptr = $8D; codeptr++ // STA abs
*codeptr = *(bytecode+i)+1; codeptr = codeptr + 2
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
^codeptr = $8D; codeptr++ // STA abs
*codeptr = *(bytecode+i); codeptr = codeptr + 2
^codeptr = $B4; codeptr++ // LDY zp,X
^codeptr = $C0+VX; codeptr++ // ESTKH
^codeptr = $8C; codeptr++ // STY abs
*codeptr = *(bytecode+i)+1; codeptr = codeptr + 2
^codeptr = $A0; codeptr++ // LDY #imm
^codeptr = $00; codeptr++ // $00
A_IS_TOSL = TRUE
i++
break
@ -1335,77 +1336,73 @@ def compiler(defptr)#0
VX++ //^codeptr = $E8; codeptr++ // INX
A_IS_TOSL = FALSE
break
is $86
//puts("MUL")
is $86 // MUL
is $88 // DIV
is $8A // MOD
is $9A // SHL
is $9C // SHR
//puts("MUL,DIV,MOD,SHL,SHR")
// when opcode
// is $86
// puts("MUL")
// is $88
// puts("DIV")
// is $8A
// puts("MOD")
// is $9A
// puts("SHL")
// is $9C
// puts("SHR")
// wend
//
// Call into VM
//
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
fin
codeptr, VX = resolveX(codeptr, VX)
^codeptr = $20; codeptr++ // JSR INTERP
*codeptr = $3D0; codeptr = codeptr + 2
^codeptr = $86; codeptr++ // MUL CODE
^codeptr = opcode; codeptr++ // OPCODE
^codeptr = $C0; codeptr++ // NATV CODE
^codeptr = $A0; codeptr++ // LDY #imm
^codeptr = $00; codeptr++ // $00
A_IS_TOSL = FALSE
break
is $88
//puts("DIV")
//
// Call into VM
//
^codeptr = $20; codeptr++ // JSR INTERP
*codeptr = $3D0; codeptr = codeptr + 2
^codeptr = $88; codeptr++ // DIV CODE
^codeptr = $C0; codeptr++ // NATV CODE
^codeptr = $A0; codeptr++ // LDY #imm
^codeptr = $00; codeptr++ // $00
A_IS_TOSL = FALSE
break
is $8A
//puts("MOD")
//
// Call into VM
//
^codeptr = $20; codeptr++ // JSR INTERP
*codeptr = $3D0; codeptr = codeptr + 2
^codeptr = $8A; codeptr++ // MOD CODE
^codeptr = $C0; codeptr++ // NATV CODE
^codeptr = $A0; codeptr++ // LDY #imm
^codeptr = $00; codeptr++ // $00
A_IS_TOSL = FALSE
break
is $8C
is $8C
//puts("INCR")
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
if not A_IS_TOSL
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
fin
^codeptr = $F6; codeptr++ // INC zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
^codeptr = $D0; codeptr++ // BNE rel
^codeptr = $02; codeptr++ // +2
^codeptr = $18; codeptr++ // CLC
^codeptr = $69; codeptr++ // ADC #imm
^codeptr = $01; codeptr++ // $01
^codeptr = $90; codeptr++ // BCC rel
^codeptr = $02; codeptr++ // +2
^codeptr = $F6; codeptr++ // INC zp,X
^codeptr = $C0+VX; codeptr++ // ESTKH
A_IS_TOSL = FALSE
//^codeptr = $95; codeptr++ // STA zp,X
//^codeptr = $D0+VX; codeptr++ // ESTKL
A_IS_TOSL = TRUE
break
is $8E
//puts("DECR")
if not A_IS_TOSL
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
else
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
^codeptr = $C9; codeptr++ // CMP #imm
^codeptr = $00; codeptr++ // $00
fin
^codeptr = $D0; codeptr++ // BNE rel
^codeptr = $02; codeptr++ // +2
^codeptr = $38; codeptr++ // SEC
^codeptr = $E9; codeptr++ // SBC #imm
^codeptr = $01; codeptr++ // $01
^codeptr = $B0; codeptr++ // BCS rel
^codeptr = $02; codeptr++ // +2
^codeptr = $D6; codeptr++ // DEC zp,X
^codeptr = $C0+VX; codeptr++ // ESTKH
^codeptr = $D6; codeptr++ // DEC zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
A_IS_TOSL = FALSE
//^codeptr = $95; codeptr++ // STA zp,X
//^codeptr = $D0+VX; codeptr++ // ESTKL
A_IS_TOSL = TRUE
break
// NEG,COMP,BAND,IOR,XOR,SHL,SHR,IDXW ; 90 92 94 96 98 9A 9C 9E
is $90
@ -1502,32 +1499,6 @@ def compiler(defptr)#0
VX++ //^codeptr = $E8; codeptr++ // INX
A_IS_TOSL = FALSE
break
is $9A
//puts("SHL")
//
// Call into VM
//
^codeptr = $20; codeptr++ // JSR INTERP
*codeptr = $3D0; codeptr = codeptr + 2
^codeptr = $9A; codeptr++ // SHL CODE
^codeptr = $C0; codeptr++ // NATV CODE
^codeptr = $A0; codeptr++ // LDY #imm
^codeptr = $00; codeptr++ // $00
A_IS_TOSL = FALSE
break
is $9C
//puts("SHR")
//
// Call into VM
//
^codeptr = $20; codeptr++ // JSR INTERP
*codeptr = $3D0; codeptr = codeptr + 2
^codeptr = $9C; codeptr++ // SHR CODE
^codeptr = $C0; codeptr++ // NATV CODE
^codeptr = $A0; codeptr++ // LDY #imm
^codeptr = $00; codeptr++ // $00
A_IS_TOSL = FALSE
break
is $9E
//puts("IDXW")
if not A_IS_TOSL
@ -1629,14 +1600,17 @@ def compiler(defptr)#0
//
// INCR
//
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
if not A_IS_TOSL
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
fin
^codeptr = $F6; codeptr++ // INC zp,X
^codeptr = $18; codeptr++ // CLC
^codeptr = $69; codeptr++ // ADC #imm
^codeptr = $01; codeptr++ // $01
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
^codeptr = $D0; codeptr++ // BNE rel
^codeptr = $02; codeptr++ // +2
^codeptr = $90; codeptr++ // BCC rel
^codeptr = $02; codeptr++ // +2
^codeptr = $F6; codeptr++ // INC zp,X
^codeptr = $C0+VX; codeptr++ // ESTKH
//
@ -1730,24 +1704,22 @@ def compiler(defptr)#0
if not A_IS_TOSL
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
else
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
^codeptr = $C9; codeptr++ // CMP #imm
^codeptr = $00; codeptr++ // $00
fin
^codeptr = $D0; codeptr++ // BNE rel
^codeptr = $02; codeptr++ // +2
^codeptr = $38; codeptr++ // SEC
^codeptr = $E9; codeptr++ // SBC #imm
^codeptr = $01; codeptr++ // $01
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
^codeptr = $B0; codeptr++ // BCS rel
^codeptr = $02; codeptr++ // +2
^codeptr = $D6; codeptr++ // DEC zp,X
^codeptr = $C0+VX; codeptr++ // ESTKH
^codeptr = $D6; codeptr++ // DEC zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
//
// BRGE
//
codeptr, VX = resolveX(codeptr, VX)
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
//^codeptr = $B5; codeptr++ // LDA zp,X
//^codeptr = $D0+VX; codeptr++ // ESTKL
^codeptr = $D5; codeptr++ // CMP zp,X
^codeptr = $D0+1+VX; codeptr++ // ESTKL+1
^codeptr = $B5; codeptr++ // LDA zp,X
@ -1830,7 +1802,10 @@ def compiler(defptr)#0
i++
//puts("BRAND "); puti(dest)
codeptr, VX = resolveX(codeptr, VX)
if not A_IS_TOSL
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
else
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
fin
@ -1844,7 +1819,7 @@ def compiler(defptr)#0
addrxlate=>[dest] = codeptr - *jitcodeptr
fin
codeptr = codeptr + 2
^codeptr = $E8; codeptr++ // INX
^codeptr = $E8; codeptr++ // INX VX++ ???
A_IS_TOSL = FALSE
break
is $AE
@ -1853,7 +1828,10 @@ def compiler(defptr)#0
i++
//puts("BROR "); puti(dest)
codeptr, VX = resolveX(codeptr, VX)
if not A_IS_TOSL
if A_IS_TOSL
^codeptr = $95; codeptr++ // STA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
else
^codeptr = $B5; codeptr++ // LDA zp,X
^codeptr = $D0+VX; codeptr++ // ESTKL
fin
@ -1867,7 +1845,7 @@ def compiler(defptr)#0
addrxlate=>[dest] = codeptr - *jitcodeptr
fin
codeptr = codeptr + 2
^codeptr = $E8; codeptr++ // INX
^codeptr = $E8; codeptr++ // INX // VX++ ???
A_IS_TOSL = FALSE
break
// ADDLB,ADDLW,ADDAB,ADDAW,IDXLB,IDXLW,IDXAB,IDXAW ; B0 B2 B4 B6 B8 BA BC BE
@ -2120,8 +2098,8 @@ def compiler(defptr)#0
// Free working bufffers
//
//heaprelease(addrxlate)
//puts("Done compiling: $"); puth(defptr=>interpaddr); putln
//getc
puts("Done compiling: $"); puth(defptr=>interpaddr); putln
getc
return
fin
//getc