Skip to content

Commit c9edeb4

Browse files
SARG-FBjayrm
authored andcommitted
gas64 : - regs not freed when transfering regs for argument
- removal of irvreg not used in emit_macro - in emit_convert enum is now equivalent to uint - missing space in one asm_code() - changes/removals of comments
1 parent cf51189 commit c9edeb4

File tree

1 file changed

+52
-39
lines changed

1 file changed

+52
-39
lines changed

src/compiler/ir-gas64.bas

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ rbp+24 param 2
6363
rbp+16 param 1 calling --> rsp + x / called --> rbp + 16, 24,.. to 8+x*8
6464
return addr (after call)
6565
rbp old rbp
66-
rbp-8 rax 14 saved registers (arbitrary even not used) --> rbp -8 to -12
66+
rbp-8 rax 14 saved registers (arbitrary even not used) --> rbp -8 to -112
6767
-16 rbx
6868
-24 rcx
6969
-32 rdx
@@ -158,7 +158,7 @@ if variadic register save area, only for parameters not defined
158158
#define KREGFREE -2 ''register not free
159159
#define KREGRSVD -3 ''register reserved
160160
#define KREGLOCK -4 ''register locked (used as parameter)
161-
#define KREGUPPER 15
161+
#define KREGUPPER 15 ''registers 0 to 15 / 16=rip /17=dummy reg to avoid crash
162162
#define KNOTFOUND -1
163163
#define KSIZEPROCTXT 4000000 ''initial size of proc_txt used for speed up text adding
164164
#define KLIMITROOM 150 ''upper limit for spilling (number of room = KLIMITROOM+1, zero based)
@@ -473,7 +473,6 @@ private sub check_optim(byref code as string)
473473
code="#O4"+code+newline+string( ctx.indent*3, 32 )+newcode+" #Optim 4"
474474
end if
475475
else
476-
'print code;" part1="+part1+" part2="+part2+" prevpart1="+prevpart1+" prevpart2="+prevpart2
477476
if part2=prevpart1 and part1[0]=asc("r") then
478477
asm_info("OPTIMIZATION 5 (lea)")
479478
'asm_info("removed =lea "+prevpart1+", "+prevpart2)
@@ -1172,7 +1171,7 @@ private function hgetdatatype_asm64 _
11721171
select case as const dtype
11731172
'' UDT?
11741173
case FB_DATATYPE_STRUCT
1175-
''à creuser à un moment appel hGetDataType_asm64 avec direct subtype et non sym....
1174+
''to be checked hGetDataType_asm64 with direct subtype and not sym....
11761175
if subtype then
11771176
if( subtype->udt.dbg.typenum = INVALID ) then hDeclUDT_asm64( subtype, dimtbelements )
11781177
desc += str( subtype->udt.dbg.typenum )
@@ -1512,9 +1511,10 @@ private function reg_findfree(byval vreg as long,byval regparam as integer=-1) a
15121511
end function
15131512
''=====================================================================
15141513
'' in case of callptr avoid potential register used as parameter
1515-
'' eg op1=-1520[rdx] --> op1=-1520[r11] and mov r11, rdx
1514+
'' eg op1=-1520[rdx] --> op1=-1520[r11] and 'mov r11, rdx'
15161515

1517-
''todo find an more efficient way as it swaps all the POTENTIAL registers not only those laterly used
1516+
'' note : it swaps all the POTENTIAL registers not only those laterly used
1517+
'' op1=-1520[r8] --> op1=-1520[r11] and 'mov r11, r8' even if r8 will be not be used as parameter
15181518
''=====================================================================
15191519
private sub reg_callptr(byref op1 as string,byref op3 as string)
15201520
dim as long regfree
@@ -1665,11 +1665,15 @@ end function
16651665
''=====================================================================
16661666
private sub reg_transfer(byval regtrans as long,byval regsource as long)
16671667
dim as long regfree
1668-
1669-
''not used so nothing to do
1670-
if reghandle(regtrans)=KREGFREE then exit sub
1668+
''not used or already transfered in reg_callptr so nothing to do
1669+
if reghandle(regtrans)=KREGFREE or reghandle(regtrans)=KREGLOCK then exit sub
16711670
''used in source of parameter
1672-
if regtrans=regsource then exit sub
1671+
if regtrans=regsource then
1672+
''freeing the register
1673+
asm_info("in reg_transfer release regtrans="+*regstrq(regtrans))
1674+
reghandle(regtrans)=KREGFREE
1675+
exit sub
1676+
end if
16731677

