Fix some bugs on compiler, debugger and assembler

master
Joachim Stolberg 2022-06-20 18:48:57 +02:00
parent f2f6d59c4f
commit 73f8745537
9 changed files with 39 additions and 20 deletions

View File

@ -347,6 +347,7 @@ function vm16.run(pos, cpu_def, breakpoints, steps)
elseif resp == VM16_SYS then
local io = vm16lib.get_io_reg(vm)
io.data, costs = cpu_def.on_system(pos, io.addr, io.A, io.B, io.C)
io.data = io.data or 0
vm16lib.set_io_reg(vm, io)
costs = tonumber(costs)
cycles = cycles - (costs or cpu_def.system_costs)

View File

@ -182,10 +182,6 @@ function Asm:scanner(text, filename)
self.filename = filename
self.lineno = 0
if not vm16.is_ascii(text) then
self:err_msg("Invalid ASCII file format!")
end
append(lOut, {"file", 0, filename})
self.ctype = "code"

View File

@ -127,7 +127,7 @@ function vm16.compile(pos, filename, readfile, options)
local prs = vm16.BPars:new({pos = pos, readfile = readfile, options = options})
prs:bpars_init()
local sts, res = pcall(prs.scanner, prs, filename)
local sts, res = pcall(prs.scanner, prs, filename, options)
if not sts then
return false, error_msg(res)
end

View File

@ -365,7 +365,11 @@ function BExpr:number()
local tok = self:tk_match()
if self:sym_is_const(tok.val) then
local val = self:sym_get_const(tok.val)
return tonumber(val:sub(2)) or 0
if type(val) == "string" then
return tonumber(val:sub(2)) or 0
else
return val
end
end
return tok.val
end

View File

@ -145,13 +145,13 @@ end
--[[
const_def:
= ident '=' expression ';' def_list
= ident '=' number ';' def_list
]]--
function BPars:const_def()
self:switch_to_var_def()
local ident = self:ident()
self:tk_match("=")
local right = self:expression()
local right = '#' .. self:number()
self:sym_add_const(ident, right, "global")
self:tk_match(";")
self:reset_reg_use()
@ -204,8 +204,15 @@ function BPars:const_list(ident, size)
size = size - 1
while self:tk_peek().val == ',' do
self:tk_match(",")
local tok = self:tk_match(T_NUMBER)
self:append_val(tok.val)
if self:tk_peek().val == '-' then
self:tk_match()
local tok = self:tk_match(T_NUMBER)
self:append_val(0x10000 - tok.val)
else
local tok = self:tk_match(T_NUMBER)
self:append_val(tok.val)
end
size = size - 1
end

View File

@ -37,6 +37,9 @@ local T_ENDFILE = 8
local lTypeString = {"ident", "number", "operand", "brace", "string", "asm code", "new file", "end file"}
local lToken = {}
local tScannedFiles = {}
local InvalidOperands = {
[",-"] = true, ["=-"] = true, ["/-"] = true, ["*-"] = true, ["|-"] = true, ["&-"] = true,
["<-"] = true, [">-"] = true, [";-"] = true}
local function file_ext(filename)
local _, ext = unpack(string.split(filename, ".", true, 1))
@ -135,8 +138,13 @@ function BScan:tokenize(text)
if operand:sub(1, 2) == "//" then -- EOL comment
break
end
table.insert(lToken, {type = T_OPERAND, val = operand, lineno = self.lineno})
idx = idx + #operand
if InvalidOperands[operand] then
table.insert(lToken, {type = T_OPERAND, val = ch, lineno = self.lineno})
idx = idx + 1
else
table.insert(lToken, {type = T_OPERAND, val = operand, lineno = self.lineno})
idx = idx + #operand
end
elseif ch:match(BRACE) then
table.insert(lToken, {type = T_BRACE, val = ch, lineno = self.lineno})
idx = idx + 1
@ -152,7 +160,7 @@ function BScan:tokenize(text)
self:import_file(string.sub(str, 2, -2))
else
local str2
if string.find(str, "\\") then
if not self.gen_asm_code and string.find(str, "\\") then
str2 = handle_escape_sequence(str)
else
str2 = str
@ -167,8 +175,9 @@ function BScan:tokenize(text)
end
end
function BScan:scanner(filename)
function BScan:scanner(filename, gen_asm_code)
self.filename = filename
self.gen_asm_code = gen_asm_code
self.lineno = 0
if self.nested_calls > 10 then
self:error_msg("Maximum number of nested imports exceeded")

View File

@ -21,7 +21,7 @@ local OPCODES = vm16.Asm.OPCODES
local function var_address(cpu, offs, num_stack_var)
-- Valid Base Pointer (before ret instruction)
if cpu.SP <= cpu.BP or cpu.BP == 0 then
if cpu.SP < cpu.BP or cpu.BP == 0 then
return cpu.BP + offs
else
return cpu.SP + offs + num_stack_var

View File

@ -172,16 +172,16 @@ brnch 17 0027: "@lbl2"
svar 24 1: "d"
svar 24 0: "@num_stack_var@"
func 27 0037: "test_expr"
call 28 000D: "get_five"
ret 28 0000: "0"
call 28 000D: "get_five"
svar 28 4: "a"
svar 28 2: "c"
svar 28 3: "b"
svar 28 1: "d"
svar 28 0: "@num_stack_var@"
func 31 0048: "test_expr2"
call 32 001C: "test_basic"
ret 32 0000: "0"
call 32 001C: "test_basic"
svar 32 4: "a"
svar 32 2: "c"
svar 32 3: "b"
@ -1100,8 +1100,8 @@ brnch 37 00A3: "exit"
code 3 0011: 0000
code 4 0012: 01FF 01FF 01FF
code 1 0015: 6572 726F 0072 0000
code 2 0019: 7830 3053 7830 3065 0000
code 3 001E: 7830 3001 7830 3002 7830 3003 0000
code 2 0019: 5C31 3233 5C31 3435 0000
code 3 001E: 5C30 3031 5C30 3032 5C30 3033 0000
#### Debug ####
file 0 0000: "test11.c"
gvar 1 000F: "s0"

View File

@ -124,7 +124,9 @@ local function compile_and_assemble(filename)
local pos = {x=0, y=0, z=0}
local sts, res = vm16.compile(pos, filename, read_file, {gen_asm_code = true})
if sts then
--write_file(pos, "out.asm", res)
print("######################### Assembler ############################")
print(res)
write_file(pos, "out.asm", res)
sts, res = vm16.assemble(pos, "out.asm", read_file, true)
if sts then
print("######################### BIN ############################")