#50 New Option to FRAME

open
nobody
None
5
2013-04-24
2013-04-15
habran
No

I would like to free RBP register so that it is not used for setting the frame for locals
It is pity to waist it if not really neccessery
I have looked MSVC code and it doesn't use RBP register for the frame
so it can use all 8 nonvolatile registers for locals
here is how I envision implementation:

somefunction PROC FRAME USES r12 r13 r14 r15 somevar:QWORD
mov [rsp+8], rbp ;these 4 registers could be stored
mov [rsp+16], rbx ;in a shadow space
mov [rsp+24], rdi ;if we need to use rcx, rdx, r8, r9
mov [rsp+32], rsi ;we can put them in rbp, rbx, rdi, rsi
push r12
push r13
push r14
push r15
sub rsp, 1040 ;locals + @ReservedStack
;some code here
;..............
;
lea rsp, [rsp+1040]
pop r15
pop r14
pop r13
pop r12
mov rbp, [rsp+8]
mov rbx, [rsp+16]
mov rdi, [rsp+24]
mov rsi, [rsp+32]
ret
somefunction ENDP

regards

habran

Discussion

  • japheth
    japheth
    2013-04-16

    > I have looked MSVC code and it doesn't use RBP register for the frame

    Yes, but unlike an assembler a C compiler has full control over registers.

    It might be possible to somehow introduce a "frame pointer omission" option in jwasm, but the effect of such an option will have to be more generic than your proposal, IMO.

     
  • habran
    habran
    2013-04-16

    >Yes, but unlike an assembler a C compiler has full control over registers.

    IMO, ASSEMBLER should have more control over registers than C
    that's why we are ASM programmers

    >It might be possible to somehow introduce a "frame pointer omission" option

    Do it your way, more generic than my proposal, as long as you FREE the RBP register

    Problem solving will keep your brains young;)

    OPTION FRAME_POINTER_OMISSION sounds good to me

    best regards

     
  • habran
    habran
    2013-04-24

    Hi Japheth
    I am back
    have look what Tomas Gristar has done for Fasm
    can you understand these macros?
    can we also create simular macros?

    ;First example is the static-RSP procedure frame for 64-bit case.
    ;These macros define the common frame for all the procedure calls
    ;inside the "proc" (the same as "frame" macro) and therefore assume
    ;that RSP is not changed during the procedure life. This allows to
    ;access the parameters and locals with RSP-based addresses.
    ;The instructions are longer that with RBP-based addressing,
    ;however you get the simpler code and RBP register free for
    ;any custom usage. Choose whatever is more suitable for your needs.

    macro static_rsp_prologue procname,flag,parmbytes,localbytes,reglist
    { local counter,loc,regs,frame,current
    loc = (localbytes+7) and (not 7)
    counter = 0
    irps reg, reglist { counter = counter+1 }
    regs = 8*( counter + (counter+loc shr 3+1) and 1 )
    totalbytes@proc equ frame+loc+regs
    sub rsp,totalbytes@proc
    localbase@proc equ rsp+frame
    regsbase@proc equ rsp+frame+loc
    parmbase@proc equ rsp+frame+loc+regs+8
    current = 0
    current@frame equ current
    size@frame equ frame
    counter = 0
    irps reg, reglist { mov [regsbase@proc+8*counter],reg
    counter = counter+1 } }

    macro static_rsp_epilogue procname,flag,parmbytes,localbytes,reglist
    { local counter
    counter = 0
    irps reg, reglist { mov reg,[regsbase@proc+8*counter]
    counter = counter+1 }
    add rsp,totalbytes@proc
    retn }

    macro static_rsp_close procname,flag,parmbytes,localbytes,reglist
    { size@frame = current@frame
    restore size@frame,current@frame }

    prologue@proc equ static_rsp_prologue
    epilogue@proc equ static_rsp_epilogue
    close@proc equ static_rsp_close

    proc WindowProc uses rbx rsi rdi, hwnd,wmsg,wparam,lparam
    locals
    rc RECT
    pfd PIXELFORMATDESCRIPTOR
    endl
    mov [hwnd],rcx
    cmp edx,WM_CREATE
    je .wmcreate
    cmp edx,WM_SIZE
    je .wmsize
    cmp edx,WM_PAINT
    je .wmpaint
    cmp edx,WM_KEYDOWN
    je .wmkeydown
    cmp edx,WM_DESTROY
    je .wmdestroy
    .defwndproc:
    invoke DefWindowProc,rcx,rdx,r8,r9
    jmp .finish
    .wmcreate:
    invoke GetDC,rcx
    mov [hdc],rax
    lea rdi,[pfd]
    mov rcx,sizeof.PIXELFORMATDESCRIPTOR shr 3
    xor eax,eax
    rep stosq
    mov [pfd.nSize],sizeof.PIXELFORMATDESCRIPTOR
    mov [pfd.nVersion],1
    mov [pfd.dwFlags],PFD_SUPPORT_OPENGL+PFD_DOUBLEBUFFER+PFD_DRAW_TO_WINDOW
    mov [pfd.iLayerType],PFD_MAIN_PLANE
    mov [pfd.iPixelType],PFD_TYPE_RGBA
    mov [pfd.cColorBits],16
    mov [pfd.cDepthBits],16
    mov [pfd.cAccumBits],0
    mov [pfd.cStencilBits],0
    invoke ChoosePixelFormat,[hdc],addr pfd
    invoke SetPixelFormat,[hdc],eax,addr pfd
    invoke wglCreateContext,[hdc]
    mov [hrc],rax
    invoke wglMakeCurrent,[hdc],[hrc]
    invoke GetClientRect,[hwnd],addr rc
    invoke glViewport,0,0,[rc.right],[rc.bottom]
    invoke GetTickCount
    mov [clock],eax
    xor eax,eax
    jmp .finish
    .wmsize:
    invoke GetClientRect,[hwnd],addr rc
    invoke glViewport,0,0,[rc.right],[rc.bottom]
    xor eax,eax
    jmp .finish
    .wmpaint:
    invoke GetTickCount
    sub eax,[clock]
    cmp eax,10
    jb .animation_ok
    add [clock],eax
    invoke glRotatef,float [theta],float dword 0.0,float dword 0.0,float dword 1.0
    .animation_ok:
    invoke glClear,GL_COLOR_BUFFER_BIT
    invoke glBegin,GL_QUADS
    invoke glColor3f,float dword 1.0,float dword 0.1,float dword 0.1
    invoke glVertex3d,float -0.6,float -0.6,float 0.0
    invoke glColor3f,float dword 0.1,float dword 0.1,float dword 0.1
    invoke glVertex3d,float 0.6,float -0.6,float 0.0
    invoke glColor3f,float dword 0.1,float dword 0.1,float dword 1.0
    invoke glVertex3d,float 0.6,float 0.6,float 0.0
    invoke glColor3f,float dword 1.0,float dword 0.1,float dword 1.0
    invoke glVertex3d,float -0.6,float 0.6,float 0.0
    invoke glEnd
    invoke SwapBuffers,[hdc]
    xor eax,eax
    jmp .finish
    .wmkeydown:
    cmp r8d,VK_ESCAPE
    jne .defwndproc
    .wmdestroy:
    invoke wglMakeCurrent,0,0
    invoke wglDeleteContext,[hrc]
    invoke ReleaseDC,[hwnd],[hdc]
    invoke PostQuitMessage,0
    xor eax,eax
    .finish:
    ret
    endp
    compiled code:

    00000362:a4883EC78 sub (q) rsp,+78
    00000366:a48895C2458 mov [rsp+58],rbx
    0000036B:a4889742460 mov [rsp+60],rsi
    00000370:a48897C2468 mov [rsp+68],rdi
    00000375:a48898C2480000000 mov [rsp+00000080],rcx
    0000037D:a83FA01 cmp (d) edx,+01
    00000380:a7432 je file:000003B4
    00000382:a83FA05 cmp (d) edx,+05
    00000385:a0F840D010000 je file:00000498
    0000038B:a83FA0F cmp (d) edx,+0F
    0000038E:a0F843C010000 je file:000004D0
    00000394:a81FA00010000 cmp edx,00000100
    0000039A:a0F84E5020000 je file:00000685
    000003A0:a83FA02 cmp (d) edx,+02
    000003A3:a0F84E6020000 je file:0000068F
    000003A9:aFF15D11F0000 call (q) [rip+00001FD1]
    000003AF:aE920030000 jmpn file:000006D4
    000003B4:aFF15FE1F0000 call (q) [rip+00001FFE]
    000003BA:a488905A70E0000 mov [rip+00000EA7],rax
    000003C1:a488D7C2430 lea rdi,[rsp+30]
    000003C6:a48C7C105000000 mov rcx,00000005
    000003CD:a31C0 xor eax,eax
    000003CF:aF348AB rep; stosq
    000003D2:a66C74424302800 mov [rsp+30],0028
    000003D9:a66C74424320100 mov [rsp+32],0001
    000003E0:aC744243425000000 mov [rsp+34],00000025
    000003E8:aC644244A00 mov [rsp+4A],00
    000003ED:aC644243800 mov [rsp+38],00
    000003F2:aC644243910 mov [rsp+39],10
    000003F7:aC644244710 mov [rsp+47],10
    000003FC:aC644244200 mov [rsp+42],00
    00000401:aC644244800 mov [rsp+48],00
    00000406:a488B0D5B0E0000 mov rcx,[rip+00000E5B]
    0000040D:a488D542430 lea rdx,[rsp+30]
    00000412:aFF159C200000 call (q) [rip+0000209C]
    00000418:a488B0D490E0000 mov rcx,[rip+00000E49]
    0000041F:a89C2 mov edx,eax
    00000421:a4C8D442430 lea r8,[rsp+30]
    00000426:aFF1590200000 call (q) [rip+00002090]
    0000042C:a488B0D350E0000 mov rcx,[rip+00000E35]
    00000433:aFF155F210000 call (q) [rip+0000215F]
    00000439:a488905300E0000 mov [rip+00000E30],rax
    00000440:a488B0D210E0000 mov rcx,[rip+00000E21]
    00000447:a488B15220E0000 mov rdx,[rip+00000E22]
    0000044E:aFF1554210000 call (q) [rip+00002154]
    00000454:a488B8C2480000000 mov rcx,[rsp+00000080]
    0000045C:a488D542420 lea rdx,[rsp+20]
    00000461:aFF15491F0000 call (q) [rip+00001F49]
    00000467:a48C7C100000000 mov rcx,00000000
    0000046E:a48C7C200000000 mov rdx,00000000
    00000475:a448B442428 mov r8d,[rsp+28]
    0000047A:a448B4C242C mov r9d,[rsp+2C]
    0000047F:aFF150B210000 call (q) [rip+0000210B]
    00000485:aFF15331E0000 call (q) [rip+00001E33]
    0000048B:a8905130E0000 mov [rip+00000E13],eax
    00000491:a31C0 xor eax,eax
    00000493:aE93C020000 jmpn file:000006D4
    00000498:a488B8C2480000000 mov rcx,[rsp+00000080]
    000004A0:a488D542420 lea rdx,[rsp+20]
    000004A5:aFF15051F0000 call (q) [rip+00001F05]
    000004AB:a48C7C100000000 mov rcx,00000000
    000004B2:a48C7C200000000 mov rdx,00000000
    000004B9:a448B442428 mov r8d,[rsp+28]
    000004BE:a448B4C242C mov r9d,[rsp+2C]
    000004C3:aFF15C7200000 call (q) [rip+000020C7]
    000004C9:a31C0 xor eax,eax
    000004CB:aE904020000 jmpn file:000006D4
    000004D0:aFF15E81D0000 call (q) [rip+00001DE8]
    000004D6:a2B05C80D0000 sub eax,[rip+00000DC8]
    000004DC:a83F80A cmp (d) eax,+0A
    000004DF:a722F jc file:00000510
    000004E1:a0105BD0D0000 add [rip+00000DBD],eax
    000004E7:a660F6E052D0D0000 movd xmm0,[rip+00000D2D]
    000004EF:aB800000000 mov eax,00000000
    000004F4:a660F6EC8 movd xmm1,eax
    000004F8:aB800000000 mov eax,00000000
    000004FD:a660F6ED0 movd xmm2,eax
    00000501:aB80000803F mov eax,3F800000
    00000506:a660F6ED8 movd xmm3,eax
    0000050A:aFF1570200000 call (q) [rip+00002070]
    00000510:a48C7C100400000 mov rcx,00004000
    00000517:aFF154B200000 call (q) [rip+0000204B]
    0000051D:a48C7C107000000 mov rcx,00000007
    00000524:aFF1536200000 call (q) [rip+00002036]
    0000052A:aB80000803F mov eax,3F800000
    0000052F:a660F6EC0 movd xmm0,eax
    00000533:aB8CDCCCC3D mov eax,3DCCCCCD
    00000538:a660F6EC8 movd xmm1,eax
    0000053C:aB8CDCCCC3D mov eax,3DCCCCCD
    00000541:a660F6ED0 movd xmm2,eax
    00000545:aFF1525200000 call (q) [rip+00002025]
    0000054B:a48B8333333333333E3BF mov rax,BFE3333333333333
    00000555:a66480F6EC0 movd xmm0,rax
    0000055A:a48B8333333333333E3BF mov rax,BFE3333333333333
    00000564:a66480F6EC8 movd xmm1,rax
    00000569:a48C7C000000000 mov rax,00000000
    00000570:a66480F6ED0 movd xmm2,rax
    00000575:aFF150D200000 call (q) [rip+0000200D]
    0000057B:aB8CDCCCC3D mov eax,3DCCCCCD
    00000580:a660F6EC0 movd xmm0,eax
    00000584:aB8CDCCCC3D mov eax,3DCCCCCD
    00000589:a660F6EC8 movd xmm1,eax
    0000058D:aB8CDCCCC3D mov eax,3DCCCCCD
    00000592:a660F6ED0 movd xmm2,eax
    00000596:aFF15D41F0000 call (q) [rip+00001FD4]
    0000059C:a48B8333333333333E33F mov rax,3FE3333333333333
    000005A6:a66480F6EC0 movd xmm0,rax
    000005AB:a48B8333333333333E3BF mov rax,BFE3333333333333
    000005B5:a66480F6EC8 movd xmm1,rax
    000005BA:a48C7C000000000 mov rax,00000000
    000005C1:a66480F6ED0 movd xmm2,rax
    000005C6:aFF15BC1F0000 call (q) [rip+00001FBC]
    000005CC:aB8CDCCCC3D mov eax,3DCCCCCD
    000005D1:a660F6EC0 movd xmm0,eax
    000005D5:aB8CDCCCC3D mov eax,3DCCCCCD
    000005DA:a660F6EC8 movd xmm1,eax
    000005DE:aB80000803F mov eax,3F800000
    000005E3:a660F6ED0 movd xmm2,eax
    000005E7:aFF15831F0000 call (q) [rip+00001F83]
    000005ED:a48B8333333333333E33F mov rax,3FE3333333333333
    000005F7:a66480F6EC0 movd xmm0,rax
    000005FC:a48B8333333333333E33F mov rax,3FE3333333333333
    00000606:a66480F6EC8 movd xmm1,rax
    0000060B:a48C7C000000000 mov rax,00000000
    00000612:a66480F6ED0 movd xmm2,rax
    00000617:aFF156B1F0000 call (q) [rip+00001F6B]
    0000061D:aB80000803F mov eax,3F800000
    00000622:a660F6EC0 movd xmm0,eax
    00000626:aB8CDCCCC3D mov eax,3DCCCCCD
    0000062B:a660F6EC8 movd xmm1,eax
    0000062F:aB80000803F mov eax,3F800000
    00000634:a660F6ED0 movd xmm2,eax
    00000638:aFF15321F0000 call (q) [rip+00001F32]
    0000063E:a48B8333333333333E3BF mov rax,BFE3333333333333
    00000648:a66480F6EC0 movd xmm0,rax
    0000064D:a48B8333333333333E33F mov rax,3FE3333333333333
    00000657:a66480F6EC8 movd xmm1,rax
    0000065C:a48C7C000000000 mov rax,00000000
    00000663:a66480F6ED0 movd xmm2,rax
    00000668:aFF151A1F0000 call (q) [rip+00001F1A]
    0000066E:aFF15041F0000 call (q) [rip+00001F04]
    00000674:a488B0DED0B0000 mov rcx,[rip+00000BED]
    0000067B:aFF15431E0000 call (q) [rip+00001E43]
    00000681:a31C0 xor eax,eax
    00000683:aEB4F jmps file:000006D4
    00000685:a4183F81B cmp (d) r8d,+1B
    00000689:a0F851AFDFFFF jne file:000003A9
    0000068F:a48C7C100000000 mov rcx,00000000
    00000696:a48C7C200000000 mov rdx,00000000
    0000069D:aFF15051F0000 call (q) [rip+00001F05]
    000006A3:a488B0DC60B0000 mov rcx,[rip+00000BC6]
    000006AA:aFF15F01E0000 call (q) [rip+00001EF0]
    000006B0:a488B8C2480000000 mov rcx,[rsp+00000080]
    000006B8:a488B15A90B0000 mov rdx,[rip+00000BA9]
    000006BF:aFF15FB1C0000 call (q) [rip+00001CFB]
    000006C5:a48C7C100000000 mov rcx,00000000
    000006CC:aFF15F61C0000 call (q) [rip+00001CF6]
    000006D2:a31C0 xor eax,eax
    000006D4:a488B5C2458 mov rbx,[rsp+58]
    000006D9:a488B742460 mov rsi,[rsp+60]
    000006DE:a488B7C2468 mov rdi,[rsp+68]
    000006E3:a4883C478 add (q) rsp,+78
    000006E7:aC3 retn