cgx86.pas 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215
  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. { the optimizer will change it to "xor reg,reg" when loading zero, }
  787. { no need to do it here too (JM) }
  788. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg))
  789. end;
  790. procedure tcgx86.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  791. var
  792. tmpref : treference;
  793. begin
  794. tmpref:=ref;
  795. make_simple_ref(list,tmpref);
  796. {$ifdef x86_64}
  797. { x86_64 only supports signed 32 bits constants directly }
  798. if (tosize in [OS_S64,OS_64]) and
  799. ((a<low(longint)) or (a>high(longint))) then
  800. begin
  801. a_load_const_ref(list,OS_32,longint(a and $ffffffff),tmpref);
  802. inc(tmpref.offset,4);
  803. a_load_const_ref(list,OS_32,longint(a shr 32),tmpref);
  804. end
  805. else
  806. {$endif x86_64}
  807. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  808. end;
  809. procedure tcgx86.a_load_reg_ref(list : TAsmList; fromsize,tosize: TCGSize; reg : tregister;const ref : treference);
  810. var
  811. op: tasmop;
  812. s: topsize;
  813. tmpsize : tcgsize;
  814. tmpreg : tregister;
  815. tmpref : treference;
  816. begin
  817. tmpref:=ref;
  818. make_simple_ref(list,tmpref);
  819. check_register_size(fromsize,reg);
  820. sizes2load(fromsize,tosize,op,s);
  821. case s of
  822. {$ifdef x86_64}
  823. S_BQ,S_WQ,S_LQ,
  824. {$endif x86_64}
  825. S_BW,S_BL,S_WL :
  826. begin
  827. tmpreg:=getintregister(list,tosize);
  828. {$ifdef x86_64}
  829. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  830. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  831. 64 bit (FK) }
  832. if s in [S_BL,S_WL,S_L] then
  833. begin
  834. tmpreg:=makeregsize(list,tmpreg,OS_32);
  835. tmpsize:=OS_32;
  836. end
  837. else
  838. {$endif x86_64}
  839. tmpsize:=tosize;
  840. list.concat(taicpu.op_reg_reg(op,s,reg,tmpreg));
  841. a_load_reg_ref(list,tmpsize,tosize,tmpreg,tmpref);
  842. end;
  843. else
  844. list.concat(taicpu.op_reg_ref(op,s,reg,tmpref));
  845. end;
  846. end;
  847. procedure tcgx86.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  848. var
  849. op: tasmop;
  850. s: topsize;
  851. tmpref : treference;
  852. begin
  853. tmpref:=ref;
  854. make_simple_ref(list,tmpref);
  855. check_register_size(tosize,reg);
  856. sizes2load(fromsize,tosize,op,s);
  857. {$ifdef x86_64}
  858. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  859. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  860. 64 bit (FK) }
  861. if s in [S_BL,S_WL,S_L] then
  862. reg:=makeregsize(list,reg,OS_32);
  863. {$endif x86_64}
  864. list.concat(taicpu.op_ref_reg(op,s,tmpref,reg));
  865. end;
  866. procedure tcgx86.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  867. var
  868. op: tasmop;
  869. s: topsize;
  870. instr:Taicpu;
  871. begin
  872. check_register_size(fromsize,reg1);
  873. check_register_size(tosize,reg2);
  874. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  875. begin
  876. reg1:=makeregsize(list,reg1,tosize);
  877. s:=tcgsize2opsize[tosize];
  878. op:=A_MOV;
  879. end
  880. else
  881. sizes2load(fromsize,tosize,op,s);
  882. {$ifdef x86_64}
  883. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  884. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  885. 64 bit (FK)
  886. }
  887. if s in [S_BL,S_WL,S_L] then
  888. reg2:=makeregsize(list,reg2,OS_32);
  889. {$endif x86_64}
  890. if (reg1<>reg2) then
  891. begin
  892. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  893. { Notify the register allocator that we have written a move instruction so
  894. it can try to eliminate it. }
  895. if (reg1<>current_procinfo.framepointer) and (reg1<>NR_STACK_POINTER_REG) then
  896. add_move_instruction(instr);
  897. list.concat(instr);
  898. end;
  899. {$ifdef x86_64}
  900. { avoid merging of registers and killing the zero extensions (FK) }
  901. if (tosize in [OS_64,OS_S64]) and (s=S_L) then
  902. list.concat(taicpu.op_const_reg(A_AND,S_L,$ffffffff,reg2));
  903. {$endif x86_64}
  904. end;
  905. procedure tcgx86.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  906. var
  907. dirref,tmpref : treference;
  908. begin
  909. dirref:=ref;
  910. { this could probably done in a more optimized way, but for now this
  911. is sufficent }
  912. make_direct_ref(list,dirref);
  913. with dirref do
  914. begin
  915. if (base=NR_NO) and (index=NR_NO) then
  916. begin
  917. if assigned(dirref.symbol) then
  918. begin
  919. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  920. ((dirref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  921. (cs_create_pic in current_settings.moduleswitches)) then
  922. begin
  923. if (dirref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  924. ((cs_create_pic in current_settings.moduleswitches) and
  925. (dirref.symbol.bind in [AB_COMMON,AB_GLOBAL,AB_PRIVATE_EXTERN])) then
  926. begin
  927. reference_reset_base(tmpref,
  928. g_indirect_sym_load(list,dirref.symbol.name,asmsym2indsymflags(dirref.symbol)),
  929. offset,sizeof(pint));
  930. a_loadaddr_ref_reg(list,tmpref,r);
  931. end
  932. else
  933. begin
  934. include(current_procinfo.flags,pi_needs_got);
  935. reference_reset_base(tmpref,current_procinfo.got,offset,dirref.alignment);
  936. tmpref.symbol:=symbol;
  937. tmpref.relsymbol:=current_procinfo.CurrGOTLabel;
  938. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  939. end;
  940. end
  941. else if (cs_create_pic in current_settings.moduleswitches)
  942. {$ifdef x86_64}
  943. and not(dirref.symbol.bind=AB_LOCAL)
  944. {$endif x86_64}
  945. then
  946. begin
  947. {$ifdef x86_64}
  948. reference_reset_symbol(tmpref,dirref.symbol,0,dirref.alignment);
  949. tmpref.refaddr:=addr_pic;
  950. tmpref.base:=NR_RIP;
  951. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,tmpref,r));
  952. {$else x86_64}
  953. reference_reset_symbol(tmpref,dirref.symbol,0,dirref.alignment);
  954. tmpref.refaddr:=addr_pic;
  955. tmpref.base:=current_procinfo.got;
  956. include(current_procinfo.flags,pi_needs_got);
  957. list.concat(taicpu.op_ref_reg(A_MOV,S_L,tmpref,r));
  958. {$endif x86_64}
  959. if offset<>0 then
  960. a_op_const_reg(list,OP_ADD,OS_ADDR,offset,r);
  961. end
  962. {$ifdef x86_64}
  963. else if (target_info.system in (systems_all_windows+[system_x86_64_darwin,system_x86_64_iphonesim]))
  964. or (cs_create_pic in current_settings.moduleswitches)
  965. then
  966. begin
  967. { Win64 and Darwin/x86_64 always require RIP-relative addressing }
  968. tmpref:=dirref;
  969. tmpref.base:=NR_RIP;
  970. tmpref.refaddr:=addr_pic_no_got;
  971. list.concat(Taicpu.op_ref_reg(A_LEA,S_Q,tmpref,r));
  972. end
  973. {$endif x86_64}
  974. else
  975. begin
  976. tmpref:=dirref;
  977. tmpref.refaddr:=ADDR_FULL;
  978. list.concat(Taicpu.op_ref_reg(A_MOV,tcgsize2opsize[OS_ADDR],tmpref,r));
  979. end
  980. end
  981. else
  982. a_load_const_reg(list,OS_ADDR,offset,r)
  983. end
  984. else if (base=NR_NO) and (index<>NR_NO) and
  985. (offset=0) and (scalefactor=0) and (symbol=nil) then
  986. a_load_reg_reg(list,OS_ADDR,OS_ADDR,index,r)
  987. else if (base<>NR_NO) and (index=NR_NO) and
  988. (offset=0) and (symbol=nil) then
  989. a_load_reg_reg(list,OS_ADDR,OS_ADDR,base,r)
  990. else
  991. begin
  992. tmpref:=dirref;
  993. make_simple_ref(list,tmpref);
  994. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  995. end;
  996. if segment<>NR_NO then
  997. begin
  998. {$ifdef i8086}
  999. if is_segment_reg(segment) then
  1000. list.concat(Taicpu.op_reg_reg(A_MOV,S_W,segment,GetNextReg(r)))
  1001. else
  1002. a_load_reg_reg(list,OS_16,OS_16,segment,GetNextReg(r));
  1003. {$else i8086}
  1004. if (tf_section_threadvars in target_info.flags) then
  1005. begin
  1006. { Convert thread local address to a process global addres
  1007. as we cannot handle far pointers.}
  1008. case target_info.system of
  1009. system_i386_linux,system_i386_android:
  1010. if segment=NR_GS then
  1011. begin
  1012. reference_reset_symbol(tmpref,current_asmdata.RefAsmSymbol('___fpc_threadvar_offset'),0,dirref.alignment);
  1013. tmpref.segment:=NR_GS;
  1014. list.concat(Taicpu.op_ref_reg(A_ADD,tcgsize2opsize[OS_ADDR],tmpref,r));
  1015. end
  1016. else
  1017. cgmessage(cg_e_cant_use_far_pointer_there);
  1018. else
  1019. cgmessage(cg_e_cant_use_far_pointer_there);
  1020. end;
  1021. end
  1022. else
  1023. cgmessage(cg_e_cant_use_far_pointer_there);
  1024. {$endif i8086}
  1025. end;
  1026. end;
  1027. end;
  1028. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  1029. { R_ST means "the current value at the top of the fpu stack" (JM) }
  1030. procedure tcgx86.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  1031. var
  1032. href: treference;
  1033. op: tasmop;
  1034. s: topsize;
  1035. begin
  1036. if (reg1<>NR_ST) then
  1037. begin
  1038. floatloadops(tosize,op,s);
  1039. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  1040. inc_fpu_stack;
  1041. end;
  1042. if (reg2<>NR_ST) then
  1043. begin
  1044. floatstoreops(tosize,op,s);
  1045. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  1046. dec_fpu_stack;
  1047. end;
  1048. { OS_F80 < OS_C64, but OS_C64 fits perfectly in OS_F80 }
  1049. if (reg1=NR_ST) and
  1050. (reg2=NR_ST) and
  1051. (tosize<>OS_F80) and
  1052. (tosize<fromsize) then
  1053. begin
  1054. { can't round down to lower precision in x87 :/ }
  1055. tg.gettemp(list,tcgsize2size[tosize],tcgsize2size[tosize],tt_normal,href);
  1056. a_loadfpu_reg_ref(list,fromsize,tosize,NR_ST,href);
  1057. a_loadfpu_ref_reg(list,tosize,tosize,href,NR_ST);
  1058. tg.ungettemp(list,href);
  1059. end;
  1060. end;
  1061. procedure tcgx86.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  1062. var
  1063. tmpref : treference;
  1064. begin
  1065. tmpref:=ref;
  1066. make_simple_ref(list,tmpref);
  1067. floatload(list,fromsize,tmpref);
  1068. a_loadfpu_reg_reg(list,fromsize,tosize,NR_ST,reg);
  1069. end;
  1070. procedure tcgx86.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  1071. var
  1072. tmpref : treference;
  1073. begin
  1074. tmpref:=ref;
  1075. make_simple_ref(list,tmpref);
  1076. { in case a record returned in a floating point register
  1077. (LOC_FPUREGISTER with OS_F32/OS_F64) is stored in memory
  1078. (LOC_REFERENCE with OS_32/OS_64), we have to adjust the
  1079. tosize }
  1080. if (fromsize in [OS_F32,OS_F64]) and
  1081. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1082. case tosize of
  1083. OS_32:
  1084. tosize:=OS_F32;
  1085. OS_64:
  1086. tosize:=OS_F64;
  1087. end;
  1088. if reg<>NR_ST then
  1089. a_loadfpu_reg_reg(list,fromsize,tosize,reg,NR_ST);
  1090. floatstore(list,tosize,tmpref);
  1091. end;
  1092. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  1093. const
  1094. convertopsse : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1095. (A_MOVSS,A_CVTSS2SD,A_NONE,A_NONE,A_NONE),
  1096. (A_CVTSD2SS,A_MOVSD,A_NONE,A_NONE,A_NONE),
  1097. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1098. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1099. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  1100. convertopavx : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1101. (A_VMOVSS,A_VCVTSS2SD,A_NONE,A_NONE,A_NONE),
  1102. (A_VCVTSD2SS,A_VMOVSD,A_NONE,A_NONE,A_NONE),
  1103. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1104. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1105. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  1106. begin
  1107. { we can have OS_F32/OS_F64 (record in function result/LOC_MMREGISTER) to
  1108. OS_32/OS_64 (record in memory/LOC_REFERENCE) }
  1109. if (fromsize in [OS_F32,OS_F64]) and
  1110. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1111. case tosize of
  1112. OS_32:
  1113. tosize:=OS_F32;
  1114. OS_64:
  1115. tosize:=OS_F64;
  1116. end;
  1117. if (fromsize in [low(convertopsse)..high(convertopsse)]) and
  1118. (tosize in [low(convertopsse)..high(convertopsse)]) then
  1119. begin
  1120. if UseAVX then
  1121. result:=convertopavx[fromsize,tosize]
  1122. else
  1123. result:=convertopsse[fromsize,tosize];
  1124. end
  1125. { we can have OS_M64 (record in function result/LOC_MMREGISTER) to
  1126. OS_64 (record in memory/LOC_REFERENCE) }
  1127. else if (tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1128. (fromsize=OS_M64) then
  1129. begin
  1130. if UseAVX then
  1131. result:=A_VMOVQ
  1132. else
  1133. result:=A_MOVQ;
  1134. end
  1135. else
  1136. internalerror(2010060104);
  1137. if result=A_NONE then
  1138. internalerror(200312205);
  1139. end;
  1140. procedure tcgx86.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  1141. var
  1142. instr : taicpu;
  1143. op : TAsmOp;
  1144. begin
  1145. if shuffle=nil then
  1146. begin
  1147. if fromsize=tosize then
  1148. { needs correct size in case of spilling }
  1149. case fromsize of
  1150. OS_F32:
  1151. if UseAVX then
  1152. instr:=taicpu.op_reg_reg(A_VMOVAPS,S_NO,reg1,reg2)
  1153. else
  1154. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2);
  1155. OS_F64:
  1156. if UseAVX then
  1157. instr:=taicpu.op_reg_reg(A_VMOVAPD,S_NO,reg1,reg2)
  1158. else
  1159. instr:=taicpu.op_reg_reg(A_MOVAPD,S_NO,reg1,reg2);
  1160. OS_M64:
  1161. if UseAVX then
  1162. instr:=taicpu.op_reg_reg(A_VMOVQ,S_NO,reg1,reg2)
  1163. else
  1164. instr:=taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2);
  1165. else
  1166. internalerror(2006091201);
  1167. end
  1168. else
  1169. internalerror(200312202);
  1170. add_move_instruction(instr);
  1171. end
  1172. else if shufflescalar(shuffle) then
  1173. begin
  1174. op:=get_scalar_mm_op(fromsize,tosize);
  1175. { MOVAPD/MOVAPS are normally faster }
  1176. if op=A_MOVSD then
  1177. op:=A_MOVAPD
  1178. else if op=A_MOVSS then
  1179. op:=A_MOVAPS
  1180. { VMOVSD/SS is not available with two register operands }
  1181. else if op=A_VMOVSD then
  1182. op:=A_VMOVAPD
  1183. else if op=A_VMOVSS then
  1184. op:=A_VMOVAPS;
  1185. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1186. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1187. instr:=taicpu.op_reg_reg_reg(op,S_NO,reg1,reg2,reg2)
  1188. else
  1189. instr:=taicpu.op_reg_reg(op,S_NO,reg1,reg2);
  1190. case op of
  1191. A_VMOVAPD,
  1192. A_VMOVAPS,
  1193. A_VMOVSS,
  1194. A_VMOVSD,
  1195. A_VMOVQ,
  1196. A_MOVAPD,
  1197. A_MOVAPS,
  1198. A_MOVSS,
  1199. A_MOVSD,
  1200. A_MOVQ:
  1201. add_move_instruction(instr);
  1202. end;
  1203. end
  1204. else
  1205. internalerror(200312201);
  1206. list.concat(instr);
  1207. end;
  1208. procedure tcgx86.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1209. var
  1210. tmpref : treference;
  1211. op : tasmop;
  1212. begin
  1213. tmpref:=ref;
  1214. make_simple_ref(list,tmpref);
  1215. if shuffle=nil then
  1216. begin
  1217. if fromsize=OS_M64 then
  1218. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,reg))
  1219. else
  1220. {$ifdef x86_64}
  1221. { x86-64 has always properly aligned data }
  1222. list.concat(taicpu.op_ref_reg(A_MOVDQA,S_NO,tmpref,reg));
  1223. {$else x86_64}
  1224. list.concat(taicpu.op_ref_reg(A_MOVDQU,S_NO,tmpref,reg));
  1225. {$endif x86_64}
  1226. end
  1227. else if shufflescalar(shuffle) then
  1228. begin
  1229. op:=get_scalar_mm_op(fromsize,tosize);
  1230. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1231. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1232. list.concat(taicpu.op_ref_reg_reg(op,S_NO,tmpref,reg,reg))
  1233. else
  1234. list.concat(taicpu.op_ref_reg(op,S_NO,tmpref,reg))
  1235. end
  1236. else
  1237. internalerror(200312252);
  1238. end;
  1239. procedure tcgx86.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  1240. var
  1241. hreg : tregister;
  1242. tmpref : treference;
  1243. op : tasmop;
  1244. begin
  1245. tmpref:=ref;
  1246. make_simple_ref(list,tmpref);
  1247. if shuffle=nil then
  1248. begin
  1249. if fromsize=OS_M64 then
  1250. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,tmpref))
  1251. else
  1252. {$ifdef x86_64}
  1253. { x86-64 has always properly aligned data }
  1254. list.concat(taicpu.op_reg_ref(A_MOVDQA,S_NO,reg,tmpref))
  1255. {$else x86_64}
  1256. list.concat(taicpu.op_reg_ref(A_MOVDQU,S_NO,reg,tmpref))
  1257. {$endif x86_64}
  1258. end
  1259. else if shufflescalar(shuffle) then
  1260. begin
  1261. if tcgsize2size[tosize]<>tcgsize2size[fromsize] then
  1262. begin
  1263. hreg:=getmmregister(list,tosize);
  1264. op:=get_scalar_mm_op(fromsize,tosize);
  1265. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1266. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1267. list.concat(taicpu.op_reg_reg_reg(op,S_NO,reg,hreg,hreg))
  1268. else
  1269. list.concat(taicpu.op_reg_reg(op,S_NO,reg,hreg));
  1270. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize),S_NO,hreg,tmpref))
  1271. end
  1272. else
  1273. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize),S_NO,reg,tmpref));
  1274. end
  1275. else
  1276. internalerror(200312252);
  1277. end;
  1278. procedure tcgx86.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1279. var
  1280. l : tlocation;
  1281. begin
  1282. l.loc:=LOC_REFERENCE;
  1283. l.reference:=ref;
  1284. l.size:=size;
  1285. opmm_loc_reg(list,op,size,l,reg,shuffle);
  1286. end;
  1287. procedure tcgx86.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  1288. var
  1289. l : tlocation;
  1290. begin
  1291. l.loc:=LOC_MMREGISTER;
  1292. l.register:=src;
  1293. l.size:=size;
  1294. opmm_loc_reg(list,op,size,l,dst,shuffle);
  1295. end;
  1296. procedure tcgx86.opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;src,dst: tregister; shuffle : pmmshuffle);
  1297. const
  1298. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1299. ( { scalar }
  1300. ( { OS_F32 }
  1301. 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
  1302. ),
  1303. ( { OS_F64 }
  1304. 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
  1305. )
  1306. ),
  1307. ( { vectorized/packed }
  1308. { because the logical packed single instructions have shorter op codes, we use always
  1309. these
  1310. }
  1311. ( { OS_F32 }
  1312. 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
  1313. ),
  1314. ( { OS_F64 }
  1315. 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
  1316. )
  1317. )
  1318. );
  1319. var
  1320. resultreg : tregister;
  1321. asmop : tasmop;
  1322. begin
  1323. { this is an internally used procedure so the parameters have
  1324. some constrains
  1325. }
  1326. if loc.size<>size then
  1327. internalerror(2013061108);
  1328. resultreg:=dst;
  1329. { deshuffle }
  1330. //!!!
  1331. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1332. begin
  1333. internalerror(2013061107);
  1334. end
  1335. else if (shuffle=nil) then
  1336. asmop:=opmm2asmop[1,size,op]
  1337. else if shufflescalar(shuffle) then
  1338. begin
  1339. asmop:=opmm2asmop[0,size,op];
  1340. { no scalar operation available? }
  1341. if asmop=A_NOP then
  1342. begin
  1343. { do vectorized and shuffle finally }
  1344. internalerror(2010060102);
  1345. end;
  1346. end
  1347. else
  1348. internalerror(2013061106);
  1349. if asmop=A_NOP then
  1350. internalerror(2013061105);
  1351. case loc.loc of
  1352. LOC_CREFERENCE,LOC_REFERENCE:
  1353. begin
  1354. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1355. list.concat(taicpu.op_ref_reg_reg(asmop,S_NO,loc.reference,src,resultreg));
  1356. end;
  1357. LOC_CMMREGISTER,LOC_MMREGISTER:
  1358. list.concat(taicpu.op_reg_reg_reg(asmop,S_NO,loc.register,src,resultreg));
  1359. else
  1360. internalerror(2013061104);
  1361. end;
  1362. { shuffle }
  1363. if resultreg<>dst then
  1364. begin
  1365. internalerror(2013061103);
  1366. end;
  1367. end;
  1368. procedure tcgx86.a_opmm_reg_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src1,src2,dst: tregister;shuffle : pmmshuffle);
  1369. var
  1370. l : tlocation;
  1371. begin
  1372. l.loc:=LOC_MMREGISTER;
  1373. l.register:=src1;
  1374. l.size:=size;
  1375. opmm_loc_reg_reg(list,op,size,l,src2,dst,shuffle);
  1376. end;
  1377. procedure tcgx86.a_opmm_ref_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; src,dst: tregister;shuffle : pmmshuffle);
  1378. var
  1379. l : tlocation;
  1380. begin
  1381. l.loc:=LOC_REFERENCE;
  1382. l.reference:=ref;
  1383. l.size:=size;
  1384. opmm_loc_reg_reg(list,op,size,l,src,dst,shuffle);
  1385. end;
  1386. procedure tcgx86.opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  1387. const
  1388. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1389. ( { scalar }
  1390. ( { OS_F32 }
  1391. 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
  1392. ),
  1393. ( { OS_F64 }
  1394. 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
  1395. )
  1396. ),
  1397. ( { vectorized/packed }
  1398. { because the logical packed single instructions have shorter op codes, we use always
  1399. these
  1400. }
  1401. ( { OS_F32 }
  1402. 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
  1403. ),
  1404. ( { OS_F64 }
  1405. 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
  1406. )
  1407. )
  1408. );
  1409. var
  1410. resultreg : tregister;
  1411. asmop : tasmop;
  1412. begin
  1413. { this is an internally used procedure so the parameters have
  1414. some constrains
  1415. }
  1416. if loc.size<>size then
  1417. internalerror(200312213);
  1418. resultreg:=dst;
  1419. { deshuffle }
  1420. //!!!
  1421. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1422. begin
  1423. internalerror(2010060101);
  1424. end
  1425. else if (shuffle=nil) then
  1426. asmop:=opmm2asmop[1,size,op]
  1427. else if shufflescalar(shuffle) then
  1428. begin
  1429. asmop:=opmm2asmop[0,size,op];
  1430. { no scalar operation available? }
  1431. if asmop=A_NOP then
  1432. begin
  1433. { do vectorized and shuffle finally }
  1434. internalerror(2010060102);
  1435. end;
  1436. end
  1437. else
  1438. internalerror(200312211);
  1439. if asmop=A_NOP then
  1440. internalerror(200312216);
  1441. case loc.loc of
  1442. LOC_CREFERENCE,LOC_REFERENCE:
  1443. begin
  1444. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1445. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  1446. end;
  1447. LOC_CMMREGISTER,LOC_MMREGISTER:
  1448. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  1449. else
  1450. internalerror(200312214);
  1451. end;
  1452. { shuffle }
  1453. if resultreg<>dst then
  1454. begin
  1455. internalerror(200312212);
  1456. end;
  1457. end;
  1458. {$ifndef i8086}
  1459. procedure tcgx86.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1460. a:tcgint;src,dst:Tregister);
  1461. var
  1462. power,al : longint;
  1463. href : treference;
  1464. begin
  1465. power:=0;
  1466. optimize_op_const(size,op,a);
  1467. case op of
  1468. OP_NONE:
  1469. begin
  1470. a_load_reg_reg(list,size,size,src,dst);
  1471. exit;
  1472. end;
  1473. OP_MOVE:
  1474. begin
  1475. a_load_const_reg(list,size,a,dst);
  1476. exit;
  1477. end;
  1478. end;
  1479. if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1480. not(cs_check_overflow in current_settings.localswitches) and
  1481. (a>1) and ispowerof2(int64(a-1),power) and (power in [1..3]) then
  1482. begin
  1483. reference_reset_base(href,src,0,0);
  1484. href.index:=src;
  1485. href.scalefactor:=a-1;
  1486. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1487. end
  1488. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1489. not(cs_check_overflow in current_settings.localswitches) and
  1490. (a>1) and ispowerof2(int64(a),power) and (power in [1..3]) then
  1491. begin
  1492. reference_reset_base(href,NR_NO,0,0);
  1493. href.index:=src;
  1494. href.scalefactor:=a;
  1495. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1496. end
  1497. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1498. (a>1) and (a<=maxLongint) and not ispowerof2(int64(a),power) then
  1499. begin
  1500. { MUL with overflow checking should be handled specifically in the code generator }
  1501. if (op=OP_MUL) and (cs_check_overflow in current_settings.localswitches) then
  1502. internalerror(2014011801);
  1503. list.concat(taicpu.op_const_reg_reg(A_IMUL,TCgSize2OpSize[size],a,src,dst));
  1504. end
  1505. else if (op=OP_ADD) and
  1506. ((size in [OS_32,OS_S32]) or
  1507. { lea supports only 32 bit signed displacments }
  1508. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1509. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1510. ) and
  1511. not(cs_check_overflow in current_settings.localswitches) then
  1512. begin
  1513. { a might still be in the range 0x80000000 to 0xffffffff
  1514. which might trigger a range check error as
  1515. reference_reset_base expects a longint value. }
  1516. {$push} {$R-}{$Q-}
  1517. al := longint (a);
  1518. {$pop}
  1519. reference_reset_base(href,src,al,0);
  1520. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1521. end
  1522. else if (op=OP_SUB) and
  1523. ((size in [OS_32,OS_S32]) or
  1524. { lea supports only 32 bit signed displacments }
  1525. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1526. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1527. ) and
  1528. not(cs_check_overflow in current_settings.localswitches) then
  1529. begin
  1530. reference_reset_base(href,src,-a,0);
  1531. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1532. end
  1533. else if (op in [OP_ROR,OP_ROL]) and
  1534. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1535. (size in [OS_32,OS_S32
  1536. {$ifdef x86_64}
  1537. ,OS_64,OS_S64
  1538. {$endif x86_64}
  1539. ]) then
  1540. begin
  1541. if op=OP_ROR then
  1542. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size], a,src,dst))
  1543. else
  1544. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size],TCgSize2Size[size]*8-a,src,dst));
  1545. end
  1546. else
  1547. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1548. end;
  1549. procedure tcgx86.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1550. size: tcgsize; src1, src2, dst: tregister);
  1551. var
  1552. href : treference;
  1553. begin
  1554. if (op=OP_ADD) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1555. not(cs_check_overflow in current_settings.localswitches) then
  1556. begin
  1557. reference_reset_base(href,src1,0,0);
  1558. href.index:=src2;
  1559. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1560. end
  1561. else if (op in [OP_SHR,OP_SHL]) and
  1562. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1563. (size in [OS_32,OS_S32
  1564. {$ifdef x86_64}
  1565. ,OS_64,OS_S64
  1566. {$endif x86_64}
  1567. ]) then
  1568. begin
  1569. if op=OP_SHL then
  1570. list.concat(taicpu.op_reg_reg_reg(A_SHLX,TCgSize2OpSize[size],src1,src2,dst))
  1571. else
  1572. list.concat(taicpu.op_reg_reg_reg(A_SHRX,TCgSize2OpSize[size],src1,src2,dst));
  1573. end
  1574. else
  1575. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1576. end;
  1577. {$endif not i8086}
  1578. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  1579. {$ifdef x86_64}
  1580. var
  1581. tmpreg : tregister;
  1582. {$endif x86_64}
  1583. begin
  1584. optimize_op_const(size, op, a);
  1585. {$ifdef x86_64}
  1586. { x86_64 only supports signed 32 bits constants directly }
  1587. if not(op in [OP_NONE,OP_MOVE]) and
  1588. (size in [OS_S64,OS_64]) and
  1589. ((a<low(longint)) or (a>high(longint))) then
  1590. begin
  1591. tmpreg:=getintregister(list,size);
  1592. a_load_const_reg(list,size,a,tmpreg);
  1593. a_op_reg_reg(list,op,size,tmpreg,reg);
  1594. exit;
  1595. end;
  1596. {$endif x86_64}
  1597. check_register_size(size,reg);
  1598. case op of
  1599. OP_NONE :
  1600. begin
  1601. { Opcode is optimized away }
  1602. end;
  1603. OP_MOVE :
  1604. begin
  1605. { Optimized, replaced with a simple load }
  1606. a_load_const_reg(list,size,a,reg);
  1607. end;
  1608. OP_DIV, OP_IDIV:
  1609. begin
  1610. { should be handled specifically in the code }
  1611. { generator because of the silly register usage restraints }
  1612. internalerror(200109224);
  1613. end;
  1614. OP_MUL,OP_IMUL:
  1615. begin
  1616. if not (cs_check_overflow in current_settings.localswitches) then
  1617. op:=OP_IMUL;
  1618. if op = OP_IMUL then
  1619. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  1620. else
  1621. { OP_MUL should be handled specifically in the code }
  1622. { generator because of the silly register usage restraints }
  1623. internalerror(200109225);
  1624. end;
  1625. OP_ADD, OP_SUB:
  1626. if not(cs_check_overflow in current_settings.localswitches) and
  1627. (a = 1) and
  1628. UseIncDec then
  1629. begin
  1630. if op = OP_ADD then
  1631. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  1632. else
  1633. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  1634. end
  1635. else
  1636. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],aint(a),reg));
  1637. OP_AND,OP_OR:
  1638. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],aint(a),reg));
  1639. OP_XOR:
  1640. if (aword(a)=high(aword)) then
  1641. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg))
  1642. else
  1643. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],aint(a),reg));
  1644. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1645. begin
  1646. {$if defined(x86_64)}
  1647. if (a and 63) <> 0 Then
  1648. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,reg));
  1649. if (a shr 6) <> 0 Then
  1650. internalerror(200609073);
  1651. {$elseif defined(i386)}
  1652. if (a and 31) <> 0 Then
  1653. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1654. if (a shr 5) <> 0 Then
  1655. internalerror(200609071);
  1656. {$elseif defined(i8086)}
  1657. if (a shr 5) <> 0 Then
  1658. internalerror(2013043002);
  1659. a := a and 31;
  1660. if a <> 0 Then
  1661. begin
  1662. if (current_settings.cputype < cpu_186) and (a <> 1) then
  1663. begin
  1664. getcpuregister(list,NR_CL);
  1665. a_load_const_reg(list,OS_8,a,NR_CL);
  1666. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,reg));
  1667. ungetcpuregister(list,NR_CL);
  1668. end
  1669. else
  1670. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  1671. end;
  1672. {$endif}
  1673. end
  1674. else internalerror(200609072);
  1675. end;
  1676. end;
  1677. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1678. var
  1679. {$ifdef x86_64}
  1680. tmpreg : tregister;
  1681. {$endif x86_64}
  1682. tmpref : treference;
  1683. begin
  1684. optimize_op_const(size, op, a);
  1685. if op in [OP_NONE,OP_MOVE] then
  1686. begin
  1687. if (op=OP_MOVE) then
  1688. a_load_const_ref(list,size,a,ref);
  1689. exit;
  1690. end;
  1691. {$ifdef x86_64}
  1692. { x86_64 only supports signed 32 bits constants directly }
  1693. if (size in [OS_S64,OS_64]) and
  1694. ((a<low(longint)) or (a>high(longint))) then
  1695. begin
  1696. tmpreg:=getintregister(list,size);
  1697. a_load_const_reg(list,size,a,tmpreg);
  1698. a_op_reg_ref(list,op,size,tmpreg,ref);
  1699. exit;
  1700. end;
  1701. {$endif x86_64}
  1702. tmpref:=ref;
  1703. make_simple_ref(list,tmpref);
  1704. Case Op of
  1705. OP_DIV, OP_IDIV:
  1706. Begin
  1707. { should be handled specifically in the code }
  1708. { generator because of the silly register usage restraints }
  1709. internalerror(200109231);
  1710. End;
  1711. OP_MUL,OP_IMUL:
  1712. begin
  1713. if not (cs_check_overflow in current_settings.localswitches) then
  1714. op:=OP_IMUL;
  1715. { can't multiply a memory location directly with a constant }
  1716. if op = OP_IMUL then
  1717. inherited a_op_const_ref(list,op,size,a,tmpref)
  1718. else
  1719. { OP_MUL should be handled specifically in the code }
  1720. { generator because of the silly register usage restraints }
  1721. internalerror(200109232);
  1722. end;
  1723. OP_ADD, OP_SUB:
  1724. if not(cs_check_overflow in current_settings.localswitches) and
  1725. (a = 1) and
  1726. UseIncDec then
  1727. begin
  1728. if op = OP_ADD then
  1729. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  1730. else
  1731. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  1732. end
  1733. else
  1734. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1735. OP_AND,OP_OR:
  1736. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1737. OP_XOR:
  1738. if (aword(a)=high(aword)) then
  1739. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref))
  1740. else
  1741. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1742. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1743. begin
  1744. {$if defined(x86_64)}
  1745. if (a and 63) <> 0 Then
  1746. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,tmpref));
  1747. if (a shr 6) <> 0 Then
  1748. internalerror(2013111003);
  1749. {$elseif defined(i386)}
  1750. if (a and 31) <> 0 Then
  1751. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  1752. if (a shr 5) <> 0 Then
  1753. internalerror(2013111002);
  1754. {$elseif defined(i8086)}
  1755. if (a shr 5) <> 0 Then
  1756. internalerror(2013111001);
  1757. a := a and 31;
  1758. if a <> 0 Then
  1759. begin
  1760. if (current_settings.cputype < cpu_186) and (a <> 1) then
  1761. begin
  1762. getcpuregister(list,NR_CL);
  1763. a_load_const_reg(list,OS_8,a,NR_CL);
  1764. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,tmpref));
  1765. ungetcpuregister(list,NR_CL);
  1766. end
  1767. else
  1768. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1769. end;
  1770. {$endif}
  1771. end
  1772. else internalerror(68992);
  1773. end;
  1774. end;
  1775. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1776. const
  1777. {$if defined(cpu64bitalu)}
  1778. REGCX=NR_RCX;
  1779. REGCX_Size = OS_64;
  1780. {$elseif defined(cpu32bitalu)}
  1781. REGCX=NR_ECX;
  1782. REGCX_Size = OS_32;
  1783. {$elseif defined(cpu16bitalu)}
  1784. REGCX=NR_CX;
  1785. REGCX_Size = OS_16;
  1786. {$endif}
  1787. var
  1788. dstsize: topsize;
  1789. instr:Taicpu;
  1790. begin
  1791. check_register_size(size,src);
  1792. check_register_size(size,dst);
  1793. dstsize := tcgsize2opsize[size];
  1794. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  1795. op:=OP_IMUL;
  1796. case op of
  1797. OP_NEG,OP_NOT:
  1798. begin
  1799. if src<>dst then
  1800. a_load_reg_reg(list,size,size,src,dst);
  1801. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  1802. end;
  1803. OP_MUL,OP_DIV,OP_IDIV:
  1804. { special stuff, needs separate handling inside code }
  1805. { generator }
  1806. internalerror(200109233);
  1807. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  1808. begin
  1809. { Use ecx to load the value, that allows better coalescing }
  1810. getcpuregister(list,REGCX);
  1811. a_load_reg_reg(list,size,REGCX_Size,src,REGCX);
  1812. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  1813. ungetcpuregister(list,REGCX);
  1814. end;
  1815. else
  1816. begin
  1817. if reg2opsize(src) <> dstsize then
  1818. internalerror(200109226);
  1819. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  1820. list.concat(instr);
  1821. end;
  1822. end;
  1823. end;
  1824. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1825. var
  1826. tmpref : treference;
  1827. begin
  1828. tmpref:=ref;
  1829. make_simple_ref(list,tmpref);
  1830. check_register_size(size,reg);
  1831. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  1832. op:=OP_IMUL;
  1833. case op of
  1834. OP_NEG,OP_NOT,OP_IMUL:
  1835. begin
  1836. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1837. end;
  1838. OP_MUL,OP_DIV,OP_IDIV:
  1839. { special stuff, needs separate handling inside code }
  1840. { generator }
  1841. internalerror(200109239);
  1842. else
  1843. begin
  1844. reg := makeregsize(list,reg,size);
  1845. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  1846. end;
  1847. end;
  1848. end;
  1849. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1850. var
  1851. tmpref : treference;
  1852. begin
  1853. tmpref:=ref;
  1854. make_simple_ref(list,tmpref);
  1855. check_register_size(size,reg);
  1856. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  1857. op:=OP_IMUL;
  1858. case op of
  1859. OP_NEG,OP_NOT:
  1860. begin
  1861. if reg<>NR_NO then
  1862. internalerror(200109237);
  1863. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  1864. end;
  1865. OP_IMUL:
  1866. begin
  1867. { this one needs a load/imul/store, which is the default }
  1868. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1869. end;
  1870. OP_MUL,OP_DIV,OP_IDIV:
  1871. { special stuff, needs separate handling inside code }
  1872. { generator }
  1873. internalerror(200109238);
  1874. else
  1875. begin
  1876. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  1877. end;
  1878. end;
  1879. end;
  1880. procedure tcgx86.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister);
  1881. var
  1882. tmpreg: tregister;
  1883. opsize: topsize;
  1884. l : TAsmLabel;
  1885. begin
  1886. { no bsf/bsr for byte }
  1887. if srcsize in [OS_8,OS_S8] then
  1888. begin
  1889. tmpreg:=getintregister(list,OS_INT);
  1890. a_load_reg_reg(list,srcsize,OS_INT,src,tmpreg);
  1891. src:=tmpreg;
  1892. srcsize:=OS_INT;
  1893. end;
  1894. { source and destination register must have the same size }
  1895. if tcgsize2size[srcsize]<>tcgsize2size[dstsize] then
  1896. tmpreg:=getintregister(list,srcsize)
  1897. else
  1898. tmpreg:=dst;
  1899. opsize:=tcgsize2opsize[srcsize];
  1900. if not reverse then
  1901. list.concat(taicpu.op_reg_reg(A_BSF,opsize,src,tmpreg))
  1902. else
  1903. list.concat(taicpu.op_reg_reg(A_BSR,opsize,src,tmpreg));
  1904. current_asmdata.getjumplabel(l);
  1905. a_jmp_cond(list,OC_NE,l);
  1906. list.concat(taicpu.op_const_reg(A_MOV,opsize,$ff,tmpreg));
  1907. a_label(list,l);
  1908. if tmpreg<>dst then
  1909. a_load_reg_reg(list,srcsize,dstsize,tmpreg,dst);
  1910. end;
  1911. {*************** compare instructructions ****************}
  1912. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1913. l : tasmlabel);
  1914. {$ifdef x86_64}
  1915. var
  1916. tmpreg : tregister;
  1917. {$endif x86_64}
  1918. begin
  1919. {$ifdef x86_64}
  1920. { x86_64 only supports signed 32 bits constants directly }
  1921. if (size in [OS_S64,OS_64]) and
  1922. ((a<low(longint)) or (a>high(longint))) then
  1923. begin
  1924. tmpreg:=getintregister(list,size);
  1925. a_load_const_reg(list,size,a,tmpreg);
  1926. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1927. exit;
  1928. end;
  1929. {$endif x86_64}
  1930. if (a = 0) then
  1931. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  1932. else
  1933. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  1934. a_jmp_cond(list,cmp_op,l);
  1935. end;
  1936. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  1937. l : tasmlabel);
  1938. var
  1939. {$ifdef x86_64}
  1940. tmpreg : tregister;
  1941. {$endif x86_64}
  1942. tmpref : treference;
  1943. begin
  1944. tmpref:=ref;
  1945. make_simple_ref(list,tmpref);
  1946. {$ifdef x86_64}
  1947. { x86_64 only supports signed 32 bits constants directly }
  1948. if (size in [OS_S64,OS_64]) and
  1949. ((a<low(longint)) or (a>high(longint))) then
  1950. begin
  1951. tmpreg:=getintregister(list,size);
  1952. a_load_const_reg(list,size,a,tmpreg);
  1953. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  1954. exit;
  1955. end;
  1956. {$endif x86_64}
  1957. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  1958. a_jmp_cond(list,cmp_op,l);
  1959. end;
  1960. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  1961. reg1,reg2 : tregister;l : tasmlabel);
  1962. begin
  1963. check_register_size(size,reg1);
  1964. check_register_size(size,reg2);
  1965. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  1966. a_jmp_cond(list,cmp_op,l);
  1967. end;
  1968. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  1969. var
  1970. tmpref : treference;
  1971. begin
  1972. tmpref:=ref;
  1973. make_simple_ref(list,tmpref);
  1974. check_register_size(size,reg);
  1975. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  1976. a_jmp_cond(list,cmp_op,l);
  1977. end;
  1978. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  1979. var
  1980. tmpref : treference;
  1981. begin
  1982. tmpref:=ref;
  1983. make_simple_ref(list,tmpref);
  1984. check_register_size(size,reg);
  1985. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  1986. a_jmp_cond(list,cmp_op,l);
  1987. end;
  1988. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1989. var
  1990. ai : taicpu;
  1991. begin
  1992. if cond=OC_None then
  1993. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1994. else
  1995. begin
  1996. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  1997. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1998. end;
  1999. ai.is_jmp:=true;
  2000. list.concat(ai);
  2001. end;
  2002. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  2003. var
  2004. ai : taicpu;
  2005. hl : tasmlabel;
  2006. f2 : tresflags;
  2007. begin
  2008. hl:=nil;
  2009. f2:=f;
  2010. case f of
  2011. F_FNE:
  2012. begin
  2013. ai:=Taicpu.op_sym(A_Jcc,S_NO,l);
  2014. ai.SetCondition(C_P);
  2015. ai.is_jmp:=true;
  2016. list.concat(ai);
  2017. f2:=F_NE;
  2018. end;
  2019. F_FE,F_FA,F_FAE,F_FB,F_FBE:
  2020. begin
  2021. { JP before JA/JAE is redundant, but it must be generated here
  2022. and left for peephole optimizer to remove. }
  2023. current_asmdata.getjumplabel(hl);
  2024. ai:=Taicpu.op_sym(A_Jcc,S_NO,hl);
  2025. ai.SetCondition(C_P);
  2026. ai.is_jmp:=true;
  2027. list.concat(ai);
  2028. f2:=FPUFlags2Flags[f];
  2029. end;
  2030. end;
  2031. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  2032. ai.SetCondition(flags_to_cond(f2));
  2033. ai.is_jmp := true;
  2034. list.concat(ai);
  2035. if assigned(hl) then
  2036. a_label(list,hl);
  2037. end;
  2038. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  2039. var
  2040. ai : taicpu;
  2041. f2 : tresflags;
  2042. hreg,hreg2 : tregister;
  2043. op: tasmop;
  2044. begin
  2045. hreg2:=NR_NO;
  2046. op:=A_AND;
  2047. f2:=f;
  2048. case f of
  2049. F_FE,F_FNE,F_FB,F_FBE:
  2050. begin
  2051. hreg2:=getintregister(list,OS_8);
  2052. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg2);
  2053. if (f=F_FNE) then { F_FNE means "PF or (not ZF)" }
  2054. begin
  2055. ai.setcondition(C_P);
  2056. op:=A_OR;
  2057. end
  2058. else
  2059. ai.setcondition(C_NP);
  2060. list.concat(ai);
  2061. f2:=FPUFlags2Flags[f];
  2062. end;
  2063. F_FA,F_FAE: { These do not need PF check }
  2064. f2:=FPUFlags2Flags[f];
  2065. end;
  2066. hreg:=makeregsize(list,reg,OS_8);
  2067. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  2068. ai.setcondition(flags_to_cond(f2));
  2069. list.concat(ai);
  2070. if (hreg2<>NR_NO) then
  2071. list.concat(taicpu.op_reg_reg(op,S_B,hreg2,hreg));
  2072. if reg<>hreg then
  2073. a_load_reg_reg(list,OS_8,size,hreg,reg);
  2074. end;
  2075. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  2076. var
  2077. ai : taicpu;
  2078. tmpref : treference;
  2079. f2 : tresflags;
  2080. begin
  2081. f2:=f;
  2082. case f of
  2083. F_FE,F_FNE,F_FB,F_FBE:
  2084. begin
  2085. inherited g_flags2ref(list,size,f,ref);
  2086. exit;
  2087. end;
  2088. F_FA,F_FAE:
  2089. f2:=FPUFlags2Flags[f];
  2090. end;
  2091. tmpref:=ref;
  2092. make_simple_ref(list,tmpref);
  2093. if not(size in [OS_8,OS_S8]) then
  2094. a_load_const_ref(list,size,0,tmpref);
  2095. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  2096. ai.setcondition(flags_to_cond(f2));
  2097. list.concat(ai);
  2098. {$ifndef cpu64bitalu}
  2099. if size in [OS_S64,OS_64] then
  2100. begin
  2101. inc(tmpref.offset,4);
  2102. a_load_const_ref(list,OS_32,0,tmpref);
  2103. end;
  2104. {$endif cpu64bitalu}
  2105. end;
  2106. { ************* concatcopy ************ }
  2107. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:tcgint);
  2108. const
  2109. {$if defined(cpu64bitalu)}
  2110. REGCX=NR_RCX;
  2111. REGSI=NR_RSI;
  2112. REGDI=NR_RDI;
  2113. copy_len_sizes = [1, 2, 4, 8];
  2114. push_segment_size = S_L;
  2115. {$elseif defined(cpu32bitalu)}
  2116. REGCX=NR_ECX;
  2117. REGSI=NR_ESI;
  2118. REGDI=NR_EDI;
  2119. copy_len_sizes = [1, 2, 4];
  2120. push_segment_size = S_L;
  2121. {$elseif defined(cpu16bitalu)}
  2122. REGCX=NR_CX;
  2123. REGSI=NR_SI;
  2124. REGDI=NR_DI;
  2125. copy_len_sizes = [1, 2, 4]; { 4 is included here, because it's still more
  2126. efficient to use copy_move instead of copy_string for copying 4 bytes }
  2127. push_segment_size = S_W;
  2128. {$endif}
  2129. type copymode=(copy_move,copy_mmx,copy_string,copy_mm,copy_avx);
  2130. var srcref,dstref:Treference;
  2131. r,r0,r1,r2,r3:Tregister;
  2132. helpsize:tcgint;
  2133. copysize:byte;
  2134. cgsize:Tcgsize;
  2135. cm:copymode;
  2136. saved_ds,saved_es: Boolean;
  2137. begin
  2138. srcref:=source;
  2139. dstref:=dest;
  2140. {$ifndef i8086}
  2141. make_simple_ref(list,srcref);
  2142. make_simple_ref(list,dstref);
  2143. {$endif not i8086}
  2144. cm:=copy_move;
  2145. helpsize:=3*sizeof(aword);
  2146. if cs_opt_size in current_settings.optimizerswitches then
  2147. helpsize:=2*sizeof(aword);
  2148. {$ifndef i8086}
  2149. { avx helps only to reduce size, using it in general does at least not help on
  2150. an i7-4770 (FK) }
  2151. if (CPUX86_HAS_AVXUNIT in cpu_capabilities[current_settings.cputype]) and
  2152. // (cs_opt_size in current_settings.optimizerswitches) and
  2153. ((len=8) or (len=16) or (len=24) or (len=32) { or (len=40) or (len=48)}) then
  2154. cm:=copy_avx
  2155. else
  2156. {$ifdef dummy}
  2157. { I'am not sure what CPUs would benefit from using sse instructions for moves (FK) }
  2158. if
  2159. {$ifdef x86_64}
  2160. ((current_settings.fputype>=fpu_sse64)
  2161. {$else x86_64}
  2162. ((current_settings.fputype>=fpu_sse)
  2163. {$endif x86_64}
  2164. or (CPUX86_HAS_SSEUNIT in cpu_capabilities[current_settings.cputype])) and
  2165. ((len=8) or (len=16) or (len=24) or (len=32) or (len=40) or (len=48)) then
  2166. cm:=copy_mm
  2167. else
  2168. {$endif dummy}
  2169. {$endif i8086}
  2170. if (cs_mmx in current_settings.localswitches) and
  2171. not(pi_uses_fpu in current_procinfo.flags) and
  2172. ((len=8) or (len=16) or (len=24) or (len=32)) then
  2173. cm:=copy_mmx;
  2174. if (len>helpsize) then
  2175. cm:=copy_string;
  2176. if (cs_opt_size in current_settings.optimizerswitches) and
  2177. not((len<=16) and (cm in [copy_mmx,copy_mm,copy_avx])) and
  2178. not(len in copy_len_sizes) then
  2179. cm:=copy_string;
  2180. {$ifndef i8086}
  2181. if (srcref.segment<>NR_NO) or
  2182. (dstref.segment<>NR_NO) then
  2183. cm:=copy_string;
  2184. {$endif not i8086}
  2185. case cm of
  2186. copy_move:
  2187. begin
  2188. copysize:=sizeof(aint);
  2189. cgsize:=int_cgsize(copysize);
  2190. while len<>0 do
  2191. begin
  2192. if len<2 then
  2193. begin
  2194. copysize:=1;
  2195. cgsize:=OS_8;
  2196. end
  2197. else if len<4 then
  2198. begin
  2199. copysize:=2;
  2200. cgsize:=OS_16;
  2201. end
  2202. {$if defined(cpu32bitalu) or defined(cpu64bitalu)}
  2203. else if len<8 then
  2204. begin
  2205. copysize:=4;
  2206. cgsize:=OS_32;
  2207. end
  2208. {$endif cpu32bitalu or cpu64bitalu}
  2209. {$ifdef cpu64bitalu}
  2210. else if len<16 then
  2211. begin
  2212. copysize:=8;
  2213. cgsize:=OS_64;
  2214. end
  2215. {$endif}
  2216. ;
  2217. dec(len,copysize);
  2218. r:=getintregister(list,cgsize);
  2219. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2220. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2221. inc(srcref.offset,copysize);
  2222. inc(dstref.offset,copysize);
  2223. end;
  2224. end;
  2225. copy_mmx:
  2226. begin
  2227. r0:=getmmxregister(list);
  2228. r1:=NR_NO;
  2229. r2:=NR_NO;
  2230. r3:=NR_NO;
  2231. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  2232. if len>=16 then
  2233. begin
  2234. inc(srcref.offset,8);
  2235. r1:=getmmxregister(list);
  2236. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  2237. end;
  2238. if len>=24 then
  2239. begin
  2240. inc(srcref.offset,8);
  2241. r2:=getmmxregister(list);
  2242. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  2243. end;
  2244. if len>=32 then
  2245. begin
  2246. inc(srcref.offset,8);
  2247. r3:=getmmxregister(list);
  2248. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2249. end;
  2250. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  2251. if len>=16 then
  2252. begin
  2253. inc(dstref.offset,8);
  2254. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  2255. end;
  2256. if len>=24 then
  2257. begin
  2258. inc(dstref.offset,8);
  2259. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  2260. end;
  2261. if len>=32 then
  2262. begin
  2263. inc(dstref.offset,8);
  2264. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2265. end;
  2266. end;
  2267. copy_mm:
  2268. begin
  2269. r0:=NR_NO;
  2270. r1:=NR_NO;
  2271. r2:=NR_NO;
  2272. r3:=NR_NO;
  2273. if len>=16 then
  2274. begin
  2275. r0:=getmmregister(list,OS_M128);
  2276. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r0,nil);
  2277. inc(srcref.offset,16);
  2278. end;
  2279. if len>=32 then
  2280. begin
  2281. r1:=getmmregister(list,OS_M128);
  2282. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r1,nil);
  2283. inc(srcref.offset,16);
  2284. end;
  2285. if len>=48 then
  2286. begin
  2287. r2:=getmmregister(list,OS_M128);
  2288. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r2,nil);
  2289. inc(srcref.offset,16);
  2290. end;
  2291. if (len=8) or (len=24) or (len=40) then
  2292. begin
  2293. r3:=getmmregister(list,OS_M64);
  2294. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2295. end;
  2296. if len>=16 then
  2297. begin
  2298. a_loadmm_reg_ref(list,OS_M128,OS_M128,r0,dstref,nil);
  2299. inc(dstref.offset,16);
  2300. end;
  2301. if len>=32 then
  2302. begin
  2303. a_loadmm_reg_ref(list,OS_M128,OS_M128,r1,dstref,nil);
  2304. inc(dstref.offset,16);
  2305. end;
  2306. if len>=48 then
  2307. begin
  2308. a_loadmm_reg_ref(list,OS_M128,OS_M128,r2,dstref,nil);
  2309. inc(dstref.offset,16);
  2310. end;
  2311. if (len=8) or (len=24) or (len=40) then
  2312. begin
  2313. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2314. end;
  2315. end;
  2316. copy_avx:
  2317. begin
  2318. r0:=NR_NO;
  2319. r1:=NR_NO;
  2320. r2:=NR_NO;
  2321. r3:=NR_NO;
  2322. if len>=16 then
  2323. begin
  2324. r0:=getmmregister(list,OS_M128);
  2325. { we want to force the use of vmovups, so do not use a_loadmm_ref_reg }
  2326. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r0));
  2327. inc(srcref.offset,16);
  2328. end;
  2329. if len>=32 then
  2330. begin
  2331. r1:=getmmregister(list,OS_M128);
  2332. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r1));
  2333. inc(srcref.offset,16);
  2334. end;
  2335. if len>=48 then
  2336. begin
  2337. r2:=getmmregister(list,OS_M128);
  2338. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r2));
  2339. inc(srcref.offset,16);
  2340. end;
  2341. if (len=8) or (len=24) or (len=40) then
  2342. begin
  2343. r3:=getmmregister(list,OS_M64);
  2344. list.concat(taicpu.op_ref_reg(A_VMOVSD,S_NO,srcref,r3));
  2345. end;
  2346. if len>=16 then
  2347. begin
  2348. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r0,dstref));
  2349. inc(dstref.offset,16);
  2350. end;
  2351. if len>=32 then
  2352. begin
  2353. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r1,dstref));
  2354. inc(dstref.offset,16);
  2355. end;
  2356. if len>=48 then
  2357. begin
  2358. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r2,dstref));
  2359. inc(dstref.offset,16);
  2360. end;
  2361. if (len=8) or (len=24) or (len=40) then
  2362. begin
  2363. list.concat(taicpu.op_reg_ref(A_VMOVSD,S_NO,r3,dstref));
  2364. end;
  2365. end
  2366. else {copy_string, should be a good fallback in case of unhandled}
  2367. begin
  2368. getcpuregister(list,REGDI);
  2369. if (dest.segment=NR_NO) and
  2370. (segment_regs_equal(NR_SS,NR_DS) or ((dest.base<>NR_BP) and (dest.base<>NR_SP))) then
  2371. begin
  2372. a_loadaddr_ref_reg(list,dstref,REGDI);
  2373. saved_es:=false;
  2374. {$ifdef volatile_es}
  2375. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2376. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2377. {$endif volatile_es}
  2378. end
  2379. else
  2380. begin
  2381. dstref.segment:=NR_NO;
  2382. a_loadaddr_ref_reg(list,dstref,REGDI);
  2383. {$ifdef volatile_es}
  2384. saved_es:=false;
  2385. {$else volatile_es}
  2386. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_ES));
  2387. saved_es:=true;
  2388. {$endif volatile_es}
  2389. if dest.segment<>NR_NO then
  2390. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,dest.segment))
  2391. else if (dest.base=NR_BP) or (dest.base=NR_SP) then
  2392. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2393. else
  2394. internalerror(2014040401);
  2395. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2396. end;
  2397. getcpuregister(list,REGSI);
  2398. if ((source.segment=NR_NO) and (segment_regs_equal(NR_SS,NR_DS) or ((source.base<>NR_BP) and (source.base<>NR_SP)))) or
  2399. (is_segment_reg(source.segment) and segment_regs_equal(source.segment,NR_DS)) then
  2400. begin
  2401. srcref.segment:=NR_NO;
  2402. a_loadaddr_ref_reg(list,srcref,REGSI);
  2403. saved_ds:=false;
  2404. end
  2405. else
  2406. begin
  2407. srcref.segment:=NR_NO;
  2408. a_loadaddr_ref_reg(list,srcref,REGSI);
  2409. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2410. saved_ds:=true;
  2411. if source.segment<>NR_NO then
  2412. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,source.segment))
  2413. else if (source.base=NR_BP) or (source.base=NR_SP) then
  2414. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2415. else
  2416. internalerror(2014040402);
  2417. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2418. end;
  2419. getcpuregister(list,REGCX);
  2420. if ts_cld in current_settings.targetswitches then
  2421. list.concat(Taicpu.op_none(A_CLD,S_NO));
  2422. if (cs_opt_size in current_settings.optimizerswitches) and
  2423. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  2424. begin
  2425. a_load_const_reg(list,OS_INT,len,REGCX);
  2426. list.concat(Taicpu.op_none(A_REP,S_NO));
  2427. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2428. end
  2429. else
  2430. begin
  2431. helpsize:=len div sizeof(aint);
  2432. len:=len mod sizeof(aint);
  2433. if helpsize>1 then
  2434. begin
  2435. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  2436. list.concat(Taicpu.op_none(A_REP,S_NO));
  2437. end;
  2438. if helpsize>0 then
  2439. begin
  2440. {$if defined(cpu64bitalu)}
  2441. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  2442. {$elseif defined(cpu32bitalu)}
  2443. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2444. {$elseif defined(cpu16bitalu)}
  2445. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2446. {$endif}
  2447. end;
  2448. if len>=4 then
  2449. begin
  2450. dec(len,4);
  2451. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2452. end;
  2453. if len>=2 then
  2454. begin
  2455. dec(len,2);
  2456. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2457. end;
  2458. if len=1 then
  2459. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2460. end;
  2461. ungetcpuregister(list,REGCX);
  2462. ungetcpuregister(list,REGSI);
  2463. ungetcpuregister(list,REGDI);
  2464. if saved_ds then
  2465. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2466. if saved_es then
  2467. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2468. end;
  2469. end;
  2470. end;
  2471. {****************************************************************************
  2472. Entry/Exit Code Helpers
  2473. ****************************************************************************}
  2474. procedure tcgx86.g_profilecode(list : TAsmList);
  2475. var
  2476. pl : tasmlabel;
  2477. mcountprefix : String[4];
  2478. begin
  2479. case target_info.system of
  2480. {$ifndef NOTARGETWIN}
  2481. system_i386_win32,
  2482. {$endif}
  2483. system_i386_freebsd,
  2484. system_i386_netbsd,
  2485. // system_i386_openbsd,
  2486. system_i386_wdosx :
  2487. begin
  2488. Case target_info.system Of
  2489. system_i386_freebsd : mcountprefix:='.';
  2490. system_i386_netbsd : mcountprefix:='__';
  2491. // system_i386_openbsd : mcountprefix:='.';
  2492. else
  2493. mcountPrefix:='';
  2494. end;
  2495. current_asmdata.getaddrlabel(pl);
  2496. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(pint));
  2497. list.concat(Tai_label.Create(pl));
  2498. list.concat(Tai_const.Create_32bit(0));
  2499. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  2500. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2501. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  2502. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount',false);
  2503. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  2504. end;
  2505. system_i386_linux:
  2506. a_call_name(list,target_info.Cprefix+'mcount',false);
  2507. system_i386_go32v2,system_i386_watcom:
  2508. begin
  2509. a_call_name(list,'MCOUNT',false);
  2510. end;
  2511. system_x86_64_linux,
  2512. system_x86_64_darwin,
  2513. system_x86_64_iphonesim:
  2514. begin
  2515. a_call_name(list,'mcount',false);
  2516. end;
  2517. end;
  2518. end;
  2519. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2520. procedure decrease_sp(a : tcgint);
  2521. var
  2522. href : treference;
  2523. begin
  2524. reference_reset_base(href,NR_STACK_POINTER_REG,-a,0);
  2525. { normally, lea is a better choice than a sub to adjust the stack pointer }
  2526. list.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_STACK_POINTER_REG));
  2527. end;
  2528. {$ifdef x86}
  2529. {$ifndef NOTARGETWIN}
  2530. var
  2531. href : treference;
  2532. i : integer;
  2533. again : tasmlabel;
  2534. {$endif NOTARGETWIN}
  2535. {$endif x86}
  2536. begin
  2537. if localsize>0 then
  2538. begin
  2539. {$ifdef i386}
  2540. {$ifndef NOTARGETWIN}
  2541. { windows guards only a few pages for stack growing,
  2542. so we have to access every page first }
  2543. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  2544. (localsize>=winstackpagesize) then
  2545. begin
  2546. if localsize div winstackpagesize<=5 then
  2547. begin
  2548. decrease_sp(localsize-4);
  2549. for i:=1 to localsize div winstackpagesize do
  2550. begin
  2551. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize,4);
  2552. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2553. end;
  2554. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2555. end
  2556. else
  2557. begin
  2558. current_asmdata.getjumplabel(again);
  2559. { Using a_reg_alloc instead of getcpuregister, so this procedure
  2560. does not change "used_in_proc" state of EDI and therefore can be
  2561. called after saving registers with "push" instruction
  2562. without creating an unbalanced "pop edi" in epilogue }
  2563. a_reg_alloc(list,NR_EDI);
  2564. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  2565. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  2566. a_label(list,again);
  2567. decrease_sp(winstackpagesize-4);
  2568. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2569. if UseIncDec then
  2570. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI))
  2571. else
  2572. list.concat(Taicpu.op_const_reg(A_SUB,S_L,1,NR_EDI));
  2573. a_jmp_cond(list,OC_NE,again);
  2574. decrease_sp(localsize mod winstackpagesize-4);
  2575. reference_reset_base(href,NR_ESP,localsize-4,4);
  2576. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  2577. a_reg_dealloc(list,NR_EDI);
  2578. end
  2579. end
  2580. else
  2581. {$endif NOTARGETWIN}
  2582. {$endif i386}
  2583. {$ifdef x86_64}
  2584. {$ifndef NOTARGETWIN}
  2585. { windows guards only a few pages for stack growing,
  2586. so we have to access every page first }
  2587. if (target_info.system=system_x86_64_win64) and
  2588. (localsize>=winstackpagesize) then
  2589. begin
  2590. if localsize div winstackpagesize<=5 then
  2591. begin
  2592. decrease_sp(localsize);
  2593. for i:=1 to localsize div winstackpagesize do
  2594. begin
  2595. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4,4);
  2596. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2597. end;
  2598. reference_reset_base(href,NR_RSP,0,4);
  2599. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2600. end
  2601. else
  2602. begin
  2603. current_asmdata.getjumplabel(again);
  2604. getcpuregister(list,NR_R10);
  2605. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  2606. a_label(list,again);
  2607. decrease_sp(winstackpagesize);
  2608. reference_reset_base(href,NR_RSP,0,4);
  2609. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2610. if UseIncDec then
  2611. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10))
  2612. else
  2613. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,1,NR_R10));
  2614. a_jmp_cond(list,OC_NE,again);
  2615. decrease_sp(localsize mod winstackpagesize);
  2616. ungetcpuregister(list,NR_R10);
  2617. end
  2618. end
  2619. else
  2620. {$endif NOTARGETWIN}
  2621. {$endif x86_64}
  2622. decrease_sp(localsize);
  2623. end;
  2624. end;
  2625. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  2626. var
  2627. stackmisalignment: longint;
  2628. regsize: longint;
  2629. {$ifdef i8086}
  2630. dgroup: treference;
  2631. fardataseg: treference;
  2632. {$endif i8086}
  2633. procedure push_regs;
  2634. var
  2635. r: longint;
  2636. usedregs: tcpuregisterset;
  2637. begin
  2638. regsize:=0;
  2639. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  2640. for r := low(saved_standard_registers) to high(saved_standard_registers) do
  2641. if saved_standard_registers[r] in usedregs then
  2642. begin
  2643. inc(regsize,sizeof(aint));
  2644. list.concat(Taicpu.Op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE)));
  2645. end;
  2646. end;
  2647. begin
  2648. {$ifdef i8086}
  2649. { Win16 callback/exported proc prologue support.
  2650. Since callbacks can be called from different modules, DS on entry may be
  2651. initialized with the data segment of a different module, so we need to
  2652. get ours. But we can't do
  2653. push ds
  2654. mov ax, dgroup
  2655. mov ds, ax
  2656. because code segments are shared between different instances of the same
  2657. module (which have different instances of the current program's data segment),
  2658. so the same 'mov ax, dgroup' instruction will be used for all instances
  2659. of the program and it will load the same segment into ax.
  2660. So, the standard win16 prologue looks like this:
  2661. mov ax, ds
  2662. nop
  2663. inc bp
  2664. push bp
  2665. mov bp, sp
  2666. push ds
  2667. mov ds, ax
  2668. By default, this does nothing, except wasting a few extra machine cycles and
  2669. destroying ax in the process. However, Windows checks the first three bytes
  2670. of every exported function and if they are 'mov ax,ds/nop', they are replaced
  2671. with nop/nop/nop. Then the MakeProcInstance api call should be used to create
  2672. a thunk that loads ds for the current program instance in ax before calling
  2673. the routine.
  2674. And now the fun part comes: somebody (Michael Geary) figured out that all this
  2675. crap was unnecessary, because in Win16 exe modules, we always have DS=SS, so we
  2676. can simply initialize DS from SS :) And then calling MakeProcInstance becomes
  2677. unnecessary. This is what "smart callbacks" (cs_win16_smartcallbacks) do. However,
  2678. this only works for exe files, not for dlls, because dlls run with DS<>SS. There's
  2679. another solution for dlls - since win16 dlls only have a single instance of their
  2680. data segment, we can initialize ds from dgroup. However, there's not a single
  2681. solution for both exe and dlls, so we don't know what to use e.g. in a unit. So,
  2682. that's why there's still an option to turn smart callbacks off and go the
  2683. MakeProcInstance way.
  2684. Additional details here: http://www.geary.com/fixds.html }
  2685. if (current_settings.x86memorymodel<>mm_huge) and
  2686. (po_exports in current_procinfo.procdef.procoptions) and
  2687. (target_info.system=system_i8086_win16) then
  2688. begin
  2689. if cs_win16_smartcallbacks in current_settings.moduleswitches then
  2690. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_SS,NR_AX))
  2691. else
  2692. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_DS,NR_AX));
  2693. list.concat(Taicpu.op_none(A_NOP));
  2694. end
  2695. { interrupt support for i8086 }
  2696. else if po_interrupt in current_procinfo.procdef.procoptions then
  2697. begin
  2698. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_AX));
  2699. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_BX));
  2700. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CX));
  2701. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DX));
  2702. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  2703. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  2704. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  2705. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  2706. if current_settings.x86memorymodel=mm_tiny then
  2707. begin
  2708. { in the tiny memory model, we can't use dgroup, because that
  2709. adds a relocation entry to the .exe and we can't produce a
  2710. .com file (because they don't support relactions), so instead
  2711. we initialize DS from CS. }
  2712. if cs_opt_size in current_settings.optimizerswitches then
  2713. begin
  2714. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CS));
  2715. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  2716. end
  2717. else
  2718. begin
  2719. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_CS,NR_AX));
  2720. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2721. end;
  2722. end
  2723. else if current_settings.x86memorymodel=mm_huge then
  2724. begin
  2725. reference_reset(fardataseg,0);
  2726. fardataseg.refaddr:=addr_fardataseg;
  2727. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  2728. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2729. end
  2730. else
  2731. begin
  2732. reference_reset(dgroup,0);
  2733. dgroup.refaddr:=addr_dgroup;
  2734. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,dgroup,NR_AX));
  2735. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2736. end;
  2737. end;
  2738. {$endif i8086}
  2739. {$ifdef i386}
  2740. { interrupt support for i386 }
  2741. if (po_interrupt in current_procinfo.procdef.procoptions) and
  2742. { this messes up stack alignment }
  2743. not(target_info.system in [system_i386_darwin,system_i386_iphonesim,system_i386_android]) then
  2744. begin
  2745. { .... also the segment registers }
  2746. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  2747. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  2748. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  2749. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  2750. { save the registers of an interrupt procedure }
  2751. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  2752. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  2753. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2754. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  2755. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  2756. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  2757. end;
  2758. {$endif i386}
  2759. { save old framepointer }
  2760. if not nostackframe then
  2761. begin
  2762. { return address }
  2763. stackmisalignment := sizeof(pint);
  2764. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  2765. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  2766. begin
  2767. {$ifdef i386}
  2768. if (not paramanager.use_fixed_stack) then
  2769. push_regs;
  2770. {$endif i386}
  2771. CGmessage(cg_d_stackframe_omited);
  2772. end
  2773. else
  2774. begin
  2775. {$ifdef i8086}
  2776. if ((ts_x86_far_procs_push_odd_bp in current_settings.targetswitches) or
  2777. ((po_exports in current_procinfo.procdef.procoptions) and
  2778. (target_info.system=system_i8086_win16))) and
  2779. is_proc_far(current_procinfo.procdef) then
  2780. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,1,current_procinfo.framepointer);
  2781. {$endif i8086}
  2782. { push <frame_pointer> }
  2783. inc(stackmisalignment,sizeof(pint));
  2784. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  2785. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  2786. { Return address and FP are both on stack }
  2787. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(pint));
  2788. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(pint)));
  2789. if current_procinfo.procdef.proctypeoption<>potype_exceptfilter then
  2790. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG))
  2791. else
  2792. begin
  2793. push_regs;
  2794. gen_load_frame_for_exceptfilter(list);
  2795. { Need only as much stack space as necessary to do the calls.
  2796. Exception filters don't have own local vars, and temps are 'mapped'
  2797. to the parent procedure.
  2798. maxpushedparasize is already aligned at least on x86_64. }
  2799. localsize:=current_procinfo.maxpushedparasize;
  2800. end;
  2801. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  2802. end;
  2803. { allocate stackframe space }
  2804. if (localsize<>0) or
  2805. ((target_info.stackalign>sizeof(pint)) and
  2806. (stackmisalignment <> 0) and
  2807. ((pi_do_call in current_procinfo.flags) or
  2808. (po_assembler in current_procinfo.procdef.procoptions))) then
  2809. begin
  2810. if target_info.stackalign>sizeof(pint) then
  2811. localsize := align(localsize+stackmisalignment,target_info.stackalign)-stackmisalignment;
  2812. g_stackpointer_alloc(list,localsize);
  2813. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  2814. current_asmdata.asmcfi.cfa_def_cfa_offset(list,localsize+sizeof(pint));
  2815. current_procinfo.final_localsize:=localsize;
  2816. end
  2817. {$ifdef i8086}
  2818. else
  2819. { on i8086 we always call g_stackpointer_alloc, even with a zero size,
  2820. because it will generate code for stack checking, if stack checking is on }
  2821. g_stackpointer_alloc(list,0)
  2822. {$endif i8086}
  2823. ;
  2824. {$ifdef i8086}
  2825. { win16 exported proc prologue follow-up (see the huge comment above for details) }
  2826. if (current_settings.x86memorymodel<>mm_huge) and
  2827. (po_exports in current_procinfo.procdef.procoptions) and
  2828. (target_info.system=system_i8086_win16) then
  2829. begin
  2830. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  2831. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2832. end
  2833. else if (current_settings.x86memorymodel=mm_huge) and
  2834. not (po_interrupt in current_procinfo.procdef.procoptions) then
  2835. begin
  2836. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  2837. reference_reset(fardataseg,0);
  2838. fardataseg.refaddr:=addr_fardataseg;
  2839. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  2840. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2841. end;
  2842. { SI and DI are volatile in the BP7 and FPC's pascal calling convention,
  2843. but must be preserved in Microsoft C's pascal calling convention, and
  2844. since Windows is compiled with Microsoft compilers, these registers
  2845. must be saved for exported procedures (BP7 for Win16 also does this). }
  2846. if (po_exports in current_procinfo.procdef.procoptions) and
  2847. (target_info.system=system_i8086_win16) then
  2848. begin
  2849. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  2850. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  2851. end;
  2852. {$endif i8086}
  2853. {$ifdef i386}
  2854. if (not paramanager.use_fixed_stack) and
  2855. (current_procinfo.framepointer<>NR_STACK_POINTER_REG) and
  2856. (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  2857. begin
  2858. regsize:=0;
  2859. push_regs;
  2860. reference_reset_base(current_procinfo.save_regs_ref,
  2861. current_procinfo.framepointer,
  2862. -(localsize+regsize),sizeof(aint));
  2863. end;
  2864. {$endif i386}
  2865. end;
  2866. end;
  2867. procedure tcgx86.g_save_registers(list: TAsmList);
  2868. begin
  2869. {$ifdef i386}
  2870. if paramanager.use_fixed_stack then
  2871. {$endif i386}
  2872. inherited g_save_registers(list);
  2873. end;
  2874. procedure tcgx86.g_restore_registers(list: TAsmList);
  2875. begin
  2876. {$ifdef i386}
  2877. if paramanager.use_fixed_stack then
  2878. {$endif i386}
  2879. inherited g_restore_registers(list);
  2880. end;
  2881. procedure tcgx86.internal_restore_regs(list: TAsmList; use_pop: boolean);
  2882. var
  2883. r: longint;
  2884. hreg: tregister;
  2885. href: treference;
  2886. usedregs: tcpuregisterset;
  2887. begin
  2888. href:=current_procinfo.save_regs_ref;
  2889. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  2890. for r:=high(saved_standard_registers) downto low(saved_standard_registers) do
  2891. if saved_standard_registers[r] in usedregs then
  2892. begin
  2893. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  2894. { Allocate register so the optimizer does not remove the load }
  2895. a_reg_alloc(list,hreg);
  2896. if use_pop then
  2897. list.concat(Taicpu.Op_reg(A_POP,tcgsize2opsize[OS_ADDR],hreg))
  2898. else
  2899. begin
  2900. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2901. inc(href.offset,sizeof(aint));
  2902. end;
  2903. end;
  2904. end;
  2905. procedure tcgx86.generate_leave(list: TAsmList);
  2906. begin
  2907. if UseLeave then
  2908. list.concat(taicpu.op_none(A_LEAVE,S_NO))
  2909. else
  2910. begin
  2911. {$if defined(x86_64)}
  2912. list.Concat(taicpu.op_reg_reg(A_MOV,S_Q,NR_RBP,NR_RSP));
  2913. list.Concat(taicpu.op_reg(A_POP,S_Q,NR_RBP));
  2914. {$elseif defined(i386)}
  2915. list.Concat(taicpu.op_reg_reg(A_MOV,S_L,NR_EBP,NR_ESP));
  2916. list.Concat(taicpu.op_reg(A_POP,S_L,NR_EBP));
  2917. {$elseif defined(i8086)}
  2918. list.Concat(taicpu.op_reg_reg(A_MOV,S_W,NR_BP,NR_SP));
  2919. list.Concat(taicpu.op_reg(A_POP,S_W,NR_BP));
  2920. {$endif}
  2921. end;
  2922. end;
  2923. { produces if necessary overflowcode }
  2924. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  2925. var
  2926. hl : tasmlabel;
  2927. ai : taicpu;
  2928. cond : TAsmCond;
  2929. begin
  2930. if not(cs_check_overflow in current_settings.localswitches) then
  2931. exit;
  2932. current_asmdata.getjumplabel(hl);
  2933. if not ((def.typ=pointerdef) or
  2934. ((def.typ=orddef) and
  2935. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  2936. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  2937. cond:=C_NO
  2938. else
  2939. cond:=C_NB;
  2940. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  2941. ai.SetCondition(cond);
  2942. ai.is_jmp:=true;
  2943. list.concat(ai);
  2944. a_call_name(list,'FPC_OVERFLOW',false);
  2945. a_label(list,hl);
  2946. end;
  2947. end.