Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing max-pc on generating pc-sp table #4

Merged
merged 2 commits into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions asm2asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,7 @@ def __repr__(self):
class Pcsp:
entry: int
maxpc: int
maxpc_sp: int
out : List[Tuple[int, int]]
pc : int
sp : int
Expand All @@ -985,6 +986,8 @@ def __str__(self) -> str:
def optimize(self):
# push the last record
self.out.append((self.pc - self.entry, self.sp))
# push the max pc
self.out.append((self.maxpc - self.entry, self.maxpc_sp))
# sort by pc
self.out.sort(key=lambda x: x[0])
# NOTICE: first pair {1, 0} to be compitable with golang
Expand All @@ -1009,6 +1012,7 @@ def update(self, dpc: int, dsp: int):
self.sp += dsp
if self.pc > self.maxpc:
self.maxpc = self.pc
self.maxpc_sp = self.sp

class Prototype:
args: List[Parameter]
Expand Down Expand Up @@ -2069,19 +2073,11 @@ def pcsp(self, name: str, entry: int) -> int:
self.labels[name].func = True
return self._trace_block(self.labels[name], pcsp)

def debug(self, pos: int, inss: List[Instruction]):
def inject(bb: BasicBlock) -> bool:
if (not bb.func) and (bb.name not in self.funcs):
return True
nonlocal pos
if pos >= len(bb.body):
return
for ins in inss:
bb.body.insert(pos, ins)
pos += 1
visited = {}
for _, bb in self.labels.items():
CodeSection._dfs_jump_first(bb, visited, inject)
def debug(self, label: str, pos: int, inss: List[Instruction]):
b = self.labels.get(label)
for ins in inss:
b.body.insert(pos, ins)
pos += 1

_STUB_NAME = '__native_entry__'
STUB_SIZE = 67
Expand Down Expand Up @@ -2439,12 +2435,13 @@ def _declare_functions(self, protos: PrototypeMap):

def parse(self, src: List[str], proto: PrototypeMap):
self._parse(src)
# print("DEBUG...")
# self.code.debug(0, [
# X86Instr(Instruction('int3', []))
# # X86Instr(Instruction('xorq', [Register('rax'), Register('rax')])),
# # X86Instr(Instruction('movq', [Memory(Register('rax'), Immediate(0), None), Register('rax')]))
# ])
if len(DEBUG_POS) > 2 and self.name == DEBUG_POS[0]:
print("DEBUG for file '%s' label '%s' instr %s" % (DEBUG_POS[0], DEBUG_POS[1], DEBUG_POS[2]))
self.code.debug(DEBUG_POS[1], int(DEBUG_POS[2]), [
# X86Instr(Instruction('int3', []))
X86Instr(Instruction('xorq', [Register('rbx'), Register('rbx')])),
X86Instr(Instruction('movq', [Register('rbx'), Memory(Register('rbx'), Immediate(0), None)]))
])
self._declare(proto)

GOOS = {
Expand Down Expand Up @@ -2512,6 +2509,11 @@ def make_subr_filename(name: str) -> str:
"_write_syscall"
}

def remove_one_arg(i: int, args: list[str]):
for j in range(i, len(args)-1):
args[j] = args[j + 1]
args.pop()

def main():
# check for arguments
if len(sys.argv) < 3:
Expand All @@ -2521,15 +2523,21 @@ def main():
# check if optional flag is enabled
global OUTPUT_RAW
OUTPUT_RAW = False
global DEBUG_POS
DEBUG_POS = []
if len(sys.argv) >= 4:
i = 0
while i<len(sys.argv):
flag = sys.argv[i]
if flag == '-r':
OUTPUT_RAW = True
for j in range(i, len(sys.argv)-1):
sys.argv[j] = sys.argv[j + 1]
sys.argv.pop()
remove_one_arg(i, sys.argv)
continue
if flag == '-d':
remove_one_arg(i, sys.argv)
val = sys.argv[i]
remove_one_arg(i, sys.argv)
DEBUG_POS = val.split(":")
continue
i += 1

Expand Down
Loading