Skip to content

WASM_X86: Fix text format #1380

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 3 commits into from
Dec 24, 2022
Merged

Conversation

ubaidsk
Copy link
Collaborator

@ubaidsk ubaidsk commented Dec 24, 2022

Example:

(lp) lpython$ cat examples/expr2.py 
def main0():
    x: i32
    x = (2+3)*5
    print(x)

main0()

# Not implemented yet in LPython:
#if __name__ == "__main__":
#    main()
(lp) lpython$ lpython examples/expr2.py --backend wasm_x86 -o tmp
BITS 32
    org 0x08048000

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 _start
    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_neg
    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
    dec 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_neg:
    db 0x2d
my_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
    pop eax
    mov eax, 0x00000004
    mov ebx, 0x00000001
    mov ecx, string_newline
    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 my_exit
    mov esp, ebp
    pop ebp
    ret
string_newline:
    db 0x0a

filesize equ $ - $$


(lp) lpython$ ./tmp
25
(wasm_asm) lpython$ nasm -fbin main.asm && chmod +x main && ./main
25
(wasm_asm) lpython$ 

@ubaidsk
Copy link
Collaborator Author

ubaidsk commented Dec 24, 2022

The major goal of this PR was to fix the following segmentation fault:

(wasm_asm) lpython$ cat examples/expr2.py 
def main0():
    x: i32
    x = (2+3)*5
    print(x)

main0()

(wasm_asm) lpython$ lpython examples/expr2.py --backend wasm_x86 -o tmp > main.asm
(wasm_asm) lpython$ ./tmp
25
(wasm_asm) lpython$ nasm -fbin main.asm && chmod +x main && ./main
25���Ԝ!1���(e|���(Qe|����
                         $Vl����(1Km���6u�����O]hp~��U����Lr��� @!3��d4 

�
�U36mLbbS_A=ab-oip31Rt.-x4nudn-a--Rt.-x4TCII1LEtcrGN_StddbutdSG_NRokiNI/ea./ssd12iun_UE=mT_G_S=4GESPSN=s-rtCAEoudnn//dC=NGn:GEESIM=nSA_Kus1/r/DE@iK_S=nGMLg:-dD_R__EsnDoudspeoeynGSNSPuN=iDEOYwaCAE=mb/ide/mmSDEI7XHTru/0urantW1C__P_Esh/edMN_CNEoudEMbCIH=AeBFLOS=i;l1:0i;s1:05=31=31=31=s7:33=4w;o4:34=3.=3.=3.=3.=3.=3.=3.=3.=3.a;*z;*z;*o;*z;*p;*01d1:z;*z;*=3.=3.01z01t=3.=3.01t01t=3.01d01r01j01w01e01s01r01a01a01z01c=3.01r1:a1:i1:w1:w1:s1:p1:p05m=3.e1:i1:m1:b1:g1:p1:g1:b1:p1:i1:i05p05s05s=3.=3.=3.=3.=3.g;*v;*v;*b1:e05o05m05m05m=3.=3.05n05w05a05r1:m05f05a05f05f05g1:l;*f;*d;*v;*m;*f;*v;*x;*c;*=3.c;*a;*d;*d0:k0:p0:p0:g0:a;*v;*a;*u0:p0:s06DUNEOnL_P=l-O_MMF=ss_P=rac/oepxsstssshVT_fa549ff5AP1ODTc--d.k_=mb/mC__P_RR--bet--nME_P=LCEsiei%sGSNAuMe2o_DLO=ublp U=iSETCN=ne0vdi164cO_VP=SLNCL=_MLbD_R_R=xtru/0sid12d8938CAT_=mb/idbpoDUMIru/0NDUEw_OGAAM=rac/oepxsstsssajOATM27GTI//rbuso/r//r///peoDAN1A/eab/ea.aih/iih/iih/iio3vaab/eamcaob/ea./ssd12b/ea.aih/iiulliullnsb//:ib//eullm/pnnbGEObuIADUNEObuOD_S__R=xtru/0sid12d8938NB/ea./ssd12bCAE_h/iio3OUEEOIP4_NDSPLusepanodtT_G=o_ma

After much debugging, I found the root cause for this bug at:

    void asm_dec_r32(X86Reg r32) {
        m_code.push_back(m_al, 0x48+r32);
        EMIT("inc " + r2s(r32));
    }

Ironically, it happens that I unknowingly fixed it in WASM_X64: Minor fix in asm_add_r64_r64() and asm_dec_r32().

@ubaidsk ubaidsk marked this pull request as ready for review December 24, 2022 04:31
@ubaidsk
Copy link
Collaborator Author

ubaidsk commented Dec 24, 2022

This is ready. Please possibly review and please share feedback.

@ubaidsk ubaidsk requested a review from certik December 24, 2022 04:31
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.

Looks great!

@certik certik merged commit d916a2b into lcompilers:main Dec 24, 2022
@ubaidsk ubaidsk deleted the wasm_x86_fix_text_format branch December 24, 2022 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants