cgx86.pas 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200
  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_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  51. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  52. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  53. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  54. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  55. {$ifndef i8086}
  56. procedure a_op_const_reg_reg(list : TAsmList; op : Topcg; size : Tcgsize; a : tcgint; src,dst : Tregister); override;
  57. procedure a_op_reg_reg_reg(list : TAsmList; op : TOpCg; size : tcgsize; src1,src2,dst : tregister); override;
  58. {$endif not i8086}
  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; srcsize, dstsize: 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 make_simple_ref(list:TAsmList;var ref: treference);
  102. procedure make_direct_ref(list:TAsmList;var ref: treference);
  103. function get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  104. procedure generate_leave(list : TAsmList);
  105. protected
  106. in_make_direct_ref : boolean;
  107. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  108. procedure check_register_size(size:tcgsize;reg:tregister);
  109. procedure opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  110. procedure opmm_loc_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;loc : tlocation;src,dst : tregister;shuffle : pmmshuffle);
  111. procedure sizes2load(s1,s2 : tcgsize;var op: tasmop; var s3: topsize);
  112. procedure floatload(list: TAsmList; t : tcgsize;const ref : treference);
  113. procedure floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  114. procedure floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  115. procedure floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  116. procedure internal_restore_regs(list: TAsmList; use_pop: boolean);
  117. end;
  118. const
  119. {$if defined(x86_64)}
  120. TCGSize2OpSize: Array[tcgsize] of topsize =
  121. (S_NO,S_B,S_W,S_L,S_Q,S_XMM,S_B,S_W,S_L,S_Q,S_XMM,
  122. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  123. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,
  124. S_NO,S_NO,S_NO,S_NO,S_XMM,S_YMM);
  125. {$elseif defined(i386)}
  126. TCGSize2OpSize: Array[tcgsize] of topsize =
  127. (S_NO,S_B,S_W,S_L,S_L,S_T,S_B,S_W,S_L,S_L,S_L,
  128. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  129. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,
  130. S_NO,S_NO,S_NO,S_NO,S_XMM,S_YMM);
  131. {$elseif defined(i8086)}
  132. TCGSize2OpSize: Array[tcgsize] of topsize =
  133. (S_NO,S_B,S_W,S_W,S_W,S_T,S_B,S_W,S_W,S_W,S_W,
  134. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  135. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,
  136. S_NO,S_NO,S_NO,S_NO,S_XMM,S_YMM);
  137. {$endif}
  138. {$ifndef NOTARGETWIN}
  139. winstackpagesize = 4096;
  140. {$endif NOTARGETWIN}
  141. function UseAVX: boolean;
  142. function UseIncDec: boolean;
  143. { returns true, if the compiler should use leave instead of mov/pop }
  144. function UseLeave: boolean;
  145. implementation
  146. uses
  147. globals,verbose,systems,cutils,
  148. defutil,paramgr,procinfo,
  149. tgobj,ncgutil,
  150. fmodule,symsym,symcpu;
  151. function UseAVX: boolean;
  152. begin
  153. Result:=(current_settings.fputype in fpu_avx_instructionsets) {$ifndef i8086}or (CPUX86_HAS_AVXUNIT in cpu_capabilities[current_settings.cputype]){$endif i8086};
  154. end;
  155. { modern CPUs prefer add/sub over inc/dec because add/sub break instructions dependencies on flags
  156. because they modify all flags }
  157. function UseIncDec: boolean;
  158. begin
  159. {$if defined(x86_64)}
  160. Result:=cs_opt_size in current_settings.optimizerswitches;
  161. {$elseif defined(i386)}
  162. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.cputype in [cpu_386]);
  163. {$elseif defined(i8086)}
  164. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.cputype in [cpu_8086..cpu_386]);
  165. {$endif}
  166. end;
  167. function UseLeave: boolean;
  168. begin
  169. {$if defined(x86_64)}
  170. { Modern processors should be happy with mov;pop, maybe except older AMDs }
  171. Result:=cs_opt_size in current_settings.optimizerswitches;
  172. {$elseif defined(i386)}
  173. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.optimizecputype<cpu_Pentium2);
  174. {$elseif defined(i8086)}
  175. Result:=current_settings.cputype>=cpu_186;
  176. {$endif}
  177. end;
  178. const
  179. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_MOV,A_ADD,A_AND,A_DIV,
  180. A_IDIV,A_IMUL,A_MUL,A_NEG,A_NOT,A_OR,
  181. A_SAR,A_SHL,A_SHR,A_SUB,A_XOR,A_ROL,A_ROR);
  182. TOpCmp2AsmCond: Array[topcmp] of TAsmCond = (C_NONE,
  183. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  184. procedure Tcgx86.done_register_allocators;
  185. begin
  186. rg[R_INTREGISTER].free;
  187. rg[R_MMREGISTER].free;
  188. rg[R_MMXREGISTER].free;
  189. rgfpu.free;
  190. inherited done_register_allocators;
  191. end;
  192. function Tcgx86.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  193. begin
  194. result:=rgfpu.getregisterfpu(list);
  195. end;
  196. function Tcgx86.getmmxregister(list:TAsmList):Tregister;
  197. begin
  198. if not assigned(rg[R_MMXREGISTER]) then
  199. internalerror(2003121214);
  200. result:=rg[R_MMXREGISTER].getregister(list,R_SUBNONE);
  201. end;
  202. function Tcgx86.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  203. begin
  204. if not assigned(rg[R_MMREGISTER]) then
  205. internalerror(2003121234);
  206. case size of
  207. OS_F64:
  208. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMD);
  209. OS_F32:
  210. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMS);
  211. OS_M64:
  212. result:=rg[R_MMREGISTER].getregister(list,R_SUBQ);
  213. OS_M128:
  214. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMWHOLE);
  215. else
  216. internalerror(200506041);
  217. end;
  218. end;
  219. procedure Tcgx86.getcpuregister(list:TAsmList;r:Tregister);
  220. begin
  221. if getregtype(r)=R_FPUREGISTER then
  222. internalerror(2003121210)
  223. else
  224. inherited getcpuregister(list,r);
  225. end;
  226. procedure tcgx86.ungetcpuregister(list:TAsmList;r:Tregister);
  227. begin
  228. if getregtype(r)=R_FPUREGISTER then
  229. rgfpu.ungetregisterfpu(list,r)
  230. else
  231. inherited ungetcpuregister(list,r);
  232. end;
  233. procedure Tcgx86.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  234. begin
  235. if rt<>R_FPUREGISTER then
  236. inherited alloccpuregisters(list,rt,r);
  237. end;
  238. procedure Tcgx86.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  239. begin
  240. if rt<>R_FPUREGISTER then
  241. inherited dealloccpuregisters(list,rt,r);
  242. end;
  243. function Tcgx86.uses_registers(rt:Tregistertype):boolean;
  244. begin
  245. if rt=R_FPUREGISTER then
  246. result:=false
  247. else
  248. result:=inherited uses_registers(rt);
  249. end;
  250. procedure tcgx86.add_reg_instruction(instr:Tai;r:tregister);
  251. begin
  252. if getregtype(r)<>R_FPUREGISTER then
  253. inherited add_reg_instruction(instr,r);
  254. end;
  255. procedure tcgx86.dec_fpu_stack;
  256. begin
  257. if rgfpu.fpuvaroffset<=0 then
  258. internalerror(200604201);
  259. dec(rgfpu.fpuvaroffset);
  260. end;
  261. procedure tcgx86.inc_fpu_stack;
  262. begin
  263. if rgfpu.fpuvaroffset>=7 then
  264. internalerror(2012062901);
  265. inc(rgfpu.fpuvaroffset);
  266. end;
  267. {****************************************************************************
  268. This is private property, keep out! :)
  269. ****************************************************************************}
  270. procedure tcgx86.sizes2load(s1,s2 : tcgsize; var op: tasmop; var s3: topsize);
  271. begin
  272. { ensure to have always valid sizes }
  273. if s1=OS_NO then
  274. s1:=s2;
  275. if s2=OS_NO then
  276. s2:=s1;
  277. case s2 of
  278. OS_8,OS_S8 :
  279. if S1 in [OS_8,OS_S8] then
  280. s3 := S_B
  281. else
  282. internalerror(200109221);
  283. OS_16,OS_S16:
  284. case s1 of
  285. OS_8,OS_S8:
  286. s3 := S_BW;
  287. OS_16,OS_S16:
  288. s3 := S_W;
  289. else
  290. internalerror(200109222);
  291. end;
  292. OS_32,OS_S32:
  293. case s1 of
  294. OS_8,OS_S8:
  295. s3 := S_BL;
  296. OS_16,OS_S16:
  297. s3 := S_WL;
  298. OS_32,OS_S32:
  299. s3 := S_L;
  300. else
  301. internalerror(200109223);
  302. end;
  303. {$ifdef x86_64}
  304. OS_64,OS_S64:
  305. case s1 of
  306. OS_8:
  307. s3 := S_BL;
  308. OS_S8:
  309. s3 := S_BQ;
  310. OS_16:
  311. s3 := S_WL;
  312. OS_S16:
  313. s3 := S_WQ;
  314. OS_32:
  315. s3 := S_L;
  316. OS_S32:
  317. s3 := S_LQ;
  318. OS_64,OS_S64:
  319. s3 := S_Q;
  320. else
  321. internalerror(200304302);
  322. end;
  323. {$endif x86_64}
  324. else
  325. internalerror(200109227);
  326. end;
  327. if s3 in [S_B,S_W,S_L,S_Q] then
  328. op := A_MOV
  329. else if s1 in [OS_8,OS_16,OS_32,OS_64] then
  330. op := A_MOVZX
  331. else
  332. {$ifdef x86_64}
  333. if s3 in [S_LQ] then
  334. op := A_MOVSXD
  335. else
  336. {$endif x86_64}
  337. op := A_MOVSX;
  338. end;
  339. procedure tcgx86.make_simple_ref(list:TAsmList;var ref: treference);
  340. var
  341. hreg : tregister;
  342. href : treference;
  343. {$ifndef x86_64}
  344. add_hreg: boolean;
  345. {$endif not x86_64}
  346. begin
  347. hreg:=NR_NO;
  348. { make_simple_ref() may have already been called earlier, and in that
  349. case make sure we don't perform the PIC-simplifications twice }
  350. if (ref.refaddr in [addr_pic,addr_pic_no_got]) then
  351. exit;
  352. { handle indirect symbols first }
  353. make_direct_ref(list,ref);
  354. {$if defined(x86_64)}
  355. { Only 32bit is allowed }
  356. { Note that this isn't entirely correct: for RIP-relative targets/memory models,
  357. it is actually (offset+@symbol-RIP) that should fit into 32 bits. Since two last
  358. members aren't known until link time, ABIs place very pessimistic limits
  359. on offset values, e.g. SysV AMD64 allows +/-$1000000 (16 megabytes) }
  360. if ((ref.offset<low(longint)) or (ref.offset>high(longint))) or
  361. { absolute address is not a common thing in x64, but nevertheless a possible one }
  362. ((ref.base=NR_NO) and (ref.index=NR_NO) and (ref.symbol=nil)) then
  363. begin
  364. { Load constant value to register }
  365. hreg:=GetAddressRegister(list);
  366. list.concat(taicpu.op_const_reg(A_MOV,S_Q,ref.offset,hreg));
  367. ref.offset:=0;
  368. {if assigned(ref.symbol) then
  369. begin
  370. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_Q,ref.symbol,0,hreg));
  371. ref.symbol:=nil;
  372. end;}
  373. { Add register to reference }
  374. if ref.base=NR_NO then
  375. ref.base:=hreg
  376. else if ref.index=NR_NO then
  377. ref.index:=hreg
  378. else
  379. begin
  380. { don't use add, as the flags may contain a value }
  381. reference_reset_base(href,ref.base,0,8);
  382. href.index:=hreg;
  383. if ref.scalefactor<>0 then
  384. begin
  385. reference_reset_base(href,ref.base,0,8);
  386. href.index:=hreg;
  387. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  388. ref.base:=hreg;
  389. end
  390. else
  391. begin
  392. reference_reset_base(href,ref.index,0,8);
  393. href.index:=hreg;
  394. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.index,hreg));
  395. ref.index:=hreg;
  396. end;
  397. end;
  398. end;
  399. if assigned(ref.symbol) then
  400. begin
  401. if cs_create_pic in current_settings.moduleswitches then
  402. begin
  403. { Local symbols must not be accessed via the GOT }
  404. if (ref.symbol.bind=AB_LOCAL) then
  405. begin
  406. { unfortunately, RIP-based addresses don't support an index }
  407. if (ref.base<>NR_NO) or
  408. (ref.index<>NR_NO) then
  409. begin
  410. reference_reset_symbol(href,ref.symbol,0,ref.alignment);
  411. hreg:=getaddressregister(list);
  412. href.refaddr:=addr_pic_no_got;
  413. href.base:=NR_RIP;
  414. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  415. ref.symbol:=nil;
  416. end
  417. else
  418. begin
  419. ref.refaddr:=addr_pic_no_got;
  420. hreg:=NR_NO;
  421. ref.base:=NR_RIP;
  422. end;
  423. end
  424. else
  425. begin
  426. reference_reset_symbol(href,ref.symbol,0,ref.alignment);
  427. hreg:=getaddressregister(list);
  428. href.refaddr:=addr_pic;
  429. href.base:=NR_RIP;
  430. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,href,hreg));
  431. ref.symbol:=nil;
  432. end;
  433. if ref.base=NR_NO then
  434. ref.base:=hreg
  435. else if ref.index=NR_NO then
  436. begin
  437. ref.index:=hreg;
  438. ref.scalefactor:=1;
  439. end
  440. else
  441. begin
  442. { don't use add, as the flags may contain a value }
  443. reference_reset_base(href,ref.base,0,8);
  444. href.index:=hreg;
  445. ref.base:=getaddressregister(list);
  446. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,ref.base));
  447. end;
  448. end
  449. else
  450. { Always use RIP relative symbol addressing for Windows and Darwin targets. }
  451. if (target_info.system in (systems_all_windows+[system_x86_64_darwin,system_x86_64_iphonesim])) and (ref.base<>NR_RIP) then
  452. begin
  453. if (ref.refaddr=addr_no) and (ref.base=NR_NO) and (ref.index=NR_NO) then
  454. begin
  455. { Set RIP relative addressing for simple symbol references }
  456. ref.base:=NR_RIP;
  457. ref.refaddr:=addr_pic_no_got
  458. end
  459. else
  460. begin
  461. { Use temp register to load calculated 64-bit symbol address for complex references }
  462. reference_reset_symbol(href,ref.symbol,0,sizeof(pint));
  463. href.base:=NR_RIP;
  464. href.refaddr:=addr_pic_no_got;
  465. hreg:=GetAddressRegister(list);
  466. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  467. ref.symbol:=nil;
  468. if ref.base=NR_NO then
  469. ref.base:=hreg
  470. else if ref.index=NR_NO then
  471. begin
  472. ref.index:=hreg;
  473. ref.scalefactor:=0;
  474. end
  475. else
  476. begin
  477. { don't use add, as the flags may contain a value }
  478. reference_reset_base(href,ref.base,0,8);
  479. href.index:=hreg;
  480. ref.base:=getaddressregister(list);
  481. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,ref.base));
  482. end;
  483. end;
  484. end;
  485. end;
  486. {$elseif defined(i386)}
  487. add_hreg:=false;
  488. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  489. begin
  490. if assigned(ref.symbol) and
  491. not(assigned(ref.relsymbol)) and
  492. ((ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN]) or
  493. (cs_create_pic in current_settings.moduleswitches)) then
  494. begin
  495. if ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN] then
  496. begin
  497. hreg:=g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol));
  498. ref.symbol:=nil;
  499. end
  500. else
  501. begin
  502. include(current_procinfo.flags,pi_needs_got);
  503. { make a copy of the got register, hreg can get modified }
  504. hreg:=getaddressregister(list);
  505. a_load_reg_reg(list,OS_ADDR,OS_ADDR,current_procinfo.got,hreg);
  506. ref.relsymbol:=current_procinfo.CurrGOTLabel;
  507. end;
  508. add_hreg:=true
  509. end
  510. end
  511. else if (cs_create_pic in current_settings.moduleswitches) and
  512. assigned(ref.symbol) then
  513. begin
  514. reference_reset_symbol(href,ref.symbol,0,sizeof(pint));
  515. href.base:=current_procinfo.got;
  516. href.refaddr:=addr_pic;
  517. include(current_procinfo.flags,pi_needs_got);
  518. hreg:=getaddressregister(list);
  519. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,hreg));
  520. ref.symbol:=nil;
  521. add_hreg:=true;
  522. end;
  523. if add_hreg then
  524. begin
  525. if ref.base=NR_NO then
  526. ref.base:=hreg
  527. else if ref.index=NR_NO then
  528. begin
  529. ref.index:=hreg;
  530. ref.scalefactor:=1;
  531. end
  532. else
  533. begin
  534. { don't use add, as the flags may contain a value }
  535. reference_reset_base(href,ref.base,0,8);
  536. href.index:=hreg;
  537. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  538. ref.base:=hreg;
  539. end;
  540. end;
  541. {$elseif defined(i8086)}
  542. { i8086 does not support stack relative addressing }
  543. if ref.base = NR_STACK_POINTER_REG then
  544. begin
  545. href:=ref;
  546. href.base:=getaddressregister(list);
  547. { let the register allocator find a suitable register for the reference }
  548. list.Concat(Taicpu.op_reg_reg(A_MOV, S_W, NR_SP, href.base));
  549. { if DS<>SS in the current memory model, we need to add an SS: segment override as well }
  550. if (ref.segment=NR_NO) and not segment_regs_equal(NR_DS,NR_SS) then
  551. href.segment:=NR_SS;
  552. ref:=href;
  553. end;
  554. { if there is a segment in an int register, move it to ES }
  555. if (ref.segment<>NR_NO) and (not is_segment_reg(ref.segment)) then
  556. begin
  557. list.concat(taicpu.op_reg_reg(A_MOV,S_W,ref.segment,NR_ES));
  558. ref.segment:=NR_ES;
  559. end;
  560. { can the segment override be dropped? }
  561. if ref.segment<>NR_NO then
  562. begin
  563. if (ref.base=NR_BP) and segment_regs_equal(ref.segment,NR_SS) then
  564. ref.segment:=NR_NO;
  565. if (ref.base<>NR_BP) and segment_regs_equal(ref.segment,NR_DS) then
  566. ref.segment:=NR_NO;
  567. end;
  568. {$endif}
  569. end;
  570. procedure tcgx86.make_direct_ref(list:tasmlist;var ref:treference);
  571. var
  572. href : treference;
  573. hreg : tregister;
  574. begin
  575. if in_make_direct_ref then
  576. exit;
  577. in_make_direct_ref:=true;
  578. if assigned(ref.symbol) and (ref.symbol.bind in asmsymbindindirect) then
  579. begin
  580. hreg:=getaddressregister(list);
  581. reference_reset_symbol(href,ref.symbol,0,sizeof(pint));
  582. a_op_ref_reg(list,OP_MOVE,OS_ADDR,href,hreg);
  583. if ref.base<>NR_NO then
  584. a_op_reg_reg(list,OP_ADD,OS_ADDR,ref.base,hreg);
  585. ref.symbol:=nil;
  586. ref.base:=hreg;
  587. end;
  588. in_make_direct_ref:=false;
  589. end;
  590. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  591. begin
  592. case t of
  593. OS_F32 :
  594. begin
  595. op:=A_FLD;
  596. s:=S_FS;
  597. end;
  598. OS_F64 :
  599. begin
  600. op:=A_FLD;
  601. s:=S_FL;
  602. end;
  603. OS_F80 :
  604. begin
  605. op:=A_FLD;
  606. s:=S_FX;
  607. end;
  608. OS_C64 :
  609. begin
  610. op:=A_FILD;
  611. s:=S_IQ;
  612. end;
  613. else
  614. internalerror(200204043);
  615. end;
  616. end;
  617. procedure tcgx86.floatload(list: TAsmList; t : tcgsize;const ref : treference);
  618. var
  619. op : tasmop;
  620. s : topsize;
  621. tmpref : treference;
  622. begin
  623. tmpref:=ref;
  624. make_simple_ref(list,tmpref);
  625. floatloadops(t,op,s);
  626. list.concat(Taicpu.Op_ref(op,s,tmpref));
  627. inc_fpu_stack;
  628. end;
  629. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  630. begin
  631. case t of
  632. OS_F32 :
  633. begin
  634. op:=A_FSTP;
  635. s:=S_FS;
  636. end;
  637. OS_F64 :
  638. begin
  639. op:=A_FSTP;
  640. s:=S_FL;
  641. end;
  642. OS_F80 :
  643. begin
  644. op:=A_FSTP;
  645. s:=S_FX;
  646. end;
  647. OS_C64 :
  648. begin
  649. op:=A_FISTP;
  650. s:=S_IQ;
  651. end;
  652. else
  653. internalerror(200204042);
  654. end;
  655. end;
  656. procedure tcgx86.floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  657. var
  658. op : tasmop;
  659. s : topsize;
  660. tmpref : treference;
  661. begin
  662. tmpref:=ref;
  663. make_simple_ref(list,tmpref);
  664. floatstoreops(t,op,s);
  665. list.concat(Taicpu.Op_ref(op,s,tmpref));
  666. { storing non extended floats can cause a floating point overflow }
  667. if ((t<>OS_F80) and (cs_fpu_fwait in current_settings.localswitches))
  668. {$ifdef i8086}
  669. { 8087 and 80287 need a FWAIT after a memory store, before it can be
  670. read with the integer unit }
  671. or (current_settings.cputype<=cpu_286)
  672. {$endif i8086}
  673. then
  674. list.concat(Taicpu.Op_none(A_FWAIT,S_NO));
  675. dec_fpu_stack;
  676. end;
  677. procedure tcgx86.check_register_size(size:tcgsize;reg:tregister);
  678. begin
  679. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  680. internalerror(200306031);
  681. end;
  682. {****************************************************************************
  683. Assembler code
  684. ****************************************************************************}
  685. procedure tcgx86.a_jmp_name(list : TAsmList;const s : string);
  686. var
  687. r: treference;
  688. begin
  689. if (target_info.system <> system_i386_darwin) then
  690. list.concat(taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s)))
  691. else
  692. begin
  693. reference_reset_symbol(r,get_darwin_call_stub(s,false),0,sizeof(pint));
  694. r.refaddr:=addr_full;
  695. list.concat(taicpu.op_ref(A_JMP,S_NO,r));
  696. end;
  697. end;
  698. procedure tcgx86.a_jmp_always(list : TAsmList;l: tasmlabel);
  699. begin
  700. a_jmp_cond(list, OC_NONE, l);
  701. end;
  702. function tcgx86.get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  703. var
  704. stubname: string;
  705. begin
  706. stubname := 'L'+s+'$stub';
  707. result := current_asmdata.getasmsymbol(stubname);
  708. if assigned(result) then
  709. exit;
  710. if current_asmdata.asmlists[al_imports]=nil then
  711. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  712. new_section(current_asmdata.asmlists[al_imports],sec_stub,'',0);
  713. result := current_asmdata.DefineAsmSymbol(stubname,AB_LOCAL,AT_FUNCTION);
  714. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  715. { register as a weak symbol if necessary }
  716. if weak then
  717. current_asmdata.weakrefasmsymbol(s);
  718. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  719. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  720. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  721. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  722. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  723. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  724. end;
  725. procedure tcgx86.a_call_name(list : TAsmList;const s : string; weak: boolean);
  726. begin
  727. a_call_name_near(list,s,weak);
  728. end;
  729. procedure tcgx86.a_call_name_near(list : TAsmList;const s : string; weak: boolean);
  730. var
  731. sym : tasmsymbol;
  732. r : treference;
  733. begin
  734. if (target_info.system <> system_i386_darwin) then
  735. begin
  736. if not(weak) then
  737. sym:=current_asmdata.RefAsmSymbol(s)
  738. else
  739. sym:=current_asmdata.WeakRefAsmSymbol(s);
  740. reference_reset_symbol(r,sym,0,sizeof(pint));
  741. if (cs_create_pic in current_settings.moduleswitches) and
  742. { darwin's assembler doesn't want @PLT after call symbols }
  743. not(target_info.system in [system_x86_64_darwin,system_i386_iphonesim,system_x86_64_iphonesim]) then
  744. begin
  745. {$ifdef i386}
  746. include(current_procinfo.flags,pi_needs_got);
  747. {$endif i386}
  748. r.refaddr:=addr_pic
  749. end
  750. else
  751. r.refaddr:=addr_full;
  752. end
  753. else
  754. begin
  755. reference_reset_symbol(r,get_darwin_call_stub(s,weak),0,sizeof(pint));
  756. r.refaddr:=addr_full;
  757. end;
  758. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  759. end;
  760. procedure tcgx86.a_call_name_static(list : TAsmList;const s : string);
  761. begin
  762. a_call_name_static_near(list,s);
  763. end;
  764. procedure tcgx86.a_call_name_static_near(list : TAsmList;const s : string);
  765. var
  766. sym : tasmsymbol;
  767. r : treference;
  768. begin
  769. sym:=current_asmdata.RefAsmSymbol(s);
  770. reference_reset_symbol(r,sym,0,sizeof(pint));
  771. r.refaddr:=addr_full;
  772. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  773. end;
  774. procedure tcgx86.a_call_reg(list : TAsmList;reg : tregister);
  775. begin
  776. a_call_reg_near(list,reg);
  777. end;
  778. procedure tcgx86.a_call_reg_near(list: TAsmList; reg: tregister);
  779. begin
  780. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  781. end;
  782. {********************** load instructions ********************}
  783. procedure tcgx86.a_load_const_reg(list : TAsmList; tosize: TCGSize; a : tcgint; reg : TRegister);
  784. begin
  785. check_register_size(tosize,reg);
  786. if a=0 then
  787. list.concat(taicpu.op_reg_reg(A_XOR,TCGSize2OpSize[tosize],reg,reg))
  788. else
  789. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg));
  790. end;
  791. procedure tcgx86.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  792. var
  793. tmpref : treference;
  794. begin
  795. tmpref:=ref;
  796. make_simple_ref(list,tmpref);
  797. {$ifdef x86_64}
  798. { x86_64 only supports signed 32 bits constants directly }
  799. if (tosize in [OS_S64,OS_64]) and
  800. ((a<low(longint)) or (a>high(longint))) then
  801. begin
  802. a_load_const_ref(list,OS_32,longint(a and $ffffffff),tmpref);
  803. inc(tmpref.offset,4);
  804. a_load_const_ref(list,OS_32,longint(a shr 32),tmpref);
  805. end
  806. else
  807. {$endif x86_64}
  808. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  809. end;
  810. procedure tcgx86.a_load_reg_ref(list : TAsmList; fromsize,tosize: TCGSize; reg : tregister;const ref : treference);
  811. var
  812. op: tasmop;
  813. s: topsize;
  814. tmpsize : tcgsize;
  815. tmpreg : tregister;
  816. tmpref : treference;
  817. begin
  818. tmpref:=ref;
  819. make_simple_ref(list,tmpref);
  820. check_register_size(fromsize,reg);
  821. sizes2load(fromsize,tosize,op,s);
  822. case s of
  823. {$ifdef x86_64}
  824. S_BQ,S_WQ,S_LQ,
  825. {$endif x86_64}
  826. S_BW,S_BL,S_WL :
  827. begin
  828. tmpreg:=getintregister(list,tosize);
  829. {$ifdef x86_64}
  830. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  831. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  832. 64 bit (FK) }
  833. if s in [S_BL,S_WL,S_L] then
  834. begin
  835. tmpreg:=makeregsize(list,tmpreg,OS_32);
  836. tmpsize:=OS_32;
  837. end
  838. else
  839. {$endif x86_64}
  840. tmpsize:=tosize;
  841. list.concat(taicpu.op_reg_reg(op,s,reg,tmpreg));
  842. a_load_reg_ref(list,tmpsize,tosize,tmpreg,tmpref);
  843. end;
  844. else
  845. list.concat(taicpu.op_reg_ref(op,s,reg,tmpref));
  846. end;
  847. end;
  848. procedure tcgx86.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  849. var
  850. op: tasmop;
  851. s: topsize;
  852. tmpref : treference;
  853. begin
  854. tmpref:=ref;
  855. make_simple_ref(list,tmpref);
  856. check_register_size(tosize,reg);
  857. sizes2load(fromsize,tosize,op,s);
  858. {$ifdef x86_64}
  859. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  860. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  861. 64 bit (FK) }
  862. if s in [S_BL,S_WL,S_L] then
  863. reg:=makeregsize(list,reg,OS_32);
  864. {$endif x86_64}
  865. list.concat(taicpu.op_ref_reg(op,s,tmpref,reg));
  866. end;
  867. procedure tcgx86.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  868. var
  869. op: tasmop;
  870. s: topsize;
  871. instr:Taicpu;
  872. begin
  873. check_register_size(fromsize,reg1);
  874. check_register_size(tosize,reg2);
  875. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  876. begin
  877. reg1:=makeregsize(list,reg1,tosize);
  878. s:=tcgsize2opsize[tosize];
  879. op:=A_MOV;
  880. end
  881. else
  882. sizes2load(fromsize,tosize,op,s);
  883. {$ifdef x86_64}
  884. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  885. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  886. 64 bit (FK)
  887. }
  888. if s in [S_BL,S_WL,S_L] then
  889. reg2:=makeregsize(list,reg2,OS_32);
  890. {$endif x86_64}
  891. if (reg1<>reg2) then
  892. begin
  893. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  894. { Notify the register allocator that we have written a move instruction so
  895. it can try to eliminate it. }
  896. if (reg1<>current_procinfo.framepointer) and (reg1<>NR_STACK_POINTER_REG) then
  897. add_move_instruction(instr);
  898. list.concat(instr);
  899. end;
  900. {$ifdef x86_64}
  901. { avoid merging of registers and killing the zero extensions (FK) }
  902. if (tosize in [OS_64,OS_S64]) and (s=S_L) then
  903. list.concat(taicpu.op_const_reg(A_AND,S_L,$ffffffff,reg2));
  904. {$endif x86_64}
  905. end;
  906. procedure tcgx86.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  907. var
  908. dirref,tmpref : treference;
  909. begin
  910. dirref:=ref;
  911. { this could probably done in a more optimized way, but for now this
  912. is sufficent }
  913. make_direct_ref(list,dirref);
  914. with dirref do
  915. begin
  916. if (base=NR_NO) and (index=NR_NO) then
  917. begin
  918. if assigned(dirref.symbol) then
  919. begin
  920. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  921. ((dirref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  922. (cs_create_pic in current_settings.moduleswitches)) then
  923. begin
  924. if (dirref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  925. ((cs_create_pic in current_settings.moduleswitches) and
  926. (dirref.symbol.bind in [AB_COMMON,AB_GLOBAL,AB_PRIVATE_EXTERN])) then
  927. begin
  928. reference_reset_base(tmpref,
  929. g_indirect_sym_load(list,dirref.symbol.name,asmsym2indsymflags(dirref.symbol)),
  930. offset,sizeof(pint));
  931. a_loadaddr_ref_reg(list,tmpref,r);
  932. end
  933. else
  934. begin
  935. include(current_procinfo.flags,pi_needs_got);
  936. reference_reset_base(tmpref,current_procinfo.got,offset,dirref.alignment);
  937. tmpref.symbol:=symbol;
  938. tmpref.relsymbol:=current_procinfo.CurrGOTLabel;
  939. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  940. end;
  941. end
  942. else if (cs_create_pic in current_settings.moduleswitches)
  943. {$ifdef x86_64}
  944. and not(dirref.symbol.bind=AB_LOCAL)
  945. {$endif x86_64}
  946. then
  947. begin
  948. {$ifdef x86_64}
  949. reference_reset_symbol(tmpref,dirref.symbol,0,dirref.alignment);
  950. tmpref.refaddr:=addr_pic;
  951. tmpref.base:=NR_RIP;
  952. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,tmpref,r));
  953. {$else x86_64}
  954. reference_reset_symbol(tmpref,dirref.symbol,0,dirref.alignment);
  955. tmpref.refaddr:=addr_pic;
  956. tmpref.base:=current_procinfo.got;
  957. include(current_procinfo.flags,pi_needs_got);
  958. list.concat(taicpu.op_ref_reg(A_MOV,S_L,tmpref,r));
  959. {$endif x86_64}
  960. if offset<>0 then
  961. a_op_const_reg(list,OP_ADD,OS_ADDR,offset,r);
  962. end
  963. {$ifdef x86_64}
  964. else if (target_info.system in (systems_all_windows+[system_x86_64_darwin,system_x86_64_iphonesim]))
  965. or (cs_create_pic in current_settings.moduleswitches)
  966. then
  967. begin
  968. { Win64 and Darwin/x86_64 always require RIP-relative addressing }
  969. tmpref:=dirref;
  970. tmpref.base:=NR_RIP;
  971. tmpref.refaddr:=addr_pic_no_got;
  972. list.concat(Taicpu.op_ref_reg(A_LEA,S_Q,tmpref,r));
  973. end
  974. {$endif x86_64}
  975. else
  976. begin
  977. tmpref:=dirref;
  978. tmpref.refaddr:=ADDR_FULL;
  979. list.concat(Taicpu.op_ref_reg(A_MOV,tcgsize2opsize[OS_ADDR],tmpref,r));
  980. end
  981. end
  982. else
  983. a_load_const_reg(list,OS_ADDR,offset,r)
  984. end
  985. else if (base=NR_NO) and (index<>NR_NO) and
  986. (offset=0) and (scalefactor=0) and (symbol=nil) then
  987. a_load_reg_reg(list,OS_ADDR,OS_ADDR,index,r)
  988. else if (base<>NR_NO) and (index=NR_NO) and
  989. (offset=0) and (symbol=nil) then
  990. a_load_reg_reg(list,OS_ADDR,OS_ADDR,base,r)
  991. else
  992. begin
  993. tmpref:=dirref;
  994. make_simple_ref(list,tmpref);
  995. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  996. end;
  997. if segment<>NR_NO then
  998. begin
  999. {$ifdef i8086}
  1000. if is_segment_reg(segment) then
  1001. list.concat(Taicpu.op_reg_reg(A_MOV,S_W,segment,GetNextReg(r)))
  1002. else
  1003. a_load_reg_reg(list,OS_16,OS_16,segment,GetNextReg(r));
  1004. {$else i8086}
  1005. if (tf_section_threadvars in target_info.flags) then
  1006. begin
  1007. { Convert thread local address to a process global addres
  1008. as we cannot handle far pointers.}
  1009. case target_info.system of
  1010. system_i386_linux,system_i386_android:
  1011. if segment=NR_GS then
  1012. begin
  1013. reference_reset_symbol(tmpref,current_asmdata.RefAsmSymbol('___fpc_threadvar_offset'),0,dirref.alignment);
  1014. tmpref.segment:=NR_GS;
  1015. list.concat(Taicpu.op_ref_reg(A_ADD,tcgsize2opsize[OS_ADDR],tmpref,r));
  1016. end
  1017. else
  1018. cgmessage(cg_e_cant_use_far_pointer_there);
  1019. else
  1020. cgmessage(cg_e_cant_use_far_pointer_there);
  1021. end;
  1022. end
  1023. else
  1024. cgmessage(cg_e_cant_use_far_pointer_there);
  1025. {$endif i8086}
  1026. end;
  1027. end;
  1028. end;
  1029. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  1030. { R_ST means "the current value at the top of the fpu stack" (JM) }
  1031. procedure tcgx86.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  1032. var
  1033. href: treference;
  1034. op: tasmop;
  1035. s: topsize;
  1036. begin
  1037. if (reg1<>NR_ST) then
  1038. begin
  1039. floatloadops(tosize,op,s);
  1040. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  1041. inc_fpu_stack;
  1042. end;
  1043. if (reg2<>NR_ST) then
  1044. begin
  1045. floatstoreops(tosize,op,s);
  1046. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  1047. dec_fpu_stack;
  1048. end;
  1049. { OS_F80 < OS_C64, but OS_C64 fits perfectly in OS_F80 }
  1050. if (reg1=NR_ST) and
  1051. (reg2=NR_ST) and
  1052. (tosize<>OS_F80) and
  1053. (tosize<fromsize) then
  1054. begin
  1055. { can't round down to lower precision in x87 :/ }
  1056. tg.gettemp(list,tcgsize2size[tosize],tcgsize2size[tosize],tt_normal,href);
  1057. a_loadfpu_reg_ref(list,fromsize,tosize,NR_ST,href);
  1058. a_loadfpu_ref_reg(list,tosize,tosize,href,NR_ST);
  1059. tg.ungettemp(list,href);
  1060. end;
  1061. end;
  1062. procedure tcgx86.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  1063. var
  1064. tmpref : treference;
  1065. begin
  1066. tmpref:=ref;
  1067. make_simple_ref(list,tmpref);
  1068. floatload(list,fromsize,tmpref);
  1069. a_loadfpu_reg_reg(list,fromsize,tosize,NR_ST,reg);
  1070. end;
  1071. procedure tcgx86.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  1072. var
  1073. tmpref : treference;
  1074. begin
  1075. tmpref:=ref;
  1076. make_simple_ref(list,tmpref);
  1077. { in case a record returned in a floating point register
  1078. (LOC_FPUREGISTER with OS_F32/OS_F64) is stored in memory
  1079. (LOC_REFERENCE with OS_32/OS_64), we have to adjust the
  1080. tosize }
  1081. if (fromsize in [OS_F32,OS_F64]) and
  1082. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1083. case tosize of
  1084. OS_32:
  1085. tosize:=OS_F32;
  1086. OS_64:
  1087. tosize:=OS_F64;
  1088. end;
  1089. if reg<>NR_ST then
  1090. a_loadfpu_reg_reg(list,fromsize,tosize,reg,NR_ST);
  1091. floatstore(list,tosize,tmpref);
  1092. end;
  1093. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  1094. const
  1095. convertopsse : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1096. (A_MOVSS,A_CVTSS2SD,A_NONE,A_NONE,A_NONE),
  1097. (A_CVTSD2SS,A_MOVSD,A_NONE,A_NONE,A_NONE),
  1098. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1099. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1100. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  1101. convertopavx : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1102. (A_VMOVSS,A_VCVTSS2SD,A_NONE,A_NONE,A_NONE),
  1103. (A_VCVTSD2SS,A_VMOVSD,A_NONE,A_NONE,A_NONE),
  1104. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1105. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1106. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  1107. begin
  1108. { we can have OS_F32/OS_F64 (record in function result/LOC_MMREGISTER) to
  1109. OS_32/OS_64 (record in memory/LOC_REFERENCE) }
  1110. if (fromsize in [OS_F32,OS_F64]) and
  1111. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1112. case tosize of
  1113. OS_32:
  1114. tosize:=OS_F32;
  1115. OS_64:
  1116. tosize:=OS_F64;
  1117. end;
  1118. if (fromsize in [low(convertopsse)..high(convertopsse)]) and
  1119. (tosize in [low(convertopsse)..high(convertopsse)]) then
  1120. begin
  1121. if UseAVX then
  1122. result:=convertopavx[fromsize,tosize]
  1123. else
  1124. result:=convertopsse[fromsize,tosize];
  1125. end
  1126. { we can have OS_M64 (record in function result/LOC_MMREGISTER) to
  1127. OS_64 (record in memory/LOC_REFERENCE) }
  1128. else if (tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1129. (fromsize=OS_M64) then
  1130. begin
  1131. if UseAVX then
  1132. result:=A_VMOVQ
  1133. else
  1134. result:=A_MOVQ;
  1135. end
  1136. else
  1137. internalerror(2010060104);
  1138. if result=A_NONE then
  1139. internalerror(200312205);
  1140. end;
  1141. procedure tcgx86.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  1142. var
  1143. instr : taicpu;
  1144. op : TAsmOp;
  1145. begin
  1146. if shuffle=nil then
  1147. begin
  1148. if fromsize=tosize then
  1149. { needs correct size in case of spilling }
  1150. case fromsize of
  1151. OS_F32:
  1152. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2);
  1153. OS_F64:
  1154. instr:=taicpu.op_reg_reg(A_MOVAPD,S_NO,reg1,reg2);
  1155. OS_M64:
  1156. instr:=taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2);
  1157. else
  1158. internalerror(2006091201);
  1159. end
  1160. else
  1161. internalerror(200312202);
  1162. add_move_instruction(instr);
  1163. end
  1164. else if shufflescalar(shuffle) then
  1165. begin
  1166. op:=get_scalar_mm_op(fromsize,tosize);
  1167. { MOVAPD/MOVAPS are normally faster }
  1168. if op=A_MOVSD then
  1169. op:=A_MOVAPD
  1170. else if op=A_MOVSS then
  1171. op:=A_MOVAPS
  1172. { VMOVSD/SS is not available with two register operands }
  1173. else if op=A_VMOVSD then
  1174. op:=A_VMOVAPD
  1175. else if op=A_VMOVSS then
  1176. op:=A_VMOVAPS;
  1177. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1178. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1179. instr:=taicpu.op_reg_reg_reg(op,S_NO,reg1,reg2,reg2)
  1180. else
  1181. instr:=taicpu.op_reg_reg(op,S_NO,reg1,reg2);
  1182. case op of
  1183. A_VMOVAPD,
  1184. A_VMOVAPS,
  1185. A_VMOVSS,
  1186. A_VMOVSD,
  1187. A_VMOVQ,
  1188. A_MOVAPD,
  1189. A_MOVAPS,
  1190. A_MOVSS,
  1191. A_MOVSD,
  1192. A_MOVQ:
  1193. add_move_instruction(instr);
  1194. end;
  1195. end
  1196. else
  1197. internalerror(200312201);
  1198. list.concat(instr);
  1199. end;
  1200. procedure tcgx86.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1201. var
  1202. tmpref : treference;
  1203. op : tasmop;
  1204. begin
  1205. tmpref:=ref;
  1206. make_simple_ref(list,tmpref);
  1207. if shuffle=nil then
  1208. begin
  1209. if fromsize=OS_M64 then
  1210. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,reg))
  1211. else
  1212. {$ifdef x86_64}
  1213. { x86-64 has always properly aligned data }
  1214. list.concat(taicpu.op_ref_reg(A_MOVDQA,S_NO,tmpref,reg));
  1215. {$else x86_64}
  1216. list.concat(taicpu.op_ref_reg(A_MOVDQU,S_NO,tmpref,reg));
  1217. {$endif x86_64}
  1218. end
  1219. else if shufflescalar(shuffle) then
  1220. begin
  1221. op:=get_scalar_mm_op(fromsize,tosize);
  1222. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1223. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1224. list.concat(taicpu.op_ref_reg_reg(op,S_NO,tmpref,reg,reg))
  1225. else
  1226. list.concat(taicpu.op_ref_reg(op,S_NO,tmpref,reg))
  1227. end
  1228. else
  1229. internalerror(200312252);
  1230. end;
  1231. procedure tcgx86.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  1232. var
  1233. hreg : tregister;
  1234. tmpref : treference;
  1235. op : tasmop;
  1236. begin
  1237. tmpref:=ref;
  1238. make_simple_ref(list,tmpref);
  1239. if shuffle=nil then
  1240. begin
  1241. if fromsize=OS_M64 then
  1242. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,tmpref))
  1243. else
  1244. {$ifdef x86_64}
  1245. { x86-64 has always properly aligned data }
  1246. list.concat(taicpu.op_reg_ref(A_MOVDQA,S_NO,reg,tmpref))
  1247. {$else x86_64}
  1248. list.concat(taicpu.op_reg_ref(A_MOVDQU,S_NO,reg,tmpref))
  1249. {$endif x86_64}
  1250. end
  1251. else if shufflescalar(shuffle) then
  1252. begin
  1253. if tcgsize2size[tosize]<>tcgsize2size[fromsize] then
  1254. begin
  1255. hreg:=getmmregister(list,tosize);
  1256. op:=get_scalar_mm_op(fromsize,tosize);
  1257. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1258. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1259. list.concat(taicpu.op_reg_reg_reg(op,S_NO,reg,hreg,hreg))
  1260. else
  1261. list.concat(taicpu.op_reg_reg(op,S_NO,reg,hreg));
  1262. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize),S_NO,hreg,tmpref))
  1263. end
  1264. else
  1265. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize),S_NO,reg,tmpref));
  1266. end
  1267. else
  1268. internalerror(200312252);
  1269. end;
  1270. procedure tcgx86.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1271. var
  1272. l : tlocation;
  1273. begin
  1274. l.loc:=LOC_REFERENCE;
  1275. l.reference:=ref;
  1276. l.size:=size;
  1277. opmm_loc_reg(list,op,size,l,reg,shuffle);
  1278. end;
  1279. procedure tcgx86.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  1280. var
  1281. l : tlocation;
  1282. begin
  1283. l.loc:=LOC_MMREGISTER;
  1284. l.register:=src;
  1285. l.size:=size;
  1286. opmm_loc_reg(list,op,size,l,dst,shuffle);
  1287. end;
  1288. procedure tcgx86.opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;src,dst: tregister; shuffle : pmmshuffle);
  1289. const
  1290. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1291. ( { scalar }
  1292. ( { OS_F32 }
  1293. 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
  1294. ),
  1295. ( { OS_F64 }
  1296. 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
  1297. )
  1298. ),
  1299. ( { vectorized/packed }
  1300. { because the logical packed single instructions have shorter op codes, we use always
  1301. these
  1302. }
  1303. ( { OS_F32 }
  1304. 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
  1305. ),
  1306. ( { OS_F64 }
  1307. 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
  1308. )
  1309. )
  1310. );
  1311. var
  1312. resultreg : tregister;
  1313. asmop : tasmop;
  1314. begin
  1315. { this is an internally used procedure so the parameters have
  1316. some constrains
  1317. }
  1318. if loc.size<>size then
  1319. internalerror(2013061108);
  1320. resultreg:=dst;
  1321. { deshuffle }
  1322. //!!!
  1323. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1324. begin
  1325. internalerror(2013061107);
  1326. end
  1327. else if (shuffle=nil) then
  1328. asmop:=opmm2asmop[1,size,op]
  1329. else if shufflescalar(shuffle) then
  1330. begin
  1331. asmop:=opmm2asmop[0,size,op];
  1332. { no scalar operation available? }
  1333. if asmop=A_NOP then
  1334. begin
  1335. { do vectorized and shuffle finally }
  1336. internalerror(2010060102);
  1337. end;
  1338. end
  1339. else
  1340. internalerror(2013061106);
  1341. if asmop=A_NOP then
  1342. internalerror(2013061105);
  1343. case loc.loc of
  1344. LOC_CREFERENCE,LOC_REFERENCE:
  1345. begin
  1346. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1347. list.concat(taicpu.op_ref_reg_reg(asmop,S_NO,loc.reference,src,resultreg));
  1348. end;
  1349. LOC_CMMREGISTER,LOC_MMREGISTER:
  1350. list.concat(taicpu.op_reg_reg_reg(asmop,S_NO,loc.register,src,resultreg));
  1351. else
  1352. internalerror(2013061104);
  1353. end;
  1354. { shuffle }
  1355. if resultreg<>dst then
  1356. begin
  1357. internalerror(2013061103);
  1358. end;
  1359. end;
  1360. procedure tcgx86.a_opmm_reg_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src1,src2,dst: tregister;shuffle : pmmshuffle);
  1361. var
  1362. l : tlocation;
  1363. begin
  1364. l.loc:=LOC_MMREGISTER;
  1365. l.register:=src1;
  1366. l.size:=size;
  1367. opmm_loc_reg_reg(list,op,size,l,src2,dst,shuffle);
  1368. end;
  1369. procedure tcgx86.a_opmm_ref_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; src,dst: tregister;shuffle : pmmshuffle);
  1370. var
  1371. l : tlocation;
  1372. begin
  1373. l.loc:=LOC_REFERENCE;
  1374. l.reference:=ref;
  1375. l.size:=size;
  1376. opmm_loc_reg_reg(list,op,size,l,src,dst,shuffle);
  1377. end;
  1378. procedure tcgx86.opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  1379. const
  1380. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1381. ( { scalar }
  1382. ( { OS_F32 }
  1383. 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
  1384. ),
  1385. ( { OS_F64 }
  1386. 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
  1387. )
  1388. ),
  1389. ( { vectorized/packed }
  1390. { because the logical packed single instructions have shorter op codes, we use always
  1391. these
  1392. }
  1393. ( { OS_F32 }
  1394. 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
  1395. ),
  1396. ( { OS_F64 }
  1397. 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
  1398. )
  1399. )
  1400. );
  1401. var
  1402. resultreg : tregister;
  1403. asmop : tasmop;
  1404. begin
  1405. { this is an internally used procedure so the parameters have
  1406. some constrains
  1407. }
  1408. if loc.size<>size then
  1409. internalerror(200312213);
  1410. resultreg:=dst;
  1411. { deshuffle }
  1412. //!!!
  1413. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1414. begin
  1415. internalerror(2010060101);
  1416. end
  1417. else if (shuffle=nil) then
  1418. asmop:=opmm2asmop[1,size,op]
  1419. else if shufflescalar(shuffle) then
  1420. begin
  1421. asmop:=opmm2asmop[0,size,op];
  1422. { no scalar operation available? }
  1423. if asmop=A_NOP then
  1424. begin
  1425. { do vectorized and shuffle finally }
  1426. internalerror(2010060102);
  1427. end;
  1428. end
  1429. else
  1430. internalerror(200312211);
  1431. if asmop=A_NOP then
  1432. internalerror(200312216);
  1433. case loc.loc of
  1434. LOC_CREFERENCE,LOC_REFERENCE:
  1435. begin
  1436. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1437. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  1438. end;
  1439. LOC_CMMREGISTER,LOC_MMREGISTER:
  1440. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  1441. else
  1442. internalerror(200312214);
  1443. end;
  1444. { shuffle }
  1445. if resultreg<>dst then
  1446. begin
  1447. internalerror(200312212);
  1448. end;
  1449. end;
  1450. {$ifndef i8086}
  1451. procedure tcgx86.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1452. a:tcgint;src,dst:Tregister);
  1453. var
  1454. power,al : longint;
  1455. href : treference;
  1456. begin
  1457. power:=0;
  1458. optimize_op_const(size,op,a);
  1459. case op of
  1460. OP_NONE:
  1461. begin
  1462. a_load_reg_reg(list,size,size,src,dst);
  1463. exit;
  1464. end;
  1465. OP_MOVE:
  1466. begin
  1467. a_load_const_reg(list,size,a,dst);
  1468. exit;
  1469. end;
  1470. end;
  1471. if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1472. not(cs_check_overflow in current_settings.localswitches) and
  1473. (a>1) and ispowerof2(int64(a-1),power) and (power in [1..3]) then
  1474. begin
  1475. reference_reset_base(href,src,0,0);
  1476. href.index:=src;
  1477. href.scalefactor:=a-1;
  1478. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1479. end
  1480. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1481. not(cs_check_overflow in current_settings.localswitches) and
  1482. (a>1) and ispowerof2(int64(a),power) and (power in [1..3]) then
  1483. begin
  1484. reference_reset_base(href,NR_NO,0,0);
  1485. href.index:=src;
  1486. href.scalefactor:=a;
  1487. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1488. end
  1489. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1490. (a>1) and (a<=maxLongint) and not ispowerof2(int64(a),power) then
  1491. begin
  1492. { MUL with overflow checking should be handled specifically in the code generator }
  1493. if (op=OP_MUL) and (cs_check_overflow in current_settings.localswitches) then
  1494. internalerror(2014011801);
  1495. list.concat(taicpu.op_const_reg_reg(A_IMUL,TCgSize2OpSize[size],a,src,dst));
  1496. end
  1497. else if (op=OP_ADD) and
  1498. ((size in [OS_32,OS_S32]) or
  1499. { lea supports only 32 bit signed displacments }
  1500. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1501. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1502. ) and
  1503. not(cs_check_overflow in current_settings.localswitches) then
  1504. begin
  1505. { a might still be in the range 0x80000000 to 0xffffffff
  1506. which might trigger a range check error as
  1507. reference_reset_base expects a longint value. }
  1508. {$push} {$R-}{$Q-}
  1509. al := longint (a);
  1510. {$pop}
  1511. reference_reset_base(href,src,al,0);
  1512. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1513. end
  1514. else if (op=OP_SUB) and
  1515. ((size in [OS_32,OS_S32]) or
  1516. { lea supports only 32 bit signed displacments }
  1517. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1518. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1519. ) and
  1520. not(cs_check_overflow in current_settings.localswitches) then
  1521. begin
  1522. reference_reset_base(href,src,-a,0);
  1523. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1524. end
  1525. else if (op in [OP_ROR,OP_ROL]) and
  1526. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1527. (size in [OS_32,OS_S32
  1528. {$ifdef x86_64}
  1529. ,OS_64,OS_S64
  1530. {$endif x86_64}
  1531. ]) then
  1532. begin
  1533. if op=OP_ROR then
  1534. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size], a,src,dst))
  1535. else
  1536. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size],TCgSize2Size[size]*8-a,src,dst));
  1537. end
  1538. else
  1539. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1540. end;
  1541. procedure tcgx86.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1542. size: tcgsize; src1, src2, dst: tregister);
  1543. var
  1544. href : treference;
  1545. begin
  1546. if (op=OP_ADD) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1547. not(cs_check_overflow in current_settings.localswitches) then
  1548. begin
  1549. reference_reset_base(href,src1,0,0);
  1550. href.index:=src2;
  1551. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1552. end
  1553. else if (op in [OP_SHR,OP_SHL]) and
  1554. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1555. (size in [OS_32,OS_S32
  1556. {$ifdef x86_64}
  1557. ,OS_64,OS_S64
  1558. {$endif x86_64}
  1559. ]) then
  1560. begin
  1561. if op=OP_SHL then
  1562. list.concat(taicpu.op_reg_reg_reg(A_SHLX,TCgSize2OpSize[size],src1,src2,dst))
  1563. else
  1564. list.concat(taicpu.op_reg_reg_reg(A_SHRX,TCgSize2OpSize[size],src1,src2,dst));
  1565. end
  1566. else
  1567. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1568. end;
  1569. {$endif not i8086}
  1570. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  1571. {$ifdef x86_64}
  1572. var
  1573. tmpreg : tregister;
  1574. {$endif x86_64}
  1575. begin
  1576. optimize_op_const(size, op, a);
  1577. {$ifdef x86_64}
  1578. { x86_64 only supports signed 32 bits constants directly }
  1579. if not(op in [OP_NONE,OP_MOVE]) and
  1580. (size in [OS_S64,OS_64]) and
  1581. ((a<low(longint)) or (a>high(longint))) then
  1582. begin
  1583. tmpreg:=getintregister(list,size);
  1584. a_load_const_reg(list,size,a,tmpreg);
  1585. a_op_reg_reg(list,op,size,tmpreg,reg);
  1586. exit;
  1587. end;
  1588. {$endif x86_64}
  1589. check_register_size(size,reg);
  1590. case op of
  1591. OP_NONE :
  1592. begin
  1593. { Opcode is optimized away }
  1594. end;
  1595. OP_MOVE :
  1596. begin
  1597. { Optimized, replaced with a simple load }
  1598. a_load_const_reg(list,size,a,reg);
  1599. end;
  1600. OP_DIV, OP_IDIV:
  1601. begin
  1602. { should be handled specifically in the code }
  1603. { generator because of the silly register usage restraints }
  1604. internalerror(200109224);
  1605. end;
  1606. OP_MUL,OP_IMUL:
  1607. begin
  1608. if not (cs_check_overflow in current_settings.localswitches) then
  1609. op:=OP_IMUL;
  1610. if op = OP_IMUL then
  1611. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  1612. else
  1613. { OP_MUL should be handled specifically in the code }
  1614. { generator because of the silly register usage restraints }
  1615. internalerror(200109225);
  1616. end;
  1617. OP_ADD, OP_SUB:
  1618. if not(cs_check_overflow in current_settings.localswitches) and
  1619. (a = 1) and
  1620. UseIncDec then
  1621. begin
  1622. if op = OP_ADD then
  1623. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  1624. else
  1625. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  1626. end
  1627. else
  1628. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],aint(a),reg));
  1629. OP_AND,OP_OR:
  1630. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],aint(a),reg));
  1631. OP_XOR:
  1632. if (aword(a)=high(aword)) then
  1633. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg))
  1634. else
  1635. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],aint(a),reg));
  1636. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1637. begin
  1638. {$if defined(x86_64)}
  1639. if (a and 63) <> 0 Then
  1640. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,reg));
  1641. if (a shr 6) <> 0 Then
  1642. internalerror(200609073);
  1643. {$elseif defined(i386)}
  1644. if (a and 31) <> 0 Then
  1645. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1646. if (a shr 5) <> 0 Then
  1647. internalerror(200609071);
  1648. {$elseif defined(i8086)}
  1649. if (a shr 5) <> 0 Then
  1650. internalerror(2013043002);
  1651. a := a and 31;
  1652. if a <> 0 Then
  1653. begin
  1654. if (current_settings.cputype < cpu_186) and (a <> 1) then
  1655. begin
  1656. getcpuregister(list,NR_CL);
  1657. a_load_const_reg(list,OS_8,a,NR_CL);
  1658. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,reg));
  1659. ungetcpuregister(list,NR_CL);
  1660. end
  1661. else
  1662. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  1663. end;
  1664. {$endif}
  1665. end
  1666. else internalerror(200609072);
  1667. end;
  1668. end;
  1669. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1670. var
  1671. {$ifdef x86_64}
  1672. tmpreg : tregister;
  1673. {$endif x86_64}
  1674. tmpref : treference;
  1675. begin
  1676. optimize_op_const(size, op, a);
  1677. if op in [OP_NONE,OP_MOVE] then
  1678. begin
  1679. if (op=OP_MOVE) then
  1680. a_load_const_ref(list,size,a,ref);
  1681. exit;
  1682. end;
  1683. {$ifdef x86_64}
  1684. { x86_64 only supports signed 32 bits constants directly }
  1685. if (size in [OS_S64,OS_64]) and
  1686. ((a<low(longint)) or (a>high(longint))) then
  1687. begin
  1688. tmpreg:=getintregister(list,size);
  1689. a_load_const_reg(list,size,a,tmpreg);
  1690. a_op_reg_ref(list,op,size,tmpreg,ref);
  1691. exit;
  1692. end;
  1693. {$endif x86_64}
  1694. tmpref:=ref;
  1695. make_simple_ref(list,tmpref);
  1696. Case Op of
  1697. OP_DIV, OP_IDIV:
  1698. Begin
  1699. { should be handled specifically in the code }
  1700. { generator because of the silly register usage restraints }
  1701. internalerror(200109231);
  1702. End;
  1703. OP_MUL,OP_IMUL:
  1704. begin
  1705. if not (cs_check_overflow in current_settings.localswitches) then
  1706. op:=OP_IMUL;
  1707. { can't multiply a memory location directly with a constant }
  1708. if op = OP_IMUL then
  1709. inherited a_op_const_ref(list,op,size,a,tmpref)
  1710. else
  1711. { OP_MUL should be handled specifically in the code }
  1712. { generator because of the silly register usage restraints }
  1713. internalerror(200109232);
  1714. end;
  1715. OP_ADD, OP_SUB:
  1716. if not(cs_check_overflow in current_settings.localswitches) and
  1717. (a = 1) and
  1718. UseIncDec then
  1719. begin
  1720. if op = OP_ADD then
  1721. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  1722. else
  1723. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  1724. end
  1725. else
  1726. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1727. OP_AND,OP_OR:
  1728. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1729. OP_XOR:
  1730. if (aword(a)=high(aword)) then
  1731. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref))
  1732. else
  1733. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1734. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1735. begin
  1736. {$if defined(x86_64)}
  1737. if (a and 63) <> 0 Then
  1738. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,tmpref));
  1739. if (a shr 6) <> 0 Then
  1740. internalerror(2013111003);
  1741. {$elseif defined(i386)}
  1742. if (a and 31) <> 0 Then
  1743. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  1744. if (a shr 5) <> 0 Then
  1745. internalerror(2013111002);
  1746. {$elseif defined(i8086)}
  1747. if (a shr 5) <> 0 Then
  1748. internalerror(2013111001);
  1749. a := a and 31;
  1750. if a <> 0 Then
  1751. begin
  1752. if (current_settings.cputype < cpu_186) and (a <> 1) then
  1753. begin
  1754. getcpuregister(list,NR_CL);
  1755. a_load_const_reg(list,OS_8,a,NR_CL);
  1756. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,tmpref));
  1757. ungetcpuregister(list,NR_CL);
  1758. end
  1759. else
  1760. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1761. end;
  1762. {$endif}
  1763. end
  1764. else internalerror(68992);
  1765. end;
  1766. end;
  1767. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1768. const
  1769. {$if defined(cpu64bitalu)}
  1770. REGCX=NR_RCX;
  1771. REGCX_Size = OS_64;
  1772. {$elseif defined(cpu32bitalu)}
  1773. REGCX=NR_ECX;
  1774. REGCX_Size = OS_32;
  1775. {$elseif defined(cpu16bitalu)}
  1776. REGCX=NR_CX;
  1777. REGCX_Size = OS_16;
  1778. {$endif}
  1779. var
  1780. dstsize: topsize;
  1781. instr:Taicpu;
  1782. begin
  1783. check_register_size(size,src);
  1784. check_register_size(size,dst);
  1785. dstsize := tcgsize2opsize[size];
  1786. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  1787. op:=OP_IMUL;
  1788. case op of
  1789. OP_NEG,OP_NOT:
  1790. begin
  1791. if src<>dst then
  1792. a_load_reg_reg(list,size,size,src,dst);
  1793. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  1794. end;
  1795. OP_MUL,OP_DIV,OP_IDIV:
  1796. { special stuff, needs separate handling inside code }
  1797. { generator }
  1798. internalerror(200109233);
  1799. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  1800. begin
  1801. { Use ecx to load the value, that allows better coalescing }
  1802. getcpuregister(list,REGCX);
  1803. a_load_reg_reg(list,size,REGCX_Size,src,REGCX);
  1804. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  1805. ungetcpuregister(list,REGCX);
  1806. end;
  1807. else
  1808. begin
  1809. if reg2opsize(src) <> dstsize then
  1810. internalerror(200109226);
  1811. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  1812. list.concat(instr);
  1813. end;
  1814. end;
  1815. end;
  1816. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1817. var
  1818. tmpref : treference;
  1819. begin
  1820. tmpref:=ref;
  1821. make_simple_ref(list,tmpref);
  1822. check_register_size(size,reg);
  1823. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  1824. op:=OP_IMUL;
  1825. case op of
  1826. OP_NEG,OP_NOT,OP_IMUL:
  1827. begin
  1828. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1829. end;
  1830. OP_MUL,OP_DIV,OP_IDIV:
  1831. { special stuff, needs separate handling inside code }
  1832. { generator }
  1833. internalerror(200109239);
  1834. else
  1835. begin
  1836. reg := makeregsize(list,reg,size);
  1837. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  1838. end;
  1839. end;
  1840. end;
  1841. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1842. var
  1843. tmpref : treference;
  1844. begin
  1845. tmpref:=ref;
  1846. make_simple_ref(list,tmpref);
  1847. check_register_size(size,reg);
  1848. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  1849. op:=OP_IMUL;
  1850. case op of
  1851. OP_NEG,OP_NOT:
  1852. begin
  1853. if reg<>NR_NO then
  1854. internalerror(200109237);
  1855. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  1856. end;
  1857. OP_IMUL:
  1858. begin
  1859. { this one needs a load/imul/store, which is the default }
  1860. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1861. end;
  1862. OP_MUL,OP_DIV,OP_IDIV:
  1863. { special stuff, needs separate handling inside code }
  1864. { generator }
  1865. internalerror(200109238);
  1866. else
  1867. begin
  1868. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  1869. end;
  1870. end;
  1871. end;
  1872. procedure tcgx86.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister);
  1873. var
  1874. tmpreg: tregister;
  1875. opsize: topsize;
  1876. l : TAsmLabel;
  1877. begin
  1878. { no bsf/bsr for byte }
  1879. if srcsize in [OS_8,OS_S8] then
  1880. begin
  1881. tmpreg:=getintregister(list,OS_INT);
  1882. a_load_reg_reg(list,srcsize,OS_INT,src,tmpreg);
  1883. src:=tmpreg;
  1884. srcsize:=OS_INT;
  1885. end;
  1886. { source and destination register must have the same size }
  1887. if tcgsize2size[srcsize]<>tcgsize2size[dstsize] then
  1888. tmpreg:=getintregister(list,srcsize)
  1889. else
  1890. tmpreg:=dst;
  1891. opsize:=tcgsize2opsize[srcsize];
  1892. if not reverse then
  1893. list.concat(taicpu.op_reg_reg(A_BSF,opsize,src,tmpreg))
  1894. else
  1895. list.concat(taicpu.op_reg_reg(A_BSR,opsize,src,tmpreg));
  1896. current_asmdata.getjumplabel(l);
  1897. a_jmp_cond(list,OC_NE,l);
  1898. list.concat(taicpu.op_const_reg(A_MOV,opsize,$ff,tmpreg));
  1899. a_label(list,l);
  1900. if tmpreg<>dst then
  1901. a_load_reg_reg(list,srcsize,dstsize,tmpreg,dst);
  1902. end;
  1903. {*************** compare instructructions ****************}
  1904. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1905. l : tasmlabel);
  1906. {$ifdef x86_64}
  1907. var
  1908. tmpreg : tregister;
  1909. {$endif x86_64}
  1910. begin
  1911. {$ifdef x86_64}
  1912. { x86_64 only supports signed 32 bits constants directly }
  1913. if (size in [OS_S64,OS_64]) and
  1914. ((a<low(longint)) or (a>high(longint))) then
  1915. begin
  1916. tmpreg:=getintregister(list,size);
  1917. a_load_const_reg(list,size,a,tmpreg);
  1918. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1919. exit;
  1920. end;
  1921. {$endif x86_64}
  1922. if (a = 0) then
  1923. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  1924. else
  1925. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  1926. a_jmp_cond(list,cmp_op,l);
  1927. end;
  1928. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  1929. l : tasmlabel);
  1930. var
  1931. {$ifdef x86_64}
  1932. tmpreg : tregister;
  1933. {$endif x86_64}
  1934. tmpref : treference;
  1935. begin
  1936. tmpref:=ref;
  1937. make_simple_ref(list,tmpref);
  1938. {$ifdef x86_64}
  1939. { x86_64 only supports signed 32 bits constants directly }
  1940. if (size in [OS_S64,OS_64]) and
  1941. ((a<low(longint)) or (a>high(longint))) then
  1942. begin
  1943. tmpreg:=getintregister(list,size);
  1944. a_load_const_reg(list,size,a,tmpreg);
  1945. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  1946. exit;
  1947. end;
  1948. {$endif x86_64}
  1949. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  1950. a_jmp_cond(list,cmp_op,l);
  1951. end;
  1952. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  1953. reg1,reg2 : tregister;l : tasmlabel);
  1954. begin
  1955. check_register_size(size,reg1);
  1956. check_register_size(size,reg2);
  1957. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  1958. a_jmp_cond(list,cmp_op,l);
  1959. end;
  1960. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  1961. var
  1962. tmpref : treference;
  1963. begin
  1964. tmpref:=ref;
  1965. make_simple_ref(list,tmpref);
  1966. check_register_size(size,reg);
  1967. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  1968. a_jmp_cond(list,cmp_op,l);
  1969. end;
  1970. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  1971. var
  1972. tmpref : treference;
  1973. begin
  1974. tmpref:=ref;
  1975. make_simple_ref(list,tmpref);
  1976. check_register_size(size,reg);
  1977. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  1978. a_jmp_cond(list,cmp_op,l);
  1979. end;
  1980. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1981. var
  1982. ai : taicpu;
  1983. begin
  1984. if cond=OC_None then
  1985. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1986. else
  1987. begin
  1988. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  1989. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1990. end;
  1991. ai.is_jmp:=true;
  1992. list.concat(ai);
  1993. end;
  1994. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1995. var
  1996. ai : taicpu;
  1997. hl : tasmlabel;
  1998. f2 : tresflags;
  1999. begin
  2000. hl:=nil;
  2001. f2:=f;
  2002. case f of
  2003. F_FNE:
  2004. begin
  2005. ai:=Taicpu.op_sym(A_Jcc,S_NO,l);
  2006. ai.SetCondition(C_P);
  2007. ai.is_jmp:=true;
  2008. list.concat(ai);
  2009. f2:=F_NE;
  2010. end;
  2011. F_FE,F_FA,F_FAE,F_FB,F_FBE:
  2012. begin
  2013. { JP before JA/JAE is redundant, but it must be generated here
  2014. and left for peephole optimizer to remove. }
  2015. current_asmdata.getjumplabel(hl);
  2016. ai:=Taicpu.op_sym(A_Jcc,S_NO,hl);
  2017. ai.SetCondition(C_P);
  2018. ai.is_jmp:=true;
  2019. list.concat(ai);
  2020. f2:=FPUFlags2Flags[f];
  2021. end;
  2022. end;
  2023. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  2024. ai.SetCondition(flags_to_cond(f2));
  2025. ai.is_jmp := true;
  2026. list.concat(ai);
  2027. if assigned(hl) then
  2028. a_label(list,hl);
  2029. end;
  2030. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  2031. var
  2032. ai : taicpu;
  2033. f2 : tresflags;
  2034. hreg,hreg2 : tregister;
  2035. op: tasmop;
  2036. begin
  2037. hreg2:=NR_NO;
  2038. op:=A_AND;
  2039. f2:=f;
  2040. case f of
  2041. F_FE,F_FNE,F_FB,F_FBE:
  2042. begin
  2043. hreg2:=getintregister(list,OS_8);
  2044. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg2);
  2045. if (f=F_FNE) then { F_FNE means "PF or (not ZF)" }
  2046. begin
  2047. ai.setcondition(C_P);
  2048. op:=A_OR;
  2049. end
  2050. else
  2051. ai.setcondition(C_NP);
  2052. list.concat(ai);
  2053. f2:=FPUFlags2Flags[f];
  2054. end;
  2055. F_FA,F_FAE: { These do not need PF check }
  2056. f2:=FPUFlags2Flags[f];
  2057. end;
  2058. hreg:=makeregsize(list,reg,OS_8);
  2059. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  2060. ai.setcondition(flags_to_cond(f2));
  2061. list.concat(ai);
  2062. if (hreg2<>NR_NO) then
  2063. list.concat(taicpu.op_reg_reg(op,S_B,hreg2,hreg));
  2064. if reg<>hreg then
  2065. a_load_reg_reg(list,OS_8,size,hreg,reg);
  2066. end;
  2067. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  2068. var
  2069. ai : taicpu;
  2070. tmpref : treference;
  2071. f2 : tresflags;
  2072. begin
  2073. f2:=f;
  2074. case f of
  2075. F_FE,F_FNE,F_FB,F_FBE:
  2076. begin
  2077. inherited g_flags2ref(list,size,f,ref);
  2078. exit;
  2079. end;
  2080. F_FA,F_FAE:
  2081. f2:=FPUFlags2Flags[f];
  2082. end;
  2083. tmpref:=ref;
  2084. make_simple_ref(list,tmpref);
  2085. if not(size in [OS_8,OS_S8]) then
  2086. a_load_const_ref(list,size,0,tmpref);
  2087. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  2088. ai.setcondition(flags_to_cond(f2));
  2089. list.concat(ai);
  2090. {$ifndef cpu64bitalu}
  2091. if size in [OS_S64,OS_64] then
  2092. begin
  2093. inc(tmpref.offset,4);
  2094. a_load_const_ref(list,OS_32,0,tmpref);
  2095. end;
  2096. {$endif cpu64bitalu}
  2097. end;
  2098. { ************* concatcopy ************ }
  2099. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:tcgint);
  2100. const
  2101. {$if defined(cpu64bitalu)}
  2102. REGCX=NR_RCX;
  2103. REGSI=NR_RSI;
  2104. REGDI=NR_RDI;
  2105. copy_len_sizes = [1, 2, 4, 8];
  2106. push_segment_size = S_L;
  2107. {$elseif defined(cpu32bitalu)}
  2108. REGCX=NR_ECX;
  2109. REGSI=NR_ESI;
  2110. REGDI=NR_EDI;
  2111. copy_len_sizes = [1, 2, 4];
  2112. push_segment_size = S_L;
  2113. {$elseif defined(cpu16bitalu)}
  2114. REGCX=NR_CX;
  2115. REGSI=NR_SI;
  2116. REGDI=NR_DI;
  2117. copy_len_sizes = [1, 2, 4]; { 4 is included here, because it's still more
  2118. efficient to use copy_move instead of copy_string for copying 4 bytes }
  2119. push_segment_size = S_W;
  2120. {$endif}
  2121. type copymode=(copy_move,copy_mmx,copy_string,copy_mm,copy_avx);
  2122. var srcref,dstref:Treference;
  2123. r,r0,r1,r2,r3:Tregister;
  2124. helpsize:tcgint;
  2125. copysize:byte;
  2126. cgsize:Tcgsize;
  2127. cm:copymode;
  2128. saved_ds,saved_es: Boolean;
  2129. begin
  2130. srcref:=source;
  2131. dstref:=dest;
  2132. {$ifndef i8086}
  2133. make_simple_ref(list,srcref);
  2134. make_simple_ref(list,dstref);
  2135. {$endif not i8086}
  2136. cm:=copy_move;
  2137. helpsize:=3*sizeof(aword);
  2138. if cs_opt_size in current_settings.optimizerswitches then
  2139. helpsize:=2*sizeof(aword);
  2140. {$ifndef i8086}
  2141. { avx helps only to reduce size, using it in general does at least not help on
  2142. an i7-4770 (FK) }
  2143. if (CPUX86_HAS_AVXUNIT in cpu_capabilities[current_settings.cputype]) and
  2144. // (cs_opt_size in current_settings.optimizerswitches) and
  2145. ((len=8) or (len=16) or (len=24) or (len=32) { or (len=40) or (len=48)}) then
  2146. cm:=copy_avx
  2147. else
  2148. {$ifdef dummy}
  2149. { I'am not sure what CPUs would benefit from using sse instructions for moves (FK) }
  2150. if
  2151. {$ifdef x86_64}
  2152. ((current_settings.fputype>=fpu_sse64)
  2153. {$else x86_64}
  2154. ((current_settings.fputype>=fpu_sse)
  2155. {$endif x86_64}
  2156. or (CPUX86_HAS_SSEUNIT in cpu_capabilities[current_settings.cputype])) and
  2157. ((len=8) or (len=16) or (len=24) or (len=32) or (len=40) or (len=48)) then
  2158. cm:=copy_mm
  2159. else
  2160. {$endif dummy}
  2161. {$endif i8086}
  2162. if (cs_mmx in current_settings.localswitches) and
  2163. not(pi_uses_fpu in current_procinfo.flags) and
  2164. ((len=8) or (len=16) or (len=24) or (len=32)) then
  2165. cm:=copy_mmx;
  2166. if (len>helpsize) then
  2167. cm:=copy_string;
  2168. if (cs_opt_size in current_settings.optimizerswitches) and
  2169. not((len<=16) and (cm in [copy_mmx,copy_mm,copy_avx])) and
  2170. not(len in copy_len_sizes) then
  2171. cm:=copy_string;
  2172. {$ifndef i8086}
  2173. if (srcref.segment<>NR_NO) or
  2174. (dstref.segment<>NR_NO) then
  2175. cm:=copy_string;
  2176. {$endif not i8086}
  2177. case cm of
  2178. copy_move:
  2179. begin
  2180. copysize:=sizeof(aint);
  2181. cgsize:=int_cgsize(copysize);
  2182. while len<>0 do
  2183. begin
  2184. if len<2 then
  2185. begin
  2186. copysize:=1;
  2187. cgsize:=OS_8;
  2188. end
  2189. else if len<4 then
  2190. begin
  2191. copysize:=2;
  2192. cgsize:=OS_16;
  2193. end
  2194. {$if defined(cpu32bitalu) or defined(cpu64bitalu)}
  2195. else if len<8 then
  2196. begin
  2197. copysize:=4;
  2198. cgsize:=OS_32;
  2199. end
  2200. {$endif cpu32bitalu or cpu64bitalu}
  2201. {$ifdef cpu64bitalu}
  2202. else if len<16 then
  2203. begin
  2204. copysize:=8;
  2205. cgsize:=OS_64;
  2206. end
  2207. {$endif}
  2208. ;
  2209. dec(len,copysize);
  2210. r:=getintregister(list,cgsize);
  2211. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2212. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2213. inc(srcref.offset,copysize);
  2214. inc(dstref.offset,copysize);
  2215. end;
  2216. end;
  2217. copy_mmx:
  2218. begin
  2219. r0:=getmmxregister(list);
  2220. r1:=NR_NO;
  2221. r2:=NR_NO;
  2222. r3:=NR_NO;
  2223. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  2224. if len>=16 then
  2225. begin
  2226. inc(srcref.offset,8);
  2227. r1:=getmmxregister(list);
  2228. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  2229. end;
  2230. if len>=24 then
  2231. begin
  2232. inc(srcref.offset,8);
  2233. r2:=getmmxregister(list);
  2234. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  2235. end;
  2236. if len>=32 then
  2237. begin
  2238. inc(srcref.offset,8);
  2239. r3:=getmmxregister(list);
  2240. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2241. end;
  2242. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  2243. if len>=16 then
  2244. begin
  2245. inc(dstref.offset,8);
  2246. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  2247. end;
  2248. if len>=24 then
  2249. begin
  2250. inc(dstref.offset,8);
  2251. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  2252. end;
  2253. if len>=32 then
  2254. begin
  2255. inc(dstref.offset,8);
  2256. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2257. end;
  2258. end;
  2259. copy_mm:
  2260. begin
  2261. r0:=NR_NO;
  2262. r1:=NR_NO;
  2263. r2:=NR_NO;
  2264. r3:=NR_NO;
  2265. if len>=16 then
  2266. begin
  2267. r0:=getmmregister(list,OS_M128);
  2268. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r0,nil);
  2269. inc(srcref.offset,16);
  2270. end;
  2271. if len>=32 then
  2272. begin
  2273. r1:=getmmregister(list,OS_M128);
  2274. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r1,nil);
  2275. inc(srcref.offset,16);
  2276. end;
  2277. if len>=48 then
  2278. begin
  2279. r2:=getmmregister(list,OS_M128);
  2280. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r2,nil);
  2281. inc(srcref.offset,16);
  2282. end;
  2283. if (len=8) or (len=24) or (len=40) then
  2284. begin
  2285. r3:=getmmregister(list,OS_M64);
  2286. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2287. end;
  2288. if len>=16 then
  2289. begin
  2290. a_loadmm_reg_ref(list,OS_M128,OS_M128,r0,dstref,nil);
  2291. inc(dstref.offset,16);
  2292. end;
  2293. if len>=32 then
  2294. begin
  2295. a_loadmm_reg_ref(list,OS_M128,OS_M128,r1,dstref,nil);
  2296. inc(dstref.offset,16);
  2297. end;
  2298. if len>=48 then
  2299. begin
  2300. a_loadmm_reg_ref(list,OS_M128,OS_M128,r2,dstref,nil);
  2301. inc(dstref.offset,16);
  2302. end;
  2303. if (len=8) or (len=24) or (len=40) then
  2304. begin
  2305. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2306. end;
  2307. end;
  2308. copy_avx:
  2309. begin
  2310. r0:=NR_NO;
  2311. r1:=NR_NO;
  2312. r2:=NR_NO;
  2313. r3:=NR_NO;
  2314. if len>=16 then
  2315. begin
  2316. r0:=getmmregister(list,OS_M128);
  2317. { we want to force the use of vmovups, so do not use a_loadmm_ref_reg }
  2318. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r0));
  2319. inc(srcref.offset,16);
  2320. end;
  2321. if len>=32 then
  2322. begin
  2323. r1:=getmmregister(list,OS_M128);
  2324. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r1));
  2325. inc(srcref.offset,16);
  2326. end;
  2327. if len>=48 then
  2328. begin
  2329. r2:=getmmregister(list,OS_M128);
  2330. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r2));
  2331. inc(srcref.offset,16);
  2332. end;
  2333. if (len=8) or (len=24) or (len=40) then
  2334. begin
  2335. r3:=getmmregister(list,OS_M64);
  2336. list.concat(taicpu.op_ref_reg(A_VMOVSD,S_NO,srcref,r3));
  2337. end;
  2338. if len>=16 then
  2339. begin
  2340. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r0,dstref));
  2341. inc(dstref.offset,16);
  2342. end;
  2343. if len>=32 then
  2344. begin
  2345. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r1,dstref));
  2346. inc(dstref.offset,16);
  2347. end;
  2348. if len>=48 then
  2349. begin
  2350. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r2,dstref));
  2351. inc(dstref.offset,16);
  2352. end;
  2353. if (len=8) or (len=24) or (len=40) then
  2354. begin
  2355. list.concat(taicpu.op_reg_ref(A_VMOVSD,S_NO,r3,dstref));
  2356. end;
  2357. end
  2358. else {copy_string, should be a good fallback in case of unhandled}
  2359. begin
  2360. getcpuregister(list,REGDI);
  2361. if (dest.segment=NR_NO) and
  2362. (segment_regs_equal(NR_SS,NR_DS) or ((dest.base<>NR_BP) and (dest.base<>NR_SP))) then
  2363. begin
  2364. a_loadaddr_ref_reg(list,dstref,REGDI);
  2365. saved_es:=false;
  2366. {$ifdef volatile_es}
  2367. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2368. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2369. {$endif volatile_es}
  2370. end
  2371. else
  2372. begin
  2373. dstref.segment:=NR_NO;
  2374. a_loadaddr_ref_reg(list,dstref,REGDI);
  2375. {$ifdef volatile_es}
  2376. saved_es:=false;
  2377. {$else volatile_es}
  2378. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_ES));
  2379. saved_es:=true;
  2380. {$endif volatile_es}
  2381. if dest.segment<>NR_NO then
  2382. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,dest.segment))
  2383. else if (dest.base=NR_BP) or (dest.base=NR_SP) then
  2384. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2385. else
  2386. internalerror(2014040401);
  2387. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2388. end;
  2389. getcpuregister(list,REGSI);
  2390. if ((source.segment=NR_NO) and (segment_regs_equal(NR_SS,NR_DS) or ((source.base<>NR_BP) and (source.base<>NR_SP)))) or
  2391. (is_segment_reg(source.segment) and segment_regs_equal(source.segment,NR_DS)) then
  2392. begin
  2393. srcref.segment:=NR_NO;
  2394. a_loadaddr_ref_reg(list,srcref,REGSI);
  2395. saved_ds:=false;
  2396. end
  2397. else
  2398. begin
  2399. srcref.segment:=NR_NO;
  2400. a_loadaddr_ref_reg(list,srcref,REGSI);
  2401. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2402. saved_ds:=true;
  2403. if source.segment<>NR_NO then
  2404. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,source.segment))
  2405. else if (source.base=NR_BP) or (source.base=NR_SP) then
  2406. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2407. else
  2408. internalerror(2014040402);
  2409. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2410. end;
  2411. getcpuregister(list,REGCX);
  2412. if ts_cld in current_settings.targetswitches then
  2413. list.concat(Taicpu.op_none(A_CLD,S_NO));
  2414. if (cs_opt_size in current_settings.optimizerswitches) and
  2415. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  2416. begin
  2417. a_load_const_reg(list,OS_INT,len,REGCX);
  2418. list.concat(Taicpu.op_none(A_REP,S_NO));
  2419. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2420. end
  2421. else
  2422. begin
  2423. helpsize:=len div sizeof(aint);
  2424. len:=len mod sizeof(aint);
  2425. if helpsize>1 then
  2426. begin
  2427. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  2428. list.concat(Taicpu.op_none(A_REP,S_NO));
  2429. end;
  2430. if helpsize>0 then
  2431. begin
  2432. {$if defined(cpu64bitalu)}
  2433. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  2434. {$elseif defined(cpu32bitalu)}
  2435. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2436. {$elseif defined(cpu16bitalu)}
  2437. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2438. {$endif}
  2439. end;
  2440. if len>=4 then
  2441. begin
  2442. dec(len,4);
  2443. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2444. end;
  2445. if len>=2 then
  2446. begin
  2447. dec(len,2);
  2448. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2449. end;
  2450. if len=1 then
  2451. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2452. end;
  2453. ungetcpuregister(list,REGCX);
  2454. ungetcpuregister(list,REGSI);
  2455. ungetcpuregister(list,REGDI);
  2456. if saved_ds then
  2457. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2458. if saved_es then
  2459. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2460. end;
  2461. end;
  2462. end;
  2463. {****************************************************************************
  2464. Entry/Exit Code Helpers
  2465. ****************************************************************************}
  2466. procedure tcgx86.g_profilecode(list : TAsmList);
  2467. var
  2468. pl : tasmlabel;
  2469. mcountprefix : String[4];
  2470. begin
  2471. case target_info.system of
  2472. {$ifndef NOTARGETWIN}
  2473. system_i386_win32,
  2474. {$endif}
  2475. system_i386_freebsd,
  2476. system_i386_netbsd,
  2477. // system_i386_openbsd,
  2478. system_i386_wdosx :
  2479. begin
  2480. Case target_info.system Of
  2481. system_i386_freebsd : mcountprefix:='.';
  2482. system_i386_netbsd : mcountprefix:='__';
  2483. // system_i386_openbsd : mcountprefix:='.';
  2484. else
  2485. mcountPrefix:='';
  2486. end;
  2487. current_asmdata.getaddrlabel(pl);
  2488. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(pint));
  2489. list.concat(Tai_label.Create(pl));
  2490. list.concat(Tai_const.Create_32bit(0));
  2491. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  2492. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2493. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  2494. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount',false);
  2495. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  2496. end;
  2497. system_i386_linux:
  2498. a_call_name(list,target_info.Cprefix+'mcount',false);
  2499. system_i386_go32v2,system_i386_watcom:
  2500. begin
  2501. a_call_name(list,'MCOUNT',false);
  2502. end;
  2503. system_x86_64_linux,
  2504. system_x86_64_darwin,
  2505. system_x86_64_iphonesim:
  2506. begin
  2507. a_call_name(list,'mcount',false);
  2508. end;
  2509. end;
  2510. end;
  2511. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2512. procedure decrease_sp(a : tcgint);
  2513. var
  2514. href : treference;
  2515. begin
  2516. reference_reset_base(href,NR_STACK_POINTER_REG,-a,0);
  2517. { normally, lea is a better choice than a sub to adjust the stack pointer }
  2518. list.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_STACK_POINTER_REG));
  2519. end;
  2520. {$ifdef x86}
  2521. {$ifndef NOTARGETWIN}
  2522. var
  2523. href : treference;
  2524. i : integer;
  2525. again : tasmlabel;
  2526. {$endif NOTARGETWIN}
  2527. {$endif x86}
  2528. begin
  2529. if localsize>0 then
  2530. begin
  2531. {$ifdef i386}
  2532. {$ifndef NOTARGETWIN}
  2533. { windows guards only a few pages for stack growing,
  2534. so we have to access every page first }
  2535. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  2536. (localsize>=winstackpagesize) then
  2537. begin
  2538. if localsize div winstackpagesize<=5 then
  2539. begin
  2540. decrease_sp(localsize-4);
  2541. for i:=1 to localsize div winstackpagesize do
  2542. begin
  2543. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize,4);
  2544. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2545. end;
  2546. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2547. end
  2548. else
  2549. begin
  2550. current_asmdata.getjumplabel(again);
  2551. { Using a_reg_alloc instead of getcpuregister, so this procedure
  2552. does not change "used_in_proc" state of EDI and therefore can be
  2553. called after saving registers with "push" instruction
  2554. without creating an unbalanced "pop edi" in epilogue }
  2555. a_reg_alloc(list,NR_EDI);
  2556. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  2557. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  2558. a_label(list,again);
  2559. decrease_sp(winstackpagesize-4);
  2560. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2561. if UseIncDec then
  2562. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI))
  2563. else
  2564. list.concat(Taicpu.op_const_reg(A_SUB,S_L,1,NR_EDI));
  2565. a_jmp_cond(list,OC_NE,again);
  2566. decrease_sp(localsize mod winstackpagesize-4);
  2567. reference_reset_base(href,NR_ESP,localsize-4,4);
  2568. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  2569. a_reg_dealloc(list,NR_EDI);
  2570. end
  2571. end
  2572. else
  2573. {$endif NOTARGETWIN}
  2574. {$endif i386}
  2575. {$ifdef x86_64}
  2576. {$ifndef NOTARGETWIN}
  2577. { windows guards only a few pages for stack growing,
  2578. so we have to access every page first }
  2579. if (target_info.system=system_x86_64_win64) and
  2580. (localsize>=winstackpagesize) then
  2581. begin
  2582. if localsize div winstackpagesize<=5 then
  2583. begin
  2584. decrease_sp(localsize);
  2585. for i:=1 to localsize div winstackpagesize do
  2586. begin
  2587. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4,4);
  2588. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2589. end;
  2590. reference_reset_base(href,NR_RSP,0,4);
  2591. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2592. end
  2593. else
  2594. begin
  2595. current_asmdata.getjumplabel(again);
  2596. getcpuregister(list,NR_R10);
  2597. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  2598. a_label(list,again);
  2599. decrease_sp(winstackpagesize);
  2600. reference_reset_base(href,NR_RSP,0,4);
  2601. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2602. if UseIncDec then
  2603. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10))
  2604. else
  2605. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,1,NR_R10));
  2606. a_jmp_cond(list,OC_NE,again);
  2607. decrease_sp(localsize mod winstackpagesize);
  2608. ungetcpuregister(list,NR_R10);
  2609. end
  2610. end
  2611. else
  2612. {$endif NOTARGETWIN}
  2613. {$endif x86_64}
  2614. decrease_sp(localsize);
  2615. end;
  2616. end;
  2617. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  2618. var
  2619. stackmisalignment: longint;
  2620. regsize: longint;
  2621. {$ifdef i8086}
  2622. dgroup: treference;
  2623. fardataseg: treference;
  2624. {$endif i8086}
  2625. procedure push_regs;
  2626. var
  2627. r: longint;
  2628. usedregs: tcpuregisterset;
  2629. begin
  2630. regsize:=0;
  2631. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  2632. for r := low(saved_standard_registers) to high(saved_standard_registers) do
  2633. if saved_standard_registers[r] in usedregs then
  2634. begin
  2635. inc(regsize,sizeof(aint));
  2636. list.concat(Taicpu.Op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE)));
  2637. end;
  2638. end;
  2639. begin
  2640. {$ifdef i8086}
  2641. { Win16 callback/exported proc prologue support.
  2642. Since callbacks can be called from different modules, DS on entry may be
  2643. initialized with the data segment of a different module, so we need to
  2644. get ours. But we can't do
  2645. push ds
  2646. mov ax, dgroup
  2647. mov ds, ax
  2648. because code segments are shared between different instances of the same
  2649. module (which have different instances of the current program's data segment),
  2650. so the same 'mov ax, dgroup' instruction will be used for all instances
  2651. of the program and it will load the same segment into ax.
  2652. So, the standard win16 prologue looks like this:
  2653. mov ax, ds
  2654. nop
  2655. inc bp
  2656. push bp
  2657. mov bp, sp
  2658. push ds
  2659. mov ds, ax
  2660. By default, this does nothing, except wasting a few extra machine cycles and
  2661. destroying ax in the process. However, Windows checks the first three bytes
  2662. of every exported function and if they are 'mov ax,ds/nop', they are replaced
  2663. with nop/nop/nop. Then the MakeProcInstance api call should be used to create
  2664. a thunk that loads ds for the current program instance in ax before calling
  2665. the routine.
  2666. And now the fun part comes: somebody (Michael Geary) figured out that all this
  2667. crap was unnecessary, because in Win16 exe modules, we always have DS=SS, so we
  2668. can simply initialize DS from SS :) And then calling MakeProcInstance becomes
  2669. unnecessary. This is what "smart callbacks" (cs_win16_smartcallbacks) do. However,
  2670. this only works for exe files, not for dlls, because dlls run with DS<>SS. There's
  2671. another solution for dlls - since win16 dlls only have a single instance of their
  2672. data segment, we can initialize ds from dgroup. However, there's not a single
  2673. solution for both exe and dlls, so we don't know what to use e.g. in a unit. So,
  2674. that's why there's still an option to turn smart callbacks off and go the
  2675. MakeProcInstance way.
  2676. Additional details here: http://www.geary.com/fixds.html }
  2677. if (current_settings.x86memorymodel<>mm_huge) and
  2678. (po_exports in current_procinfo.procdef.procoptions) and
  2679. (target_info.system=system_i8086_win16) then
  2680. begin
  2681. if cs_win16_smartcallbacks in current_settings.moduleswitches then
  2682. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_SS,NR_AX))
  2683. else
  2684. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_DS,NR_AX));
  2685. list.concat(Taicpu.op_none(A_NOP));
  2686. end
  2687. { interrupt support for i8086 }
  2688. else if po_interrupt in current_procinfo.procdef.procoptions then
  2689. begin
  2690. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_AX));
  2691. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_BX));
  2692. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CX));
  2693. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DX));
  2694. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  2695. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  2696. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  2697. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  2698. if current_settings.x86memorymodel=mm_tiny then
  2699. begin
  2700. { in the tiny memory model, we can't use dgroup, because that
  2701. adds a relocation entry to the .exe and we can't produce a
  2702. .com file (because they don't support relactions), so instead
  2703. we initialize DS from CS. }
  2704. if cs_opt_size in current_settings.optimizerswitches then
  2705. begin
  2706. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CS));
  2707. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  2708. end
  2709. else
  2710. begin
  2711. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_CS,NR_AX));
  2712. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2713. end;
  2714. end
  2715. else if current_settings.x86memorymodel=mm_huge then
  2716. begin
  2717. reference_reset(fardataseg,0);
  2718. fardataseg.refaddr:=addr_fardataseg;
  2719. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  2720. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2721. end
  2722. else
  2723. begin
  2724. reference_reset(dgroup,0);
  2725. dgroup.refaddr:=addr_dgroup;
  2726. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,dgroup,NR_AX));
  2727. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2728. end;
  2729. end;
  2730. {$endif i8086}
  2731. {$ifdef i386}
  2732. { interrupt support for i386 }
  2733. if (po_interrupt in current_procinfo.procdef.procoptions) and
  2734. { this messes up stack alignment }
  2735. not(target_info.system in [system_i386_darwin,system_i386_iphonesim,system_i386_android]) then
  2736. begin
  2737. { .... also the segment registers }
  2738. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  2739. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  2740. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  2741. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  2742. { save the registers of an interrupt procedure }
  2743. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  2744. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  2745. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2746. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  2747. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  2748. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  2749. end;
  2750. {$endif i386}
  2751. { save old framepointer }
  2752. if not nostackframe then
  2753. begin
  2754. { return address }
  2755. stackmisalignment := sizeof(pint);
  2756. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  2757. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  2758. begin
  2759. {$ifdef i386}
  2760. if (not paramanager.use_fixed_stack) then
  2761. push_regs;
  2762. {$endif i386}
  2763. CGmessage(cg_d_stackframe_omited);
  2764. end
  2765. else
  2766. begin
  2767. {$ifdef i8086}
  2768. if ((ts_x86_far_procs_push_odd_bp in current_settings.targetswitches) or
  2769. ((po_exports in current_procinfo.procdef.procoptions) and
  2770. (target_info.system=system_i8086_win16))) and
  2771. is_proc_far(current_procinfo.procdef) then
  2772. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,1,current_procinfo.framepointer);
  2773. {$endif i8086}
  2774. { push <frame_pointer> }
  2775. inc(stackmisalignment,sizeof(pint));
  2776. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  2777. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  2778. { Return address and FP are both on stack }
  2779. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(pint));
  2780. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(pint)));
  2781. if current_procinfo.procdef.proctypeoption<>potype_exceptfilter then
  2782. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG))
  2783. else
  2784. begin
  2785. push_regs;
  2786. gen_load_frame_for_exceptfilter(list);
  2787. { Need only as much stack space as necessary to do the calls.
  2788. Exception filters don't have own local vars, and temps are 'mapped'
  2789. to the parent procedure.
  2790. maxpushedparasize is already aligned at least on x86_64. }
  2791. localsize:=current_procinfo.maxpushedparasize;
  2792. end;
  2793. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  2794. end;
  2795. { allocate stackframe space }
  2796. if (localsize<>0) or
  2797. ((target_info.stackalign>sizeof(pint)) and
  2798. (stackmisalignment <> 0) and
  2799. ((pi_do_call in current_procinfo.flags) or
  2800. (po_assembler in current_procinfo.procdef.procoptions))) then
  2801. begin
  2802. if target_info.stackalign>sizeof(pint) then
  2803. localsize := align(localsize+stackmisalignment,target_info.stackalign)-stackmisalignment;
  2804. g_stackpointer_alloc(list,localsize);
  2805. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  2806. current_asmdata.asmcfi.cfa_def_cfa_offset(list,localsize+sizeof(pint));
  2807. current_procinfo.final_localsize:=localsize;
  2808. end;
  2809. {$ifdef i8086}
  2810. { win16 exported proc prologue follow-up (see the huge comment above for details) }
  2811. if (current_settings.x86memorymodel<>mm_huge) and
  2812. (po_exports in current_procinfo.procdef.procoptions) and
  2813. (target_info.system=system_i8086_win16) then
  2814. begin
  2815. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  2816. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2817. end
  2818. else if (current_settings.x86memorymodel=mm_huge) and
  2819. not (po_interrupt in current_procinfo.procdef.procoptions) then
  2820. begin
  2821. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  2822. reference_reset(fardataseg,0);
  2823. fardataseg.refaddr:=addr_fardataseg;
  2824. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  2825. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2826. end;
  2827. { SI and DI are volatile in the BP7 and FPC's pascal calling convention,
  2828. but must be preserved in Microsoft C's pascal calling convention, and
  2829. since Windows is compiled with Microsoft compilers, these registers
  2830. must be saved for exported procedures (BP7 for Win16 also does this). }
  2831. if (po_exports in current_procinfo.procdef.procoptions) and
  2832. (target_info.system=system_i8086_win16) then
  2833. begin
  2834. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  2835. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  2836. end;
  2837. {$endif i8086}
  2838. {$ifdef i386}
  2839. if (not paramanager.use_fixed_stack) and
  2840. (current_procinfo.framepointer<>NR_STACK_POINTER_REG) and
  2841. (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  2842. begin
  2843. regsize:=0;
  2844. push_regs;
  2845. reference_reset_base(current_procinfo.save_regs_ref,
  2846. current_procinfo.framepointer,
  2847. -(localsize+regsize),sizeof(aint));
  2848. end;
  2849. {$endif i386}
  2850. end;
  2851. end;
  2852. procedure tcgx86.g_save_registers(list: TAsmList);
  2853. begin
  2854. {$ifdef i386}
  2855. if paramanager.use_fixed_stack then
  2856. {$endif i386}
  2857. inherited g_save_registers(list);
  2858. end;
  2859. procedure tcgx86.g_restore_registers(list: TAsmList);
  2860. begin
  2861. {$ifdef i386}
  2862. if paramanager.use_fixed_stack then
  2863. {$endif i386}
  2864. inherited g_restore_registers(list);
  2865. end;
  2866. procedure tcgx86.internal_restore_regs(list: TAsmList; use_pop: boolean);
  2867. var
  2868. r: longint;
  2869. hreg: tregister;
  2870. href: treference;
  2871. usedregs: tcpuregisterset;
  2872. begin
  2873. href:=current_procinfo.save_regs_ref;
  2874. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  2875. for r:=high(saved_standard_registers) downto low(saved_standard_registers) do
  2876. if saved_standard_registers[r] in usedregs then
  2877. begin
  2878. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  2879. { Allocate register so the optimizer does not remove the load }
  2880. a_reg_alloc(list,hreg);
  2881. if use_pop then
  2882. list.concat(Taicpu.Op_reg(A_POP,tcgsize2opsize[OS_ADDR],hreg))
  2883. else
  2884. begin
  2885. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2886. inc(href.offset,sizeof(aint));
  2887. end;
  2888. end;
  2889. end;
  2890. procedure tcgx86.generate_leave(list: TAsmList);
  2891. begin
  2892. if UseLeave then
  2893. list.concat(taicpu.op_none(A_LEAVE,S_NO))
  2894. else
  2895. begin
  2896. {$if defined(x86_64)}
  2897. list.Concat(taicpu.op_reg_reg(A_MOV,S_Q,NR_RBP,NR_RSP));
  2898. list.Concat(taicpu.op_reg(A_POP,S_Q,NR_RBP));
  2899. {$elseif defined(i386)}
  2900. list.Concat(taicpu.op_reg_reg(A_MOV,S_L,NR_EBP,NR_ESP));
  2901. list.Concat(taicpu.op_reg(A_POP,S_L,NR_EBP));
  2902. {$elseif defined(i8086)}
  2903. list.Concat(taicpu.op_reg_reg(A_MOV,S_W,NR_BP,NR_SP));
  2904. list.Concat(taicpu.op_reg(A_POP,S_W,NR_BP));
  2905. {$endif}
  2906. end;
  2907. end;
  2908. { produces if necessary overflowcode }
  2909. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  2910. var
  2911. hl : tasmlabel;
  2912. ai : taicpu;
  2913. cond : TAsmCond;
  2914. begin
  2915. if not(cs_check_overflow in current_settings.localswitches) then
  2916. exit;
  2917. current_asmdata.getjumplabel(hl);
  2918. if not ((def.typ=pointerdef) or
  2919. ((def.typ=orddef) and
  2920. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  2921. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  2922. cond:=C_NO
  2923. else
  2924. cond:=C_NB;
  2925. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  2926. ai.SetCondition(cond);
  2927. ai.is_jmp:=true;
  2928. list.concat(ai);
  2929. a_call_name(list,'FPC_OVERFLOW',false);
  2930. a_label(list,hl);
  2931. end;
  2932. end.