Skip to content

How to eval raw x86 code? #18

Open
@waynee95

Description

@waynee95

Using x86_parser.parseon the following assembly program (equivalent to print(40 + 2))

  .globl main
main:
  pushq %rbp
  movq %rsp, %rbp
  subq $16, %rsp
  movq $40, -8(%rbp)
  addq $2, -8(%rbp)
  movq -8(%rbp), %rdi
  callq print_int
  addq $16, %rsp
  popq %rbp
  retq

yields something of the form (I truncated the output)

Tree(Token('RULE', 'prog'), [Tree(Token('RULE', 'block'), [Token('CNAME', 'main')]), Tree(Token('RULE', 'block'), [Token('CNAME', 'main'), Tree('pushq', [Tree('reg_a', [Token('RBP', 'rbp')])]), Tree('movq', [Tree('reg_a', [Token('RSP', 'rsp')]), Tree('reg_a', [Token('RBP', 'rbp')])]), Tree('subq', [Tree('int_a', [Tree('int_a', [Token('NUMBER', '16')])]), Tree('reg_a', [Token('RSP', 'rsp')])]), Tree('movq', [Tree('int_a', [Tree('int_a', [Token('NUMBER', '40')])]), ...

which is not in the correct shape for eval_x86.eval_program to accept it.

However, evaluating assembly generated by the compiler works.

Using a finished compiler from chapter 2 to compile print(40 + 2) to x86 ast and then using convert_x86.convert_program yields something of the form (I truncated the output again)

Tree('prog', [Tree('block', ['main', Tree('pushq', [Tree('reg_a', ['rbp'])]), Tree('movq', [Tree('reg_a', ['rsp']), Tree('reg_a', ['rbp'])]), Tree('subq', [Tree('int_a', [16]), Tree('reg_a', ['rsp'])]), Tree('movq', [Tree('int_a', [40]), Tree('mem_a', [Tree('int_a', [-8]), 'rbp'])]),  ...

which is of the correct form for eval_x86.eval_program to accept and produce a result.

I feel like I am missing something since eval_x86.eval_program is written to operate on the result that lark produces no?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions