-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate simple Ast and Symboltable from codeCore into new simpleAst …
…module. VirtualMachine and Intermediate do not need them, just codeCore.
- Loading branch information
Showing
43 changed files
with
295 additions
and
216 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package prog8.code | ||
|
||
|
||
// the automatically generated module where all string literals are interned to: | ||
const val INTERNED_STRINGS_MODULENAME = "prog8_interned_strings" | ||
|
||
// all automatically generated labels everywhere need to have the same label name prefix: | ||
const val GENERATED_LABEL_PREFIX = "p8_label_gen_" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
codeGenIntermediate/src/prog8/codegen/intermediate/StConvert.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package prog8.codegen.intermediate | ||
|
||
import prog8.code.StArrayElement | ||
import prog8.code.StConstant | ||
import prog8.code.StMemVar | ||
import prog8.code.StMemorySlab | ||
import prog8.code.StNodeType | ||
import prog8.code.StStaticVariable | ||
import prog8.code.SymbolTable | ||
import prog8.code.core.DataType | ||
import prog8.intermediate.IRStArrayElement | ||
import prog8.intermediate.IRStConstant | ||
import prog8.intermediate.IRStMemVar | ||
import prog8.intermediate.IRStMemorySlab | ||
import prog8.intermediate.IRStStaticVariable | ||
import prog8.intermediate.IRSymbolTable | ||
|
||
|
||
fun convertStToIRSt(sourceSt: SymbolTable?): IRSymbolTable { | ||
val st = IRSymbolTable() | ||
if (sourceSt != null) { | ||
sourceSt.flat.forEach { | ||
when(it.value.type) { | ||
StNodeType.STATICVAR -> st.add(convert(it.value as StStaticVariable)) | ||
StNodeType.MEMVAR -> st.add(convert(it.value as StMemVar)) | ||
StNodeType.CONSTANT -> st.add(convert(it.value as StConstant)) | ||
StNodeType.MEMORYSLAB -> st.add(convert(it.value as StMemorySlab)) | ||
else -> { } | ||
} | ||
} | ||
|
||
st.validate() | ||
|
||
st.allVariables().forEach { variable -> | ||
variable.onetimeInitializationArrayValue?.let { | ||
it.forEach { arrayElt -> | ||
val addrOfSymbol = arrayElt.addressOfSymbol | ||
if (addrOfSymbol != null) { | ||
require(addrOfSymbol.contains('.')) { | ||
"pointer var in array should be properly scoped: ${addrOfSymbol} in ${variable.name}" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return st | ||
} | ||
|
||
|
||
private fun convert(variable: StStaticVariable): IRStStaticVariable { | ||
|
||
fun convertArrayElt(elt: StArrayElement): IRStArrayElement = if(elt.boolean!=null) | ||
IRStArrayElement(elt.boolean, null, elt.addressOfSymbol) | ||
else | ||
IRStArrayElement(null, elt.number, elt.addressOfSymbol) | ||
|
||
val scopedName: String | ||
if('.' in variable.name) { | ||
scopedName = variable.name | ||
return IRStStaticVariable(variable.name, | ||
variable.dt, | ||
variable.initializationNumericValue, | ||
variable.initializationStringValue, | ||
variable.initializationArrayValue?.map { convertArrayElt(it) }, | ||
variable.length, | ||
variable.zpwish, | ||
variable.align) | ||
} else { | ||
fun fixupAddressOfInArray(array: List<StArrayElement>?): List<IRStArrayElement>? { | ||
if(array==null) | ||
return null | ||
val newArray = mutableListOf<IRStArrayElement>() | ||
array.forEach { | ||
if(it.addressOfSymbol!=null) { | ||
val target = variable.lookup(it.addressOfSymbol!!) ?: throw NoSuchElementException("can't find variable ${it.addressOfSymbol}") | ||
newArray.add(IRStArrayElement(null, null, target.scopedName)) | ||
} else { | ||
newArray.add(convertArrayElt(it)) | ||
} | ||
} | ||
return newArray | ||
} | ||
scopedName = variable.scopedName | ||
return IRStStaticVariable(scopedName, | ||
variable.dt, | ||
variable.initializationNumericValue, | ||
variable.initializationStringValue, | ||
fixupAddressOfInArray(variable.initializationArrayValue), | ||
variable.length, | ||
variable.zpwish, | ||
variable.align | ||
) | ||
} | ||
} | ||
|
||
|
||
private fun convert(variable: StMemVar): IRStMemVar { | ||
val scopedName: String | ||
if('.' in variable.name) { | ||
scopedName = variable.name | ||
return IRStMemVar( | ||
variable.name, | ||
variable.dt, | ||
variable.address, | ||
variable.length | ||
) | ||
} else { | ||
scopedName = try { | ||
variable.scopedName | ||
} catch (_: UninitializedPropertyAccessException) { | ||
variable.name | ||
} | ||
return IRStMemVar(scopedName, variable.dt, variable.address, variable.length) | ||
} | ||
} | ||
|
||
|
||
private fun convert(constant: StConstant): IRStConstant { | ||
val dt = DataType.forDt(constant.dt) | ||
val scopedName = if('.' in constant.name) { | ||
constant.name | ||
} else { | ||
try { | ||
constant.scopedName | ||
} catch (_: UninitializedPropertyAccessException) { | ||
constant.name | ||
} | ||
} | ||
return IRStConstant(scopedName, dt, constant.value) | ||
} | ||
|
||
|
||
private fun convert(variable: StMemorySlab): IRStMemorySlab { | ||
return if('.' in variable.name) | ||
IRStMemorySlab(variable.name, variable.size, variable.align) | ||
else | ||
IRStMemorySlab("prog8_slabs.${variable.name}", variable.size, variable.align) | ||
} | ||
|
||
/* | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.