cgx86.pas 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238
  1. {
  2. Copyright (c) 1998-2005 by Florian Klaempfl
  3. This unit implements the common parts of the code generator for the i386 and the x86-64.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { This unit implements the common parts of the code generator for the i386 and the x86-64.
  18. }
  19. unit cgx86;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,
  24. cgbase,cgutils,cgobj,
  25. aasmbase,aasmtai,aasmdata,aasmcpu,
  26. cpubase,cpuinfo,rgobj,rgx86,rgcpu,
  27. symconst,symtype,symdef;
  28. type
  29. { tcgx86 }
  30. tcgx86 = class(tcg)
  31. rgfpu : Trgx86fpu;
  32. procedure done_register_allocators;override;
  33. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;override;
  34. function getmmxregister(list:TAsmList):Tregister;
  35. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;override;
  36. procedure getcpuregister(list:TAsmList;r:Tregister);override;
  37. procedure ungetcpuregister(list:TAsmList;r:Tregister);override;
  38. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  39. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  40. function uses_registers(rt:Tregistertype):boolean;override;
  41. procedure add_reg_instruction(instr:Tai;r:tregister);override;
  42. procedure dec_fpu_stack;
  43. procedure inc_fpu_stack;
  44. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  45. procedure a_call_name_near(list : TAsmList;const s : string; weak: boolean);
  46. procedure a_call_name_static(list : TAsmList;const s : string);override;
  47. procedure a_call_name_static_near(list : TAsmList;const s : string);
  48. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  49. procedure a_call_reg_near(list : TAsmList;reg : tregister);
  50. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  51. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  52. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  53. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  54. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  55. {$ifndef i8086}
  56. procedure a_op_const_reg_reg(list : TAsmList; op : Topcg; size : Tcgsize; a : tcgint; src,dst : Tregister); override;
  57. procedure a_op_reg_reg_reg(list : TAsmList; op : TOpCg; size : tcgsize; src1,src2,dst : tregister); override;
  58. {$endif not i8086}
  59. { move instructions }
  60. procedure a_load_const_reg(list : TAsmList; tosize: tcgsize; a : tcgint;reg : tregister);override;
  61. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  62. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);override;
  63. { 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. defutil,paramgr,procinfo,
  151. tgobj,ncgutil,
  152. fmodule,symsym,symcpu;
  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. check_register_size(size,src);
  1799. check_register_size(size,dst);
  1800. dstsize := tcgsize2opsize[size];
  1801. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  1802. op:=OP_IMUL;
  1803. case op of
  1804. OP_NEG,OP_NOT:
  1805. begin
  1806. if src<>dst then
  1807. a_load_reg_reg(list,size,size,src,dst);
  1808. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  1809. end;
  1810. OP_MUL,OP_DIV,OP_IDIV:
  1811. { special stuff, needs separate handling inside code }
  1812. { generator }
  1813. internalerror(200109233);
  1814. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  1815. begin
  1816. { Use ecx to load the value, that allows better coalescing }
  1817. getcpuregister(list,REGCX);
  1818. a_load_reg_reg(list,size,REGCX_Size,src,REGCX);
  1819. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  1820. ungetcpuregister(list,REGCX);
  1821. end;
  1822. else
  1823. begin
  1824. if reg2opsize(src) <> dstsize then
  1825. internalerror(200109226);
  1826. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  1827. list.concat(instr);
  1828. end;
  1829. end;
  1830. end;
  1831. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1832. var
  1833. tmpref : treference;
  1834. begin
  1835. tmpref:=ref;
  1836. make_simple_ref(list,tmpref);
  1837. check_register_size(size,reg);
  1838. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  1839. op:=OP_IMUL;
  1840. case op of
  1841. OP_NEG,OP_NOT,OP_IMUL:
  1842. begin
  1843. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1844. end;
  1845. OP_MUL,OP_DIV,OP_IDIV:
  1846. { special stuff, needs separate handling inside code }
  1847. { generator }
  1848. internalerror(200109239);
  1849. else
  1850. begin
  1851. reg := makeregsize(list,reg,size);
  1852. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  1853. end;
  1854. end;
  1855. end;
  1856. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1857. var
  1858. tmpref : treference;
  1859. begin
  1860. tmpref:=ref;
  1861. make_simple_ref(list,tmpref);
  1862. if not (op in [OP_NEG,OP_NOT]) then
  1863. check_register_size(size,reg);
  1864. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  1865. op:=OP_IMUL;
  1866. case op of
  1867. OP_NEG,OP_NOT:
  1868. begin
  1869. if reg<>NR_NO then
  1870. internalerror(200109237);
  1871. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  1872. end;
  1873. OP_IMUL:
  1874. begin
  1875. { this one needs a load/imul/store, which is the default }
  1876. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1877. end;
  1878. OP_MUL,OP_DIV,OP_IDIV:
  1879. { special stuff, needs separate handling inside code }
  1880. { generator }
  1881. internalerror(200109238);
  1882. else
  1883. begin
  1884. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  1885. end;
  1886. end;
  1887. end;
  1888. procedure tcgx86.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister);
  1889. var
  1890. tmpreg: tregister;
  1891. opsize: topsize;
  1892. l : TAsmLabel;
  1893. begin
  1894. { no bsf/bsr for byte }
  1895. if srcsize in [OS_8,OS_S8] then
  1896. begin
  1897. tmpreg:=getintregister(list,OS_INT);
  1898. a_load_reg_reg(list,srcsize,OS_INT,src,tmpreg);
  1899. src:=tmpreg;
  1900. srcsize:=OS_INT;
  1901. end;
  1902. { source and destination register must have the same size }
  1903. if tcgsize2size[srcsize]<>tcgsize2size[dstsize] then
  1904. tmpreg:=getintregister(list,srcsize)
  1905. else
  1906. tmpreg:=dst;
  1907. opsize:=tcgsize2opsize[srcsize];
  1908. if not reverse then
  1909. list.concat(taicpu.op_reg_reg(A_BSF,opsize,src,tmpreg))
  1910. else
  1911. list.concat(taicpu.op_reg_reg(A_BSR,opsize,src,tmpreg));
  1912. current_asmdata.getjumplabel(l);
  1913. a_jmp_cond(list,OC_NE,l);
  1914. list.concat(taicpu.op_const_reg(A_MOV,opsize,$ff,tmpreg));
  1915. a_label(list,l);
  1916. if tmpreg<>dst then
  1917. a_load_reg_reg(list,srcsize,dstsize,tmpreg,dst);
  1918. end;
  1919. {*************** compare instructructions ****************}
  1920. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1921. l : tasmlabel);
  1922. {$ifdef x86_64}
  1923. var
  1924. tmpreg : tregister;
  1925. {$endif x86_64}
  1926. begin
  1927. {$ifdef x86_64}
  1928. { x86_64 only supports signed 32 bits constants directly }
  1929. if (size in [OS_S64,OS_64]) and
  1930. ((a<low(longint)) or (a>high(longint))) then
  1931. begin
  1932. tmpreg:=getintregister(list,size);
  1933. a_load_const_reg(list,size,a,tmpreg);
  1934. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1935. exit;
  1936. end;
  1937. {$endif x86_64}
  1938. if (a = 0) then
  1939. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  1940. else
  1941. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  1942. a_jmp_cond(list,cmp_op,l);
  1943. end;
  1944. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  1945. l : tasmlabel);
  1946. var
  1947. {$ifdef x86_64}
  1948. tmpreg : tregister;
  1949. {$endif x86_64}
  1950. tmpref : treference;
  1951. begin
  1952. tmpref:=ref;
  1953. make_simple_ref(list,tmpref);
  1954. {$ifdef x86_64}
  1955. { x86_64 only supports signed 32 bits constants directly }
  1956. if (size in [OS_S64,OS_64]) and
  1957. ((a<low(longint)) or (a>high(longint))) then
  1958. begin
  1959. tmpreg:=getintregister(list,size);
  1960. a_load_const_reg(list,size,a,tmpreg);
  1961. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  1962. exit;
  1963. end;
  1964. {$endif x86_64}
  1965. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  1966. a_jmp_cond(list,cmp_op,l);
  1967. end;
  1968. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  1969. reg1,reg2 : tregister;l : tasmlabel);
  1970. begin
  1971. check_register_size(size,reg1);
  1972. check_register_size(size,reg2);
  1973. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  1974. a_jmp_cond(list,cmp_op,l);
  1975. end;
  1976. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  1977. var
  1978. tmpref : treference;
  1979. begin
  1980. tmpref:=ref;
  1981. make_simple_ref(list,tmpref);
  1982. check_register_size(size,reg);
  1983. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  1984. a_jmp_cond(list,cmp_op,l);
  1985. end;
  1986. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  1987. var
  1988. tmpref : treference;
  1989. begin
  1990. tmpref:=ref;
  1991. make_simple_ref(list,tmpref);
  1992. check_register_size(size,reg);
  1993. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  1994. a_jmp_cond(list,cmp_op,l);
  1995. end;
  1996. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1997. var
  1998. ai : taicpu;
  1999. begin
  2000. if cond=OC_None then
  2001. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  2002. else
  2003. begin
  2004. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  2005. ai.SetCondition(TOpCmp2AsmCond[cond]);
  2006. end;
  2007. ai.is_jmp:=true;
  2008. list.concat(ai);
  2009. end;
  2010. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  2011. var
  2012. ai : taicpu;
  2013. hl : tasmlabel;
  2014. f2 : tresflags;
  2015. begin
  2016. hl:=nil;
  2017. f2:=f;
  2018. case f of
  2019. F_FNE:
  2020. begin
  2021. ai:=Taicpu.op_sym(A_Jcc,S_NO,l);
  2022. ai.SetCondition(C_P);
  2023. ai.is_jmp:=true;
  2024. list.concat(ai);
  2025. f2:=F_NE;
  2026. end;
  2027. F_FE,F_FA,F_FAE,F_FB,F_FBE:
  2028. begin
  2029. { JP before JA/JAE is redundant, but it must be generated here
  2030. and left for peephole optimizer to remove. }
  2031. current_asmdata.getjumplabel(hl);
  2032. ai:=Taicpu.op_sym(A_Jcc,S_NO,hl);
  2033. ai.SetCondition(C_P);
  2034. ai.is_jmp:=true;
  2035. list.concat(ai);
  2036. f2:=FPUFlags2Flags[f];
  2037. end;
  2038. end;
  2039. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  2040. ai.SetCondition(flags_to_cond(f2));
  2041. ai.is_jmp := true;
  2042. list.concat(ai);
  2043. if assigned(hl) then
  2044. a_label(list,hl);
  2045. end;
  2046. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  2047. var
  2048. ai : taicpu;
  2049. f2 : tresflags;
  2050. hreg,hreg2 : tregister;
  2051. op: tasmop;
  2052. begin
  2053. hreg2:=NR_NO;
  2054. op:=A_AND;
  2055. f2:=f;
  2056. case f of
  2057. F_FE,F_FNE,F_FB,F_FBE:
  2058. begin
  2059. hreg2:=getintregister(list,OS_8);
  2060. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg2);
  2061. if (f=F_FNE) then { F_FNE means "PF or (not ZF)" }
  2062. begin
  2063. ai.setcondition(C_P);
  2064. op:=A_OR;
  2065. end
  2066. else
  2067. ai.setcondition(C_NP);
  2068. list.concat(ai);
  2069. f2:=FPUFlags2Flags[f];
  2070. end;
  2071. F_FA,F_FAE: { These do not need PF check }
  2072. f2:=FPUFlags2Flags[f];
  2073. end;
  2074. hreg:=makeregsize(list,reg,OS_8);
  2075. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  2076. ai.setcondition(flags_to_cond(f2));
  2077. list.concat(ai);
  2078. if (hreg2<>NR_NO) then
  2079. list.concat(taicpu.op_reg_reg(op,S_B,hreg2,hreg));
  2080. if reg<>hreg then
  2081. a_load_reg_reg(list,OS_8,size,hreg,reg);
  2082. end;
  2083. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  2084. var
  2085. ai : taicpu;
  2086. tmpref : treference;
  2087. f2 : tresflags;
  2088. begin
  2089. f2:=f;
  2090. case f of
  2091. F_FE,F_FNE,F_FB,F_FBE:
  2092. begin
  2093. inherited g_flags2ref(list,size,f,ref);
  2094. exit;
  2095. end;
  2096. F_FA,F_FAE:
  2097. f2:=FPUFlags2Flags[f];
  2098. end;
  2099. tmpref:=ref;
  2100. make_simple_ref(list,tmpref);
  2101. if not(size in [OS_8,OS_S8]) then
  2102. a_load_const_ref(list,size,0,tmpref);
  2103. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  2104. ai.setcondition(flags_to_cond(f2));
  2105. list.concat(ai);
  2106. {$ifndef cpu64bitalu}
  2107. if size in [OS_S64,OS_64] then
  2108. begin
  2109. inc(tmpref.offset,4);
  2110. a_load_const_ref(list,OS_32,0,tmpref);
  2111. end;
  2112. {$endif cpu64bitalu}
  2113. end;
  2114. { ************* concatcopy ************ }
  2115. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:tcgint);
  2116. const
  2117. {$if defined(cpu64bitalu)}
  2118. REGCX=NR_RCX;
  2119. REGSI=NR_RSI;
  2120. REGDI=NR_RDI;
  2121. copy_len_sizes = [1, 2, 4, 8];
  2122. push_segment_size = S_L;
  2123. {$elseif defined(cpu32bitalu)}
  2124. REGCX=NR_ECX;
  2125. REGSI=NR_ESI;
  2126. REGDI=NR_EDI;
  2127. copy_len_sizes = [1, 2, 4];
  2128. push_segment_size = S_L;
  2129. {$elseif defined(cpu16bitalu)}
  2130. REGCX=NR_CX;
  2131. REGSI=NR_SI;
  2132. REGDI=NR_DI;
  2133. copy_len_sizes = [1, 2, 4]; { 4 is included here, because it's still more
  2134. efficient to use copy_move instead of copy_string for copying 4 bytes }
  2135. push_segment_size = S_W;
  2136. {$endif}
  2137. type copymode=(copy_move,copy_mmx,copy_string,copy_mm,copy_avx);
  2138. var srcref,dstref:Treference;
  2139. r,r0,r1,r2,r3:Tregister;
  2140. helpsize:tcgint;
  2141. copysize:byte;
  2142. cgsize:Tcgsize;
  2143. cm:copymode;
  2144. saved_ds,saved_es: Boolean;
  2145. begin
  2146. srcref:=source;
  2147. dstref:=dest;
  2148. {$ifndef i8086}
  2149. make_simple_ref(list,srcref);
  2150. make_simple_ref(list,dstref);
  2151. {$endif not i8086}
  2152. cm:=copy_move;
  2153. helpsize:=3*sizeof(aword);
  2154. if cs_opt_size in current_settings.optimizerswitches then
  2155. helpsize:=2*sizeof(aword);
  2156. {$ifndef i8086}
  2157. { avx helps only to reduce size, using it in general does at least not help on
  2158. an i7-4770 (FK) }
  2159. if (CPUX86_HAS_AVXUNIT in cpu_capabilities[current_settings.cputype]) and
  2160. // (cs_opt_size in current_settings.optimizerswitches) and
  2161. ((len=8) or (len=16) or (len=24) or (len=32) { or (len=40) or (len=48)}) then
  2162. cm:=copy_avx
  2163. else
  2164. {$ifdef dummy}
  2165. { I'am not sure what CPUs would benefit from using sse instructions for moves (FK) }
  2166. if
  2167. {$ifdef x86_64}
  2168. ((current_settings.fputype>=fpu_sse64)
  2169. {$else x86_64}
  2170. ((current_settings.fputype>=fpu_sse)
  2171. {$endif x86_64}
  2172. or (CPUX86_HAS_SSEUNIT in cpu_capabilities[current_settings.cputype])) and
  2173. ((len=8) or (len=16) or (len=24) or (len=32) or (len=40) or (len=48)) then
  2174. cm:=copy_mm
  2175. else
  2176. {$endif dummy}
  2177. {$endif i8086}
  2178. if (cs_mmx in current_settings.localswitches) and
  2179. not(pi_uses_fpu in current_procinfo.flags) and
  2180. ((len=8) or (len=16) or (len=24) or (len=32)) then
  2181. cm:=copy_mmx;
  2182. if (len>helpsize) then
  2183. cm:=copy_string;
  2184. if (cs_opt_size in current_settings.optimizerswitches) and
  2185. not((len<=16) and (cm in [copy_mmx,copy_mm,copy_avx])) and
  2186. not(len in copy_len_sizes) then
  2187. cm:=copy_string;
  2188. {$ifndef i8086}
  2189. if (srcref.segment<>NR_NO) or
  2190. (dstref.segment<>NR_NO) then
  2191. cm:=copy_string;
  2192. {$endif not i8086}
  2193. case cm of
  2194. copy_move:
  2195. begin
  2196. copysize:=sizeof(aint);
  2197. cgsize:=int_cgsize(copysize);
  2198. while len<>0 do
  2199. begin
  2200. if len<2 then
  2201. begin
  2202. copysize:=1;
  2203. cgsize:=OS_8;
  2204. end
  2205. else if len<4 then
  2206. begin
  2207. copysize:=2;
  2208. cgsize:=OS_16;
  2209. end
  2210. {$if defined(cpu32bitalu) or defined(cpu64bitalu)}
  2211. else if len<8 then
  2212. begin
  2213. copysize:=4;
  2214. cgsize:=OS_32;
  2215. end
  2216. {$endif cpu32bitalu or cpu64bitalu}
  2217. {$ifdef cpu64bitalu}
  2218. else if len<16 then
  2219. begin
  2220. copysize:=8;
  2221. cgsize:=OS_64;
  2222. end
  2223. {$endif}
  2224. ;
  2225. dec(len,copysize);
  2226. r:=getintregister(list,cgsize);
  2227. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2228. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2229. inc(srcref.offset,copysize);
  2230. inc(dstref.offset,copysize);
  2231. end;
  2232. end;
  2233. copy_mmx:
  2234. begin
  2235. r0:=getmmxregister(list);
  2236. r1:=NR_NO;
  2237. r2:=NR_NO;
  2238. r3:=NR_NO;
  2239. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  2240. if len>=16 then
  2241. begin
  2242. inc(srcref.offset,8);
  2243. r1:=getmmxregister(list);
  2244. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  2245. end;
  2246. if len>=24 then
  2247. begin
  2248. inc(srcref.offset,8);
  2249. r2:=getmmxregister(list);
  2250. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  2251. end;
  2252. if len>=32 then
  2253. begin
  2254. inc(srcref.offset,8);
  2255. r3:=getmmxregister(list);
  2256. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2257. end;
  2258. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  2259. if len>=16 then
  2260. begin
  2261. inc(dstref.offset,8);
  2262. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  2263. end;
  2264. if len>=24 then
  2265. begin
  2266. inc(dstref.offset,8);
  2267. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  2268. end;
  2269. if len>=32 then
  2270. begin
  2271. inc(dstref.offset,8);
  2272. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2273. end;
  2274. end;
  2275. copy_mm:
  2276. begin
  2277. r0:=NR_NO;
  2278. r1:=NR_NO;
  2279. r2:=NR_NO;
  2280. r3:=NR_NO;
  2281. if len>=16 then
  2282. begin
  2283. r0:=getmmregister(list,OS_M128);
  2284. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r0,nil);
  2285. inc(srcref.offset,16);
  2286. end;
  2287. if len>=32 then
  2288. begin
  2289. r1:=getmmregister(list,OS_M128);
  2290. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r1,nil);
  2291. inc(srcref.offset,16);
  2292. end;
  2293. if len>=48 then
  2294. begin
  2295. r2:=getmmregister(list,OS_M128);
  2296. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r2,nil);
  2297. inc(srcref.offset,16);
  2298. end;
  2299. if (len=8) or (len=24) or (len=40) then
  2300. begin
  2301. r3:=getmmregister(list,OS_M64);
  2302. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2303. end;
  2304. if len>=16 then
  2305. begin
  2306. a_loadmm_reg_ref(list,OS_M128,OS_M128,r0,dstref,nil);
  2307. inc(dstref.offset,16);
  2308. end;
  2309. if len>=32 then
  2310. begin
  2311. a_loadmm_reg_ref(list,OS_M128,OS_M128,r1,dstref,nil);
  2312. inc(dstref.offset,16);
  2313. end;
  2314. if len>=48 then
  2315. begin
  2316. a_loadmm_reg_ref(list,OS_M128,OS_M128,r2,dstref,nil);
  2317. inc(dstref.offset,16);
  2318. end;
  2319. if (len=8) or (len=24) or (len=40) then
  2320. begin
  2321. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2322. end;
  2323. end;
  2324. copy_avx:
  2325. begin
  2326. r0:=NR_NO;
  2327. r1:=NR_NO;
  2328. r2:=NR_NO;
  2329. r3:=NR_NO;
  2330. if len>=16 then
  2331. begin
  2332. r0:=getmmregister(list,OS_M128);
  2333. { we want to force the use of vmovups, so do not use a_loadmm_ref_reg }
  2334. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r0));
  2335. inc(srcref.offset,16);
  2336. end;
  2337. if len>=32 then
  2338. begin
  2339. r1:=getmmregister(list,OS_M128);
  2340. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r1));
  2341. inc(srcref.offset,16);
  2342. end;
  2343. if len>=48 then
  2344. begin
  2345. r2:=getmmregister(list,OS_M128);
  2346. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r2));
  2347. inc(srcref.offset,16);
  2348. end;
  2349. if (len=8) or (len=24) or (len=40) then
  2350. begin
  2351. r3:=getmmregister(list,OS_M64);
  2352. list.concat(taicpu.op_ref_reg(A_VMOVSD,S_NO,srcref,r3));
  2353. end;
  2354. if len>=16 then
  2355. begin
  2356. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r0,dstref));
  2357. inc(dstref.offset,16);
  2358. end;
  2359. if len>=32 then
  2360. begin
  2361. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r1,dstref));
  2362. inc(dstref.offset,16);
  2363. end;
  2364. if len>=48 then
  2365. begin
  2366. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r2,dstref));
  2367. inc(dstref.offset,16);
  2368. end;
  2369. if (len=8) or (len=24) or (len=40) then
  2370. begin
  2371. list.concat(taicpu.op_reg_ref(A_VMOVSD,S_NO,r3,dstref));
  2372. end;
  2373. end
  2374. else {copy_string, should be a good fallback in case of unhandled}
  2375. begin
  2376. getcpuregister(list,REGDI);
  2377. if (dest.segment=NR_NO) and
  2378. (segment_regs_equal(NR_SS,NR_DS) or ((dest.base<>NR_BP) and (dest.base<>NR_SP))) then
  2379. begin
  2380. a_loadaddr_ref_reg(list,dstref,REGDI);
  2381. saved_es:=false;
  2382. {$ifdef volatile_es}
  2383. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2384. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2385. {$endif volatile_es}
  2386. end
  2387. else
  2388. begin
  2389. dstref.segment:=NR_NO;
  2390. a_loadaddr_ref_reg(list,dstref,REGDI);
  2391. {$ifdef volatile_es}
  2392. saved_es:=false;
  2393. {$else volatile_es}
  2394. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_ES));
  2395. saved_es:=true;
  2396. {$endif volatile_es}
  2397. if dest.segment<>NR_NO then
  2398. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,dest.segment))
  2399. else if (dest.base=NR_BP) or (dest.base=NR_SP) then
  2400. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2401. else
  2402. internalerror(2014040401);
  2403. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2404. end;
  2405. getcpuregister(list,REGSI);
  2406. if ((source.segment=NR_NO) and (segment_regs_equal(NR_SS,NR_DS) or ((source.base<>NR_BP) and (source.base<>NR_SP)))) or
  2407. (is_segment_reg(source.segment) and segment_regs_equal(source.segment,NR_DS)) then
  2408. begin
  2409. srcref.segment:=NR_NO;
  2410. a_loadaddr_ref_reg(list,srcref,REGSI);
  2411. saved_ds:=false;
  2412. end
  2413. else
  2414. begin
  2415. srcref.segment:=NR_NO;
  2416. a_loadaddr_ref_reg(list,srcref,REGSI);
  2417. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2418. saved_ds:=true;
  2419. if source.segment<>NR_NO then
  2420. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,source.segment))
  2421. else if (source.base=NR_BP) or (source.base=NR_SP) then
  2422. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2423. else
  2424. internalerror(2014040402);
  2425. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2426. end;
  2427. getcpuregister(list,REGCX);
  2428. if ts_cld in current_settings.targetswitches then
  2429. list.concat(Taicpu.op_none(A_CLD,S_NO));
  2430. if (cs_opt_size in current_settings.optimizerswitches) and
  2431. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  2432. begin
  2433. a_load_const_reg(list,OS_INT,len,REGCX);
  2434. list.concat(Taicpu.op_none(A_REP,S_NO));
  2435. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2436. end
  2437. else
  2438. begin
  2439. helpsize:=len div sizeof(aint);
  2440. len:=len mod sizeof(aint);
  2441. if helpsize>1 then
  2442. begin
  2443. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  2444. list.concat(Taicpu.op_none(A_REP,S_NO));
  2445. end;
  2446. if helpsize>0 then
  2447. begin
  2448. {$if defined(cpu64bitalu)}
  2449. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  2450. {$elseif defined(cpu32bitalu)}
  2451. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2452. {$elseif defined(cpu16bitalu)}
  2453. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2454. {$endif}
  2455. end;
  2456. if len>=4 then
  2457. begin
  2458. dec(len,4);
  2459. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2460. end;
  2461. if len>=2 then
  2462. begin
  2463. dec(len,2);
  2464. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2465. end;
  2466. if len=1 then
  2467. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2468. end;
  2469. ungetcpuregister(list,REGCX);
  2470. ungetcpuregister(list,REGSI);
  2471. ungetcpuregister(list,REGDI);
  2472. if saved_ds then
  2473. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2474. if saved_es then
  2475. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2476. end;
  2477. end;
  2478. end;
  2479. {****************************************************************************
  2480. Entry/Exit Code Helpers
  2481. ****************************************************************************}
  2482. procedure tcgx86.g_profilecode(list : TAsmList);
  2483. var
  2484. pl : tasmlabel;
  2485. mcountprefix : String[4];
  2486. begin
  2487. case target_info.system of
  2488. {$ifndef NOTARGETWIN}
  2489. system_i386_win32,
  2490. {$endif}
  2491. system_i386_freebsd,
  2492. system_i386_netbsd,
  2493. // system_i386_openbsd,
  2494. system_i386_wdosx :
  2495. begin
  2496. Case target_info.system Of
  2497. system_i386_freebsd : mcountprefix:='.';
  2498. system_i386_netbsd : mcountprefix:='__';
  2499. // system_i386_openbsd : mcountprefix:='.';
  2500. else
  2501. mcountPrefix:='';
  2502. end;
  2503. current_asmdata.getaddrlabel(pl);
  2504. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(pint));
  2505. list.concat(Tai_label.Create(pl));
  2506. list.concat(Tai_const.Create_32bit(0));
  2507. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  2508. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2509. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  2510. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount',false);
  2511. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  2512. end;
  2513. system_i386_linux:
  2514. a_call_name(list,target_info.Cprefix+'mcount',false);
  2515. system_i386_go32v2,system_i386_watcom:
  2516. begin
  2517. a_call_name(list,'MCOUNT',false);
  2518. end;
  2519. system_x86_64_linux,
  2520. system_x86_64_darwin,
  2521. system_x86_64_iphonesim:
  2522. begin
  2523. a_call_name(list,'mcount',false);
  2524. end;
  2525. end;
  2526. end;
  2527. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2528. procedure decrease_sp(a : tcgint);
  2529. var
  2530. href : treference;
  2531. begin
  2532. reference_reset_base(href,NR_STACK_POINTER_REG,-a,0,[]);
  2533. { normally, lea is a better choice than a sub to adjust the stack pointer }
  2534. list.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_STACK_POINTER_REG));
  2535. end;
  2536. {$ifdef x86}
  2537. {$ifndef NOTARGETWIN}
  2538. var
  2539. href : treference;
  2540. i : integer;
  2541. again : tasmlabel;
  2542. {$endif NOTARGETWIN}
  2543. {$endif x86}
  2544. begin
  2545. if localsize>0 then
  2546. begin
  2547. {$ifdef i386}
  2548. {$ifndef NOTARGETWIN}
  2549. { windows guards only a few pages for stack growing,
  2550. so we have to access every page first }
  2551. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  2552. (localsize>=winstackpagesize) then
  2553. begin
  2554. if localsize div winstackpagesize<=5 then
  2555. begin
  2556. decrease_sp(localsize-4);
  2557. for i:=1 to localsize div winstackpagesize do
  2558. begin
  2559. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize,4,[]);
  2560. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2561. end;
  2562. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2563. end
  2564. else
  2565. begin
  2566. current_asmdata.getjumplabel(again);
  2567. { Using a_reg_alloc instead of getcpuregister, so this procedure
  2568. does not change "used_in_proc" state of EDI and therefore can be
  2569. called after saving registers with "push" instruction
  2570. without creating an unbalanced "pop edi" in epilogue }
  2571. a_reg_alloc(list,NR_EDI);
  2572. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  2573. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  2574. a_label(list,again);
  2575. decrease_sp(winstackpagesize-4);
  2576. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2577. if UseIncDec then
  2578. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI))
  2579. else
  2580. list.concat(Taicpu.op_const_reg(A_SUB,S_L,1,NR_EDI));
  2581. a_jmp_cond(list,OC_NE,again);
  2582. decrease_sp(localsize mod winstackpagesize-4);
  2583. reference_reset_base(href,NR_ESP,localsize-4,4,[]);
  2584. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  2585. a_reg_dealloc(list,NR_EDI);
  2586. end
  2587. end
  2588. else
  2589. {$endif NOTARGETWIN}
  2590. {$endif i386}
  2591. {$ifdef x86_64}
  2592. {$ifndef NOTARGETWIN}
  2593. { windows guards only a few pages for stack growing,
  2594. so we have to access every page first }
  2595. if (target_info.system=system_x86_64_win64) and
  2596. (localsize>=winstackpagesize) then
  2597. begin
  2598. if localsize div winstackpagesize<=5 then
  2599. begin
  2600. decrease_sp(localsize);
  2601. for i:=1 to localsize div winstackpagesize do
  2602. begin
  2603. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4,4,[]);
  2604. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2605. end;
  2606. reference_reset_base(href,NR_RSP,0,4,[]);
  2607. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2608. end
  2609. else
  2610. begin
  2611. current_asmdata.getjumplabel(again);
  2612. getcpuregister(list,NR_R10);
  2613. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  2614. a_label(list,again);
  2615. decrease_sp(winstackpagesize);
  2616. reference_reset_base(href,NR_RSP,0,4,[]);
  2617. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2618. if UseIncDec then
  2619. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10))
  2620. else
  2621. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,1,NR_R10));
  2622. a_jmp_cond(list,OC_NE,again);
  2623. decrease_sp(localsize mod winstackpagesize);
  2624. ungetcpuregister(list,NR_R10);
  2625. end
  2626. end
  2627. else
  2628. {$endif NOTARGETWIN}
  2629. {$endif x86_64}
  2630. decrease_sp(localsize);
  2631. end;
  2632. end;
  2633. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  2634. var
  2635. stackmisalignment: longint;
  2636. regsize: longint;
  2637. {$ifdef i8086}
  2638. dgroup: treference;
  2639. fardataseg: treference;
  2640. {$endif i8086}
  2641. procedure push_regs;
  2642. var
  2643. r: longint;
  2644. usedregs: tcpuregisterset;
  2645. begin
  2646. regsize:=0;
  2647. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  2648. for r := low(saved_standard_registers) to high(saved_standard_registers) do
  2649. if saved_standard_registers[r] in usedregs then
  2650. begin
  2651. inc(regsize,sizeof(aint));
  2652. list.concat(Taicpu.Op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE)));
  2653. end;
  2654. end;
  2655. begin
  2656. {$ifdef i8086}
  2657. { Win16 callback/exported proc prologue support.
  2658. Since callbacks can be called from different modules, DS on entry may be
  2659. initialized with the data segment of a different module, so we need to
  2660. get ours. But we can't do
  2661. push ds
  2662. mov ax, dgroup
  2663. mov ds, ax
  2664. because code segments are shared between different instances of the same
  2665. module (which have different instances of the current program's data segment),
  2666. so the same 'mov ax, dgroup' instruction will be used for all instances
  2667. of the program and it will load the same segment into ax.
  2668. So, the standard win16 prologue looks like this:
  2669. mov ax, ds
  2670. nop
  2671. inc bp
  2672. push bp
  2673. mov bp, sp
  2674. push ds
  2675. mov ds, ax
  2676. By default, this does nothing, except wasting a few extra machine cycles and
  2677. destroying ax in the process. However, Windows checks the first three bytes
  2678. of every exported function and if they are 'mov ax,ds/nop', they are replaced
  2679. with nop/nop/nop. Then the MakeProcInstance api call should be used to create
  2680. a thunk that loads ds for the current program instance in ax before calling
  2681. the routine.
  2682. And now the fun part comes: somebody (Michael Geary) figured out that all this
  2683. crap was unnecessary, because in Win16 exe modules, we always have DS=SS, so we
  2684. can simply initialize DS from SS :) And then calling MakeProcInstance becomes
  2685. unnecessary. This is what "smart callbacks" (cs_win16_smartcallbacks) do. However,
  2686. this only works for exe files, not for dlls, because dlls run with DS<>SS. There's
  2687. another solution for dlls - since win16 dlls only have a single instance of their
  2688. data segment, we can initialize ds from dgroup. However, there's not a single
  2689. solution for both exe and dlls, so we don't know what to use e.g. in a unit. So,
  2690. that's why there's still an option to turn smart callbacks off and go the
  2691. MakeProcInstance way.
  2692. Additional details here: http://www.geary.com/fixds.html }
  2693. if (current_settings.x86memorymodel<>mm_huge) and
  2694. (po_exports in current_procinfo.procdef.procoptions) and
  2695. (target_info.system=system_i8086_win16) then
  2696. begin
  2697. if cs_win16_smartcallbacks in current_settings.moduleswitches then
  2698. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_SS,NR_AX))
  2699. else
  2700. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_DS,NR_AX));
  2701. list.concat(Taicpu.op_none(A_NOP));
  2702. end
  2703. { interrupt support for i8086 }
  2704. else if po_interrupt in current_procinfo.procdef.procoptions then
  2705. begin
  2706. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_AX));
  2707. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_BX));
  2708. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CX));
  2709. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DX));
  2710. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  2711. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  2712. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  2713. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  2714. if current_settings.x86memorymodel=mm_tiny then
  2715. begin
  2716. { in the tiny memory model, we can't use dgroup, because that
  2717. adds a relocation entry to the .exe and we can't produce a
  2718. .com file (because they don't support relactions), so instead
  2719. we initialize DS from CS. }
  2720. if cs_opt_size in current_settings.optimizerswitches then
  2721. begin
  2722. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CS));
  2723. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  2724. end
  2725. else
  2726. begin
  2727. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_CS,NR_AX));
  2728. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2729. end;
  2730. end
  2731. else if current_settings.x86memorymodel=mm_huge then
  2732. begin
  2733. reference_reset(fardataseg,0,[]);
  2734. fardataseg.refaddr:=addr_fardataseg;
  2735. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  2736. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2737. end
  2738. else
  2739. begin
  2740. reference_reset(dgroup,0,[]);
  2741. dgroup.refaddr:=addr_dgroup;
  2742. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,dgroup,NR_AX));
  2743. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2744. end;
  2745. end;
  2746. {$endif i8086}
  2747. {$ifdef i386}
  2748. { interrupt support for i386 }
  2749. if (po_interrupt in current_procinfo.procdef.procoptions) and
  2750. { this messes up stack alignment }
  2751. not(target_info.system in [system_i386_darwin,system_i386_iphonesim,system_i386_android]) then
  2752. begin
  2753. { .... also the segment registers }
  2754. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  2755. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  2756. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  2757. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  2758. { save the registers of an interrupt procedure }
  2759. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  2760. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  2761. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2762. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  2763. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  2764. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  2765. end;
  2766. {$endif i386}
  2767. { save old framepointer }
  2768. if not nostackframe then
  2769. begin
  2770. { return address }
  2771. stackmisalignment := sizeof(pint);
  2772. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  2773. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  2774. begin
  2775. {$ifdef i386}
  2776. if (not paramanager.use_fixed_stack) then
  2777. push_regs;
  2778. {$endif i386}
  2779. CGmessage(cg_d_stackframe_omited);
  2780. end
  2781. else
  2782. begin
  2783. {$ifdef i8086}
  2784. if ((ts_x86_far_procs_push_odd_bp in current_settings.targetswitches) or
  2785. ((po_exports in current_procinfo.procdef.procoptions) and
  2786. (target_info.system=system_i8086_win16))) and
  2787. is_proc_far(current_procinfo.procdef) then
  2788. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,1,current_procinfo.framepointer);
  2789. {$endif i8086}
  2790. { push <frame_pointer> }
  2791. inc(stackmisalignment,sizeof(pint));
  2792. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  2793. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  2794. { Return address and FP are both on stack }
  2795. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(pint));
  2796. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(pint)));
  2797. if current_procinfo.procdef.proctypeoption<>potype_exceptfilter then
  2798. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG))
  2799. else
  2800. begin
  2801. push_regs;
  2802. gen_load_frame_for_exceptfilter(list);
  2803. { Need only as much stack space as necessary to do the calls.
  2804. Exception filters don't have own local vars, and temps are 'mapped'
  2805. to the parent procedure.
  2806. maxpushedparasize is already aligned at least on x86_64. }
  2807. localsize:=current_procinfo.maxpushedparasize;
  2808. end;
  2809. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  2810. end;
  2811. { allocate stackframe space }
  2812. if (localsize<>0) or
  2813. ((target_info.stackalign>sizeof(pint)) and
  2814. (stackmisalignment <> 0) and
  2815. ((pi_do_call in current_procinfo.flags) or
  2816. (po_assembler in current_procinfo.procdef.procoptions))) then
  2817. begin
  2818. if target_info.stackalign>sizeof(pint) then
  2819. localsize := align(localsize+stackmisalignment,target_info.stackalign)-stackmisalignment;
  2820. g_stackpointer_alloc(list,localsize);
  2821. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  2822. current_asmdata.asmcfi.cfa_def_cfa_offset(list,localsize+sizeof(pint));
  2823. current_procinfo.final_localsize:=localsize;
  2824. end
  2825. {$ifdef i8086}
  2826. else
  2827. { on i8086 we always call g_stackpointer_alloc, even with a zero size,
  2828. because it will generate code for stack checking, if stack checking is on }
  2829. g_stackpointer_alloc(list,0)
  2830. {$endif i8086}
  2831. ;
  2832. {$ifdef i8086}
  2833. { win16 exported proc prologue follow-up (see the huge comment above for details) }
  2834. if (current_settings.x86memorymodel<>mm_huge) and
  2835. (po_exports in current_procinfo.procdef.procoptions) and
  2836. (target_info.system=system_i8086_win16) then
  2837. begin
  2838. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  2839. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2840. end
  2841. else if (current_settings.x86memorymodel=mm_huge) and
  2842. not (po_interrupt in current_procinfo.procdef.procoptions) then
  2843. begin
  2844. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  2845. reference_reset(fardataseg,0,[]);
  2846. fardataseg.refaddr:=addr_fardataseg;
  2847. if current_procinfo.procdef.proccalloption=pocall_register then
  2848. begin
  2849. { Use BX register if using register convention
  2850. as it is not a register used to store parameters }
  2851. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_BX));
  2852. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_BX,NR_DS));
  2853. end
  2854. else
  2855. begin
  2856. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  2857. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2858. end;
  2859. end;
  2860. { SI and DI are volatile in the BP7 and FPC's pascal calling convention,
  2861. but must be preserved in Microsoft C's pascal calling convention, and
  2862. since Windows is compiled with Microsoft compilers, these registers
  2863. must be saved for exported procedures (BP7 for Win16 also does this). }
  2864. if (po_exports in current_procinfo.procdef.procoptions) and
  2865. (target_info.system=system_i8086_win16) then
  2866. begin
  2867. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  2868. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  2869. end;
  2870. {$endif i8086}
  2871. {$ifdef i386}
  2872. if (not paramanager.use_fixed_stack) and
  2873. (current_procinfo.framepointer<>NR_STACK_POINTER_REG) and
  2874. (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  2875. begin
  2876. regsize:=0;
  2877. push_regs;
  2878. reference_reset_base(current_procinfo.save_regs_ref,
  2879. current_procinfo.framepointer,
  2880. -(localsize+regsize),sizeof(aint),[]);
  2881. end;
  2882. {$endif i386}
  2883. end;
  2884. end;
  2885. procedure tcgx86.g_save_registers(list: TAsmList);
  2886. begin
  2887. {$ifdef i386}
  2888. if paramanager.use_fixed_stack then
  2889. {$endif i386}
  2890. inherited g_save_registers(list);
  2891. end;
  2892. procedure tcgx86.g_restore_registers(list: TAsmList);
  2893. begin
  2894. {$ifdef i386}
  2895. if paramanager.use_fixed_stack then
  2896. {$endif i386}
  2897. inherited g_restore_registers(list);
  2898. end;
  2899. procedure tcgx86.internal_restore_regs(list: TAsmList; use_pop: boolean);
  2900. var
  2901. r: longint;
  2902. hreg: tregister;
  2903. href: treference;
  2904. usedregs: tcpuregisterset;
  2905. begin
  2906. href:=current_procinfo.save_regs_ref;
  2907. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  2908. for r:=high(saved_standard_registers) downto low(saved_standard_registers) do
  2909. if saved_standard_registers[r] in usedregs then
  2910. begin
  2911. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  2912. { Allocate register so the optimizer does not remove the load }
  2913. a_reg_alloc(list,hreg);
  2914. if use_pop then
  2915. list.concat(Taicpu.Op_reg(A_POP,tcgsize2opsize[OS_ADDR],hreg))
  2916. else
  2917. begin
  2918. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2919. inc(href.offset,sizeof(aint));
  2920. end;
  2921. end;
  2922. end;
  2923. procedure tcgx86.generate_leave(list: TAsmList);
  2924. begin
  2925. if UseLeave then
  2926. list.concat(taicpu.op_none(A_LEAVE,S_NO))
  2927. else
  2928. begin
  2929. {$if defined(x86_64)}
  2930. list.Concat(taicpu.op_reg_reg(A_MOV,S_Q,NR_RBP,NR_RSP));
  2931. list.Concat(taicpu.op_reg(A_POP,S_Q,NR_RBP));
  2932. {$elseif defined(i386)}
  2933. list.Concat(taicpu.op_reg_reg(A_MOV,S_L,NR_EBP,NR_ESP));
  2934. list.Concat(taicpu.op_reg(A_POP,S_L,NR_EBP));
  2935. {$elseif defined(i8086)}
  2936. list.Concat(taicpu.op_reg_reg(A_MOV,S_W,NR_BP,NR_SP));
  2937. list.Concat(taicpu.op_reg(A_POP,S_W,NR_BP));
  2938. {$endif}
  2939. end;
  2940. end;
  2941. { produces if necessary overflowcode }
  2942. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  2943. var
  2944. hl : tasmlabel;
  2945. ai : taicpu;
  2946. cond : TAsmCond;
  2947. begin
  2948. if not(cs_check_overflow in current_settings.localswitches) then
  2949. exit;
  2950. current_asmdata.getjumplabel(hl);
  2951. if not ((def.typ=pointerdef) or
  2952. ((def.typ=orddef) and
  2953. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  2954. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  2955. cond:=C_NO
  2956. else
  2957. cond:=C_NB;
  2958. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  2959. ai.SetCondition(cond);
  2960. ai.is_jmp:=true;
  2961. list.concat(ai);
  2962. a_call_name(list,'FPC_OVERFLOW',false);
  2963. a_label(list,hl);
  2964. end;
  2965. end.