16741678
''find a free register for transfering or just spilling if no free register (returning same register)
16751679
asm_info("Transfering register in other register otherwise spilling")
@@ -2340,7 +2344,6 @@ end function
23402344
private function _supportsop(byval op as integer,byval dtype as Integer) as integer
23412345
''return true if the function is availiable directly in the backend
23422346
'asm_info("_supportsop="+astdumpopToStr( op )+" "+typedumpToStr(dtype,0))
2343-
'Print "_supportsop="+astdumpopToStr( op )+" "+typedumpToStr(dtype,0)
23442347
select case as const( op )
23452348
'case AST_OP_SGN, AST_OP_FIX, AST_OP_FRAC, _
23462349
' AST_OP_ASIN, AST_OP_ACOS, AST_OP_TAN, AST_OP_ATAN, _
@@ -3141,7 +3144,7 @@ private sub bop_float( _
31413144
asm_code(lname1+":")
31423145
end if
31433146

3144-
case AST_OP_ADD,AST_OP_SUB,AST_OP_MUL,AST_OP_DIV ''todo optimiser xmm1 si memoire faire directement
3147+
case AST_OP_ADD,AST_OP_SUB,AST_OP_MUL,AST_OP_DIV ''todo optimize xmm1 if memory
31453148
select case op
31463149
case AST_OP_ADD
31473150
asm_code(addreg+"xmm0, xmm1")
@@ -3475,12 +3478,6 @@ private sub hloadoperandsandwritebop(byval op as integer,byval v1 as IRVREG ptr,
34753478
''op2 is a register
34763479
asm_code("or "+op1bis+", "+op2)
34773480
asm_code("mov "+op1+", "+op1bis)
3478-
'===
3479-
'asm_code("mov rax, "+op2)
3480-
'asm_code("not rax")
3481-
'asm_code("or "+op1+", rax")
3482-
'asm_code("not "+op2)
3483-
'asm_code("or "+op1+", "+op2)
34843481
end if
34853482
if vr<>0 then restore_vrreg(vr,vrreg)
34863483
case AST_OP_XOR
@@ -3567,13 +3564,6 @@ private sub hloadoperandsandwritebop(byval op as integer,byval v1 as IRVREG ptr,
35673564
if ctx.target=FB_COMPTARGET_LINUX andalso fbGetOption( FB_COMPOPT_OUTTYPE ) = FB_OUTTYPE_DYNAMICLIB then
35683565

35693566
if v1->sym<>0 andalso (symbIsCommon(v1->sym)) then
3570-
''todo if two op1 are common etc
3571-
'temporeg=reg_findfree(999998)
3572-
'regtempo1=*regstrq(temporeg)
3573-
'reghandle(temporeg)=KREGFREE
3574-
'asm_code("mov "+regtempo1+", "+*symbGetMangledName(v1->sym)+"@GOTPCREL[rip]")
3575-
'op1=regtempo1
3576-
35773567
asm_code("mov rax, "+*symbGetMangledName(v1->sym)+"@GOTPCREL[rip]")
35783568
asm_code("mov rax, [rax]")
35793569
op1="rax"
@@ -3601,8 +3591,6 @@ private sub hloadoperandsandwritebop(byval op as integer,byval v1 as IRVREG ptr,
36013591
if tempodtype<>FB_DATATYPE_BOOLEAN then
36023592
asm_code("neg "+*regstrq(vrreg))
36033593
end if
3604-
'asm_code("movzx rax, al") ''eventually neg rax
3605-
'' or used movzx eax, al / neg eax / cdqe --> result in rax
36063594
restore_vrreg(vr,vrreg)
36073595
else
36083596
asm_code("j"+suffix+" "+*symbGetMangledName( label ))
@@ -3989,7 +3977,6 @@ private sub _emituop(byval op as integer,byval v1 as IRVREG ptr,byval vr as IRVR
39893977
asm_code("mov "+*regstrq(reg_findreal(tempo))+", "+*regstrq(KREG_RCX))
39903978
op1=*regstrq(reg_findreal(tempo))
39913979
if vrreg=KREG_RCX then vrreg=reg_findreal(tempo)
3992-
'if op1=*regstrq(KREG_RCX) then op1=*regstrq(reg_findreal(tempo))
39933980
else
39943981
ctx.usedreg Or=(1 Shl KREG_RCX)
39953982
end if
@@ -4138,6 +4125,7 @@ private sub _emitconvert( byval v1 as IRVREG ptr, byval v2 as IRVREG ptr )
41384125

41394126
if (v1dtype=FB_DATATYPE_INTEGER and v2dtype=FB_DATATYPE_LONGINT) or (v2dtype=FB_DATATYPE_INTEGER and v1dtype=FB_DATATYPE_LONGINT) _
41404127
or (v1dtype=FB_DATATYPE_INTEGER and v2dtype=FB_DATATYPE_ENUM) or (v2dtype=FB_DATATYPE_INTEGER and v1dtype=FB_DATATYPE_ENUM) _
4128+
or (v1dtype=FB_DATATYPE_UINT and v2dtype=FB_DATATYPE_ENUM) or (v2dtype=FB_DATATYPE_UINT and v1dtype=FB_DATATYPE_ENUM) _
41414129
Or (v1dtype=FB_DATATYPE_UINT and v2dtype=FB_DATATYPE_ULONGINT) Or (v2dtype=FB_DATATYPE_UINT and v1dtype=FB_DATATYPE_ULONGINT) then
41424130
asm_info("no convert as exactly same datatype")
41434131
*v1=*v2
@@ -5260,7 +5248,8 @@ private sub hdocall(byval proc as FBSYMBOL ptr,byref pname as string,byref first
52605248
if v2->sym=0 then
52615249
if v2->vidx->sym=0 then
52625250
if v2->vidx->reg<>-1 then
5263-
op1=Str(v2->ofs)+"["+*regstrq(reg_findreal(v2->vidx->reg))+"]"
5251+
reg2=reg_findreal(v2->vidx->reg)
5252+
op1=Str(v2->ofs)+"["+*regstrq(reg2)+"]"
52645253
else
52655254
''2 levels
52665255
reg2=reg_findreal(v2->vidx->vidx->reg)
@@ -5288,7 +5277,8 @@ private sub hdocall(byval proc as FBSYMBOL ptr,byref pname as string,byref first
52885277
end if
52895278

52905279
if v2->vidx->typ=IR_VREGTYPE_REG then
5291-
op1="["+regtempo+"+"+*regstrq(reg_findreal(v2->vidx->reg))+"]"
5280+
reg2=reg_findreal(v2->vidx->reg)
5281+
op1="["+regtempo+"+"+*regstrq(reg2)+"]"
52925282
elseif v2->vidx->typ=IR_VREGTYPE_VAR then
52935283
if symbIsStatic(v2->vidx->sym) Or symbisshared(v2->vidx->sym) then
52945284
op3+=newline2+"add "+regtempo+", "+*symbGetMangledName(v2->vidx->sym)+"[rip+"+Str(v2->vidx->ofs)+"]"
@@ -5326,7 +5316,8 @@ private sub hdocall(byval proc as FBSYMBOL ptr,byref pname as string,byref first
53265316
end if
53275317

53285318
case IR_VREGTYPE_PTR ''format ofs1 <vidx=reg>
5329-
op1=Str(v2->ofs)+"["+*regstrq(reg_findreal(v2->vidx->reg))+"]"
5319+
reg2=reg_findreal(v2->vidx->reg)
5320+
op1=Str(v2->ofs)+"["+*regstrq(reg2)+"]"
53305321

53315322
case IR_VREGTYPE_OFS ''format varname ofs1 static ofs1 could be zero
53325323
op1=*symbGetMangledName(v2->sym)+"[rip+"+str(v2->ofs)+"]"
@@ -5778,7 +5769,8 @@ private sub hdocall(byval proc as FBSYMBOL ptr,byref pname as string,byref first
57785769
asm_code(pushstr(istr))
57795770
next
57805771
end if
5781-
5772+
''unlock registers to avoid save just below
5773+
if callptr then reg_allowed(true)
57825774
''preparing the save of registers in use by the calling and that can be used by the called
57835775
reg_save
57845776

@@ -6077,7 +6069,7 @@ private sub _emitmem(byval op as integer,byval v1 as IRVREG ptr,byval v2 as IRVR
60776069

60786070
if v2->typ=IR_VREGTYPE_REG then ''todo to be replace by repsto
60796071
op2=*regstrq(reg_findreal(v2->reg))
6080-
asm_code("test "+op2+","+op2)
6072+
asm_code("test "+op2+", "+op2)
60816073
lname2=*symbUniqueLabel( )
60826074
''zero byte to clear so skip
60836075
asm_code("jz "+lname2)
@@ -6251,7 +6243,6 @@ end sub
62516243
private sub _emitcomment( byval text as zstring ptr )
62526244
#ifdef basicdata
62536245
if text=0 Or LTrim(*text)="" Or left(ltrim(*text, Any Chr(32)+Chr(9)),1)="'" then exit sub
6254-
'print left(Trim(*text),20)
62556246

62566247
hWriteasm64 ( "# -----------------------------------------")
62576248
hWriteasm64 ( "# basic --> " + Trim(*text, Any " "+Chr(9)) )
@@ -6393,17 +6384,16 @@ private sub _emitprocbegin(byval proc as FBSYMBOL ptr,byval initlabel as FBSYMBO
63936384
asm_info( hEmitProcHeader( proc) )
63946385

63956386
''variadic ?
6396-
var param = symbGetProcLastParam( proc )
6397-
while( param )
6387+
var param = symbGetProcTailParam( proc )
6388+
if param then
63986389
if( symbGetParamMode( param ) = FB_PARAMMODE_VARARG ) then
63996390
ctx.variadic=true
64006391
if ctx.target=FB_COMPTARGET_LINUX then
64016392
''14 registers (6 rxx and 8 xmmn) could be used for arguments, the others are on stack
64026393
ctx.stk+=14*8
64036394
end if
64046395
end if
6405-
param = symbGetProcPrevParam( proc, param )
6406-
wend
6396+
end if
64076397

64086398
asm_code(".text")
64096399
if symbisprivate(proc)=FALSE then
@@ -6597,7 +6587,7 @@ end sub
65976587

65986588
private sub _emitMacro( byval op as integer,byval v1 as IRVREG ptr, byval v2 as IRVREG ptr, byval vr as IRVREG ptr )
65996589
dim as IRVREG ptr tempo1,tempo2
6600-
dim as long savereg,startarg
6590+
dim as long savereg,startarg,vreg
66016591
dim as string regvalist,lname1,lname2
66026592
asm_info( "Macro op=" + astdumpopToStr( op ))
66036593
asm_info("v1="+vregdumpfull(v1))
@@ -6637,6 +6627,18 @@ private sub _emitMacro( byval op as integer,byval v1 as IRVREG ptr, byval v2 as
66376627
_emitstore(v1,tempo1)
66386628
end if
66396629

6630+
if v2->typ=IR_VREGTYPE_PTR then
6631+
''removal of useless register as v2 not used / PTR -> ofs1 <vidx=reg>
6632+
vreg=v2->vidx->reg
6633+
for ireg as integer =1 to KREGUPPER
6634+
if reghandle(ireg)=vreg then
6635+
asm_info("VA_START release for PTR reg not used="+str(vreg))
6636+
reghandle(ireg)=KREGFREE
6637+
exit for
6638+
end if
6639+
next
6640+
end if
6641+
66406642
case AST_OP_VA_ARG
66416643

66426644
'# -----------------------------------------
@@ -6717,6 +6719,17 @@ private sub _emitMacro( byval op as integer,byval v1 as IRVREG ptr, byval v2 as
67176719

67186720
case AST_OP_VA_END
67196721
asm_info("Call emit macro END nothing to do ?")
6722+
if v1->typ=IR_VREGTYPE_PTR then
6723+
''removal of useless register as v1 not used / PTR -> ofs1 <vidx=reg>
6724+
vreg=v1->vidx->reg
6725+
for ireg as integer =1 to KREGUPPER
6726+
if reghandle(ireg)=vreg then
6727+
asm_info("VA_END release for PTR reg not used="+str(vreg))
6728+
reghandle(ireg)=KREGFREE
6729+
exit for
6730+
end if
6731+
next
6732+
end if
67206733
case AST_OP_VA_COPY
67216734
if ctx.target=FB_COMPTARGET_LINUX then
67226735
''linux

0 commit comments

Comments
 (0)