cgx86.pas 136 KB

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