2022-09-21 00:59:36 +00:00
|
|
|
import io.kotest.core.spec.style.FunSpec
|
|
|
|
import io.kotest.matchers.ints.shouldBeGreaterThan
|
|
|
|
import io.kotest.matchers.shouldBe
|
|
|
|
import prog8.code.StStaticVariable
|
|
|
|
import prog8.code.core.CbmPrgLauncherType
|
|
|
|
import prog8.code.core.CompilationOptions
|
|
|
|
import prog8.code.core.OutputType
|
|
|
|
import prog8.code.core.ZeropageType
|
|
|
|
import prog8.code.target.Cx16Target
|
|
|
|
import prog8.intermediate.IRFileReader
|
|
|
|
import prog8.intermediate.IRFileWriter
|
|
|
|
import prog8.intermediate.IRProgram
|
2022-09-25 15:14:44 +00:00
|
|
|
import prog8.intermediate.IRSymbolTable
|
2022-09-24 11:54:00 +00:00
|
|
|
import kotlin.io.path.*
|
2022-09-21 00:59:36 +00:00
|
|
|
|
|
|
|
class TestIRFileInOut: FunSpec({
|
|
|
|
test("test IR writer") {
|
|
|
|
val target = Cx16Target()
|
2022-09-24 11:54:00 +00:00
|
|
|
val tempdir = Path(System.getProperty("java.io.tmpdir"))
|
2022-09-21 00:59:36 +00:00
|
|
|
val options = CompilationOptions(
|
|
|
|
OutputType.RAW,
|
|
|
|
CbmPrgLauncherType.NONE,
|
|
|
|
ZeropageType.DONTUSE,
|
|
|
|
emptyList(),
|
|
|
|
floats = false,
|
|
|
|
noSysInit = true,
|
|
|
|
compTarget = target,
|
|
|
|
loadAddress = target.machine.PROGRAM_LOAD_ADDRESS,
|
|
|
|
outputDir = tempdir
|
|
|
|
)
|
2022-09-25 15:14:44 +00:00
|
|
|
val program = IRProgram("unittest-irwriter", IRSymbolTable(null), options, target)
|
2022-09-26 12:44:24 +00:00
|
|
|
val writer = IRFileWriter(program, null)
|
|
|
|
val generatedFile = writer.write()
|
2022-09-21 00:59:36 +00:00
|
|
|
val lines = generatedFile.readLines()
|
2022-11-11 22:35:52 +00:00
|
|
|
lines[0] shouldBe "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
|
|
|
|
lines[1] shouldBe "<PROGRAM NAME=\"unittest-irwriter\">"
|
2022-09-21 00:59:36 +00:00
|
|
|
lines.last() shouldBe "</PROGRAM>"
|
|
|
|
generatedFile.deleteExisting()
|
|
|
|
lines.size shouldBeGreaterThan 20
|
|
|
|
}
|
|
|
|
|
|
|
|
test("test IR reader") {
|
2022-11-11 22:35:52 +00:00
|
|
|
val source="""<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<PROGRAM NAME="test-ir-reader">
|
2022-09-21 00:59:36 +00:00
|
|
|
<OPTIONS>
|
|
|
|
compTarget=virtual
|
|
|
|
output=PRG
|
|
|
|
launcher=BASIC
|
|
|
|
zeropage=KERNALSAFE
|
2023-03-06 20:42:08 +00:00
|
|
|
loadAddress=$0000
|
|
|
|
evalStackBaseAddress=
|
2022-09-21 00:59:36 +00:00
|
|
|
</OPTIONS>
|
|
|
|
|
2022-09-30 13:27:03 +00:00
|
|
|
<ASMSYMBOLS>
|
|
|
|
</ASMSYMBOLS>
|
|
|
|
|
2023-02-19 02:07:55 +00:00
|
|
|
<VARIABLESNOINIT>
|
2022-12-03 16:46:06 +00:00
|
|
|
uword sys.bssvar zp=DONTCARE
|
2023-02-19 02:07:55 +00:00
|
|
|
</VARIABLESNOINIT>
|
|
|
|
<VARIABLESWITHINIT>
|
2022-12-03 16:46:06 +00:00
|
|
|
uword sys.wait.jiffies=10 zp=DONTCARE
|
2023-03-04 14:35:54 +00:00
|
|
|
ubyte[3] sys.emptystring=0,0,0 zp=DONTCARE
|
2023-02-19 02:07:55 +00:00
|
|
|
</VARIABLESWITHINIT>
|
2022-09-21 00:59:36 +00:00
|
|
|
|
|
|
|
<MEMORYMAPPEDVARIABLES>
|
2022-11-11 22:35:52 +00:00
|
|
|
@uword cx16.r0=65282
|
2022-09-21 00:59:36 +00:00
|
|
|
</MEMORYMAPPEDVARIABLES>
|
|
|
|
|
|
|
|
<MEMORYSLABS>
|
|
|
|
</MEMORYSLABS>
|
|
|
|
|
|
|
|
<INITGLOBALS>
|
2022-11-16 23:15:47 +00:00
|
|
|
<CODE>
|
2022-09-21 00:59:36 +00:00
|
|
|
load.b r1,42
|
2022-11-16 23:15:47 +00:00
|
|
|
</CODE>
|
2022-09-21 00:59:36 +00:00
|
|
|
</INITGLOBALS>
|
|
|
|
|
2023-03-06 20:42:08 +00:00
|
|
|
<BLOCK NAME="main" ADDRESS="" ALIGN="NONE" POS="[examples/test.p8: line 2 col 2-5]">
|
|
|
|
<SUB NAME="main.start" RETURNTYPE="" POS="[examples/test.p8: line 4 col 6-8]">
|
2022-09-21 00:59:36 +00:00
|
|
|
<PARAMS>
|
|
|
|
</PARAMS>
|
2022-11-16 23:15:47 +00:00
|
|
|
<CODE LABEL="main.start">
|
2022-09-21 00:59:36 +00:00
|
|
|
return
|
2022-11-16 23:15:47 +00:00
|
|
|
</CODE>
|
2022-09-21 00:59:36 +00:00
|
|
|
</SUB>
|
|
|
|
</BLOCK>
|
|
|
|
|
2023-03-06 20:42:08 +00:00
|
|
|
<BLOCK NAME="sys" ADDRESS="" ALIGN="NONE" POS="[library:/prog8lib/virtual/syslib.p8: line 3 col 2-4]">
|
|
|
|
<SUB NAME="sys.wait" RETURNTYPE="" POS="[library:/prog8lib/virtual/syslib.p8: line 15 col 6-8]">
|
2022-09-21 00:59:36 +00:00
|
|
|
<PARAMS>
|
|
|
|
uword sys.wait.jiffies
|
|
|
|
</PARAMS>
|
2022-11-11 22:35:52 +00:00
|
|
|
<INLINEASM LABEL="sys.wait" IR="true" POS="[library:/prog8lib/virtual/syslib.p8: line 17 col 10-13]">
|
2022-09-21 00:59:36 +00:00
|
|
|
loadm.w r0,sys.wait.jiffies
|
|
|
|
syscall 13
|
|
|
|
</INLINEASM>
|
2022-11-16 23:15:47 +00:00
|
|
|
<CODE>
|
2022-09-21 00:59:36 +00:00
|
|
|
return
|
2022-11-16 23:15:47 +00:00
|
|
|
</CODE>
|
2022-09-21 00:59:36 +00:00
|
|
|
</SUB>
|
|
|
|
</BLOCK>
|
|
|
|
</PROGRAM>
|
|
|
|
"""
|
2022-09-25 15:14:44 +00:00
|
|
|
val tempfile = createTempFile(suffix = ".p8ir")
|
2022-09-21 00:59:36 +00:00
|
|
|
tempfile.writeText(source)
|
2022-09-26 12:44:24 +00:00
|
|
|
val program = IRFileReader().read(tempfile)
|
2022-09-21 00:59:36 +00:00
|
|
|
tempfile.deleteExisting()
|
|
|
|
program.name shouldBe "test-ir-reader"
|
|
|
|
program.blocks.size shouldBe 2
|
2023-03-04 14:35:54 +00:00
|
|
|
program.st.allVariables().count() shouldBe 3
|
2022-12-03 16:46:06 +00:00
|
|
|
val var1 = program.st.lookup("sys.wait.jiffies") as StStaticVariable
|
|
|
|
val var2 = program.st.lookup("sys.bssvar") as StStaticVariable
|
2023-03-04 14:35:54 +00:00
|
|
|
val var3 = program.st.lookup("sys.emptystring") as StStaticVariable
|
2023-02-19 14:23:57 +00:00
|
|
|
var1.uninitialized shouldBe false
|
|
|
|
var2.uninitialized shouldBe true
|
2023-03-04 14:35:54 +00:00
|
|
|
var3.uninitialized shouldBe true
|
2022-09-21 00:59:36 +00:00
|
|
|
}
|
|
|
|
})
|