cgx86.pas 122 KB

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