Skip to content

WASM_X86: Use alphabets and underscore for label names #1329

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

Merged
merged 1 commit into from
Nov 29, 2022

Conversation

ubaidsk
Copy link
Collaborator

@ubaidsk ubaidsk commented Nov 28, 2022

Because, special characters like '-' are leading to assembler errors with the generated x86 textual representation

Also,

  1. Rename loc_to_str to label_to_str and use string as key instead of int
  2. Use map instead of unordered_map, because its worst case complexity is better

Because, special characters like '-' are leading to assembler errors with
the generated x86 textual representation

Also,
1. Rename loc_to_str to label_to_str and use string as key instead of int
2. Use map instead of unordered_map, because its worst case complexity is better
@ubaidsk ubaidsk requested a review from certik November 28, 2022 21:00
Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine, but using strings is slower than using integers. What errors were you getting? I think we should rather fix the assembler if there are issues there, so that we can keep the compilation high performing.

@ubaidsk
Copy link
Collaborator Author

ubaidsk commented Nov 28, 2022

are leading to assembler errors with the generated x86 textual representation

I think we should rather fix the assembler if there are issues there

Sorry, by assembler, I mean nasm assembler.

What errors were you getting?

Example:

def main0():
    x: i32
    x = (2+3)*5
    print(x)

main0()
x86 text format generated (filename: tmp.asm) BITS 32

ehdr:
db 0x7f
db 0x45
db 0x4c
db 0x46
db 0x01
db 0x01
db 0x01
db 0x00
db 0x00
db 0x00
db 0x00
db 0x00
db 0x00
db 0x00
db 0x00
db 0x00
dw 0x0002
dw 0x0003
dd 0x00000001
dd e_entry
dd e_phoff
dd 0x00000000
dd 0x00000000
dw ehdrsize
dw phdrsize
dw 0x0001
dw 0x0000
dw 0x0000
dw 0x0000

ehdrsize equ 0x00000034

phdr:
dd 0x00000001
dd 0x00000000
dd 0x08048000
dd 0x08048000
dd filesize
dd filesize
dd 0x00000005
dd 0x00001000

phdrsize equ 0x00000020

e_phoff equ 0x00000034

print_i32:
push ebp
mov ebp, esp
mov eax, [ebp+8]
mov ecx, eax
mov ebx, 0x00000000
cmp eax, ebx
jge .print_int_
mov eax, 0x00000004
mov ebx, 0x00000001
mov ecx, string-
mov edx, 0x00000001
int 0x80
mov ecx, [ebp+8]
neg ecx
.print_int_:
mov eax, ecx
xor esi, esi
.loop:
mov edx, 0x00000000
mov ebx, 0x0000000a
div ebx
add edx, 0x00000030
push edx
inc esi
cmp eax, 0x00
je .print
jmp .loop
.print:
cmp esi, 0x00
je .end
inc esi
mov eax, 0x00000004
mov ecx, esp
mov ebx, 0x00000001
mov edx, 0x00000001
int 0x80
add esp, 0x00000004
jmp .print
.end:
mov esp, ebp
pop ebp
ret
string-:
db 0x2d
exit:
mov eax, 0x00000001
pop ebx
int 0x80
_lpython_main_program:
push ebp
mov ebp, esp
mov eax, 0x00000000
call main0
mov esp, ebp
pop ebp
ret
main0:
push ebp
mov ebp, esp
mov eax, 0x00000000
push eax
push 0x00000019
pop eax
mov [ebp-4], eax
mov eax, [ebp-4]
push eax
call print_i32
mov eax, 0x00000004
mov ebx, 0x00000001
mov ecx, string-1
mov edx, 0x00000001
int 0x80
mov esp, ebp
pop ebp
ret
_start:
push ebp
mov ebp, esp
mov eax, 0x00000000
call _lpython_main_program
push 0x00000000
jmp exit
mov esp, ebp
pop ebp
ret
string-1:
db 0x0a

e_entry equ 0x08048125

filesize equ 0x00000141

Compiling with nasm:

$ nasm -f bin tmp.asm
tmp.asm:61: error: expression syntax error
tmp.asm:94: error: parser: instruction expected
tmp.asm:137: error: label `string' inconsistently redefined
tmp.asm:94: note: label `string' originally defined here
tmp.asm:137: error: parser: instruction expected

Note:

//! Helpful for debugging
// std::cout << x86_visitor.m_a.get_asm() << std::endl;

To generate the x86-32bit textual representation using lpython, I uncommented line 415 in the above code.

@ubaidsk
Copy link
Collaborator Author

ubaidsk commented Nov 28, 2022

using strings is slower than using integers.
so that we can keep the compilation high performing.

Got it. I shared about the nasm errors in #1329 (comment). Please, could you possibly share if we should merge this PR or there could be another approach to fix those errors?

Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine to merge.

@ubaidsk ubaidsk merged commit 860c4b3 into lcompilers:main Nov 29, 2022
@ubaidsk ubaidsk deleted the wasm_x86_text_bugs branch November 29, 2022 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants