optimize @( &thing )) in ast into just thing

This commit is contained in:
Irmen de Jong 2019-04-12 23:59:26 +02:00
parent c71b78dee6
commit 5497de4234
5 changed files with 38 additions and 82 deletions

View File

@ -69,7 +69,7 @@ class IntermediateProgram(val name: String, var loadAddress: Int, val heap: Heap
optimizeMultipleSequentialLineInstrs()
optimizeCallReturnIntoJump()
optimizeConditionalBranches()
// todo: add more optimizations to stackvm code
// todo: add more optimizations to intermediate code! such as: pop X + push X -> peek X (might require new opcodes)
optimizeRemoveNops() // must be done as the last step
optimizeMultipleSequentialLineInstrs() // once more

View File

@ -339,7 +339,7 @@ private fun builtinLen(args: List<IExpression>, position: Position, namespace:IN
var argument = args[0].constValue(namespace, heap)
if(argument==null) {
if(args[0] !is IdentifierReference)
throw SyntaxError("len over weird argument ${args[0]}", position)
throw SyntaxError("len argument should be an identifier, but is ${args[0]}", position)
val target = (args[0] as IdentifierReference).targetStatement(namespace)
val argValue = (target as? VarDecl)?.value
argument = argValue?.constValue(namespace, heap)

View File

@ -173,6 +173,21 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV
}
}
override fun process(memread: DirectMemoryRead): IExpression {
// @( &thing ) --> thing
val addrOf = memread.addressExpression as? AddressOf
if(addrOf!=null)
return super.process(addrOf.identifier)
return super.process(memread)
}
override fun process(memwrite: DirectMemoryWrite): IExpression {
// @( &thing ) --> thing
val addrOf = memwrite.addressExpression as? AddressOf
if(addrOf!=null)
return super.process(addrOf.identifier)
return super.process(memwrite)
}
/**
* Try to process a unary prefix expression.
@ -673,5 +688,3 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV
return assignment
}
}

View File

@ -18,6 +18,22 @@ class SimplifyExpressions(private val namespace: INameScope, private val heap: H
return super.process(assignment)
}
override fun process(memread: DirectMemoryRead): IExpression {
// @( &thing ) --> thing
val addrOf = memread.addressExpression as? AddressOf
if(addrOf!=null)
return super.process(addrOf.identifier)
return super.process(memread)
}
override fun process(memwrite: DirectMemoryWrite): IExpression {
// @( &thing ) --> thing
val addrOf = memwrite.addressExpression as? AddressOf
if(addrOf!=null)
return super.process(addrOf.identifier)
return super.process(memwrite)
}
override fun process(expr: PrefixExpression): IExpression {
if (expr.operator == "+") {
// +X --> X

View File

@ -1,89 +1,16 @@
%zeropage basicsafe
%option enable_floats
%import c64flt
~ main {
sub start() {
ubyte[3] array1
ubyte[3] array2
ubyte[3] array3
ubyte[3] array1
str string1="hello"
str string1="hello"
str string2="bye"
uword x = &array1
uword pointer = &array1
uword pointer2
uword pointer3
byte bt
pointer2 = &array2
pointer3 = &string1
uword[4] pointers = [&array1, &array2, &string1, &string2]
ptrsubasm("moet werken")
pointersub("moet werken")
myprintasm(string1)
myprintasm(string2)
myprintasm("moet werken3")
myprintasm("moet werken3")
myprintasm("moet werken4")
c64scr.print("this print must work\n")
c64.CHROUT('\n')
ptrsubasm(&array1)
ptrsubasm(&array2)
ptrsubasm(&string1)
ptrsubasm(&string2)
pointersub(&array1)
pointersub(&array2)
pointersub(&string1)
pointersub(&string2)
c64scr.print_uwhex(1, pointers[0])
c64.CHROUT(',')
c64scr.print_uwhex(1, pointers[1])
c64.CHROUT(',')
c64scr.print_uwhex(1, pointers[2])
c64.CHROUT(',')
c64scr.print_uwhex(1, pointers[3])
}
sub pointersub(uword arg) {
c64scr.print_uwhex(1, arg)
c64.CHROUT('\n')
}
asmsub ptrsubasm(uword arg @ AY) -> clobbers() -> () {
%asm {{
sec
jsr c64scr.print_uwhex
lda #13
jmp c64.CHROUT
}}
}
asmsub myprintasm(str arg @ AY) -> clobbers() -> () {
%asm {{
sec
jsr c64scr.print_uwhex
lda #13
jmp c64.CHROUT
}}
}
}
~ test {
sub testsub() {
uword[4] pointers = [&main.start.array1, &main.start.array2, &main.start.string1, &main.start.string2] ; @todo make it possible to initialize array with pointers
word sl = len(@( &string1 ))
c64scr.print_w(sl) ; should be 5
}
}