cgx86.pas 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741
  1. {
  2. Copyright (c) 1998-2005 by Florian Klaempfl
  3. This unit implements the common parts of the code generator for the i386 and the x86-64.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { This unit implements the common parts of the code generator for the i386 and the x86-64.
  18. }
  19. unit cgx86;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,
  24. cgbase,cgutils,cgobj,
  25. aasmbase,aasmtai,aasmdata,aasmcpu,
  26. cpubase,cpuinfo,rgobj,rgx86,rgcpu,
  27. symconst,symtype,symdef;
  28. type
  29. { tcgx86 }
  30. tcgx86 = class(tcg)
  31. rgfpu : Trgx86fpu;
  32. procedure done_register_allocators;override;
  33. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;override;
  34. function getmmxregister(list:TAsmList):Tregister;
  35. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;override;
  36. procedure getcpuregister(list:TAsmList;r:Tregister);override;
  37. procedure ungetcpuregister(list:TAsmList;r:Tregister);override;
  38. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  39. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  40. function uses_registers(rt:Tregistertype):boolean;override;
  41. procedure add_reg_instruction(instr:Tai;r:tregister);override;
  42. procedure dec_fpu_stack;
  43. procedure inc_fpu_stack;
  44. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  45. procedure a_call_name_near(list : TAsmList;const s : string; weak: boolean);
  46. procedure a_call_name_static(list : TAsmList;const s : string);override;
  47. procedure a_call_name_static_near(list : TAsmList;const s : string);
  48. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  49. procedure a_call_reg_near(list : TAsmList;reg : tregister);
  50. procedure a_call_ref(list : TAsmList;ref : treference);override;
  51. procedure a_call_ref_near(list : TAsmList;ref : treference);
  52. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  53. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  54. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  55. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  56. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  57. procedure a_op_const_reg_reg(list : TAsmList; op : Topcg; size : Tcgsize; a : tcgint; src,dst : Tregister); override;
  58. procedure a_op_reg_reg_reg(list : TAsmList; op : TOpCg; size : tcgsize; src1,src2,dst : tregister); override;
  59. { move instructions }
  60. procedure a_load_const_reg(list : TAsmList; tosize: tcgsize; a : tcgint;reg : tregister);override;
  61. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  62. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);override;
  63. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);override;
  64. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize: tcgsize;reg1,reg2 : tregister);override;
  65. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  66. { bit scan instructions }
  67. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: TCGSize; src, dst: TRegister); override;
  68. { fpu move instructions }
  69. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  70. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  71. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  72. { vector register move instructions }
  73. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  74. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  75. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  76. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  77. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);override;
  78. procedure a_opmm_ref_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;const ref : treference;src,dst : tregister;shuffle : pmmshuffle);override;
  79. procedure a_opmm_reg_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;src1,src2,dst : tregister;shuffle : pmmshuffle);override;
  80. { comparison operations }
  81. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  82. l : tasmlabel);override;
  83. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  84. l : tasmlabel);override;
  85. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  86. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister; l : tasmlabel); override;
  87. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); override;
  88. procedure a_jmp_name(list : TAsmList;const s : string);override;
  89. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  90. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  91. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  92. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference); override;
  93. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  94. { entry/exit code helpers }
  95. procedure g_profilecode(list : TAsmList);override;
  96. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  97. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  98. procedure g_save_registers(list: TAsmList); override;
  99. procedure g_restore_registers(list: TAsmList); override;
  100. procedure g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);override;
  101. procedure g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string); override;
  102. procedure make_simple_ref(list:TAsmList;var ref: treference);
  103. protected
  104. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  105. procedure check_register_size(size:tcgsize;reg:tregister);
  106. procedure opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  107. procedure opmm_loc_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;loc : tlocation;src,dst : tregister;shuffle : pmmshuffle);
  108. function get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  109. procedure sizes2load(s1,s2 : tcgsize;var op: tasmop; var s3: topsize);
  110. procedure floatload(list: TAsmList; t : tcgsize;const ref : treference);
  111. procedure floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  112. procedure floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  113. procedure floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  114. procedure internal_restore_regs(list: TAsmList; use_pop: boolean);
  115. end;
  116. const
  117. {$if defined(x86_64)}
  118. TCGSize2OpSize: Array[tcgsize] of topsize =
  119. (S_NO,S_B,S_W,S_L,S_Q,S_XMM,S_B,S_W,S_L,S_Q,S_XMM,
  120. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  121. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,
  122. S_NO,S_NO,S_NO,S_NO,S_XMM,S_YMM);
  123. {$elseif defined(i386)}
  124. TCGSize2OpSize: Array[tcgsize] of topsize =
  125. (S_NO,S_B,S_W,S_L,S_L,S_T,S_B,S_W,S_L,S_L,S_L,
  126. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  127. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,
  128. S_NO,S_NO,S_NO,S_NO,S_XMM,S_YMM);
  129. {$elseif defined(i8086)}
  130. TCGSize2OpSize: Array[tcgsize] of topsize =
  131. (S_NO,S_B,S_W,S_W,S_W,S_T,S_B,S_W,S_W,S_W,S_W,
  132. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  133. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,
  134. S_NO,S_NO,S_NO,S_NO,S_XMM,S_YMM);
  135. {$endif}
  136. {$ifndef NOTARGETWIN}
  137. winstackpagesize = 4096;
  138. {$endif NOTARGETWIN}
  139. function UseAVX: boolean;
  140. function UseIncDec: boolean;
  141. implementation
  142. uses
  143. globals,verbose,systems,cutils,
  144. defutil,paramgr,procinfo,
  145. tgobj,ncgutil,
  146. fmodule,symsym;
  147. function UseAVX: boolean;
  148. begin
  149. Result:=current_settings.fputype in fpu_avx_instructionsets;
  150. end;
  151. { modern CPUs prefer add/sub over inc/dec because add/sub break instructions dependencies on flags
  152. because they modify all flags }
  153. function UseIncDec: boolean;
  154. begin
  155. {$if defined(x86_64)}
  156. Result:=cs_opt_size in current_settings.optimizerswitches;
  157. {$elseif defined(i386)}
  158. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.cputype in [cpu_386]);
  159. {$elseif defined(i8086)}
  160. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.cputype in [cpu_8086..cpu_386]);
  161. {$endif}
  162. end;
  163. const
  164. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_MOV,A_ADD,A_AND,A_DIV,
  165. A_IDIV,A_IMUL,A_MUL,A_NEG,A_NOT,A_OR,
  166. A_SAR,A_SHL,A_SHR,A_SUB,A_XOR,A_ROL,A_ROR);
  167. TOpCmp2AsmCond: Array[topcmp] of TAsmCond = (C_NONE,
  168. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  169. procedure Tcgx86.done_register_allocators;
  170. begin
  171. rg[R_INTREGISTER].free;
  172. rg[R_MMREGISTER].free;
  173. rg[R_MMXREGISTER].free;
  174. rgfpu.free;
  175. inherited done_register_allocators;
  176. end;
  177. function Tcgx86.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  178. begin
  179. result:=rgfpu.getregisterfpu(list);
  180. end;
  181. function Tcgx86.getmmxregister(list:TAsmList):Tregister;
  182. begin
  183. if not assigned(rg[R_MMXREGISTER]) then
  184. internalerror(2003121214);
  185. result:=rg[R_MMXREGISTER].getregister(list,R_SUBNONE);
  186. end;
  187. function Tcgx86.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  188. begin
  189. if not assigned(rg[R_MMREGISTER]) then
  190. internalerror(2003121234);
  191. case size of
  192. OS_F64:
  193. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMD);
  194. OS_F32:
  195. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMS);
  196. OS_M64:
  197. result:=rg[R_MMREGISTER].getregister(list,R_SUBQ);
  198. OS_M128:
  199. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMWHOLE);
  200. else
  201. internalerror(200506041);
  202. end;
  203. end;
  204. procedure Tcgx86.getcpuregister(list:TAsmList;r:Tregister);
  205. begin
  206. if getregtype(r)=R_FPUREGISTER then
  207. internalerror(2003121210)
  208. else
  209. inherited getcpuregister(list,r);
  210. end;
  211. procedure tcgx86.ungetcpuregister(list:TAsmList;r:Tregister);
  212. begin
  213. if getregtype(r)=R_FPUREGISTER then
  214. rgfpu.ungetregisterfpu(list,r)
  215. else
  216. inherited ungetcpuregister(list,r);
  217. end;
  218. procedure Tcgx86.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  219. begin
  220. if rt<>R_FPUREGISTER then
  221. inherited alloccpuregisters(list,rt,r);
  222. end;
  223. procedure Tcgx86.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  224. begin
  225. if rt<>R_FPUREGISTER then
  226. inherited dealloccpuregisters(list,rt,r);
  227. end;
  228. function Tcgx86.uses_registers(rt:Tregistertype):boolean;
  229. begin
  230. if rt=R_FPUREGISTER then
  231. result:=false
  232. else
  233. result:=inherited uses_registers(rt);
  234. end;
  235. procedure tcgx86.add_reg_instruction(instr:Tai;r:tregister);
  236. begin
  237. if getregtype(r)<>R_FPUREGISTER then
  238. inherited add_reg_instruction(instr,r);
  239. end;
  240. procedure tcgx86.dec_fpu_stack;
  241. begin
  242. if rgfpu.fpuvaroffset<=0 then
  243. internalerror(200604201);
  244. dec(rgfpu.fpuvaroffset);
  245. end;
  246. procedure tcgx86.inc_fpu_stack;
  247. begin
  248. if rgfpu.fpuvaroffset>=7 then
  249. internalerror(2012062901);
  250. inc(rgfpu.fpuvaroffset);
  251. end;
  252. {****************************************************************************
  253. This is private property, keep out! :)
  254. ****************************************************************************}
  255. procedure tcgx86.sizes2load(s1,s2 : tcgsize; var op: tasmop; var s3: topsize);
  256. begin
  257. { ensure to have always valid sizes }
  258. if s1=OS_NO then
  259. s1:=s2;
  260. if s2=OS_NO then
  261. s2:=s1;
  262. case s2 of
  263. OS_8,OS_S8 :
  264. if S1 in [OS_8,OS_S8] then
  265. s3 := S_B
  266. else
  267. internalerror(200109221);
  268. OS_16,OS_S16:
  269. case s1 of
  270. OS_8,OS_S8:
  271. s3 := S_BW;
  272. OS_16,OS_S16:
  273. s3 := S_W;
  274. else
  275. internalerror(200109222);
  276. end;
  277. OS_32,OS_S32:
  278. case s1 of
  279. OS_8,OS_S8:
  280. s3 := S_BL;
  281. OS_16,OS_S16:
  282. s3 := S_WL;
  283. OS_32,OS_S32:
  284. s3 := S_L;
  285. else
  286. internalerror(200109223);
  287. end;
  288. {$ifdef x86_64}
  289. OS_64,OS_S64:
  290. case s1 of
  291. OS_8:
  292. s3 := S_BL;
  293. OS_S8:
  294. s3 := S_BQ;
  295. OS_16:
  296. s3 := S_WL;
  297. OS_S16:
  298. s3 := S_WQ;
  299. OS_32:
  300. s3 := S_L;
  301. OS_S32:
  302. s3 := S_LQ;
  303. OS_64,OS_S64:
  304. s3 := S_Q;
  305. else
  306. internalerror(200304302);
  307. end;
  308. {$endif x86_64}
  309. else
  310. internalerror(200109227);
  311. end;
  312. if s3 in [S_B,S_W,S_L,S_Q] then
  313. op := A_MOV
  314. else if s1 in [OS_8,OS_16,OS_32,OS_64] then
  315. op := A_MOVZX
  316. else
  317. {$ifdef x86_64}
  318. if s3 in [S_LQ] then
  319. op := A_MOVSXD
  320. else
  321. {$endif x86_64}
  322. op := A_MOVSX;
  323. end;
  324. procedure tcgx86.make_simple_ref(list:TAsmList;var ref: treference);
  325. var
  326. hreg : tregister;
  327. href : treference;
  328. {$ifndef x86_64}
  329. add_hreg: boolean;
  330. {$endif not x86_64}
  331. begin
  332. { make_simple_ref() may have already been called earlier, and in that
  333. case make sure we don't perform the PIC-simplifications twice }
  334. if (ref.refaddr in [addr_pic,addr_pic_no_got]) then
  335. exit;
  336. {$if defined(x86_64)}
  337. { Only 32bit is allowed }
  338. { Note that this isn't entirely correct: for RIP-relative targets/memory models,
  339. it is actually (offset+@symbol-RIP) that should fit into 32 bits. Since two last
  340. members aren't known until link time, ABIs place very pessimistic limits
  341. on offset values, e.g. SysV AMD64 allows +/-$1000000 (16 megabytes) }
  342. if ((ref.offset<low(longint)) or (ref.offset>high(longint))) or
  343. { absolute address is not a common thing in x64, but nevertheless a possible one }
  344. ((ref.base=NR_NO) and (ref.index=NR_NO) and (ref.symbol=nil)) then
  345. begin
  346. { Load constant value to register }
  347. hreg:=GetAddressRegister(list);
  348. list.concat(taicpu.op_const_reg(A_MOV,S_Q,ref.offset,hreg));
  349. ref.offset:=0;
  350. {if assigned(ref.symbol) then
  351. begin
  352. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_Q,ref.symbol,0,hreg));
  353. ref.symbol:=nil;
  354. end;}
  355. { Add register to reference }
  356. if ref.base=NR_NO then
  357. ref.base:=hreg
  358. else if ref.index=NR_NO then
  359. ref.index:=hreg
  360. else
  361. begin
  362. { don't use add, as the flags may contain a value }
  363. reference_reset_base(href,ref.base,0,8);
  364. href.index:=hreg;
  365. if ref.scalefactor<>0 then
  366. begin
  367. reference_reset_base(href,ref.base,0,8);
  368. href.index:=hreg;
  369. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  370. ref.base:=hreg;
  371. end
  372. else
  373. begin
  374. reference_reset_base(href,ref.index,0,8);
  375. href.index:=hreg;
  376. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.index,hreg));
  377. ref.index:=hreg;
  378. end;
  379. end;
  380. end;
  381. if assigned(ref.symbol) then
  382. begin
  383. if cs_create_pic in current_settings.moduleswitches then
  384. begin
  385. { Local symbols must not be accessed via the GOT }
  386. if (ref.symbol.bind=AB_LOCAL) then
  387. begin
  388. { unfortunately, RIP-based addresses don't support an index }
  389. if (ref.base<>NR_NO) or
  390. (ref.index<>NR_NO) then
  391. begin
  392. reference_reset_symbol(href,ref.symbol,0,ref.alignment);
  393. hreg:=getaddressregister(list);
  394. href.refaddr:=addr_pic_no_got;
  395. href.base:=NR_RIP;
  396. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  397. ref.symbol:=nil;
  398. end
  399. else
  400. begin
  401. ref.refaddr:=addr_pic_no_got;
  402. hreg:=NR_NO;
  403. ref.base:=NR_RIP;
  404. end;
  405. end
  406. else
  407. begin
  408. reference_reset_symbol(href,ref.symbol,0,ref.alignment);
  409. hreg:=getaddressregister(list);
  410. href.refaddr:=addr_pic;
  411. href.base:=NR_RIP;
  412. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,href,hreg));
  413. ref.symbol:=nil;
  414. end;
  415. if ref.base=NR_NO then
  416. ref.base:=hreg
  417. else if ref.index=NR_NO then
  418. begin
  419. ref.index:=hreg;
  420. ref.scalefactor:=1;
  421. end
  422. else
  423. begin
  424. { don't use add, as the flags may contain a value }
  425. reference_reset_base(href,ref.base,0,8);
  426. href.index:=hreg;
  427. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  428. ref.base:=hreg;
  429. end;
  430. end
  431. else
  432. { Always use RIP relative symbol addressing for Windows and Darwin targets. }
  433. if (target_info.system in (systems_all_windows+[system_x86_64_darwin])) and (ref.base<>NR_RIP) then
  434. begin
  435. if (ref.refaddr=addr_no) and (ref.base=NR_NO) and (ref.index=NR_NO) then
  436. begin
  437. { Set RIP relative addressing for simple symbol references }
  438. ref.base:=NR_RIP;
  439. ref.refaddr:=addr_pic_no_got
  440. end
  441. else
  442. begin
  443. { Use temp register to load calculated 64-bit symbol address for complex references }
  444. reference_reset_symbol(href,ref.symbol,0,sizeof(pint));
  445. href.base:=NR_RIP;
  446. href.refaddr:=addr_pic_no_got;
  447. hreg:=GetAddressRegister(list);
  448. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  449. ref.symbol:=nil;
  450. if ref.base=NR_NO then
  451. ref.base:=hreg
  452. else if ref.index=NR_NO then
  453. begin
  454. ref.index:=hreg;
  455. ref.scalefactor:=0;
  456. end
  457. else
  458. begin
  459. { don't use add, as the flags may contain a value }
  460. reference_reset_base(href,ref.base,0,8);
  461. href.index:=hreg;
  462. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  463. ref.base:=hreg;
  464. end;
  465. end;
  466. end;
  467. end;
  468. {$elseif defined(i386)}
  469. add_hreg:=false;
  470. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  471. begin
  472. if assigned(ref.symbol) and
  473. not(assigned(ref.relsymbol)) and
  474. ((ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN]) or
  475. (cs_create_pic in current_settings.moduleswitches)) then
  476. begin
  477. if ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN] then
  478. begin
  479. hreg:=g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol));
  480. ref.symbol:=nil;
  481. end
  482. else
  483. begin
  484. include(current_procinfo.flags,pi_needs_got);
  485. { make a copy of the got register, hreg can get modified }
  486. hreg:=cg.getaddressregister(list);
  487. a_load_reg_reg(list,OS_ADDR,OS_ADDR,current_procinfo.got,hreg);
  488. ref.relsymbol:=current_procinfo.CurrGOTLabel;
  489. end;
  490. add_hreg:=true
  491. end
  492. end
  493. else if (cs_create_pic in current_settings.moduleswitches) and
  494. assigned(ref.symbol) then
  495. begin
  496. reference_reset_symbol(href,ref.symbol,0,sizeof(pint));
  497. href.base:=current_procinfo.got;
  498. href.refaddr:=addr_pic;
  499. include(current_procinfo.flags,pi_needs_got);
  500. hreg:=cg.getaddressregister(list);
  501. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,hreg));
  502. ref.symbol:=nil;
  503. add_hreg:=true;
  504. end;
  505. if add_hreg then
  506. begin
  507. if ref.base=NR_NO then
  508. ref.base:=hreg
  509. else if ref.index=NR_NO then
  510. begin
  511. ref.index:=hreg;
  512. ref.scalefactor:=1;
  513. end
  514. else
  515. begin
  516. { don't use add, as the flags may contain a value }
  517. reference_reset_base(href,ref.base,0,8);
  518. href.index:=hreg;
  519. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  520. ref.base:=hreg;
  521. end;
  522. end;
  523. {$elseif defined(i8086)}
  524. { i8086 does not support stack relative addressing }
  525. if ref.base = NR_STACK_POINTER_REG then
  526. begin
  527. href:=ref;
  528. href.base:=getaddressregister(list);
  529. { let the register allocator find a suitable register for the reference }
  530. list.Concat(Taicpu.op_reg_reg(A_MOV, S_W, NR_SP, href.base));
  531. ref:=href;
  532. end;
  533. { if there is a segment in an int register, move it to ES }
  534. if (ref.segment<>NR_NO) and (not is_segment_reg(ref.segment)) then
  535. begin
  536. list.concat(taicpu.op_reg_reg(A_MOV,S_W,ref.segment,NR_ES));
  537. ref.segment:=NR_ES;
  538. end;
  539. {$endif}
  540. end;
  541. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  542. begin
  543. case t of
  544. OS_F32 :
  545. begin
  546. op:=A_FLD;
  547. s:=S_FS;
  548. end;
  549. OS_F64 :
  550. begin
  551. op:=A_FLD;
  552. s:=S_FL;
  553. end;
  554. OS_F80 :
  555. begin
  556. op:=A_FLD;
  557. s:=S_FX;
  558. end;
  559. OS_C64 :
  560. begin
  561. op:=A_FILD;
  562. s:=S_IQ;
  563. end;
  564. else
  565. internalerror(200204043);
  566. end;
  567. end;
  568. procedure tcgx86.floatload(list: TAsmList; t : tcgsize;const ref : treference);
  569. var
  570. op : tasmop;
  571. s : topsize;
  572. tmpref : treference;
  573. begin
  574. tmpref:=ref;
  575. make_simple_ref(list,tmpref);
  576. floatloadops(t,op,s);
  577. list.concat(Taicpu.Op_ref(op,s,tmpref));
  578. inc_fpu_stack;
  579. end;
  580. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  581. begin
  582. case t of
  583. OS_F32 :
  584. begin
  585. op:=A_FSTP;
  586. s:=S_FS;
  587. end;
  588. OS_F64 :
  589. begin
  590. op:=A_FSTP;
  591. s:=S_FL;
  592. end;
  593. OS_F80 :
  594. begin
  595. op:=A_FSTP;
  596. s:=S_FX;
  597. end;
  598. OS_C64 :
  599. begin
  600. op:=A_FISTP;
  601. s:=S_IQ;
  602. end;
  603. else
  604. internalerror(200204042);
  605. end;
  606. end;
  607. procedure tcgx86.floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  608. var
  609. op : tasmop;
  610. s : topsize;
  611. tmpref : treference;
  612. begin
  613. tmpref:=ref;
  614. make_simple_ref(list,tmpref);
  615. floatstoreops(t,op,s);
  616. list.concat(Taicpu.Op_ref(op,s,tmpref));
  617. { storing non extended floats can cause a floating point overflow }
  618. if (t<>OS_F80) and
  619. (cs_fpu_fwait in current_settings.localswitches) then
  620. list.concat(Taicpu.Op_none(A_FWAIT,S_NO));
  621. dec_fpu_stack;
  622. end;
  623. procedure tcgx86.check_register_size(size:tcgsize;reg:tregister);
  624. begin
  625. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  626. internalerror(200306031);
  627. end;
  628. {****************************************************************************
  629. Assembler code
  630. ****************************************************************************}
  631. procedure tcgx86.a_jmp_name(list : TAsmList;const s : string);
  632. var
  633. r: treference;
  634. begin
  635. if (target_info.system <> system_i386_darwin) then
  636. list.concat(taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s)))
  637. else
  638. begin
  639. reference_reset_symbol(r,get_darwin_call_stub(s,false),0,sizeof(pint));
  640. r.refaddr:=addr_full;
  641. list.concat(taicpu.op_ref(A_JMP,S_NO,r));
  642. end;
  643. end;
  644. procedure tcgx86.a_jmp_always(list : TAsmList;l: tasmlabel);
  645. begin
  646. a_jmp_cond(list, OC_NONE, l);
  647. end;
  648. function tcgx86.get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  649. var
  650. stubname: string;
  651. begin
  652. stubname := 'L'+s+'$stub';
  653. result := current_asmdata.getasmsymbol(stubname);
  654. if assigned(result) then
  655. exit;
  656. if current_asmdata.asmlists[al_imports]=nil then
  657. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  658. new_section(current_asmdata.asmlists[al_imports],sec_stub,'',0);
  659. result := current_asmdata.DefineAsmSymbol(stubname,AB_LOCAL,AT_FUNCTION);
  660. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  661. { register as a weak symbol if necessary }
  662. if weak then
  663. current_asmdata.weakrefasmsymbol(s);
  664. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  665. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  666. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  667. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  668. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  669. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  670. end;
  671. procedure tcgx86.a_call_name(list : TAsmList;const s : string; weak: boolean);
  672. begin
  673. a_call_name_near(list,s,weak);
  674. end;
  675. procedure tcgx86.a_call_name_near(list : TAsmList;const s : string; weak: boolean);
  676. var
  677. sym : tasmsymbol;
  678. r : treference;
  679. begin
  680. if (target_info.system <> system_i386_darwin) then
  681. begin
  682. if not(weak) then
  683. sym:=current_asmdata.RefAsmSymbol(s)
  684. else
  685. sym:=current_asmdata.WeakRefAsmSymbol(s);
  686. reference_reset_symbol(r,sym,0,sizeof(pint));
  687. if (cs_create_pic in current_settings.moduleswitches) and
  688. { darwin's assembler doesn't want @PLT after call symbols }
  689. not(target_info.system in [system_x86_64_darwin,system_i386_iphonesim]) then
  690. begin
  691. {$ifdef i386}
  692. include(current_procinfo.flags,pi_needs_got);
  693. {$endif i386}
  694. r.refaddr:=addr_pic
  695. end
  696. else
  697. r.refaddr:=addr_full;
  698. end
  699. else
  700. begin
  701. reference_reset_symbol(r,get_darwin_call_stub(s,weak),0,sizeof(pint));
  702. r.refaddr:=addr_full;
  703. end;
  704. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  705. end;
  706. procedure tcgx86.a_call_name_static(list : TAsmList;const s : string);
  707. begin
  708. a_call_name_static_near(list,s);
  709. end;
  710. procedure tcgx86.a_call_name_static_near(list : TAsmList;const s : string);
  711. var
  712. sym : tasmsymbol;
  713. r : treference;
  714. begin
  715. sym:=current_asmdata.RefAsmSymbol(s);
  716. reference_reset_symbol(r,sym,0,sizeof(pint));
  717. r.refaddr:=addr_full;
  718. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  719. end;
  720. procedure tcgx86.a_call_reg(list : TAsmList;reg : tregister);
  721. begin
  722. a_call_reg_near(list,reg);
  723. end;
  724. procedure tcgx86.a_call_reg_near(list: TAsmList; reg: tregister);
  725. begin
  726. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  727. end;
  728. procedure tcgx86.a_call_ref(list : TAsmList;ref : treference);
  729. begin
  730. a_call_ref_near(list,ref);
  731. end;
  732. procedure tcgx86.a_call_ref_near(list: TAsmList; ref: treference);
  733. begin
  734. list.concat(taicpu.op_ref(A_CALL,S_NO,ref));
  735. end;
  736. {********************** load instructions ********************}
  737. procedure tcgx86.a_load_const_reg(list : TAsmList; tosize: TCGSize; a : tcgint; reg : TRegister);
  738. begin
  739. check_register_size(tosize,reg);
  740. { the optimizer will change it to "xor reg,reg" when loading zero, }
  741. { no need to do it here too (JM) }
  742. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg))
  743. end;
  744. procedure tcgx86.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  745. var
  746. tmpref : treference;
  747. begin
  748. tmpref:=ref;
  749. make_simple_ref(list,tmpref);
  750. {$ifdef x86_64}
  751. { x86_64 only supports signed 32 bits constants directly }
  752. if (tosize in [OS_S64,OS_64]) and
  753. ((a<low(longint)) or (a>high(longint))) then
  754. begin
  755. a_load_const_ref(list,OS_32,longint(a and $ffffffff),tmpref);
  756. inc(tmpref.offset,4);
  757. a_load_const_ref(list,OS_32,longint(a shr 32),tmpref);
  758. end
  759. else
  760. {$endif x86_64}
  761. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  762. end;
  763. procedure tcgx86.a_load_reg_ref(list : TAsmList; fromsize,tosize: TCGSize; reg : tregister;const ref : treference);
  764. var
  765. op: tasmop;
  766. s: topsize;
  767. tmpsize : tcgsize;
  768. tmpreg : tregister;
  769. tmpref : treference;
  770. begin
  771. tmpref:=ref;
  772. make_simple_ref(list,tmpref);
  773. check_register_size(fromsize,reg);
  774. sizes2load(fromsize,tosize,op,s);
  775. case s of
  776. {$ifdef x86_64}
  777. S_BQ,S_WQ,S_LQ,
  778. {$endif x86_64}
  779. S_BW,S_BL,S_WL :
  780. begin
  781. tmpreg:=getintregister(list,tosize);
  782. {$ifdef x86_64}
  783. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  784. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  785. 64 bit (FK) }
  786. if s in [S_BL,S_WL,S_L] then
  787. begin
  788. tmpreg:=makeregsize(list,tmpreg,OS_32);
  789. tmpsize:=OS_32;
  790. end
  791. else
  792. {$endif x86_64}
  793. tmpsize:=tosize;
  794. list.concat(taicpu.op_reg_reg(op,s,reg,tmpreg));
  795. a_load_reg_ref(list,tmpsize,tosize,tmpreg,tmpref);
  796. end;
  797. else
  798. list.concat(taicpu.op_reg_ref(op,s,reg,tmpref));
  799. end;
  800. end;
  801. procedure tcgx86.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  802. var
  803. op: tasmop;
  804. s: topsize;
  805. tmpref : treference;
  806. begin
  807. tmpref:=ref;
  808. make_simple_ref(list,tmpref);
  809. check_register_size(tosize,reg);
  810. sizes2load(fromsize,tosize,op,s);
  811. {$ifdef x86_64}
  812. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  813. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  814. 64 bit (FK) }
  815. if s in [S_BL,S_WL,S_L] then
  816. reg:=makeregsize(list,reg,OS_32);
  817. {$endif x86_64}
  818. list.concat(taicpu.op_ref_reg(op,s,tmpref,reg));
  819. end;
  820. procedure tcgx86.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  821. var
  822. op: tasmop;
  823. s: topsize;
  824. instr:Taicpu;
  825. begin
  826. check_register_size(fromsize,reg1);
  827. check_register_size(tosize,reg2);
  828. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  829. begin
  830. reg1:=makeregsize(list,reg1,tosize);
  831. s:=tcgsize2opsize[tosize];
  832. op:=A_MOV;
  833. end
  834. else
  835. sizes2load(fromsize,tosize,op,s);
  836. {$ifdef x86_64}
  837. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  838. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  839. 64 bit (FK)
  840. }
  841. if s in [S_BL,S_WL,S_L] then
  842. reg2:=makeregsize(list,reg2,OS_32);
  843. {$endif x86_64}
  844. if (reg1<>reg2) then
  845. begin
  846. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  847. { Notify the register allocator that we have written a move instruction so
  848. it can try to eliminate it. }
  849. if (reg1<>current_procinfo.framepointer) and (reg1<>NR_STACK_POINTER_REG) then
  850. add_move_instruction(instr);
  851. list.concat(instr);
  852. end;
  853. {$ifdef x86_64}
  854. { avoid merging of registers and killing the zero extensions (FK) }
  855. if (tosize in [OS_64,OS_S64]) and (s=S_L) then
  856. list.concat(taicpu.op_const_reg(A_AND,S_L,$ffffffff,reg2));
  857. {$endif x86_64}
  858. end;
  859. procedure tcgx86.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  860. var
  861. tmpref : treference;
  862. begin
  863. with ref do
  864. begin
  865. if (base=NR_NO) and (index=NR_NO) then
  866. begin
  867. if assigned(ref.symbol) then
  868. begin
  869. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  870. ((ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  871. (cs_create_pic in current_settings.moduleswitches)) then
  872. begin
  873. if (ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  874. ((cs_create_pic in current_settings.moduleswitches) and
  875. (ref.symbol.bind in [AB_COMMON,AB_GLOBAL,AB_PRIVATE_EXTERN])) then
  876. begin
  877. reference_reset_base(tmpref,
  878. g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol)),
  879. offset,sizeof(pint));
  880. a_loadaddr_ref_reg(list,tmpref,r);
  881. end
  882. else
  883. begin
  884. include(current_procinfo.flags,pi_needs_got);
  885. reference_reset_base(tmpref,current_procinfo.got,offset,ref.alignment);
  886. tmpref.symbol:=symbol;
  887. tmpref.relsymbol:=current_procinfo.CurrGOTLabel;
  888. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  889. end;
  890. end
  891. else if (cs_create_pic in current_settings.moduleswitches)
  892. {$ifdef x86_64}
  893. and not(ref.symbol.bind=AB_LOCAL)
  894. {$endif x86_64}
  895. then
  896. begin
  897. {$ifdef x86_64}
  898. reference_reset_symbol(tmpref,ref.symbol,0,ref.alignment);
  899. tmpref.refaddr:=addr_pic;
  900. tmpref.base:=NR_RIP;
  901. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,tmpref,r));
  902. {$else x86_64}
  903. reference_reset_symbol(tmpref,ref.symbol,0,ref.alignment);
  904. tmpref.refaddr:=addr_pic;
  905. tmpref.base:=current_procinfo.got;
  906. include(current_procinfo.flags,pi_needs_got);
  907. list.concat(taicpu.op_ref_reg(A_MOV,S_L,tmpref,r));
  908. {$endif x86_64}
  909. if offset<>0 then
  910. a_op_const_reg(list,OP_ADD,OS_ADDR,offset,r);
  911. end
  912. {$ifdef x86_64}
  913. else if (target_info.system in (systems_all_windows+[system_x86_64_darwin]))
  914. or (cs_create_pic in current_settings.moduleswitches)
  915. then
  916. begin
  917. { Win64 and Darwin/x86_64 always require RIP-relative addressing }
  918. tmpref:=ref;
  919. tmpref.base:=NR_RIP;
  920. tmpref.refaddr:=addr_pic_no_got;
  921. list.concat(Taicpu.op_ref_reg(A_LEA,S_Q,tmpref,r));
  922. end
  923. {$endif x86_64}
  924. else
  925. begin
  926. tmpref:=ref;
  927. tmpref.refaddr:=ADDR_FULL;
  928. list.concat(Taicpu.op_ref_reg(A_MOV,tcgsize2opsize[OS_ADDR],tmpref,r));
  929. end
  930. end
  931. else
  932. a_load_const_reg(list,OS_ADDR,offset,r)
  933. end
  934. else if (base=NR_NO) and (index<>NR_NO) and
  935. (offset=0) and (scalefactor=0) and (symbol=nil) then
  936. a_load_reg_reg(list,OS_ADDR,OS_ADDR,index,r)
  937. else if (base<>NR_NO) and (index=NR_NO) and
  938. (offset=0) and (symbol=nil) then
  939. a_load_reg_reg(list,OS_ADDR,OS_ADDR,base,r)
  940. else
  941. begin
  942. tmpref:=ref;
  943. make_simple_ref(list,tmpref);
  944. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  945. end;
  946. if segment<>NR_NO then
  947. begin
  948. if (tf_section_threadvars in target_info.flags) then
  949. begin
  950. { Convert thread local address to a process global addres
  951. as we cannot handle far pointers.}
  952. case target_info.system of
  953. system_i386_linux,system_i386_android:
  954. if segment=NR_GS then
  955. begin
  956. reference_reset_symbol(tmpref,current_asmdata.RefAsmSymbol('___fpc_threadvar_offset'),0,ref.alignment);
  957. tmpref.segment:=NR_GS;
  958. list.concat(Taicpu.op_ref_reg(A_ADD,tcgsize2opsize[OS_ADDR],tmpref,r));
  959. end
  960. else
  961. cgmessage(cg_e_cant_use_far_pointer_there);
  962. else
  963. cgmessage(cg_e_cant_use_far_pointer_there);
  964. end;
  965. end
  966. else
  967. cgmessage(cg_e_cant_use_far_pointer_there);
  968. end;
  969. end;
  970. end;
  971. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  972. { R_ST means "the current value at the top of the fpu stack" (JM) }
  973. procedure tcgx86.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  974. var
  975. href: treference;
  976. op: tasmop;
  977. s: topsize;
  978. begin
  979. if (reg1<>NR_ST) then
  980. begin
  981. floatloadops(tosize,op,s);
  982. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  983. inc_fpu_stack;
  984. end;
  985. if (reg2<>NR_ST) then
  986. begin
  987. floatstoreops(tosize,op,s);
  988. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  989. dec_fpu_stack;
  990. end;
  991. { OS_F80 < OS_C64, but OS_C64 fits perfectly in OS_F80 }
  992. if (reg1=NR_ST) and
  993. (reg2=NR_ST) and
  994. (tosize<>OS_F80) and
  995. (tosize<fromsize) then
  996. begin
  997. { can't round down to lower precision in x87 :/ }
  998. tg.gettemp(list,tcgsize2size[tosize],tcgsize2size[tosize],tt_normal,href);
  999. a_loadfpu_reg_ref(list,fromsize,tosize,NR_ST,href);
  1000. a_loadfpu_ref_reg(list,tosize,tosize,href,NR_ST);
  1001. tg.ungettemp(list,href);
  1002. end;
  1003. end;
  1004. procedure tcgx86.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  1005. begin
  1006. floatload(list,fromsize,ref);
  1007. a_loadfpu_reg_reg(list,fromsize,tosize,NR_ST,reg);
  1008. end;
  1009. procedure tcgx86.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  1010. begin
  1011. { in case a record returned in a floating point register
  1012. (LOC_FPUREGISTER with OS_F32/OS_F64) is stored in memory
  1013. (LOC_REFERENCE with OS_32/OS_64), we have to adjust the
  1014. tosize }
  1015. if (fromsize in [OS_F32,OS_F64]) and
  1016. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1017. case tosize of
  1018. OS_32:
  1019. tosize:=OS_F32;
  1020. OS_64:
  1021. tosize:=OS_F64;
  1022. end;
  1023. if reg<>NR_ST then
  1024. a_loadfpu_reg_reg(list,fromsize,tosize,reg,NR_ST);
  1025. floatstore(list,tosize,ref);
  1026. end;
  1027. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  1028. const
  1029. convertopsse : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1030. (A_MOVSS,A_CVTSS2SD,A_NONE,A_NONE,A_NONE),
  1031. (A_CVTSD2SS,A_MOVSD,A_NONE,A_NONE,A_NONE),
  1032. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1033. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1034. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  1035. convertopavx : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1036. (A_VMOVSS,A_VCVTSS2SD,A_NONE,A_NONE,A_NONE),
  1037. (A_VCVTSD2SS,A_VMOVSD,A_NONE,A_NONE,A_NONE),
  1038. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1039. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1040. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  1041. begin
  1042. { we can have OS_F32/OS_F64 (record in function result/LOC_MMREGISTER) to
  1043. OS_32/OS_64 (record in memory/LOC_REFERENCE) }
  1044. if (fromsize in [OS_F32,OS_F64]) and
  1045. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1046. case tosize of
  1047. OS_32:
  1048. tosize:=OS_F32;
  1049. OS_64:
  1050. tosize:=OS_F64;
  1051. end;
  1052. if (fromsize in [low(convertopsse)..high(convertopsse)]) and
  1053. (tosize in [low(convertopsse)..high(convertopsse)]) then
  1054. begin
  1055. if UseAVX then
  1056. result:=convertopavx[fromsize,tosize]
  1057. else
  1058. result:=convertopsse[fromsize,tosize];
  1059. end
  1060. { we can have OS_M64 (record in function result/LOC_MMREGISTER) to
  1061. OS_64 (record in memory/LOC_REFERENCE) }
  1062. else if (tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1063. (fromsize=OS_M64) then
  1064. begin
  1065. if UseAVX then
  1066. result:=A_VMOVQ
  1067. else
  1068. result:=A_MOVQ;
  1069. end
  1070. else
  1071. internalerror(2010060104);
  1072. if result=A_NONE then
  1073. internalerror(200312205);
  1074. end;
  1075. procedure tcgx86.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  1076. var
  1077. instr : taicpu;
  1078. op : TAsmOp;
  1079. begin
  1080. if shuffle=nil then
  1081. begin
  1082. if fromsize=tosize then
  1083. { needs correct size in case of spilling }
  1084. case fromsize of
  1085. OS_F32:
  1086. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2);
  1087. OS_F64:
  1088. instr:=taicpu.op_reg_reg(A_MOVAPD,S_NO,reg1,reg2);
  1089. OS_M64:
  1090. instr:=taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2);
  1091. else
  1092. internalerror(2006091201);
  1093. end
  1094. else
  1095. internalerror(200312202);
  1096. add_move_instruction(instr);
  1097. end
  1098. else if shufflescalar(shuffle) then
  1099. begin
  1100. op:=get_scalar_mm_op(fromsize,tosize);
  1101. { MOVAPD/MOVAPS are normally faster }
  1102. if op=A_MOVSD then
  1103. op:=A_MOVAPD
  1104. else if op=A_MOVSS then
  1105. op:=A_MOVAPS
  1106. { VMOVSD/SS is not available with two register operands }
  1107. else if op=A_VMOVSD then
  1108. op:=A_VMOVAPD
  1109. else if op=A_VMOVSS then
  1110. op:=A_VMOVAPS;
  1111. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1112. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1113. instr:=taicpu.op_reg_reg_reg(op,S_NO,reg1,reg2,reg2)
  1114. else
  1115. instr:=taicpu.op_reg_reg(op,S_NO,reg1,reg2);
  1116. case op of
  1117. A_VMOVAPD,
  1118. A_VMOVAPS,
  1119. A_VMOVSS,
  1120. A_VMOVSD,
  1121. A_VMOVQ,
  1122. A_MOVAPD,
  1123. A_MOVAPS,
  1124. A_MOVSS,
  1125. A_MOVSD,
  1126. A_MOVQ:
  1127. add_move_instruction(instr);
  1128. end;
  1129. end
  1130. else
  1131. internalerror(200312201);
  1132. list.concat(instr);
  1133. end;
  1134. procedure tcgx86.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1135. var
  1136. tmpref : treference;
  1137. op : tasmop;
  1138. begin
  1139. tmpref:=ref;
  1140. make_simple_ref(list,tmpref);
  1141. if shuffle=nil then
  1142. begin
  1143. if fromsize=OS_M64 then
  1144. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,reg))
  1145. else
  1146. {$ifdef x86_64}
  1147. { x86-64 has always properly aligned data }
  1148. list.concat(taicpu.op_ref_reg(A_MOVDQA,S_NO,tmpref,reg));
  1149. {$else x86_64}
  1150. list.concat(taicpu.op_ref_reg(A_MOVDQU,S_NO,tmpref,reg));
  1151. {$endif x86_64}
  1152. end
  1153. else if shufflescalar(shuffle) then
  1154. begin
  1155. op:=get_scalar_mm_op(fromsize,tosize);
  1156. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1157. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1158. list.concat(taicpu.op_ref_reg_reg(op,S_NO,tmpref,reg,reg))
  1159. else
  1160. list.concat(taicpu.op_ref_reg(op,S_NO,tmpref,reg))
  1161. end
  1162. else
  1163. internalerror(200312252);
  1164. end;
  1165. procedure tcgx86.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  1166. var
  1167. hreg : tregister;
  1168. tmpref : treference;
  1169. op : tasmop;
  1170. begin
  1171. tmpref:=ref;
  1172. make_simple_ref(list,tmpref);
  1173. if shuffle=nil then
  1174. begin
  1175. if fromsize=OS_M64 then
  1176. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,tmpref))
  1177. else
  1178. {$ifdef x86_64}
  1179. { x86-64 has always properly aligned data }
  1180. list.concat(taicpu.op_reg_ref(A_MOVDQA,S_NO,reg,tmpref))
  1181. {$else x86_64}
  1182. list.concat(taicpu.op_reg_ref(A_MOVDQU,S_NO,reg,tmpref))
  1183. {$endif x86_64}
  1184. end
  1185. else if shufflescalar(shuffle) then
  1186. begin
  1187. if tcgsize2size[tosize]<>tcgsize2size[fromsize] then
  1188. begin
  1189. hreg:=getmmregister(list,tosize);
  1190. op:=get_scalar_mm_op(fromsize,tosize);
  1191. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1192. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1193. list.concat(taicpu.op_reg_reg_reg(op,S_NO,reg,hreg,hreg))
  1194. else
  1195. list.concat(taicpu.op_reg_reg(op,S_NO,reg,hreg));
  1196. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize),S_NO,hreg,tmpref))
  1197. end
  1198. else
  1199. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize),S_NO,reg,tmpref));
  1200. end
  1201. else
  1202. internalerror(200312252);
  1203. end;
  1204. procedure tcgx86.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1205. var
  1206. l : tlocation;
  1207. begin
  1208. l.loc:=LOC_REFERENCE;
  1209. l.reference:=ref;
  1210. l.size:=size;
  1211. opmm_loc_reg(list,op,size,l,reg,shuffle);
  1212. end;
  1213. procedure tcgx86.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  1214. var
  1215. l : tlocation;
  1216. begin
  1217. l.loc:=LOC_MMREGISTER;
  1218. l.register:=src;
  1219. l.size:=size;
  1220. opmm_loc_reg(list,op,size,l,dst,shuffle);
  1221. end;
  1222. procedure tcgx86.opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;src,dst: tregister; shuffle : pmmshuffle);
  1223. const
  1224. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1225. ( { scalar }
  1226. ( { OS_F32 }
  1227. A_NOP,A_NOP,A_VADDSS,A_NOP,A_VDIVSS,A_NOP,A_NOP,A_VMULSS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSS,A_NOP,A_NOP,A_NOP
  1228. ),
  1229. ( { OS_F64 }
  1230. A_NOP,A_NOP,A_VADDSD,A_NOP,A_VDIVSD,A_NOP,A_NOP,A_VMULSD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSD,A_NOP,A_NOP,A_NOP
  1231. )
  1232. ),
  1233. ( { vectorized/packed }
  1234. { because the logical packed single instructions have shorter op codes, we use always
  1235. these
  1236. }
  1237. ( { OS_F32 }
  1238. A_NOP,A_NOP,A_VADDPS,A_NOP,A_VDIVPS,A_NOP,A_NOP,A_VMULPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPS,A_VXORPS,A_NOP,A_NOP
  1239. ),
  1240. ( { OS_F64 }
  1241. A_NOP,A_NOP,A_VADDPD,A_NOP,A_VDIVPD,A_NOP,A_NOP,A_VMULPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPD,A_VXORPD,A_NOP,A_NOP
  1242. )
  1243. )
  1244. );
  1245. var
  1246. resultreg : tregister;
  1247. asmop : tasmop;
  1248. begin
  1249. { this is an internally used procedure so the parameters have
  1250. some constrains
  1251. }
  1252. if loc.size<>size then
  1253. internalerror(2013061108);
  1254. resultreg:=dst;
  1255. { deshuffle }
  1256. //!!!
  1257. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1258. begin
  1259. internalerror(2013061107);
  1260. end
  1261. else if (shuffle=nil) then
  1262. asmop:=opmm2asmop[1,size,op]
  1263. else if shufflescalar(shuffle) then
  1264. begin
  1265. asmop:=opmm2asmop[0,size,op];
  1266. { no scalar operation available? }
  1267. if asmop=A_NOP then
  1268. begin
  1269. { do vectorized and shuffle finally }
  1270. internalerror(2010060102);
  1271. end;
  1272. end
  1273. else
  1274. internalerror(2013061106);
  1275. if asmop=A_NOP then
  1276. internalerror(2013061105);
  1277. case loc.loc of
  1278. LOC_CREFERENCE,LOC_REFERENCE:
  1279. begin
  1280. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1281. list.concat(taicpu.op_ref_reg_reg(asmop,S_NO,loc.reference,src,resultreg));
  1282. end;
  1283. LOC_CMMREGISTER,LOC_MMREGISTER:
  1284. list.concat(taicpu.op_reg_reg_reg(asmop,S_NO,loc.register,src,resultreg));
  1285. else
  1286. internalerror(2013061104);
  1287. end;
  1288. { shuffle }
  1289. if resultreg<>dst then
  1290. begin
  1291. internalerror(2013061103);
  1292. end;
  1293. end;
  1294. procedure tcgx86.a_opmm_reg_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src1,src2,dst: tregister;shuffle : pmmshuffle);
  1295. var
  1296. l : tlocation;
  1297. begin
  1298. l.loc:=LOC_MMREGISTER;
  1299. l.register:=src1;
  1300. l.size:=size;
  1301. opmm_loc_reg_reg(list,op,size,l,src2,dst,shuffle);
  1302. end;
  1303. procedure tcgx86.a_opmm_ref_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; src,dst: tregister;shuffle : pmmshuffle);
  1304. var
  1305. l : tlocation;
  1306. begin
  1307. l.loc:=LOC_REFERENCE;
  1308. l.reference:=ref;
  1309. l.size:=size;
  1310. opmm_loc_reg_reg(list,op,size,l,src,dst,shuffle);
  1311. end;
  1312. procedure tcgx86.opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  1313. const
  1314. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1315. ( { scalar }
  1316. ( { OS_F32 }
  1317. A_NOP,A_NOP,A_ADDSS,A_NOP,A_DIVSS,A_NOP,A_NOP,A_MULSS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBSS,A_NOP,A_NOP,A_NOP
  1318. ),
  1319. ( { OS_F64 }
  1320. A_NOP,A_NOP,A_ADDSD,A_NOP,A_DIVSD,A_NOP,A_NOP,A_MULSD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBSD,A_NOP,A_NOP,A_NOP
  1321. )
  1322. ),
  1323. ( { vectorized/packed }
  1324. { because the logical packed single instructions have shorter op codes, we use always
  1325. these
  1326. }
  1327. ( { OS_F32 }
  1328. A_NOP,A_NOP,A_ADDPS,A_NOP,A_DIVPS,A_NOP,A_NOP,A_MULPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBPS,A_XORPS,A_NOP,A_NOP
  1329. ),
  1330. ( { OS_F64 }
  1331. A_NOP,A_NOP,A_ADDPD,A_NOP,A_DIVPD,A_NOP,A_NOP,A_MULPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBPD,A_XORPD,A_NOP,A_NOP
  1332. )
  1333. )
  1334. );
  1335. var
  1336. resultreg : tregister;
  1337. asmop : tasmop;
  1338. begin
  1339. { this is an internally used procedure so the parameters have
  1340. some constrains
  1341. }
  1342. if loc.size<>size then
  1343. internalerror(200312213);
  1344. resultreg:=dst;
  1345. { deshuffle }
  1346. //!!!
  1347. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1348. begin
  1349. internalerror(2010060101);
  1350. end
  1351. else if (shuffle=nil) then
  1352. asmop:=opmm2asmop[1,size,op]
  1353. else if shufflescalar(shuffle) then
  1354. begin
  1355. asmop:=opmm2asmop[0,size,op];
  1356. { no scalar operation available? }
  1357. if asmop=A_NOP then
  1358. begin
  1359. { do vectorized and shuffle finally }
  1360. internalerror(2010060102);
  1361. end;
  1362. end
  1363. else
  1364. internalerror(200312211);
  1365. if asmop=A_NOP then
  1366. internalerror(200312216);
  1367. case loc.loc of
  1368. LOC_CREFERENCE,LOC_REFERENCE:
  1369. begin
  1370. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1371. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  1372. end;
  1373. LOC_CMMREGISTER,LOC_MMREGISTER:
  1374. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  1375. else
  1376. internalerror(200312214);
  1377. end;
  1378. { shuffle }
  1379. if resultreg<>dst then
  1380. begin
  1381. internalerror(200312212);
  1382. end;
  1383. end;
  1384. procedure tcgx86.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1385. a:tcgint;src,dst:Tregister);
  1386. var
  1387. power : longint;
  1388. href : treference;
  1389. begin
  1390. if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1391. not(cs_check_overflow in current_settings.localswitches) and
  1392. (a>1) and ispowerof2(int64(a-1),power) and (power in [1..3]) then
  1393. begin
  1394. reference_reset_base(href,src,0,0);
  1395. href.index:=src;
  1396. href.scalefactor:=a-1;
  1397. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1398. end
  1399. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1400. not(cs_check_overflow in current_settings.localswitches) and
  1401. (a>1) and ispowerof2(int64(a),power) and (power in [1..3]) then
  1402. begin
  1403. reference_reset_base(href,src,0,0);
  1404. href.index:=src;
  1405. href.scalefactor:=a;
  1406. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1407. end
  1408. else if (op=OP_ADD) and
  1409. ((size in [OS_32,OS_S32]) or
  1410. { lea supports only 32 bit signed displacments }
  1411. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1412. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1413. ) and
  1414. not(cs_check_overflow in current_settings.localswitches) then
  1415. begin
  1416. reference_reset_base(href,src,a,0);
  1417. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1418. end
  1419. else if (op=OP_SUB) and
  1420. ((size in [OS_32,OS_S32]) or
  1421. { lea supports only 32 bit signed displacments }
  1422. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1423. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1424. ) and
  1425. not(cs_check_overflow in current_settings.localswitches) then
  1426. begin
  1427. reference_reset_base(href,src,-a,0);
  1428. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1429. end
  1430. else
  1431. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1432. end;
  1433. procedure tcgx86.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1434. size: tcgsize; src1, src2, dst: tregister);
  1435. var
  1436. href : treference;
  1437. begin
  1438. if (op=OP_ADD) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1439. not(cs_check_overflow in current_settings.localswitches) then
  1440. begin
  1441. reference_reset_base(href,src1,0,0);
  1442. href.index:=src2;
  1443. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1444. end
  1445. else
  1446. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1447. end;
  1448. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  1449. var
  1450. opcode : tasmop;
  1451. power : longint;
  1452. href : treference;
  1453. {$ifdef x86_64}
  1454. tmpreg : tregister;
  1455. {$endif x86_64}
  1456. begin
  1457. optimize_op_const(op, a);
  1458. {$ifdef x86_64}
  1459. { x86_64 only supports signed 32 bits constants directly }
  1460. if not(op in [OP_NONE,OP_MOVE]) and
  1461. (size in [OS_S64,OS_64]) and
  1462. ((a<low(longint)) or (a>high(longint))) then
  1463. begin
  1464. tmpreg:=getintregister(list,size);
  1465. a_load_const_reg(list,size,a,tmpreg);
  1466. a_op_reg_reg(list,op,size,tmpreg,reg);
  1467. exit;
  1468. end;
  1469. {$endif x86_64}
  1470. check_register_size(size,reg);
  1471. case op of
  1472. OP_NONE :
  1473. begin
  1474. { Opcode is optimized away }
  1475. end;
  1476. OP_MOVE :
  1477. begin
  1478. { Optimized, replaced with a simple load }
  1479. a_load_const_reg(list,size,a,reg);
  1480. end;
  1481. OP_DIV, OP_IDIV:
  1482. begin
  1483. if ispowerof2(int64(a),power) then
  1484. begin
  1485. case op of
  1486. OP_DIV:
  1487. opcode := A_SHR;
  1488. OP_IDIV:
  1489. opcode := A_SAR;
  1490. end;
  1491. list.concat(taicpu.op_const_reg(opcode,TCgSize2OpSize[size],power,reg));
  1492. exit;
  1493. end;
  1494. { the rest should be handled specifically in the code }
  1495. { generator because of the silly register usage restraints }
  1496. internalerror(200109224);
  1497. end;
  1498. OP_MUL,OP_IMUL:
  1499. begin
  1500. if not(cs_check_overflow in current_settings.localswitches) and
  1501. ispowerof2(int64(a),power) then
  1502. begin
  1503. list.concat(taicpu.op_const_reg(A_SHL,TCgSize2OpSize[size],power,reg));
  1504. exit;
  1505. end;
  1506. if op = OP_IMUL then
  1507. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  1508. else
  1509. { OP_MUL should be handled specifically in the code }
  1510. { generator because of the silly register usage restraints }
  1511. internalerror(200109225);
  1512. end;
  1513. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  1514. if not(cs_check_overflow in current_settings.localswitches) and
  1515. (a = 1) and
  1516. (op in [OP_ADD,OP_SUB]) and
  1517. UseIncDec then
  1518. begin
  1519. if op = OP_ADD then
  1520. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  1521. else
  1522. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  1523. end
  1524. else if (a = 0) then
  1525. if (op <> OP_AND) then
  1526. exit
  1527. else
  1528. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],0,reg))
  1529. else if (aword(a) = high(aword)) and
  1530. (op in [OP_AND,OP_OR,OP_XOR]) then
  1531. begin
  1532. case op of
  1533. OP_AND:
  1534. exit;
  1535. OP_OR:
  1536. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],aint(high(aword)),reg));
  1537. OP_XOR:
  1538. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg));
  1539. end
  1540. end
  1541. else
  1542. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],aint(a),reg));
  1543. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1544. begin
  1545. {$if defined(x86_64)}
  1546. if (a and 63) <> 0 Then
  1547. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,reg));
  1548. if (a shr 6) <> 0 Then
  1549. internalerror(200609073);
  1550. {$elseif defined(i386)}
  1551. if (a and 31) <> 0 Then
  1552. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1553. if (a shr 5) <> 0 Then
  1554. internalerror(200609071);
  1555. {$elseif defined(i8086)}
  1556. if (a shr 5) <> 0 Then
  1557. internalerror(2013043002);
  1558. a := a and 31;
  1559. if a <> 0 Then
  1560. begin
  1561. if (current_settings.cputype < cpu_186) and (a <> 1) then
  1562. begin
  1563. getcpuregister(list,NR_CL);
  1564. a_load_const_reg(list,OS_8,a,NR_CL);
  1565. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,reg));
  1566. ungetcpuregister(list,NR_CL);
  1567. end
  1568. else
  1569. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  1570. end;
  1571. {$endif}
  1572. end
  1573. else internalerror(200609072);
  1574. end;
  1575. end;
  1576. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1577. var
  1578. opcode: tasmop;
  1579. power: longint;
  1580. {$ifdef x86_64}
  1581. tmpreg : tregister;
  1582. {$endif x86_64}
  1583. tmpref : treference;
  1584. begin
  1585. optimize_op_const(op, a);
  1586. tmpref:=ref;
  1587. make_simple_ref(list,tmpref);
  1588. {$ifdef x86_64}
  1589. { x86_64 only supports signed 32 bits constants directly }
  1590. if not(op in [OP_NONE,OP_MOVE]) and
  1591. (size in [OS_S64,OS_64]) and
  1592. ((a<low(longint)) or (a>high(longint))) then
  1593. begin
  1594. tmpreg:=getintregister(list,size);
  1595. a_load_const_reg(list,size,a,tmpreg);
  1596. a_op_reg_ref(list,op,size,tmpreg,tmpref);
  1597. exit;
  1598. end;
  1599. {$endif x86_64}
  1600. Case Op of
  1601. OP_NONE :
  1602. begin
  1603. { Opcode is optimized away }
  1604. end;
  1605. OP_MOVE :
  1606. begin
  1607. { Optimized, replaced with a simple load }
  1608. a_load_const_ref(list,size,a,ref);
  1609. end;
  1610. OP_DIV, OP_IDIV:
  1611. Begin
  1612. if ispowerof2(int64(a),power) then
  1613. begin
  1614. case op of
  1615. OP_DIV:
  1616. opcode := A_SHR;
  1617. OP_IDIV:
  1618. opcode := A_SAR;
  1619. end;
  1620. list.concat(taicpu.op_const_ref(opcode,
  1621. TCgSize2OpSize[size],power,tmpref));
  1622. exit;
  1623. end;
  1624. { the rest should be handled specifically in the code }
  1625. { generator because of the silly register usage restraints }
  1626. internalerror(200109231);
  1627. End;
  1628. OP_MUL,OP_IMUL:
  1629. begin
  1630. if not(cs_check_overflow in current_settings.localswitches) and
  1631. ispowerof2(int64(a),power) then
  1632. begin
  1633. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  1634. power,tmpref));
  1635. exit;
  1636. end;
  1637. { can't multiply a memory location directly with a constant }
  1638. if op = OP_IMUL then
  1639. inherited a_op_const_ref(list,op,size,a,tmpref)
  1640. else
  1641. { OP_MUL should be handled specifically in the code }
  1642. { generator because of the silly register usage restraints }
  1643. internalerror(200109232);
  1644. end;
  1645. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  1646. if not(cs_check_overflow in current_settings.localswitches) and
  1647. (a = 1) and
  1648. (op in [OP_ADD,OP_SUB]) and
  1649. UseIncDec then
  1650. begin
  1651. if op = OP_ADD then
  1652. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  1653. else
  1654. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  1655. end
  1656. else if (a = 0) then
  1657. if (op <> OP_AND) then
  1658. exit
  1659. else
  1660. a_load_const_ref(list,size,0,tmpref)
  1661. else if (aword(a) = high(aword)) and
  1662. (op in [OP_AND,OP_OR,OP_XOR]) then
  1663. begin
  1664. case op of
  1665. OP_AND:
  1666. exit;
  1667. OP_OR:
  1668. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],aint(high(aword)),tmpref));
  1669. OP_XOR:
  1670. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref));
  1671. end
  1672. end
  1673. else
  1674. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  1675. TCgSize2OpSize[size],a,tmpref));
  1676. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1677. begin
  1678. if (a and 31) <> 0 then
  1679. list.concat(taicpu.op_const_ref(
  1680. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  1681. if (a shr 5) <> 0 Then
  1682. internalerror(68991);
  1683. end
  1684. else internalerror(68992);
  1685. end;
  1686. end;
  1687. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1688. const
  1689. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  1690. REGCX=NR_ECX;
  1691. REGCX_Size = OS_32;
  1692. {$elseif defined(cpu16bitalu)}
  1693. REGCX=NR_CX;
  1694. REGCX_Size = OS_16;
  1695. {$endif}
  1696. var
  1697. dstsize: topsize;
  1698. instr:Taicpu;
  1699. begin
  1700. check_register_size(size,src);
  1701. check_register_size(size,dst);
  1702. dstsize := tcgsize2opsize[size];
  1703. case op of
  1704. OP_NEG,OP_NOT:
  1705. begin
  1706. if src<>dst then
  1707. a_load_reg_reg(list,size,size,src,dst);
  1708. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  1709. end;
  1710. OP_MUL,OP_DIV,OP_IDIV:
  1711. { special stuff, needs separate handling inside code }
  1712. { generator }
  1713. internalerror(200109233);
  1714. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  1715. begin
  1716. { Use ecx to load the value, that allows better coalescing }
  1717. getcpuregister(list,REGCX);
  1718. a_load_reg_reg(list,size,REGCX_Size,src,REGCX);
  1719. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  1720. ungetcpuregister(list,REGCX);
  1721. end;
  1722. else
  1723. begin
  1724. if reg2opsize(src) <> dstsize then
  1725. internalerror(200109226);
  1726. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  1727. list.concat(instr);
  1728. end;
  1729. end;
  1730. end;
  1731. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1732. var
  1733. tmpref : treference;
  1734. begin
  1735. tmpref:=ref;
  1736. make_simple_ref(list,tmpref);
  1737. check_register_size(size,reg);
  1738. case op of
  1739. OP_NEG,OP_NOT,OP_IMUL:
  1740. begin
  1741. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1742. end;
  1743. OP_MUL,OP_DIV,OP_IDIV:
  1744. { special stuff, needs separate handling inside code }
  1745. { generator }
  1746. internalerror(200109239);
  1747. else
  1748. begin
  1749. reg := makeregsize(list,reg,size);
  1750. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  1751. end;
  1752. end;
  1753. end;
  1754. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1755. var
  1756. tmpref : treference;
  1757. begin
  1758. tmpref:=ref;
  1759. make_simple_ref(list,tmpref);
  1760. check_register_size(size,reg);
  1761. case op of
  1762. OP_NEG,OP_NOT:
  1763. begin
  1764. if reg<>NR_NO then
  1765. internalerror(200109237);
  1766. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  1767. end;
  1768. OP_IMUL:
  1769. begin
  1770. { this one needs a load/imul/store, which is the default }
  1771. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1772. end;
  1773. OP_MUL,OP_DIV,OP_IDIV:
  1774. { special stuff, needs separate handling inside code }
  1775. { generator }
  1776. internalerror(200109238);
  1777. else
  1778. begin
  1779. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  1780. end;
  1781. end;
  1782. end;
  1783. procedure tcgx86.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: TCGSize; src, dst: TRegister);
  1784. var
  1785. opsize: topsize;
  1786. l : TAsmLabel;
  1787. begin
  1788. opsize:=tcgsize2opsize[size];
  1789. if not reverse then
  1790. list.concat(taicpu.op_reg_reg(A_BSF,opsize,src,dst))
  1791. else
  1792. list.concat(taicpu.op_reg_reg(A_BSR,opsize,src,dst));
  1793. current_asmdata.getjumplabel(l);
  1794. a_jmp_cond(list,OC_NE,l);
  1795. list.concat(taicpu.op_const_reg(A_MOV,opsize,$ff,dst));
  1796. a_label(list,l);
  1797. end;
  1798. {*************** compare instructructions ****************}
  1799. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1800. l : tasmlabel);
  1801. {$ifdef x86_64}
  1802. var
  1803. tmpreg : tregister;
  1804. {$endif x86_64}
  1805. begin
  1806. {$ifdef x86_64}
  1807. { x86_64 only supports signed 32 bits constants directly }
  1808. if (size in [OS_S64,OS_64]) and
  1809. ((a<low(longint)) or (a>high(longint))) then
  1810. begin
  1811. tmpreg:=getintregister(list,size);
  1812. a_load_const_reg(list,size,a,tmpreg);
  1813. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1814. exit;
  1815. end;
  1816. {$endif x86_64}
  1817. if (a = 0) then
  1818. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  1819. else
  1820. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  1821. a_jmp_cond(list,cmp_op,l);
  1822. end;
  1823. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  1824. l : tasmlabel);
  1825. var
  1826. {$ifdef x86_64}
  1827. tmpreg : tregister;
  1828. {$endif x86_64}
  1829. tmpref : treference;
  1830. begin
  1831. tmpref:=ref;
  1832. make_simple_ref(list,tmpref);
  1833. {$ifdef x86_64}
  1834. { x86_64 only supports signed 32 bits constants directly }
  1835. if (size in [OS_S64,OS_64]) and
  1836. ((a<low(longint)) or (a>high(longint))) then
  1837. begin
  1838. tmpreg:=getintregister(list,size);
  1839. a_load_const_reg(list,size,a,tmpreg);
  1840. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  1841. exit;
  1842. end;
  1843. {$endif x86_64}
  1844. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  1845. a_jmp_cond(list,cmp_op,l);
  1846. end;
  1847. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  1848. reg1,reg2 : tregister;l : tasmlabel);
  1849. begin
  1850. check_register_size(size,reg1);
  1851. check_register_size(size,reg2);
  1852. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  1853. a_jmp_cond(list,cmp_op,l);
  1854. end;
  1855. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  1856. var
  1857. tmpref : treference;
  1858. begin
  1859. tmpref:=ref;
  1860. make_simple_ref(list,tmpref);
  1861. check_register_size(size,reg);
  1862. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  1863. a_jmp_cond(list,cmp_op,l);
  1864. end;
  1865. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  1866. var
  1867. tmpref : treference;
  1868. begin
  1869. tmpref:=ref;
  1870. make_simple_ref(list,tmpref);
  1871. check_register_size(size,reg);
  1872. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  1873. a_jmp_cond(list,cmp_op,l);
  1874. end;
  1875. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1876. var
  1877. ai : taicpu;
  1878. begin
  1879. if cond=OC_None then
  1880. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1881. else
  1882. begin
  1883. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  1884. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1885. end;
  1886. ai.is_jmp:=true;
  1887. list.concat(ai);
  1888. end;
  1889. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1890. var
  1891. ai : taicpu;
  1892. begin
  1893. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  1894. ai.SetCondition(flags_to_cond(f));
  1895. ai.is_jmp := true;
  1896. list.concat(ai);
  1897. end;
  1898. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1899. var
  1900. ai : taicpu;
  1901. hreg : tregister;
  1902. begin
  1903. hreg:=makeregsize(list,reg,OS_8);
  1904. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  1905. ai.setcondition(flags_to_cond(f));
  1906. list.concat(ai);
  1907. if reg<>hreg then
  1908. a_load_reg_reg(list,OS_8,size,hreg,reg);
  1909. end;
  1910. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  1911. var
  1912. ai : taicpu;
  1913. tmpref : treference;
  1914. begin
  1915. tmpref:=ref;
  1916. make_simple_ref(list,tmpref);
  1917. if not(size in [OS_8,OS_S8]) then
  1918. a_load_const_ref(list,size,0,tmpref);
  1919. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  1920. ai.setcondition(flags_to_cond(f));
  1921. list.concat(ai);
  1922. {$ifndef cpu64bitalu}
  1923. if size in [OS_S64,OS_64] then
  1924. begin
  1925. inc(tmpref.offset,4);
  1926. a_load_const_ref(list,OS_32,0,tmpref);
  1927. end;
  1928. {$endif cpu64bitalu}
  1929. end;
  1930. { ************* concatcopy ************ }
  1931. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:tcgint);
  1932. const
  1933. {$if defined(cpu64bitalu)}
  1934. REGCX=NR_RCX;
  1935. REGSI=NR_RSI;
  1936. REGDI=NR_RDI;
  1937. copy_len_sizes = [1, 2, 4, 8];
  1938. push_segment_size = S_L;
  1939. {$elseif defined(cpu32bitalu)}
  1940. REGCX=NR_ECX;
  1941. REGSI=NR_ESI;
  1942. REGDI=NR_EDI;
  1943. copy_len_sizes = [1, 2, 4];
  1944. push_segment_size = S_L;
  1945. {$elseif defined(cpu16bitalu)}
  1946. REGCX=NR_CX;
  1947. REGSI=NR_SI;
  1948. REGDI=NR_DI;
  1949. copy_len_sizes = [1, 2];
  1950. push_segment_size = S_W;
  1951. {$endif}
  1952. type copymode=(copy_move,copy_mmx,copy_string);
  1953. var srcref,dstref:Treference;
  1954. r,r0,r1,r2,r3:Tregister;
  1955. helpsize:tcgint;
  1956. copysize:byte;
  1957. cgsize:Tcgsize;
  1958. cm:copymode;
  1959. begin
  1960. cm:=copy_move;
  1961. helpsize:=3*sizeof(aword);
  1962. if cs_opt_size in current_settings.optimizerswitches then
  1963. helpsize:=2*sizeof(aword);
  1964. if (cs_mmx in current_settings.localswitches) and
  1965. not(pi_uses_fpu in current_procinfo.flags) and
  1966. ((len=8) or (len=16) or (len=24) or (len=32)) then
  1967. cm:=copy_mmx;
  1968. if (len>helpsize) then
  1969. cm:=copy_string;
  1970. if (cs_opt_size in current_settings.optimizerswitches) and
  1971. not((len<=16) and (cm=copy_mmx)) and
  1972. not(len in copy_len_sizes) then
  1973. cm:=copy_string;
  1974. {$ifndef i8086}
  1975. if (source.segment<>NR_NO) or
  1976. (dest.segment<>NR_NO) then
  1977. cm:=copy_string;
  1978. {$endif not i8086}
  1979. case cm of
  1980. copy_move:
  1981. begin
  1982. dstref:=dest;
  1983. srcref:=source;
  1984. copysize:=sizeof(aint);
  1985. cgsize:=int_cgsize(copysize);
  1986. while len<>0 do
  1987. begin
  1988. if len<2 then
  1989. begin
  1990. copysize:=1;
  1991. cgsize:=OS_8;
  1992. end
  1993. else if len<4 then
  1994. begin
  1995. copysize:=2;
  1996. cgsize:=OS_16;
  1997. end
  1998. {$if defined(cpu32bitalu) or defined(cpu64bitalu)}
  1999. else if len<8 then
  2000. begin
  2001. copysize:=4;
  2002. cgsize:=OS_32;
  2003. end
  2004. {$endif cpu32bitalu or cpu64bitalu}
  2005. {$ifdef cpu64bitalu}
  2006. else if len<16 then
  2007. begin
  2008. copysize:=8;
  2009. cgsize:=OS_64;
  2010. end
  2011. {$endif}
  2012. ;
  2013. dec(len,copysize);
  2014. r:=getintregister(list,cgsize);
  2015. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2016. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2017. inc(srcref.offset,copysize);
  2018. inc(dstref.offset,copysize);
  2019. end;
  2020. end;
  2021. copy_mmx:
  2022. begin
  2023. dstref:=dest;
  2024. srcref:=source;
  2025. r0:=getmmxregister(list);
  2026. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  2027. if len>=16 then
  2028. begin
  2029. inc(srcref.offset,8);
  2030. r1:=getmmxregister(list);
  2031. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  2032. end;
  2033. if len>=24 then
  2034. begin
  2035. inc(srcref.offset,8);
  2036. r2:=getmmxregister(list);
  2037. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  2038. end;
  2039. if len>=32 then
  2040. begin
  2041. inc(srcref.offset,8);
  2042. r3:=getmmxregister(list);
  2043. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2044. end;
  2045. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  2046. if len>=16 then
  2047. begin
  2048. inc(dstref.offset,8);
  2049. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  2050. end;
  2051. if len>=24 then
  2052. begin
  2053. inc(dstref.offset,8);
  2054. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  2055. end;
  2056. if len>=32 then
  2057. begin
  2058. inc(dstref.offset,8);
  2059. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2060. end;
  2061. end
  2062. else {copy_string, should be a good fallback in case of unhandled}
  2063. begin
  2064. getcpuregister(list,REGDI);
  2065. if (dest.segment=NR_NO) then
  2066. begin
  2067. a_loadaddr_ref_reg(list,dest,REGDI);
  2068. {$ifdef volatile_es}
  2069. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2070. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2071. {$endif volatile_es}
  2072. end
  2073. else
  2074. begin
  2075. dstref:=dest;
  2076. dstref.segment:=NR_NO;
  2077. a_loadaddr_ref_reg(list,dstref,REGDI);
  2078. {$ifndef volatile_es}
  2079. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_ES));
  2080. {$endif not volatile_es}
  2081. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,dest.segment));
  2082. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2083. end;
  2084. getcpuregister(list,REGSI);
  2085. if (source.segment=NR_NO) then
  2086. a_loadaddr_ref_reg(list,source,REGSI)
  2087. else
  2088. begin
  2089. srcref:=source;
  2090. srcref.segment:=NR_NO;
  2091. a_loadaddr_ref_reg(list,srcref,REGSI);
  2092. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_DS));
  2093. list.concat(taicpu.op_reg(A_PUSH,S_L,source.segment));
  2094. list.concat(taicpu.op_reg(A_POP,S_L,NR_DS));
  2095. end;
  2096. getcpuregister(list,REGCX);
  2097. if ts_cld in current_settings.targetswitches then
  2098. list.concat(Taicpu.op_none(A_CLD,S_NO));
  2099. if (cs_opt_size in current_settings.optimizerswitches) and
  2100. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  2101. begin
  2102. a_load_const_reg(list,OS_INT,len,REGCX);
  2103. list.concat(Taicpu.op_none(A_REP,S_NO));
  2104. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2105. end
  2106. else
  2107. begin
  2108. helpsize:=len div sizeof(aint);
  2109. len:=len mod sizeof(aint);
  2110. if helpsize>1 then
  2111. begin
  2112. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  2113. list.concat(Taicpu.op_none(A_REP,S_NO));
  2114. end;
  2115. if helpsize>0 then
  2116. begin
  2117. {$if defined(cpu64bitalu)}
  2118. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  2119. {$elseif defined(cpu32bitalu)}
  2120. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2121. {$elseif defined(cpu16bitalu)}
  2122. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2123. {$endif}
  2124. end;
  2125. if len>=4 then
  2126. begin
  2127. dec(len,4);
  2128. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2129. end;
  2130. if len>=2 then
  2131. begin
  2132. dec(len,2);
  2133. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2134. end;
  2135. if len=1 then
  2136. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2137. end;
  2138. ungetcpuregister(list,REGCX);
  2139. ungetcpuregister(list,REGSI);
  2140. ungetcpuregister(list,REGDI);
  2141. if (source.segment<>NR_NO) then
  2142. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2143. {$ifndef volatile_es}
  2144. if (dest.segment<>NR_NO) then
  2145. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2146. {$endif not volatile_es}
  2147. end;
  2148. end;
  2149. end;
  2150. {****************************************************************************
  2151. Entry/Exit Code Helpers
  2152. ****************************************************************************}
  2153. procedure tcgx86.g_profilecode(list : TAsmList);
  2154. var
  2155. pl : tasmlabel;
  2156. mcountprefix : String[4];
  2157. begin
  2158. case target_info.system of
  2159. {$ifndef NOTARGETWIN}
  2160. system_i386_win32,
  2161. {$endif}
  2162. system_i386_freebsd,
  2163. system_i386_netbsd,
  2164. // system_i386_openbsd,
  2165. system_i386_wdosx :
  2166. begin
  2167. Case target_info.system Of
  2168. system_i386_freebsd : mcountprefix:='.';
  2169. system_i386_netbsd : mcountprefix:='__';
  2170. // system_i386_openbsd : mcountprefix:='.';
  2171. else
  2172. mcountPrefix:='';
  2173. end;
  2174. current_asmdata.getaddrlabel(pl);
  2175. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(pint));
  2176. list.concat(Tai_label.Create(pl));
  2177. list.concat(Tai_const.Create_32bit(0));
  2178. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  2179. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2180. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  2181. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount',false);
  2182. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  2183. end;
  2184. system_i386_linux:
  2185. a_call_name(list,target_info.Cprefix+'mcount',false);
  2186. system_i386_go32v2,system_i386_watcom:
  2187. begin
  2188. a_call_name(list,'MCOUNT',false);
  2189. end;
  2190. system_x86_64_linux,
  2191. system_x86_64_darwin:
  2192. begin
  2193. a_call_name(list,'mcount',false);
  2194. end;
  2195. end;
  2196. end;
  2197. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2198. procedure decrease_sp(a : tcgint);
  2199. var
  2200. href : treference;
  2201. begin
  2202. reference_reset_base(href,NR_STACK_POINTER_REG,-a,0);
  2203. { normally, lea is a better choice than a sub to adjust the stack pointer }
  2204. list.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_STACK_POINTER_REG));
  2205. end;
  2206. {$ifdef x86}
  2207. {$ifndef NOTARGETWIN}
  2208. var
  2209. href : treference;
  2210. i : integer;
  2211. again : tasmlabel;
  2212. {$endif NOTARGETWIN}
  2213. {$endif x86}
  2214. begin
  2215. if localsize>0 then
  2216. begin
  2217. {$ifdef i386}
  2218. {$ifndef NOTARGETWIN}
  2219. { windows guards only a few pages for stack growing,
  2220. so we have to access every page first }
  2221. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  2222. (localsize>=winstackpagesize) then
  2223. begin
  2224. if localsize div winstackpagesize<=5 then
  2225. begin
  2226. decrease_sp(localsize-4);
  2227. for i:=1 to localsize div winstackpagesize do
  2228. begin
  2229. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize,4);
  2230. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2231. end;
  2232. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2233. end
  2234. else
  2235. begin
  2236. current_asmdata.getjumplabel(again);
  2237. { Using a_reg_alloc instead of getcpuregister, so this procedure
  2238. does not change "used_in_proc" state of EDI and therefore can be
  2239. called after saving registers with "push" instruction
  2240. without creating an unbalanced "pop edi" in epilogue }
  2241. a_reg_alloc(list,NR_EDI);
  2242. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  2243. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  2244. a_label(list,again);
  2245. decrease_sp(winstackpagesize-4);
  2246. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2247. if UseIncDec then
  2248. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI))
  2249. else
  2250. list.concat(Taicpu.op_const_reg(A_SUB,S_L,1,NR_EDI));
  2251. a_jmp_cond(list,OC_NE,again);
  2252. decrease_sp(localsize mod winstackpagesize-4);
  2253. reference_reset_base(href,NR_ESP,localsize-4,4);
  2254. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  2255. a_reg_dealloc(list,NR_EDI);
  2256. end
  2257. end
  2258. else
  2259. {$endif NOTARGETWIN}
  2260. {$endif i386}
  2261. {$ifdef x86_64}
  2262. {$ifndef NOTARGETWIN}
  2263. { windows guards only a few pages for stack growing,
  2264. so we have to access every page first }
  2265. if (target_info.system=system_x86_64_win64) and
  2266. (localsize>=winstackpagesize) then
  2267. begin
  2268. if localsize div winstackpagesize<=5 then
  2269. begin
  2270. decrease_sp(localsize);
  2271. for i:=1 to localsize div winstackpagesize do
  2272. begin
  2273. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4,4);
  2274. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2275. end;
  2276. reference_reset_base(href,NR_RSP,0,4);
  2277. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2278. end
  2279. else
  2280. begin
  2281. current_asmdata.getjumplabel(again);
  2282. getcpuregister(list,NR_R10);
  2283. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  2284. a_label(list,again);
  2285. decrease_sp(winstackpagesize);
  2286. reference_reset_base(href,NR_RSP,0,4);
  2287. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2288. if UseIncDec then
  2289. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10))
  2290. else
  2291. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,1,NR_R10));
  2292. a_jmp_cond(list,OC_NE,again);
  2293. decrease_sp(localsize mod winstackpagesize);
  2294. ungetcpuregister(list,NR_R10);
  2295. end
  2296. end
  2297. else
  2298. {$endif NOTARGETWIN}
  2299. {$endif x86_64}
  2300. decrease_sp(localsize);
  2301. end;
  2302. end;
  2303. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  2304. var
  2305. stackmisalignment: longint;
  2306. para: tparavarsym;
  2307. regsize: longint;
  2308. {$ifdef i8086}
  2309. dgroup: treference;
  2310. {$endif i8086}
  2311. procedure push_regs;
  2312. var
  2313. r: longint;
  2314. begin
  2315. regsize:=0;
  2316. for r := low(saved_standard_registers) to high(saved_standard_registers) do
  2317. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2318. begin
  2319. inc(regsize,sizeof(aint));
  2320. list.concat(Taicpu.Op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE)));
  2321. end;
  2322. end;
  2323. begin
  2324. {$ifdef i8086}
  2325. { interrupt support for i8086 }
  2326. if po_interrupt in current_procinfo.procdef.procoptions then
  2327. begin
  2328. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_AX));
  2329. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_BX));
  2330. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CX));
  2331. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DX));
  2332. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  2333. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  2334. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  2335. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  2336. reference_reset(dgroup,0);
  2337. dgroup.refaddr:=addr_dgroup;
  2338. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,dgroup,NR_AX));
  2339. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2340. end;
  2341. {$endif i8086}
  2342. {$ifdef i386}
  2343. { interrupt support for i386 }
  2344. if (po_interrupt in current_procinfo.procdef.procoptions) and
  2345. { this messes up stack alignment }
  2346. not(target_info.system in [system_i386_darwin,system_i386_iphonesim,system_i386_android]) then
  2347. begin
  2348. { .... also the segment registers }
  2349. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  2350. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  2351. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  2352. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  2353. { save the registers of an interrupt procedure }
  2354. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  2355. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  2356. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2357. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  2358. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  2359. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  2360. end;
  2361. {$endif i386}
  2362. { save old framepointer }
  2363. if not nostackframe then
  2364. begin
  2365. { return address }
  2366. stackmisalignment := sizeof(pint);
  2367. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  2368. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  2369. begin
  2370. {$ifdef i386}
  2371. if (not paramanager.use_fixed_stack) then
  2372. push_regs;
  2373. {$endif i386}
  2374. CGmessage(cg_d_stackframe_omited);
  2375. end
  2376. else
  2377. begin
  2378. { push <frame_pointer> }
  2379. inc(stackmisalignment,sizeof(pint));
  2380. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  2381. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  2382. { Return address and FP are both on stack }
  2383. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(pint));
  2384. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(pint)));
  2385. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG));
  2386. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  2387. end;
  2388. { allocate stackframe space }
  2389. if (localsize<>0) or
  2390. ((target_info.stackalign>sizeof(pint)) and
  2391. (stackmisalignment <> 0) and
  2392. ((pi_do_call in current_procinfo.flags) or
  2393. (po_assembler in current_procinfo.procdef.procoptions))) then
  2394. begin
  2395. if target_info.stackalign>sizeof(pint) then
  2396. localsize := align(localsize+stackmisalignment,target_info.stackalign)-stackmisalignment;
  2397. cg.g_stackpointer_alloc(list,localsize);
  2398. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  2399. current_asmdata.asmcfi.cfa_def_cfa_offset(list,localsize+sizeof(pint));
  2400. current_procinfo.final_localsize:=localsize;
  2401. end;
  2402. {$ifdef i386}
  2403. if (not paramanager.use_fixed_stack) and
  2404. (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  2405. begin
  2406. regsize:=0;
  2407. push_regs;
  2408. reference_reset_base(current_procinfo.save_regs_ref,
  2409. current_procinfo.framepointer,
  2410. -(localsize+regsize),sizeof(aint));
  2411. end;
  2412. {$endif i386}
  2413. end;
  2414. end;
  2415. procedure tcgx86.g_save_registers(list: TAsmList);
  2416. begin
  2417. {$ifdef i386}
  2418. if paramanager.use_fixed_stack then
  2419. {$endif i386}
  2420. inherited g_save_registers(list);
  2421. end;
  2422. procedure tcgx86.g_restore_registers(list: TAsmList);
  2423. begin
  2424. {$ifdef i386}
  2425. if paramanager.use_fixed_stack then
  2426. {$endif i386}
  2427. inherited g_restore_registers(list);
  2428. end;
  2429. procedure tcgx86.internal_restore_regs(list: TAsmList; use_pop: boolean);
  2430. var
  2431. r: longint;
  2432. hreg: tregister;
  2433. href: treference;
  2434. begin
  2435. href:=current_procinfo.save_regs_ref;
  2436. for r:=high(saved_standard_registers) downto low(saved_standard_registers) do
  2437. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2438. begin
  2439. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  2440. { Allocate register so the optimizer does not remove the load }
  2441. a_reg_alloc(list,hreg);
  2442. if use_pop then
  2443. list.concat(Taicpu.Op_reg(A_POP,tcgsize2opsize[OS_ADDR],hreg))
  2444. else
  2445. begin
  2446. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2447. inc(href.offset,sizeof(aint));
  2448. end;
  2449. end;
  2450. end;
  2451. { produces if necessary overflowcode }
  2452. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  2453. var
  2454. hl : tasmlabel;
  2455. ai : taicpu;
  2456. cond : TAsmCond;
  2457. begin
  2458. if not(cs_check_overflow in current_settings.localswitches) then
  2459. exit;
  2460. current_asmdata.getjumplabel(hl);
  2461. if not ((def.typ=pointerdef) or
  2462. ((def.typ=orddef) and
  2463. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  2464. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  2465. cond:=C_NO
  2466. else
  2467. cond:=C_NB;
  2468. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  2469. ai.SetCondition(cond);
  2470. ai.is_jmp:=true;
  2471. list.concat(ai);
  2472. a_call_name(list,'FPC_OVERFLOW',false);
  2473. a_label(list,hl);
  2474. end;
  2475. procedure tcgx86.g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string);
  2476. var
  2477. ref : treference;
  2478. sym : tasmsymbol;
  2479. begin
  2480. if (target_info.system = system_i386_darwin) then
  2481. begin
  2482. { a_jmp_name jumps to a stub which is always pic-safe on darwin }
  2483. inherited g_external_wrapper(list,procdef,externalname);
  2484. exit;
  2485. end;
  2486. sym:=current_asmdata.RefAsmSymbol(externalname);
  2487. reference_reset_symbol(ref,sym,0,sizeof(pint));
  2488. { create pic'ed? }
  2489. if (cs_create_pic in current_settings.moduleswitches) and
  2490. { darwin/x86_64's assembler doesn't want @PLT after call symbols }
  2491. not(target_info.system in [system_x86_64_darwin,system_i386_iphonesim]) then
  2492. ref.refaddr:=addr_pic
  2493. else
  2494. ref.refaddr:=addr_full;
  2495. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  2496. end;
  2497. end.