AntonLydike / riscemu

RISC-V emulator in python
MIT License
48 stars 14 forks source link

Requirement to have .text section #13

Closed tobiasgrosser closed 2 years ago

tobiasgrosser commented 2 years ago

The following code works:

        .text
        li  a0, 1             ; set exit code to 1
        li  a7, SCALL_EXIT    ; exit syscall code
        scall

but

        li  a0, 1             ; set exit code to 1
        li  a7, SCALL_EXIT    ; exit syscall code
        scall

gives me an error:

grosser@grosser-workstation:~/Projects/riscemu$ python -m riscemu examples/hello-world.asm 
Error: ParseException("INS(li) ('a0', '1') encountered in invalid context: ParseContext(
    setion=None,
    program=Program(name=hello-world.asm,sections=set(),base=[])
)", data=None)
Traceback (most recent call last):
  File "/home/grosser/Projects/riscemu/riscemu/__main__.py", line 117, in <module>
    cpu.load_program(loader.parse())
  File "/home/grosser/Projects/riscemu/riscemu/parser.py", line 109, in parse
    return parse_tokens(self.filename, tokenize(f))
  File "/home/grosser/Projects/riscemu/riscemu/parser.py", line 54, in parse_tokens
    PARSERS[token.type](token, args, context)
  File "/home/grosser/Projects/riscemu/riscemu/parser.py", line 19, in parse_instruction
    raise ParseException("{} {} encountered in invalid context: {}".format(token, args, context))
riscemu.types.exceptions.ParseException: ("INS(li) ('a0', '1') encountered in invalid context: ParseContext(\n\tsetion=None,\n\tprogram=Program(name=hello-world.asm,sections=set(),base=[])\n)", None)

Should we allow assembly code without an explicit .text section line?

AntonLydike commented 2 years ago

Maybe an implicit start of .text sections when no section is active and an instruction is read is a good thing. I believe this is how v1 handled things.

I just tested this on gcc and it seems to work just fine:

anton@whiteout ~/p/riscemu > riscv32-unknown-elf-gcc -nostdlib -mcmodel=medany test.s
/home/anton/projects/riscos/toolchain2/bin/../lib/gcc/riscv32-unknown-elf/11.1.0/../../../../riscv32-unknown-elf/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000010054
anton@whiteout ~/p/riscemu > riscv32-unknown-elf-objdump -SFldft a.out

a.out:     file format elf32-littleriscv
architecture: riscv:rv32, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x00010054

SYMBOL TABLE:
00010054 l    d  .text  00000000 .text
00000000 l    d  .riscv.attributes      00000000 .riscv.attributes
00011860 g       *ABS*  00000000 __global_pointer$
00011060 g       .text  00000000 __SDATA_BEGIN__
00000000         *UND*  00000000 _start
00011060 g       .text  00000000 __BSS_END__
00011060 g       .text  00000000 __bss_start
00011060 g       .text  00000000 __DATA_BEGIN__
00011060 g       .text  00000000 _edata
00011060 g       .text  00000000 _end

Disassembly of section .text:

00010054 <__BSS_END__-0x100c> (File Offset: 0x54):
   10054:       00100513                li      a0,1
   10058:       05d00893                li      a7,93
   1005c:       00000073                ecall

let's add this!

tobiasgrosser commented 2 years ago

Cool. Thank you!