cgcpu.pas 137 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the i8086
  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. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. cgbase,cgobj,cg64f32,cgx86,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,
  24. cpubase,parabase,cgutils,
  25. symconst,symdef
  26. ;
  27. type
  28. { tcg8086 }
  29. tcg8086 = class(tcgx86)
  30. procedure init_register_allocators;override;
  31. procedure do_register_allocation(list:TAsmList;headertai:tai);override;
  32. function getintregister(list:TAsmList;size:Tcgsize):Tregister;override;
  33. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  34. procedure a_call_name_far(list : TAsmList;const s : string; weak: boolean);
  35. procedure a_call_name_static(list : TAsmList;const s : string);override;
  36. procedure a_call_name_static_far(list : TAsmList;const s : string);
  37. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  38. procedure a_call_reg_far(list : TAsmList;reg : tregister);
  39. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  40. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  41. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  42. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  43. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  44. procedure push_const(list:TAsmList;size:tcgsize;a:tcgint);
  45. { passing parameter using push instead of mov }
  46. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);override;
  47. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);override;
  48. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);override;
  49. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);override;
  50. { move instructions }
  51. procedure a_load_const_reg(list : TAsmList; tosize: tcgsize; a : tcgint;reg : tregister);override;
  52. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  53. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);override;
  54. { use a_load_ref_reg_internal() instead }
  55. //procedure a_load_ref_reg(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);override;
  56. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize: tcgsize;reg1,reg2 : tregister);override;
  57. { comparison operations }
  58. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  59. l : tasmlabel);override;
  60. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  61. l : tasmlabel);override;
  62. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  63. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister; l : tasmlabel); override;
  64. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); override;
  65. procedure gen_cmp32_jmp1(list: TAsmList; cmp_op: topcmp; l_skip, l_target: TAsmLabel);
  66. procedure gen_cmp32_jmp2(list: TAsmList; cmp_op: topcmp; l_skip, l_target: TAsmLabel);
  67. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);override;
  68. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);override;
  69. procedure g_stackpointer_alloc(list : TAsmList;localsize: longint);override;
  70. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  71. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:tcgint;destreg:tregister);
  72. procedure g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  73. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);override;
  74. procedure get_32bit_ops(op: TOpCG; out op1,op2: TAsmOp);
  75. procedure add_move_instruction(instr:Taicpu);override;
  76. protected
  77. procedure a_load_ref_reg_internal(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister;isdirect:boolean);override;
  78. end;
  79. tcg64f8086 = class(tcg64f32)
  80. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);override;
  81. procedure a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64; const ref: treference);override;
  82. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  83. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  84. procedure a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);override;
  85. private
  86. procedure get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  87. end;
  88. procedure create_codegen;
  89. implementation
  90. uses
  91. globals,verbose,systems,cutils,
  92. paramgr,procinfo,fmodule,
  93. rgcpu,rgx86,cpuinfo,
  94. symtype,symsym,symcpu,
  95. tgobj,
  96. hlcgobj;
  97. function use_push(const cgpara:tcgpara):boolean;
  98. begin
  99. result:=(not paramanager.use_fixed_stack) and
  100. assigned(cgpara.location) and
  101. (cgpara.location^.loc=LOC_REFERENCE) and
  102. (cgpara.location^.reference.index=NR_STACK_POINTER_REG);
  103. end;
  104. procedure tcg8086.init_register_allocators;
  105. begin
  106. inherited init_register_allocators;
  107. if cs_create_pic in current_settings.moduleswitches then
  108. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_AX,RS_DX,RS_CX,RS_SI,RS_DI],first_int_imreg,[RS_BP])
  109. else
  110. if (cs_useebp in current_settings.optimizerswitches) and assigned(current_procinfo) and (current_procinfo.framepointer<>NR_BP) then
  111. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_AX,RS_DX,RS_CX,RS_BX,RS_SI,RS_DI,RS_BP],first_int_imreg,[])
  112. else
  113. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_AX,RS_DX,RS_CX,RS_BX,RS_SI,RS_DI],first_int_imreg,[RS_BP]);
  114. rg[R_MMXREGISTER]:=trgcpu.create(R_MMXREGISTER,R_SUBNONE,[RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7],first_mm_imreg,[]);
  115. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBWHOLE,[RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7],first_mm_imreg,[]);
  116. rgfpu:=Trgx86fpu.create;
  117. end;
  118. procedure tcg8086.do_register_allocation(list:TAsmList;headertai:tai);
  119. begin
  120. if (pi_needs_got in current_procinfo.flags) then
  121. begin
  122. if getsupreg(current_procinfo.got) < first_int_imreg then
  123. include(rg[R_INTREGISTER].used_in_proc,getsupreg(current_procinfo.got));
  124. end;
  125. inherited do_register_allocation(list,headertai);
  126. end;
  127. function tcg8086.getintregister(list: TAsmList; size: Tcgsize): Tregister;
  128. begin
  129. case size of
  130. OS_8, OS_S8,
  131. OS_16, OS_S16:
  132. Result := inherited getintregister(list, size);
  133. OS_32, OS_S32:
  134. begin
  135. Result:=inherited getintregister(list, OS_16);
  136. { ensure that the high register can be retrieved by
  137. GetNextReg
  138. }
  139. if inherited getintregister(list, OS_16)<>GetNextReg(Result) then
  140. internalerror(2013030202);
  141. end;
  142. else
  143. internalerror(2013030201);
  144. end;
  145. end;
  146. procedure tcg8086.a_call_name(list: TAsmList; const s: string; weak: boolean);
  147. begin
  148. if current_settings.x86memorymodel in x86_far_code_models then
  149. a_call_name_far(list,s,weak)
  150. else
  151. a_call_name_near(list,s,weak);
  152. end;
  153. procedure tcg8086.a_call_name_far(list: TAsmList; const s: string;
  154. weak: boolean);
  155. var
  156. sym : tasmsymbol;
  157. begin
  158. if not(weak) then
  159. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION)
  160. else
  161. sym:=current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION);
  162. list.concat(taicpu.op_sym(A_CALL,S_FAR,sym));
  163. end;
  164. procedure tcg8086.a_call_name_static(list: TAsmList; const s: string);
  165. begin
  166. if current_settings.x86memorymodel in x86_far_code_models then
  167. a_call_name_static_far(list,s)
  168. else
  169. a_call_name_static_near(list,s);
  170. end;
  171. procedure tcg8086.a_call_name_static_far(list: TAsmList; const s: string);
  172. var
  173. sym : tasmsymbol;
  174. begin
  175. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION);
  176. list.concat(taicpu.op_sym(A_CALL,S_FAR,sym));
  177. end;
  178. procedure tcg8086.a_call_reg(list: TAsmList; reg: tregister);
  179. begin
  180. if current_settings.x86memorymodel in x86_far_code_models then
  181. a_call_reg_far(list,reg)
  182. else
  183. a_call_reg_near(list,reg);
  184. end;
  185. procedure tcg8086.a_call_reg_far(list: TAsmList; reg: tregister);
  186. var
  187. href: treference;
  188. begin
  189. { unfortunately, x86 doesn't have a 'call far reg:reg' instruction, so }
  190. { we have to use a temp }
  191. tg.gettemp(list,4,2,tt_normal,href);
  192. { HACK!!! at this point all registers are allocated, due to the fact that
  193. in the pascal calling convention, all registers are caller saved. This
  194. causes the register allocator to fail on the next move instruction, so we
  195. temporarily deallocate 2 registers.
  196. TODO: figure out a better way to do this. }
  197. cg.ungetcpuregister(list,NR_BX);
  198. cg.ungetcpuregister(list,NR_SI);
  199. a_load_reg_ref(list,OS_32,OS_32,reg,href);
  200. cg.getcpuregister(list,NR_BX);
  201. cg.getcpuregister(list,NR_SI);
  202. href.segment:=NR_NO;
  203. list.concat(taicpu.op_ref(A_CALL,S_FAR,href));
  204. tg.ungettemp(list,href);
  205. end;
  206. procedure tcg8086.a_op_const_reg(list: TAsmList; Op: TOpCG; size: TCGSize;
  207. a: tcgint; reg: TRegister);
  208. type
  209. trox32method=(rm_unspecified,rm_unrolledleftloop,rm_unrolledrightloop,
  210. rm_loopleft,rm_loopright,rm_fast_386);
  211. var
  212. tmpreg: tregister;
  213. op1, op2: TAsmOp;
  214. ax_subreg: tregister;
  215. hl_loop_start: tasmlabel;
  216. ai: taicpu;
  217. use_loop, use_186_fast_shift, use_8086_fast_shift,
  218. use_386_fast_shift: Boolean;
  219. rox32method: trox32method=rm_unspecified;
  220. i: Integer;
  221. rol_amount, ror_amount: TCGInt;
  222. begin
  223. optimize_op_const(size, op, a);
  224. check_register_size(size,reg);
  225. if size in [OS_64, OS_S64] then
  226. internalerror(2013030904);
  227. if size in [OS_32, OS_S32] then
  228. begin
  229. case op of
  230. OP_NONE:
  231. begin
  232. { Opcode is optimized away }
  233. end;
  234. OP_MOVE:
  235. begin
  236. { Optimized, replaced with a simple load }
  237. a_load_const_reg(list,size,a,reg);
  238. end;
  239. OP_ADD, OP_SUB:
  240. begin
  241. get_32bit_ops(op, op1, op2);
  242. { Optimization when the low 16-bits of the constant are 0 }
  243. if aint(a and $FFFF) = 0 then
  244. begin
  245. { use a_op_const_reg to allow the use of inc/dec }
  246. a_op_const_reg(list,op,OS_16,aint(a shr 16),GetNextReg(reg));
  247. end
  248. else
  249. begin
  250. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  251. list.concat(taicpu.op_const_reg(op1,S_W,aint(a and $FFFF),reg));
  252. list.concat(taicpu.op_const_reg(op2,S_W,aint(a shr 16),GetNextReg(reg)));
  253. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  254. end;
  255. end;
  256. OP_AND, OP_OR, OP_XOR:
  257. begin
  258. { low word operation }
  259. if aint(a and $FFFF) = aint(0) then
  260. begin
  261. case op of
  262. OP_AND:
  263. a_load_const_reg(list,OS_16,aint(0),reg);
  264. OP_OR,OP_XOR:
  265. {do nothing};
  266. else
  267. InternalError(2013100701);
  268. end;
  269. end
  270. else if aint(a and $FFFF) = aint($FFFF) then
  271. begin
  272. case op of
  273. OP_AND:
  274. {do nothing};
  275. OP_OR:
  276. a_load_const_reg(list,OS_16,aint($FFFF),reg);
  277. OP_XOR:
  278. list.concat(taicpu.op_reg(A_NOT,S_W,reg));
  279. else
  280. InternalError(2013100701);
  281. end;
  282. end
  283. else
  284. a_op_const_reg(list,op,OS_16,aint(a and $FFFF),reg);
  285. { high word operation }
  286. if aint(a shr 16) = aint(0) then
  287. begin
  288. case op of
  289. OP_AND:
  290. a_load_const_reg(list,OS_16,aint(0),GetNextReg(reg));
  291. OP_OR,OP_XOR:
  292. {do nothing};
  293. else
  294. InternalError(2013100701);
  295. end;
  296. end
  297. else if aint(a shr 16) = aint($FFFF) then
  298. begin
  299. case op of
  300. OP_AND:
  301. {do nothing};
  302. OP_OR:
  303. a_load_const_reg(list,OS_16,aint($FFFF),GetNextReg(reg));
  304. OP_XOR:
  305. list.concat(taicpu.op_reg(A_NOT,S_W,GetNextReg(reg)));
  306. else
  307. InternalError(2013100701);
  308. end;
  309. end
  310. else
  311. a_op_const_reg(list,op,OS_16,aint(a shr 16),GetNextReg(reg));
  312. end;
  313. OP_SHR,OP_SHL,OP_SAR:
  314. begin
  315. a:=a and 31;
  316. { for shl with const >= 16, we can just move the low register
  317. to the high reg, then zero the low register, then do the
  318. remaining part of the shift (by const-16) in 16 bit on the
  319. high register. the same thing applies to shr with low and high
  320. reversed. sar is exactly like shr, except that instead of
  321. zeroing the high register, we sar it by 15. }
  322. if a>=16 then
  323. case op of
  324. OP_SHR:
  325. begin
  326. a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg),reg);
  327. a_load_const_reg(list,OS_16,0,GetNextReg(reg));
  328. a_op_const_reg(list,OP_SHR,OS_16,a-16,reg);
  329. end;
  330. OP_SHL:
  331. begin
  332. a_load_reg_reg(list,OS_16,OS_16,reg,GetNextReg(reg));
  333. a_load_const_reg(list,OS_16,0,reg);
  334. a_op_const_reg(list,OP_SHL,OS_16,a-16,GetNextReg(reg));
  335. end;
  336. OP_SAR:
  337. begin
  338. if a=31 then
  339. begin
  340. a_op_const_reg(list,OP_SAR,OS_16,15,GetNextReg(reg));
  341. a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg),reg);
  342. end
  343. else
  344. begin
  345. a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg),reg);
  346. a_op_const_reg(list,OP_SAR,OS_16,15,GetNextReg(reg));
  347. a_op_const_reg(list,OP_SAR,OS_16,a-16,reg);
  348. end;
  349. end;
  350. else
  351. internalerror(2013060201);
  352. end
  353. else if a<>0 then
  354. begin
  355. use_loop:=a>2;
  356. use_386_fast_shift:=(current_settings.cputype>=cpu_386) and (a>1);
  357. use_186_fast_shift:=not use_386_fast_shift
  358. and (current_settings.cputype>=cpu_186) and (a>2)
  359. and not (cs_opt_size in current_settings.optimizerswitches);
  360. use_8086_fast_shift:=(current_settings.cputype<cpu_186) and (a>2)
  361. and not (cs_opt_size in current_settings.optimizerswitches);
  362. if use_386_fast_shift then
  363. begin
  364. case op of
  365. OP_SHR:
  366. begin
  367. list.concat(taicpu.op_const_reg_reg(A_SHRD,S_W,a,GetNextReg(reg),reg));
  368. list.concat(taicpu.op_const_reg(A_SHR,S_W,a,GetNextReg(reg)));
  369. end;
  370. OP_SAR:
  371. begin
  372. list.concat(taicpu.op_const_reg_reg(A_SHRD,S_W,a,GetNextReg(reg),reg));
  373. list.concat(taicpu.op_const_reg(A_SAR,S_W,a,GetNextReg(reg)));
  374. end;
  375. OP_SHL:
  376. begin
  377. list.concat(taicpu.op_const_reg_reg(A_SHLD,S_W,a,reg,GetNextReg(reg)));
  378. list.concat(taicpu.op_const_reg(A_SHL,S_W,a,reg));
  379. end;
  380. else
  381. internalerror(2017040401);
  382. end;
  383. end
  384. else if use_186_fast_shift then
  385. begin
  386. tmpreg:=getintregister(list,OS_16);
  387. case op of
  388. OP_SHR:
  389. begin
  390. a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg),tmpreg);
  391. list.concat(taicpu.op_const_reg(A_SHR,S_W,a,GetNextReg(reg)));
  392. list.concat(taicpu.op_const_reg(A_SHR,S_W,a,reg));
  393. list.concat(taicpu.op_const_reg(A_SHL,S_W,16-a,tmpreg));
  394. list.concat(taicpu.op_reg_reg(A_OR,S_W,tmpreg,reg));
  395. end;
  396. OP_SAR:
  397. begin
  398. a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg),tmpreg);
  399. list.concat(taicpu.op_const_reg(A_SAR,S_W,a,GetNextReg(reg)));
  400. list.concat(taicpu.op_const_reg(A_SHR,S_W,a,reg));
  401. list.concat(taicpu.op_const_reg(A_SHL,S_W,16-a,tmpreg));
  402. list.concat(taicpu.op_reg_reg(A_OR,S_W,tmpreg,reg));
  403. end;
  404. OP_SHL:
  405. begin
  406. a_load_reg_reg(list,OS_16,OS_16,reg,tmpreg);
  407. list.concat(taicpu.op_const_reg(A_SHL,S_W,a,reg));
  408. list.concat(taicpu.op_const_reg(A_SHL,S_W,a,GetNextReg(reg)));
  409. list.concat(taicpu.op_const_reg(A_SHR,S_W,16-a,tmpreg));
  410. list.concat(taicpu.op_reg_reg(A_OR,S_W,tmpreg,GetNextReg(reg)));
  411. end;
  412. else
  413. internalerror(2017040301);
  414. end;
  415. end
  416. else if use_8086_fast_shift then
  417. begin
  418. getcpuregister(list,NR_CX);
  419. a_load_const_reg(list,OS_8,a,NR_CL);
  420. tmpreg:=getintregister(list,OS_16);
  421. case op of
  422. OP_SHR:
  423. begin
  424. a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg),tmpreg);
  425. list.concat(taicpu.op_reg_reg(A_SHR,S_W,NR_CL,GetNextReg(reg)));
  426. list.concat(taicpu.op_reg_reg(A_SHR,S_W,NR_CL,reg));
  427. if a<>8 then
  428. a_load_const_reg(list,OS_8,16-a,NR_CL);
  429. list.concat(taicpu.op_reg_reg(A_SHL,S_W,NR_CL,tmpreg));
  430. list.concat(taicpu.op_reg_reg(A_OR,S_W,tmpreg,reg));
  431. end;
  432. OP_SAR:
  433. begin
  434. a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg),tmpreg);
  435. list.concat(taicpu.op_reg_reg(A_SAR,S_W,NR_CL,GetNextReg(reg)));
  436. list.concat(taicpu.op_reg_reg(A_SHR,S_W,NR_CL,reg));
  437. if a<>8 then
  438. a_load_const_reg(list,OS_8,16-a,NR_CL);
  439. list.concat(taicpu.op_reg_reg(A_SHL,S_W,NR_CL,tmpreg));
  440. list.concat(taicpu.op_reg_reg(A_OR,S_W,tmpreg,reg));
  441. end;
  442. OP_SHL:
  443. begin
  444. a_load_reg_reg(list,OS_16,OS_16,reg,tmpreg);
  445. list.concat(taicpu.op_reg_reg(A_SHL,S_W,NR_CL,reg));
  446. list.concat(taicpu.op_reg_reg(A_SHL,S_W,NR_CL,GetNextReg(reg)));
  447. if a<>8 then
  448. a_load_const_reg(list,OS_8,16-a,NR_CL);
  449. list.concat(taicpu.op_reg_reg(A_SHR,S_W,NR_CL,tmpreg));
  450. list.concat(taicpu.op_reg_reg(A_OR,S_W,tmpreg,GetNextReg(reg)));
  451. end;
  452. else
  453. internalerror(2017040301);
  454. end;
  455. ungetcpuregister(list,NR_CX);
  456. end
  457. else if use_loop then
  458. begin
  459. getcpuregister(list,NR_CX);
  460. a_load_const_reg(list,OS_16,a,NR_CX);
  461. current_asmdata.getjumplabel(hl_loop_start);
  462. a_label(list,hl_loop_start);
  463. case op of
  464. OP_SHR:
  465. begin
  466. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  467. list.concat(taicpu.op_const_reg(A_SHR,S_W,1,GetNextReg(reg)));
  468. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg));
  469. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  470. end;
  471. OP_SAR:
  472. begin
  473. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  474. list.concat(taicpu.op_const_reg(A_SAR,S_W,1,GetNextReg(reg)));
  475. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg));
  476. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  477. end;
  478. OP_SHL:
  479. begin
  480. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  481. list.concat(taicpu.op_const_reg(A_SHL,S_W,1,reg));
  482. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(reg)));
  483. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  484. end;
  485. else
  486. internalerror(2013030903);
  487. end;
  488. ai:=Taicpu.Op_Sym(A_LOOP,S_W,hl_loop_start);
  489. ai.is_jmp:=true;
  490. list.concat(ai);
  491. ungetcpuregister(list,NR_CX);
  492. end
  493. else
  494. begin
  495. for i:=1 to a do
  496. begin
  497. case op of
  498. OP_SHR:
  499. begin
  500. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  501. list.concat(taicpu.op_const_reg(A_SHR,S_W,1,GetNextReg(reg)));
  502. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg));
  503. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  504. end;
  505. OP_SAR:
  506. begin
  507. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  508. list.concat(taicpu.op_const_reg(A_SAR,S_W,1,GetNextReg(reg)));
  509. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg));
  510. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  511. end;
  512. OP_SHL:
  513. begin
  514. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  515. list.concat(taicpu.op_const_reg(A_SHL,S_W,1,reg));
  516. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(reg)));
  517. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  518. end;
  519. else
  520. internalerror(2013030903);
  521. end;
  522. end;
  523. end;
  524. end;
  525. end;
  526. OP_ROL,OP_ROR:
  527. begin
  528. a:=a and 31;
  529. if a=0 then
  530. exit;
  531. if op=OP_ROL then
  532. begin
  533. rol_amount:=a;
  534. ror_amount:=32-a;
  535. end
  536. else
  537. begin
  538. rol_amount:=32-a;
  539. ror_amount:=a;
  540. end;
  541. case rol_amount of
  542. 1,17:
  543. rox32method:=rm_unrolledleftloop;
  544. 2,18:
  545. if current_settings.cputype>=cpu_386 then
  546. rox32method:=rm_fast_386
  547. else if not (cs_opt_size in current_settings.optimizerswitches) then
  548. rox32method:=rm_unrolledleftloop
  549. else
  550. rox32method:=rm_loopleft;
  551. 3..8,19..24:
  552. if current_settings.cputype>=cpu_386 then
  553. rox32method:=rm_fast_386
  554. else
  555. rox32method:=rm_loopleft;
  556. 15,31:
  557. rox32method:=rm_unrolledrightloop;
  558. 14,30:
  559. if current_settings.cputype>=cpu_386 then
  560. rox32method:=rm_fast_386
  561. else if not (cs_opt_size in current_settings.optimizerswitches) then
  562. rox32method:=rm_unrolledrightloop
  563. else
  564. { the left loop has a smaller size }
  565. rox32method:=rm_loopleft;
  566. 9..13,25..29:
  567. if current_settings.cputype>=cpu_386 then
  568. rox32method:=rm_fast_386
  569. else if not (cs_opt_size in current_settings.optimizerswitches) then
  570. rox32method:=rm_loopright
  571. else
  572. { the left loop has a smaller size }
  573. rox32method:=rm_loopleft;
  574. 16:
  575. rox32method:=rm_unrolledleftloop;
  576. else
  577. internalerror(2017040601);
  578. end;
  579. case rox32method of
  580. rm_unrolledleftloop:
  581. begin
  582. if rol_amount>=16 then
  583. begin
  584. list.Concat(taicpu.op_reg_reg(A_XCHG,S_W,reg,GetNextReg(reg)));
  585. dec(rol_amount,16);
  586. end;
  587. for i:=1 to rol_amount do
  588. begin
  589. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  590. list.Concat(taicpu.op_const_reg(A_SHL,S_W,1,GetNextReg(reg)));
  591. list.Concat(taicpu.op_const_reg(A_RCL,S_W,1,reg));
  592. list.Concat(taicpu.op_const_reg(A_ADC,S_W,0,GetNextReg(reg)));
  593. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  594. end;
  595. end;
  596. rm_unrolledrightloop:
  597. begin
  598. if ror_amount>=16 then
  599. begin
  600. list.Concat(taicpu.op_reg_reg(A_XCHG,S_W,reg,GetNextReg(reg)));
  601. dec(ror_amount,16);
  602. end;
  603. tmpreg:=getintregister(list,OS_16);
  604. for i:=1 to ror_amount do
  605. begin
  606. a_load_reg_reg(list,OS_16,OS_16,reg,tmpreg);
  607. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  608. list.Concat(taicpu.op_const_reg(A_SHR,S_W,1,tmpreg));
  609. list.Concat(taicpu.op_const_reg(A_RCR,S_W,1,GetNextReg(reg)));
  610. list.Concat(taicpu.op_const_reg(A_RCR,S_W,1,reg));
  611. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  612. end;
  613. end;
  614. rm_loopleft:
  615. begin
  616. if (rol_amount>=16) and not (cs_opt_size in current_settings.optimizerswitches) then
  617. begin
  618. list.Concat(taicpu.op_reg_reg(A_XCHG,S_W,reg,GetNextReg(reg)));
  619. dec(rol_amount,16);
  620. if rol_amount=0 then
  621. exit;
  622. end;
  623. getcpuregister(list,NR_CX);
  624. a_load_const_reg(list,OS_16,rol_amount,NR_CX);
  625. current_asmdata.getjumplabel(hl_loop_start);
  626. a_label(list,hl_loop_start);
  627. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  628. list.Concat(taicpu.op_const_reg(A_SHL,S_W,1,GetNextReg(reg)));
  629. list.Concat(taicpu.op_const_reg(A_RCL,S_W,1,reg));
  630. list.Concat(taicpu.op_const_reg(A_ADC,S_W,0,GetNextReg(reg)));
  631. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  632. ai:=Taicpu.Op_Sym(A_LOOP,S_W,hl_loop_start);
  633. ai.is_jmp:=true;
  634. list.concat(ai);
  635. ungetcpuregister(list,NR_CX);
  636. end;
  637. rm_loopright:
  638. begin
  639. if (ror_amount>=16) and not (cs_opt_size in current_settings.optimizerswitches) then
  640. begin
  641. list.Concat(taicpu.op_reg_reg(A_XCHG,S_W,reg,GetNextReg(reg)));
  642. dec(ror_amount,16);
  643. if ror_amount=0 then
  644. exit;
  645. end;
  646. getcpuregister(list,NR_CX);
  647. a_load_const_reg(list,OS_16,ror_amount,NR_CX);
  648. current_asmdata.getjumplabel(hl_loop_start);
  649. a_label(list,hl_loop_start);
  650. tmpreg:=getintregister(list,OS_16);
  651. a_load_reg_reg(list,OS_16,OS_16,reg,tmpreg);
  652. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  653. list.Concat(taicpu.op_const_reg(A_SHR,S_W,1,tmpreg));
  654. list.Concat(taicpu.op_const_reg(A_RCR,S_W,1,GetNextReg(reg)));
  655. list.Concat(taicpu.op_const_reg(A_RCR,S_W,1,reg));
  656. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  657. ai:=Taicpu.Op_Sym(A_LOOP,S_W,hl_loop_start);
  658. ai.is_jmp:=true;
  659. list.concat(ai);
  660. ungetcpuregister(list,NR_CX);
  661. end;
  662. rm_fast_386:
  663. begin
  664. tmpreg:=getintregister(list,OS_16);
  665. a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg),tmpreg);
  666. if op=OP_ROL then
  667. begin
  668. list.Concat(taicpu.op_const_reg_reg(A_SHLD,S_W,rol_amount,reg,GetNextReg(reg)));
  669. list.Concat(taicpu.op_const_reg_reg(A_SHLD,S_W,rol_amount,tmpreg,reg));
  670. end
  671. else
  672. begin
  673. list.Concat(taicpu.op_const_reg_reg(A_SHRD,S_W,ror_amount,reg,GetNextReg(reg)));
  674. list.Concat(taicpu.op_const_reg_reg(A_SHRD,S_W,ror_amount,tmpreg,reg));
  675. end;
  676. end;
  677. else
  678. internalerror(2017040602);
  679. end;
  680. end;
  681. else
  682. begin
  683. tmpreg:=getintregister(list,size);
  684. a_load_const_reg(list,size,a,tmpreg);
  685. a_op_reg_reg(list,op,size,tmpreg,reg);
  686. end;
  687. end;
  688. end
  689. else
  690. begin
  691. { size <= 16-bit }
  692. { 8086 doesn't support 'imul reg,const', so we handle it here }
  693. if (current_settings.cputype<cpu_186) and (op in [OP_MUL,OP_IMUL]) then
  694. begin
  695. if op = OP_IMUL then
  696. begin
  697. if size in [OS_16,OS_S16] then
  698. ax_subreg := NR_AX
  699. else
  700. if size in [OS_8,OS_S8] then
  701. ax_subreg := NR_AL
  702. else
  703. internalerror(2013050102);
  704. getcpuregister(list,NR_AX);
  705. a_load_const_reg(list,size,a,ax_subreg);
  706. if size in [OS_16,OS_S16] then
  707. getcpuregister(list,NR_DX);
  708. { prefer MUL over IMUL when overflow checking is off, }
  709. { because it's faster on the 8086 & 8088 }
  710. if not (cs_check_overflow in current_settings.localswitches) then
  711. list.concat(taicpu.op_reg(A_MUL,TCgSize2OpSize[size],reg))
  712. else
  713. list.concat(taicpu.op_reg(A_IMUL,TCgSize2OpSize[size],reg));
  714. if size in [OS_16,OS_S16] then
  715. ungetcpuregister(list,NR_DX);
  716. a_load_reg_reg(list,size,size,ax_subreg,reg);
  717. ungetcpuregister(list,NR_AX);
  718. exit;
  719. end
  720. else
  721. { OP_MUL should be handled specifically in the code }
  722. { generator because of the silly register usage restraints }
  723. internalerror(200109225);
  724. end
  725. else
  726. inherited a_op_const_reg(list, Op, size, a, reg);
  727. end;
  728. end;
  729. procedure tcg8086.a_op_const_ref(list: TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  730. var
  731. tmpref: treference;
  732. op1,op2: TAsmOp;
  733. tmpreg: TRegister;
  734. begin
  735. optimize_op_const(size, op, a);
  736. tmpref:=ref;
  737. make_simple_ref(list,tmpref);
  738. if size in [OS_64, OS_S64] then
  739. internalerror(2013050801);
  740. if size in [OS_32, OS_S32] then
  741. begin
  742. case Op of
  743. OP_NONE :
  744. begin
  745. { Opcode is optimized away }
  746. end;
  747. OP_MOVE :
  748. begin
  749. { Optimized, replaced with a simple load }
  750. a_load_const_ref(list,size,a,ref);
  751. end;
  752. OP_ADD, OP_SUB:
  753. begin
  754. get_32bit_ops(op, op1, op2);
  755. { Optimization when the low 16-bits of the constant are 0 }
  756. if aint(a and $FFFF) = 0 then
  757. begin
  758. inc(tmpref.offset, 2);
  759. { use a_op_const_ref to allow the use of inc/dec }
  760. a_op_const_ref(list,op,OS_16,aint(a shr 16),tmpref);
  761. end
  762. else
  763. begin
  764. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  765. list.concat(taicpu.op_const_ref(op1,S_W,aint(a and $FFFF),tmpref));
  766. inc(tmpref.offset, 2);
  767. list.concat(taicpu.op_const_ref(op2,S_W,aint(a shr 16),tmpref));
  768. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  769. end;
  770. end;
  771. OP_AND, OP_OR, OP_XOR:
  772. begin
  773. { low word operation }
  774. if aint(a and $FFFF) = aint(0) then
  775. begin
  776. case op of
  777. OP_AND:
  778. a_load_const_ref(list,OS_16,aint(0),ref);
  779. OP_OR,OP_XOR:
  780. {do nothing};
  781. else
  782. InternalError(2013100701);
  783. end;
  784. end
  785. else if aint(a and $FFFF) = aint($FFFF) then
  786. begin
  787. case op of
  788. OP_AND:
  789. {do nothing};
  790. OP_OR:
  791. a_load_const_ref(list,OS_16,aint($FFFF),tmpref);
  792. OP_XOR:
  793. list.concat(taicpu.op_ref(A_NOT,S_W,tmpref));
  794. else
  795. InternalError(2013100701);
  796. end;
  797. end
  798. else
  799. a_op_const_ref(list,op,OS_16,aint(a and $FFFF),tmpref);
  800. { high word operation }
  801. inc(tmpref.offset, 2);
  802. if aint(a shr 16) = aint(0) then
  803. begin
  804. case op of
  805. OP_AND:
  806. a_load_const_ref(list,OS_16,aint(0),tmpref);
  807. OP_OR,OP_XOR:
  808. {do nothing};
  809. else
  810. InternalError(2013100701);
  811. end;
  812. end
  813. else if aint(a shr 16) = aint($FFFF) then
  814. begin
  815. case op of
  816. OP_AND:
  817. {do nothing};
  818. OP_OR:
  819. a_load_const_ref(list,OS_16,aint($FFFF),tmpref);
  820. OP_XOR:
  821. list.concat(taicpu.op_ref(A_NOT,S_W,tmpref));
  822. else
  823. InternalError(2013100701);
  824. end;
  825. end
  826. else
  827. a_op_const_ref(list,op,OS_16,aint(a shr 16),tmpref);
  828. end;
  829. OP_SHR,OP_SHL,OP_SAR:
  830. begin
  831. a:=a and 31;
  832. if a=1 then
  833. begin
  834. case op of
  835. OP_SHR:
  836. begin
  837. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  838. inc(tmpref.offset, 2);
  839. list.concat(taicpu.op_const_ref(A_SHR,S_W,1,tmpref));
  840. dec(tmpref.offset, 2);
  841. list.concat(taicpu.op_const_ref(A_RCR,S_W,1,tmpref));
  842. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  843. end;
  844. OP_SAR:
  845. begin
  846. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  847. inc(tmpref.offset, 2);
  848. list.concat(taicpu.op_const_ref(A_SAR,S_W,1,tmpref));
  849. dec(tmpref.offset, 2);
  850. list.concat(taicpu.op_const_ref(A_RCR,S_W,1,tmpref));
  851. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  852. end;
  853. OP_SHL:
  854. begin
  855. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  856. list.concat(taicpu.op_const_ref(A_SHL,S_W,1,tmpref));
  857. inc(tmpref.offset, 2);
  858. list.concat(taicpu.op_const_ref(A_RCL,S_W,1,tmpref));
  859. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  860. end;
  861. else
  862. internalerror(2015042501);
  863. end;
  864. end
  865. else
  866. begin
  867. tmpreg:=getintregister(list,size);
  868. a_load_ref_reg(list,size,size,ref,tmpreg);
  869. a_op_const_reg(list,Op,size,a,tmpreg);
  870. a_load_reg_ref(list,size,size,tmpreg,ref);
  871. end;
  872. end;
  873. OP_ROL,OP_ROR:
  874. begin
  875. tmpreg:=getintregister(list,size);
  876. a_load_ref_reg(list,size,size,ref,tmpreg);
  877. a_op_const_reg(list,Op,size,a,tmpreg);
  878. a_load_reg_ref(list,size,size,tmpreg,ref);
  879. end;
  880. else
  881. internalerror(2013050802);
  882. end;
  883. end
  884. else
  885. inherited a_op_const_ref(list,Op,size,a,tmpref);
  886. end;
  887. procedure tcg8086.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize;
  888. src, dst: TRegister);
  889. var
  890. op1, op2: TAsmOp;
  891. hl_skip, hl_loop_start: TAsmLabel;
  892. ai: taicpu;
  893. tmpreg: TRegister;
  894. begin
  895. check_register_size(size,src);
  896. check_register_size(size,dst);
  897. if size in [OS_64, OS_S64] then
  898. internalerror(2013030902);
  899. if size in [OS_32, OS_S32] then
  900. begin
  901. case op of
  902. OP_NEG:
  903. begin
  904. if src<>dst then
  905. a_load_reg_reg(list,size,size,src,dst);
  906. list.concat(taicpu.op_reg(A_NOT, S_W, GetNextReg(dst)));
  907. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  908. list.concat(taicpu.op_reg(A_NEG, S_W, dst));
  909. list.concat(taicpu.op_const_reg(A_SBB, S_W,-1, GetNextReg(dst)));
  910. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  911. end;
  912. OP_NOT:
  913. begin
  914. if src<>dst then
  915. a_load_reg_reg(list,size,size,src,dst);
  916. list.concat(taicpu.op_reg(A_NOT, S_W, dst));
  917. list.concat(taicpu.op_reg(A_NOT, S_W, GetNextReg(dst)));
  918. end;
  919. OP_ADD,OP_SUB,OP_XOR,OP_OR,OP_AND:
  920. begin
  921. get_32bit_ops(op, op1, op2);
  922. if op in [OP_ADD,OP_SUB] then
  923. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  924. list.concat(taicpu.op_reg_reg(op1, S_W, src, dst));
  925. list.concat(taicpu.op_reg_reg(op2, S_W, GetNextReg(src), GetNextReg(dst)));
  926. if op in [OP_ADD,OP_SUB] then
  927. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  928. end;
  929. OP_SHR,OP_SHL,OP_SAR:
  930. begin
  931. getcpuregister(list,NR_CX);
  932. a_load_reg_reg(list,size,OS_16,src,NR_CX);
  933. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  934. list.concat(taicpu.op_const_reg(A_AND,S_W,$1f,NR_CX));
  935. current_asmdata.getjumplabel(hl_skip);
  936. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl_skip);
  937. ai.SetCondition(C_Z);
  938. ai.is_jmp:=true;
  939. list.concat(ai);
  940. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  941. current_asmdata.getjumplabel(hl_loop_start);
  942. a_label(list,hl_loop_start);
  943. case op of
  944. OP_SHR:
  945. begin
  946. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  947. list.concat(taicpu.op_const_reg(A_SHR,S_W,1,GetNextReg(dst)));
  948. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,dst));
  949. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  950. end;
  951. OP_SAR:
  952. begin
  953. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  954. list.concat(taicpu.op_const_reg(A_SAR,S_W,1,GetNextReg(dst)));
  955. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,dst));
  956. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  957. end;
  958. OP_SHL:
  959. begin
  960. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  961. list.concat(taicpu.op_const_reg(A_SHL,S_W,1,dst));
  962. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(dst)));
  963. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  964. end;
  965. else
  966. internalerror(2013030903);
  967. end;
  968. ai:=Taicpu.Op_Sym(A_LOOP,S_W,hl_loop_start);
  969. ai.is_jmp:=true;
  970. list.concat(ai);
  971. a_label(list,hl_skip);
  972. ungetcpuregister(list,NR_CX);
  973. end;
  974. OP_ROL,OP_ROR:
  975. begin
  976. getcpuregister(list,NR_CX);
  977. a_load_reg_reg(list,size,OS_16,src,NR_CX);
  978. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  979. list.concat(taicpu.op_const_reg(A_AND,S_W,$1f,NR_CX));
  980. current_asmdata.getjumplabel(hl_skip);
  981. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl_skip);
  982. ai.SetCondition(C_Z);
  983. ai.is_jmp:=true;
  984. list.concat(ai);
  985. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  986. current_asmdata.getjumplabel(hl_loop_start);
  987. a_label(list,hl_loop_start);
  988. case op of
  989. OP_ROL:
  990. begin
  991. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  992. list.Concat(taicpu.op_const_reg(A_SHL,S_W,1,GetNextReg(dst)));
  993. list.Concat(taicpu.op_const_reg(A_RCL,S_W,1,dst));
  994. list.Concat(taicpu.op_const_reg(A_ADC,S_W,0,GetNextReg(dst)));
  995. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  996. end;
  997. OP_ROR:
  998. begin
  999. tmpreg:=getintregister(list,OS_16);
  1000. a_load_reg_reg(list,OS_16,OS_16,dst,tmpreg);
  1001. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  1002. list.Concat(taicpu.op_const_reg(A_SHR,S_W,1,tmpreg));
  1003. list.Concat(taicpu.op_const_reg(A_RCR,S_W,1,GetNextReg(dst)));
  1004. list.Concat(taicpu.op_const_reg(A_RCR,S_W,1,dst));
  1005. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1006. end;
  1007. else
  1008. internalerror(2017042502);
  1009. end;
  1010. ai:=Taicpu.Op_Sym(A_LOOP,S_W,hl_loop_start);
  1011. ai.is_jmp:=true;
  1012. list.concat(ai);
  1013. a_label(list,hl_skip);
  1014. ungetcpuregister(list,NR_CX);
  1015. end;
  1016. else
  1017. internalerror(2013030901);
  1018. end;
  1019. end
  1020. else
  1021. inherited a_op_reg_reg(list, Op, size, src, dst);
  1022. end;
  1023. procedure tcg8086.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1024. var
  1025. tmpref : treference;
  1026. op1, op2: TAsmOp;
  1027. begin
  1028. tmpref:=ref;
  1029. make_simple_ref(list,tmpref);
  1030. check_register_size(size,reg);
  1031. if size in [OS_64, OS_S64] then
  1032. internalerror(2013030902);
  1033. if size in [OS_32, OS_S32] then
  1034. begin
  1035. case op of
  1036. OP_ADD,OP_SUB,OP_XOR,OP_OR,OP_AND:
  1037. begin
  1038. get_32bit_ops(op, op1, op2);
  1039. if op in [OP_ADD,OP_SUB] then
  1040. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  1041. list.concat(taicpu.op_ref_reg(op1, S_W, tmpref, reg));
  1042. inc(tmpref.offset, 2);
  1043. list.concat(taicpu.op_ref_reg(op2, S_W, tmpref, GetNextReg(reg)));
  1044. if op in [OP_ADD,OP_SUB] then
  1045. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1046. end;
  1047. else
  1048. internalerror(2013050701);
  1049. end;
  1050. end
  1051. else
  1052. inherited a_op_ref_reg(list,Op,size,tmpref,reg);
  1053. end;
  1054. procedure tcg8086.a_op_reg_ref(list: TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference);
  1055. var
  1056. tmpref: treference;
  1057. op1,op2: TAsmOp;
  1058. hl_skip, hl_loop_start: TAsmLabel;
  1059. ai: taicpu;
  1060. tmpreg: TRegister;
  1061. begin
  1062. tmpref:=ref;
  1063. make_simple_ref(list,tmpref);
  1064. if not (op in [OP_NEG,OP_NOT,OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR]) then
  1065. check_register_size(size,reg);
  1066. if size in [OS_64, OS_S64] then
  1067. internalerror(2013050803);
  1068. if size in [OS_32, OS_S32] then
  1069. begin
  1070. case op of
  1071. OP_NEG:
  1072. begin
  1073. if reg<>NR_NO then
  1074. internalerror(200109237);
  1075. inc(tmpref.offset, 2);
  1076. list.concat(taicpu.op_ref(A_NOT, S_W, tmpref));
  1077. dec(tmpref.offset, 2);
  1078. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  1079. list.concat(taicpu.op_ref(A_NEG, S_W, tmpref));
  1080. inc(tmpref.offset, 2);
  1081. list.concat(taicpu.op_const_ref(A_SBB, S_W,-1, tmpref));
  1082. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1083. end;
  1084. OP_NOT:
  1085. begin
  1086. if reg<>NR_NO then
  1087. internalerror(200109237);
  1088. list.concat(taicpu.op_ref(A_NOT, S_W, tmpref));
  1089. inc(tmpref.offset, 2);
  1090. list.concat(taicpu.op_ref(A_NOT, S_W, tmpref));
  1091. end;
  1092. OP_IMUL:
  1093. begin
  1094. { this one needs a load/imul/store, which is the default }
  1095. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1096. end;
  1097. OP_MUL,OP_DIV,OP_IDIV:
  1098. { special stuff, needs separate handling inside code }
  1099. { generator }
  1100. internalerror(200109238);
  1101. OP_ADD,OP_SUB,OP_XOR,OP_OR,OP_AND:
  1102. begin
  1103. get_32bit_ops(op, op1, op2);
  1104. if op in [OP_ADD,OP_SUB] then
  1105. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  1106. list.concat(taicpu.op_reg_ref(op1, S_W, reg, tmpref));
  1107. inc(tmpref.offset, 2);
  1108. list.concat(taicpu.op_reg_ref(op2, S_W, GetNextReg(reg), tmpref));
  1109. if op in [OP_ADD,OP_SUB] then
  1110. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1111. end;
  1112. OP_SHR,OP_SHL,OP_SAR:
  1113. begin
  1114. getcpuregister(list,NR_CX);
  1115. a_load_reg_reg(list,size,OS_16,reg,NR_CX);
  1116. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  1117. list.concat(taicpu.op_const_reg(A_AND,S_W,$1f,NR_CX));
  1118. current_asmdata.getjumplabel(hl_skip);
  1119. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl_skip);
  1120. ai.SetCondition(C_Z);
  1121. ai.is_jmp:=true;
  1122. list.concat(ai);
  1123. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1124. current_asmdata.getjumplabel(hl_loop_start);
  1125. a_label(list,hl_loop_start);
  1126. case op of
  1127. OP_SHR:
  1128. begin
  1129. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  1130. inc(tmpref.offset, 2);
  1131. list.concat(taicpu.op_const_ref(A_SHR,S_W,1,tmpref));
  1132. dec(tmpref.offset, 2);
  1133. list.concat(taicpu.op_const_ref(A_RCR,S_W,1,tmpref));
  1134. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1135. end;
  1136. OP_SAR:
  1137. begin
  1138. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  1139. inc(tmpref.offset, 2);
  1140. list.concat(taicpu.op_const_ref(A_SAR,S_W,1,tmpref));
  1141. dec(tmpref.offset, 2);
  1142. list.concat(taicpu.op_const_ref(A_RCR,S_W,1,tmpref));
  1143. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1144. end;
  1145. OP_SHL:
  1146. begin
  1147. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  1148. list.concat(taicpu.op_const_ref(A_SHL,S_W,1,tmpref));
  1149. inc(tmpref.offset, 2);
  1150. list.concat(taicpu.op_const_ref(A_RCL,S_W,1,tmpref));
  1151. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1152. end;
  1153. else
  1154. internalerror(2013030903);
  1155. end;
  1156. ai:=Taicpu.Op_Sym(A_LOOP,S_W,hl_loop_start);
  1157. ai.is_jmp:=true;
  1158. list.concat(ai);
  1159. a_label(list,hl_skip);
  1160. ungetcpuregister(list,NR_CX);
  1161. end;
  1162. OP_ROL,OP_ROR:
  1163. begin
  1164. tmpreg:=getintregister(list,size);
  1165. a_load_ref_reg(list,size,size,ref,tmpreg);
  1166. a_op_reg_reg(list,Op,size,reg,tmpreg);
  1167. a_load_reg_ref(list,size,size,tmpreg,ref);
  1168. end;
  1169. else
  1170. internalerror(2013050804);
  1171. end;
  1172. end
  1173. else
  1174. inherited a_op_reg_ref(list,Op,size,reg,tmpref);
  1175. end;
  1176. procedure tcg8086.push_const(list: TAsmList; size: tcgsize; a: tcgint);
  1177. var
  1178. tmpreg: TRegister;
  1179. begin
  1180. if not (size in [OS_16,OS_S16]) then
  1181. internalerror(2013043001);
  1182. if current_settings.cputype < cpu_186 then
  1183. begin
  1184. tmpreg:=getintregister(list,size);
  1185. a_load_const_reg(list,size,a,tmpreg);
  1186. list.concat(taicpu.op_reg(A_PUSH,S_W,tmpreg));
  1187. end
  1188. else
  1189. list.concat(taicpu.op_const(A_PUSH,TCGSize2OpSize[size],a));
  1190. end;
  1191. procedure tcg8086.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  1192. procedure load_para_loc(r : TRegister;paraloc : PCGParaLocation);
  1193. var
  1194. ref : treference;
  1195. begin
  1196. paramanager.allocparaloc(list,paraloc);
  1197. case paraloc^.loc of
  1198. LOC_REGISTER,LOC_CREGISTER:
  1199. a_load_reg_reg(list,paraloc^.size,paraloc^.size,r,paraloc^.register);
  1200. LOC_REFERENCE,LOC_CREFERENCE:
  1201. begin
  1202. reference_reset_base(ref,paraloc^.reference.index,paraloc^.reference.offset,2,[]);
  1203. a_load_reg_ref(list,paraloc^.size,paraloc^.size,r,ref);
  1204. end;
  1205. else
  1206. internalerror(2002071004);
  1207. end;
  1208. end;
  1209. var
  1210. pushsize,pushsize2 : tcgsize;
  1211. begin
  1212. check_register_size(size,r);
  1213. if use_push(cgpara) then
  1214. begin
  1215. if tcgsize2size[cgpara.Size] > 2 then
  1216. begin
  1217. if tcgsize2size[cgpara.Size] <> 4 then
  1218. internalerror(2013031101);
  1219. if cgpara.location^.Next = nil then
  1220. begin
  1221. if tcgsize2size[cgpara.location^.size] <> 4 then
  1222. internalerror(2013031101);
  1223. end
  1224. else
  1225. begin
  1226. if tcgsize2size[cgpara.location^.size] <> 2 then
  1227. internalerror(2013031101);
  1228. if tcgsize2size[cgpara.location^.Next^.size] <> 2 then
  1229. internalerror(2013031101);
  1230. if cgpara.location^.Next^.Next <> nil then
  1231. internalerror(2013031101);
  1232. end;
  1233. if tcgsize2size[cgpara.size]>cgpara.alignment then
  1234. pushsize:=cgpara.size
  1235. else
  1236. pushsize:=int_cgsize(cgpara.alignment);
  1237. pushsize2 := int_cgsize(tcgsize2size[pushsize] - 2);
  1238. list.concat(taicpu.op_reg(A_PUSH,TCgsize2opsize[pushsize2],makeregsize(list,GetNextReg(r),pushsize2)));
  1239. list.concat(taicpu.op_reg(A_PUSH,S_W,makeregsize(list,r,OS_16)));
  1240. end
  1241. else
  1242. begin
  1243. cgpara.check_simple_location;
  1244. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  1245. pushsize:=cgpara.location^.size
  1246. else
  1247. pushsize:=int_cgsize(cgpara.alignment);
  1248. list.concat(taicpu.op_reg(A_PUSH,TCgsize2opsize[pushsize],makeregsize(list,r,pushsize)));
  1249. end;
  1250. end
  1251. else
  1252. begin
  1253. if tcgsize2size[cgpara.Size]=4 then
  1254. begin
  1255. if (cgpara.location^.Next=nil) or
  1256. (tcgsize2size[cgpara.location^.size]<>2) or
  1257. (tcgsize2size[cgpara.location^.Next^.size]<>2) or
  1258. (cgpara.location^.Next^.Next<>nil) or
  1259. (cgpara.location^.shiftval<>0) then
  1260. internalerror(2013031102);
  1261. load_para_loc(r,cgpara.Location);
  1262. load_para_loc(GetNextReg(r),cgpara.Location^.Next);
  1263. end
  1264. else
  1265. inherited a_load_reg_cgpara(list,size,r,cgpara);
  1266. end;
  1267. end;
  1268. procedure tcg8086.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);
  1269. var
  1270. pushsize : tcgsize;
  1271. begin
  1272. if use_push(cgpara) then
  1273. begin
  1274. if tcgsize2size[cgpara.Size] > 2 then
  1275. begin
  1276. if tcgsize2size[cgpara.Size] <> 4 then
  1277. internalerror(2013031101);
  1278. if cgpara.location^.Next = nil then
  1279. begin
  1280. if tcgsize2size[cgpara.location^.size] <> 4 then
  1281. internalerror(2013031101);
  1282. end
  1283. else
  1284. begin
  1285. if tcgsize2size[cgpara.location^.size] <> 2 then
  1286. internalerror(2013031101);
  1287. if tcgsize2size[cgpara.location^.Next^.size] <> 2 then
  1288. internalerror(2013031101);
  1289. if cgpara.location^.Next^.Next <> nil then
  1290. internalerror(2013031101);
  1291. end;
  1292. if (cgpara.alignment <> 4) and (cgpara.alignment <> 2) then
  1293. internalerror(2013031101);
  1294. push_const(list,OS_16,a shr 16);
  1295. push_const(list,OS_16,a and $FFFF);
  1296. end
  1297. else
  1298. begin
  1299. cgpara.check_simple_location;
  1300. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  1301. pushsize:=cgpara.location^.size
  1302. else
  1303. pushsize:=int_cgsize(cgpara.alignment);
  1304. push_const(list,pushsize,a);
  1305. end;
  1306. end
  1307. else
  1308. inherited a_load_const_cgpara(list,size,a,cgpara);
  1309. end;
  1310. procedure tcg8086.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  1311. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  1312. var
  1313. pushsize : tcgsize;
  1314. opsize : topsize;
  1315. tmpreg : tregister;
  1316. href,tmpref: treference;
  1317. begin
  1318. if not assigned(paraloc) then
  1319. exit;
  1320. if (paraloc^.loc<>LOC_REFERENCE) or
  1321. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  1322. (tcgsize2size[paraloc^.size]>4) then
  1323. internalerror(200501162);
  1324. { Pushes are needed in reverse order, add the size of the
  1325. current location to the offset where to load from. This
  1326. prevents wrong calculations for the last location when
  1327. the size is not a power of 2 }
  1328. if assigned(paraloc^.next) then
  1329. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  1330. { Push the data starting at ofs }
  1331. href:=r;
  1332. inc(href.offset,ofs);
  1333. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  1334. pushsize:=paraloc^.size
  1335. else
  1336. pushsize:=int_cgsize(cgpara.alignment);
  1337. opsize:=TCgsize2opsize[pushsize];
  1338. { for go32v2 we obtain OS_F32,
  1339. but pushs is not valid, we need pushl }
  1340. if opsize=S_FS then
  1341. opsize:=S_W;
  1342. if tcgsize2size[paraloc^.size]<cgpara.alignment then
  1343. begin
  1344. tmpreg:=getintregister(list,pushsize);
  1345. a_load_ref_reg(list,paraloc^.size,pushsize,href,tmpreg);
  1346. list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  1347. end
  1348. else
  1349. begin
  1350. make_simple_ref(list,href);
  1351. if tcgsize2size[pushsize] > 2 then
  1352. begin
  1353. tmpref := href;
  1354. Inc(tmpref.offset, 2);
  1355. list.concat(taicpu.op_ref(A_PUSH,TCgsize2opsize[int_cgsize(tcgsize2size[pushsize]-2)],tmpref));
  1356. end;
  1357. list.concat(taicpu.op_ref(A_PUSH,opsize,href));
  1358. end;
  1359. end;
  1360. var
  1361. len : tcgint;
  1362. href : treference;
  1363. begin
  1364. { cgpara.size=OS_NO requires a copy on the stack }
  1365. if use_push(cgpara) then
  1366. begin
  1367. { Record copy? }
  1368. if (cgpara.size in [OS_NO,OS_F64]) or (size=OS_NO) then
  1369. begin
  1370. cgpara.check_simple_location;
  1371. len:=align(cgpara.intsize,cgpara.alignment);
  1372. g_stackpointer_alloc(list,len);
  1373. reference_reset_base(href,NR_STACK_POINTER_REG,0,4,[]);
  1374. g_concatcopy(list,r,href,len);
  1375. end
  1376. else
  1377. begin
  1378. if tcgsize2size[cgpara.size]<>tcgsize2size[size] then
  1379. internalerror(200501161);
  1380. { We need to push the data in reverse order,
  1381. therefor we use a recursive algorithm }
  1382. pushdata(cgpara.location,0);
  1383. end
  1384. end
  1385. else
  1386. inherited a_load_ref_cgpara(list,size,r,cgpara);
  1387. end;
  1388. procedure tcg8086.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);
  1389. var
  1390. tmpreg : tregister;
  1391. tmpref : treference;
  1392. begin
  1393. with r do
  1394. begin
  1395. if use_push(cgpara) then
  1396. begin
  1397. if tcgsize2size[cgpara.Size] > 2 then
  1398. begin
  1399. if tcgsize2size[cgpara.Size] <> 4 then
  1400. internalerror(2014032401);
  1401. if cgpara.location^.Next = nil then
  1402. begin
  1403. if tcgsize2size[cgpara.location^.size] <> 4 then
  1404. internalerror(2014032401);
  1405. end
  1406. else
  1407. begin
  1408. if tcgsize2size[cgpara.location^.size] <> 2 then
  1409. internalerror(2014032401);
  1410. if tcgsize2size[cgpara.location^.Next^.size] <> 2 then
  1411. internalerror(2014032401);
  1412. if cgpara.location^.Next^.Next <> nil then
  1413. internalerror(2014032401);
  1414. end;
  1415. if cgpara.alignment > 4 then
  1416. internalerror(2014032401);
  1417. if segment<>NR_NO then
  1418. begin
  1419. list.concat(Taicpu.op_reg(A_PUSH,S_W,segment));
  1420. tmpref:=r;
  1421. tmpref.segment:=NR_NO;
  1422. tmpreg:=getaddressregister(list);
  1423. a_loadaddr_ref_reg(list,tmpref,tmpreg);
  1424. list.concat(taicpu.op_reg(A_PUSH,S_W,tmpreg));
  1425. end
  1426. else
  1427. begin
  1428. if (base=NR_NO) and (index=NR_NO) then
  1429. begin
  1430. if assigned(symbol) then
  1431. begin
  1432. tmpref:=r;
  1433. tmpref.refaddr:=addr_seg;
  1434. tmpref.offset:=0;
  1435. if current_settings.cputype < cpu_186 then
  1436. begin
  1437. tmpreg:=getaddressregister(list);
  1438. a_load_ref_reg(list,OS_16,OS_16,tmpref,tmpreg);
  1439. list.concat(taicpu.op_reg(A_PUSH,S_W,tmpreg));
  1440. end
  1441. else
  1442. list.concat(Taicpu.Op_ref(A_PUSH,S_W,tmpref));
  1443. if current_settings.cputype < cpu_186 then
  1444. begin
  1445. tmpreg:=getaddressregister(list);
  1446. a_loadaddr_ref_reg(list,r,tmpreg);
  1447. list.concat(taicpu.op_reg(A_PUSH,S_W,tmpreg));
  1448. end
  1449. else
  1450. list.concat(Taicpu.Op_sym_ofs(A_PUSH,S_W,symbol,offset));
  1451. end
  1452. else
  1453. internalerror(2014032402);
  1454. end
  1455. else if assigned(symbol) then
  1456. begin
  1457. reference_reset_symbol(tmpref,r.symbol,0,r.alignment,r.volatility);
  1458. tmpref.refaddr:=addr_seg;
  1459. if current_settings.cputype < cpu_186 then
  1460. begin
  1461. tmpreg:=getaddressregister(list);
  1462. a_load_ref_reg(list,OS_16,OS_16,tmpref,tmpreg);
  1463. list.concat(taicpu.op_reg(A_PUSH,S_W,tmpreg));
  1464. end
  1465. else
  1466. list.concat(Taicpu.Op_ref(A_PUSH,S_W,tmpref));
  1467. tmpreg:=getaddressregister(list);
  1468. a_loadaddr_ref_reg(list,r,tmpreg);
  1469. list.concat(taicpu.op_reg(A_PUSH,S_W,tmpreg));
  1470. end
  1471. else if base=NR_BP then
  1472. begin
  1473. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_SS));
  1474. tmpreg:=getaddressregister(list);
  1475. a_loadaddr_ref_reg(list,r,tmpreg);
  1476. list.concat(taicpu.op_reg(A_PUSH,S_W,tmpreg));
  1477. end
  1478. else
  1479. internalerror(2014032403);
  1480. end;
  1481. end
  1482. else
  1483. begin
  1484. cgpara.check_simple_location;
  1485. tmpref:=r;
  1486. tmpref.segment:=NR_NO;
  1487. with tmpref do
  1488. begin
  1489. if (base=NR_NO) and (index=NR_NO) then
  1490. begin
  1491. if assigned(symbol) then
  1492. begin
  1493. if current_settings.cputype < cpu_186 then
  1494. begin
  1495. tmpreg:=getaddressregister(list);
  1496. a_loadaddr_ref_reg(list,tmpref,tmpreg);
  1497. list.concat(taicpu.op_reg(A_PUSH,S_W,tmpreg));
  1498. end
  1499. else
  1500. list.concat(Taicpu.Op_sym_ofs(A_PUSH,S_W,symbol,offset));
  1501. end
  1502. else
  1503. push_const(list,OS_16,offset);
  1504. end
  1505. else if (base=NR_NO) and (index<>NR_NO) and
  1506. (offset=0) and (scalefactor=0) and (symbol=nil) then
  1507. list.concat(Taicpu.Op_reg(A_PUSH,S_W,index))
  1508. else if (base<>NR_NO) and (index=NR_NO) and
  1509. (offset=0) and (symbol=nil) then
  1510. list.concat(Taicpu.Op_reg(A_PUSH,S_W,base))
  1511. else
  1512. begin
  1513. tmpreg:=getaddressregister(list);
  1514. a_loadaddr_ref_reg(list,tmpref,tmpreg);
  1515. list.concat(taicpu.op_reg(A_PUSH,S_W,tmpreg));
  1516. end;
  1517. end;
  1518. end;
  1519. end
  1520. else
  1521. inherited a_loadaddr_ref_cgpara(list,r,cgpara);
  1522. end;
  1523. end;
  1524. procedure tcg8086.a_load_const_reg(list : TAsmList; tosize: tcgsize; a : tcgint;reg : tregister);
  1525. begin
  1526. check_register_size(tosize,reg);
  1527. if tosize in [OS_S32,OS_32] then
  1528. begin
  1529. list.concat(taicpu.op_const_reg(A_MOV,S_W,longint(a and $ffff),reg));
  1530. list.concat(taicpu.op_const_reg(A_MOV,S_W,longint(a shr 16),GetNextReg(reg)));
  1531. end
  1532. else
  1533. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg));
  1534. end;
  1535. procedure tcg8086.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  1536. var
  1537. tmpref : treference;
  1538. begin
  1539. tmpref:=ref;
  1540. make_simple_ref(list,tmpref);
  1541. if tosize in [OS_S32,OS_32] then
  1542. begin
  1543. a_load_const_ref(list,OS_16,longint(a and $ffff),tmpref);
  1544. inc(tmpref.offset,2);
  1545. a_load_const_ref(list,OS_16,longint(a shr 16),tmpref);
  1546. end
  1547. else
  1548. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  1549. end;
  1550. procedure tcg8086.a_load_reg_ref(list : TAsmList;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);
  1551. var
  1552. tmpreg : tregister;
  1553. tmpref : treference;
  1554. begin
  1555. tmpref:=ref;
  1556. make_simple_ref(list,tmpref);
  1557. check_register_size(fromsize,reg);
  1558. case tosize of
  1559. OS_8,OS_S8:
  1560. if fromsize in [OS_8,OS_S8] then
  1561. list.concat(taicpu.op_reg_ref(A_MOV, S_B, reg, tmpref))
  1562. else
  1563. internalerror(2013030310);
  1564. OS_16,OS_S16:
  1565. case fromsize of
  1566. OS_8,OS_S8:
  1567. begin
  1568. tmpreg:=getintregister(list,tosize);
  1569. a_load_reg_reg(list,fromsize,tosize,reg,tmpreg);
  1570. a_load_reg_ref(list,tosize,tosize,tmpreg,tmpref);
  1571. end;
  1572. OS_16,OS_S16:
  1573. begin
  1574. list.concat(taicpu.op_reg_ref(A_MOV, S_W, reg, tmpref));
  1575. end;
  1576. else
  1577. internalerror(2013030312);
  1578. end;
  1579. OS_32,OS_S32:
  1580. case fromsize of
  1581. OS_8,OS_S8,OS_S16:
  1582. begin
  1583. tmpreg:=getintregister(list,tosize);
  1584. a_load_reg_reg(list,fromsize,tosize,reg,tmpreg);
  1585. a_load_reg_ref(list,tosize,tosize,tmpreg,tmpref);
  1586. end;
  1587. OS_16:
  1588. begin
  1589. list.concat(taicpu.op_reg_ref(A_MOV, S_W, reg, tmpref));
  1590. inc(tmpref.offset, 2);
  1591. list.concat(taicpu.op_const_ref(A_MOV, S_W, 0, tmpref));
  1592. end;
  1593. OS_32,OS_S32:
  1594. begin
  1595. list.concat(taicpu.op_reg_ref(A_MOV, S_W, reg, tmpref));
  1596. inc(tmpref.offset, 2);
  1597. list.concat(taicpu.op_reg_ref(A_MOV, S_W, GetNextReg(reg), tmpref));
  1598. end;
  1599. else
  1600. internalerror(2013030313);
  1601. end;
  1602. else
  1603. internalerror(2013030311);
  1604. end;
  1605. end;
  1606. procedure tcg8086.a_load_ref_reg_internal(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister;isdirect:boolean);
  1607. procedure add_mov(instr: Taicpu);
  1608. begin
  1609. { Notify the register allocator that we have written a move instruction so
  1610. it can try to eliminate it. }
  1611. if (instr.oper[0]^.reg<>current_procinfo.framepointer) and (instr.oper[0]^.reg<>NR_STACK_POINTER_REG) then
  1612. add_move_instruction(instr);
  1613. list.concat(instr);
  1614. end;
  1615. var
  1616. tmpref : treference;
  1617. begin
  1618. tmpref:=ref;
  1619. make_simple_ref(list,tmpref,isdirect);
  1620. check_register_size(tosize,reg);
  1621. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1622. internalerror(2011021307);
  1623. { if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  1624. fromsize:=tosize;}
  1625. case tosize of
  1626. OS_8,OS_S8:
  1627. if fromsize in [OS_8,OS_S8] then
  1628. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, reg))
  1629. else
  1630. internalerror(2013030210);
  1631. OS_16,OS_S16:
  1632. case fromsize of
  1633. OS_8:
  1634. begin
  1635. if current_settings.cputype>=cpu_386 then
  1636. list.concat(taicpu.op_ref_reg(A_MOVZX, S_BW, tmpref, reg))
  1637. else
  1638. begin
  1639. reg := makeregsize(list, reg, OS_8);
  1640. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, reg));
  1641. setsubreg(reg, R_SUBH);
  1642. list.concat(taicpu.op_const_reg(A_MOV, S_B, 0, reg));
  1643. makeregsize(list, reg, OS_16);
  1644. end;
  1645. end;
  1646. OS_S8:
  1647. begin
  1648. if current_settings.cputype>=cpu_386 then
  1649. list.concat(taicpu.op_ref_reg(A_MOVSX, S_BW, tmpref, reg))
  1650. else
  1651. begin
  1652. getcpuregister(list, NR_AX);
  1653. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, NR_AL));
  1654. list.concat(taicpu.op_none(A_CBW));
  1655. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg));
  1656. ungetcpuregister(list, NR_AX);
  1657. end;
  1658. end;
  1659. OS_16,OS_S16:
  1660. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, reg));
  1661. else
  1662. internalerror(2013030212);
  1663. end;
  1664. OS_32,OS_S32:
  1665. case fromsize of
  1666. OS_8:
  1667. begin
  1668. list.concat(taicpu.op_const_reg(A_MOV,S_W,0,GetNextReg(reg)));
  1669. if current_settings.cputype>=cpu_386 then
  1670. list.concat(taicpu.op_ref_reg(A_MOVZX, S_BW, tmpref, reg))
  1671. else
  1672. begin
  1673. reg := makeregsize(list, reg, OS_8);
  1674. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, reg));
  1675. setsubreg(reg, R_SUBH);
  1676. list.concat(taicpu.op_const_reg(A_MOV, S_B, 0, reg));
  1677. makeregsize(list, reg, OS_16);
  1678. end;
  1679. end;
  1680. OS_S8:
  1681. begin
  1682. getcpuregister(list, NR_AX);
  1683. list.concat(taicpu.op_ref_reg(A_MOV, S_B, tmpref, NR_AL));
  1684. getcpuregister(list, NR_DX);
  1685. list.concat(taicpu.op_none(A_CBW));
  1686. list.concat(taicpu.op_none(A_CWD));
  1687. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg));
  1688. ungetcpuregister(list, NR_AX);
  1689. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_DX, GetNextReg(reg)));
  1690. ungetcpuregister(list, NR_DX);
  1691. end;
  1692. OS_16:
  1693. begin
  1694. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, reg));
  1695. list.concat(taicpu.op_const_reg(A_MOV,S_W,0,GetNextReg(reg)));
  1696. end;
  1697. OS_S16:
  1698. begin
  1699. getcpuregister(list, NR_AX);
  1700. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, NR_AX));
  1701. getcpuregister(list, NR_DX);
  1702. list.concat(taicpu.op_none(A_CWD));
  1703. ungetcpuregister(list, NR_AX);
  1704. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg));
  1705. ungetcpuregister(list, NR_DX);
  1706. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_DX, GetNextReg(reg)));
  1707. end;
  1708. OS_32,OS_S32:
  1709. begin
  1710. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, reg));
  1711. inc(tmpref.offset, 2);
  1712. list.concat(taicpu.op_ref_reg(A_MOV, S_W, tmpref, GetNextReg(reg)));
  1713. end;
  1714. else
  1715. internalerror(2013030213);
  1716. end;
  1717. else
  1718. internalerror(2013030211);
  1719. end;
  1720. end;
  1721. procedure tcg8086.a_load_reg_reg(list : TAsmList;fromsize,tosize: tcgsize;reg1,reg2 : tregister);
  1722. procedure add_mov(instr: Taicpu);
  1723. begin
  1724. { Notify the register allocator that we have written a move instruction so
  1725. it can try to eliminate it. }
  1726. if (instr.oper[0]^.reg<>current_procinfo.framepointer) and (instr.oper[0]^.reg<>NR_STACK_POINTER_REG) then
  1727. add_move_instruction(instr);
  1728. list.concat(instr);
  1729. end;
  1730. begin
  1731. check_register_size(fromsize,reg1);
  1732. check_register_size(tosize,reg2);
  1733. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  1734. begin
  1735. if tosize in [OS_32, OS_S32] then
  1736. internalerror(2013031801);
  1737. reg1:=makeregsize(list,reg1,tosize);
  1738. fromsize:=tosize;
  1739. end;
  1740. if (reg1<>reg2) or (fromsize<>tosize) then
  1741. begin
  1742. case tosize of
  1743. OS_8,OS_S8:
  1744. if fromsize in [OS_8,OS_S8] then
  1745. begin
  1746. if reg1<>reg2 then
  1747. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, reg2));
  1748. end
  1749. else
  1750. internalerror(2013030210);
  1751. OS_16,OS_S16:
  1752. case fromsize of
  1753. OS_8:
  1754. begin
  1755. if current_settings.cputype>=cpu_386 then
  1756. add_mov(taicpu.op_reg_reg(A_MOVZX, S_BW, reg1, reg2))
  1757. else
  1758. begin
  1759. reg2 := makeregsize(list, reg2, OS_8);
  1760. if reg1<>reg2 then
  1761. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, reg2));
  1762. setsubreg(reg2,R_SUBH);
  1763. list.concat(taicpu.op_const_reg(A_MOV, S_B, 0, reg2));
  1764. makeregsize(list, reg2, OS_16);
  1765. end;
  1766. end;
  1767. OS_S8:
  1768. begin
  1769. if current_settings.cputype>=cpu_386 then
  1770. add_mov(taicpu.op_reg_reg(A_MOVSX, S_BW, reg1, reg2))
  1771. else
  1772. begin
  1773. getcpuregister(list, NR_AX);
  1774. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, NR_AL));
  1775. list.concat(taicpu.op_none(A_CBW));
  1776. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg2));
  1777. ungetcpuregister(list, NR_AX);
  1778. end;
  1779. end;
  1780. OS_16,OS_S16:
  1781. begin
  1782. if reg1<>reg2 then
  1783. add_mov(taicpu.op_reg_reg(A_MOV, S_W, reg1, reg2));
  1784. end
  1785. else
  1786. internalerror(2013030212);
  1787. end;
  1788. OS_32,OS_S32:
  1789. case fromsize of
  1790. OS_8:
  1791. begin
  1792. list.concat(taicpu.op_const_reg(A_MOV, S_W, 0, GetNextReg(reg2)));
  1793. if current_settings.cputype>=cpu_386 then
  1794. add_mov(taicpu.op_reg_reg(A_MOVZX, S_BW, reg1, reg2))
  1795. else
  1796. begin
  1797. reg2 := makeregsize(list, reg2, OS_8);
  1798. if reg1<>reg2 then
  1799. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, reg2));
  1800. setsubreg(reg2,R_SUBH);
  1801. list.concat(taicpu.op_const_reg(A_MOV, S_B, 0, reg2));
  1802. makeregsize(list, reg2, OS_16);
  1803. end;
  1804. end;
  1805. OS_S8:
  1806. begin
  1807. getcpuregister(list, NR_AX);
  1808. add_mov(taicpu.op_reg_reg(A_MOV, S_B, reg1, NR_AL));
  1809. getcpuregister(list, NR_DX);
  1810. list.concat(taicpu.op_none(A_CBW));
  1811. list.concat(taicpu.op_none(A_CWD));
  1812. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg2));
  1813. ungetcpuregister(list, NR_AX);
  1814. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_DX, GetNextReg(reg2)));
  1815. ungetcpuregister(list, NR_DX);
  1816. end;
  1817. OS_16:
  1818. begin
  1819. if reg1<>reg2 then
  1820. add_mov(taicpu.op_reg_reg(A_MOV, S_W, reg1, reg2));
  1821. list.concat(taicpu.op_const_reg(A_MOV,S_W,0,GetNextReg(reg2)));
  1822. end;
  1823. OS_S16:
  1824. begin
  1825. getcpuregister(list, NR_AX);
  1826. add_mov(taicpu.op_reg_reg(A_MOV, S_W, reg1, NR_AX));
  1827. getcpuregister(list, NR_DX);
  1828. list.concat(taicpu.op_none(A_CWD));
  1829. if reg1<>reg2 then
  1830. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_AX, reg2));
  1831. ungetcpuregister(list, NR_AX);
  1832. add_mov(taicpu.op_reg_reg(A_MOV, S_W, NR_DX, GetNextReg(reg2)));
  1833. ungetcpuregister(list, NR_DX);
  1834. end;
  1835. OS_32,OS_S32:
  1836. begin
  1837. if reg1<>reg2 then
  1838. begin
  1839. add_mov(taicpu.op_reg_reg(A_MOV, S_W, reg1, reg2));
  1840. add_mov(taicpu.op_reg_reg(A_MOV, S_W, GetNextReg(reg1), GetNextReg(reg2)));
  1841. end;
  1842. end;
  1843. else
  1844. internalerror(2013030213);
  1845. end;
  1846. else
  1847. internalerror(2013030211);
  1848. end;
  1849. end;
  1850. end;
  1851. procedure tcg8086.a_cmp_const_reg_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1852. var
  1853. hl_skip: TAsmLabel;
  1854. begin
  1855. if size in [OS_32, OS_S32] then
  1856. begin
  1857. if (longint(a shr 16) = 0) then
  1858. list.concat(taicpu.op_reg_reg(A_TEST,S_W,GetNextReg(reg),GetNextReg(reg)))
  1859. else
  1860. list.concat(taicpu.op_const_reg(A_CMP,S_W,longint(a shr 16),GetNextReg(reg)));
  1861. current_asmdata.getjumplabel(hl_skip);
  1862. gen_cmp32_jmp1(list, cmp_op, hl_skip, l);
  1863. if (longint(a and $ffff) = 0) then
  1864. list.concat(taicpu.op_reg_reg(A_TEST,S_W,reg,reg))
  1865. else
  1866. list.concat(taicpu.op_const_reg(A_CMP,S_W,longint(a and $ffff),reg));
  1867. gen_cmp32_jmp2(list, cmp_op, hl_skip, l);
  1868. a_label(list,hl_skip);
  1869. end
  1870. else
  1871. inherited a_cmp_const_reg_label(list, size, cmp_op, a, reg, l);
  1872. end;
  1873. procedure tcg8086.a_cmp_const_ref_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel);
  1874. var
  1875. tmpref: treference;
  1876. hl_skip: TAsmLabel;
  1877. begin
  1878. if size in [OS_32, OS_S32] then
  1879. begin
  1880. tmpref:=ref;
  1881. make_simple_ref(list,tmpref);
  1882. inc(tmpref.offset,2);
  1883. list.concat(taicpu.op_const_ref(A_CMP,S_W,longint(a shr 16),tmpref));
  1884. current_asmdata.getjumplabel(hl_skip);
  1885. gen_cmp32_jmp1(list, cmp_op, hl_skip, l);
  1886. dec(tmpref.offset,2);
  1887. list.concat(taicpu.op_const_ref(A_CMP,S_W,longint(a and $ffff),tmpref));
  1888. gen_cmp32_jmp2(list, cmp_op, hl_skip, l);
  1889. a_label(list,hl_skip);
  1890. end
  1891. else
  1892. inherited a_cmp_const_ref_label(list, size, cmp_op, a, ref, l);
  1893. end;
  1894. procedure tcg8086.a_cmp_reg_reg_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1895. var
  1896. hl_skip: TAsmLabel;
  1897. begin
  1898. if size in [OS_32, OS_S32] then
  1899. begin
  1900. check_register_size(size,reg1);
  1901. check_register_size(size,reg2);
  1902. list.concat(taicpu.op_reg_reg(A_CMP,S_W,GetNextReg(reg1),GetNextReg(reg2)));
  1903. current_asmdata.getjumplabel(hl_skip);
  1904. gen_cmp32_jmp1(list, cmp_op, hl_skip, l);
  1905. list.concat(taicpu.op_reg_reg(A_CMP,S_W,reg1,reg2));
  1906. gen_cmp32_jmp2(list, cmp_op, hl_skip, l);
  1907. a_label(list,hl_skip);
  1908. end
  1909. else
  1910. inherited a_cmp_reg_reg_label(list, size, cmp_op, reg1, reg2, l);
  1911. end;
  1912. procedure tcg8086.a_cmp_ref_reg_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  1913. var
  1914. tmpref: treference;
  1915. hl_skip: TAsmLabel;
  1916. begin
  1917. if size in [OS_32, OS_S32] then
  1918. begin
  1919. tmpref:=ref;
  1920. make_simple_ref(list,tmpref);
  1921. check_register_size(size,reg);
  1922. inc(tmpref.offset,2);
  1923. list.concat(taicpu.op_ref_reg(A_CMP,S_W,tmpref,GetNextReg(reg)));
  1924. current_asmdata.getjumplabel(hl_skip);
  1925. gen_cmp32_jmp1(list, cmp_op, hl_skip, l);
  1926. dec(tmpref.offset,2);
  1927. list.concat(taicpu.op_ref_reg(A_CMP,S_W,tmpref,reg));
  1928. gen_cmp32_jmp2(list, cmp_op, hl_skip, l);
  1929. a_label(list,hl_skip);
  1930. end
  1931. else
  1932. inherited a_cmp_ref_reg_label(list, size, cmp_op, ref, reg, l);
  1933. end;
  1934. procedure tcg8086.a_cmp_reg_ref_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  1935. var
  1936. tmpref: treference;
  1937. hl_skip: TAsmLabel;
  1938. begin
  1939. if size in [OS_32, OS_S32] then
  1940. begin
  1941. tmpref:=ref;
  1942. make_simple_ref(list,tmpref);
  1943. check_register_size(size,reg);
  1944. inc(tmpref.offset,2);
  1945. list.concat(taicpu.op_reg_ref(A_CMP,S_W,GetNextReg(reg),tmpref));
  1946. current_asmdata.getjumplabel(hl_skip);
  1947. gen_cmp32_jmp1(list, cmp_op, hl_skip, l);
  1948. dec(tmpref.offset,2);
  1949. list.concat(taicpu.op_reg_ref(A_CMP,S_W,reg,tmpref));
  1950. gen_cmp32_jmp2(list, cmp_op, hl_skip, l);
  1951. a_label(list,hl_skip);
  1952. end
  1953. else
  1954. inherited a_cmp_reg_ref_label(list, size, cmp_op, reg, ref, l);
  1955. end;
  1956. procedure tcg8086.gen_cmp32_jmp1(list: TAsmList; cmp_op: topcmp; l_skip, l_target: TAsmLabel);
  1957. begin
  1958. case cmp_op of
  1959. OC_EQ:
  1960. a_jmp_cond(list, OC_NE, l_skip);
  1961. OC_NE:
  1962. a_jmp_cond(list, OC_NE, l_target);
  1963. OC_GT,OC_GTE:
  1964. begin
  1965. a_jmp_cond(list, OC_GT, l_target);
  1966. a_jmp_cond(list, OC_LT, l_skip);
  1967. end;
  1968. OC_LT,OC_LTE:
  1969. begin
  1970. a_jmp_cond(list, OC_LT, l_target);
  1971. a_jmp_cond(list, OC_GT, l_skip);
  1972. end;
  1973. OC_B,OC_BE:
  1974. begin
  1975. a_jmp_cond(list, OC_B, l_target);
  1976. a_jmp_cond(list, OC_A, l_skip);
  1977. end;
  1978. OC_A,OC_AE:
  1979. begin
  1980. a_jmp_cond(list, OC_A, l_target);
  1981. a_jmp_cond(list, OC_B, l_skip);
  1982. end;
  1983. else
  1984. internalerror(2014010305);
  1985. end;
  1986. end;
  1987. procedure tcg8086.gen_cmp32_jmp2(list: TAsmList; cmp_op: topcmp; l_skip, l_target: TAsmLabel);
  1988. begin
  1989. case cmp_op of
  1990. OC_EQ:
  1991. a_jmp_cond(list, OC_EQ, l_target);
  1992. OC_GT:
  1993. a_jmp_cond(list, OC_A, l_target);
  1994. OC_LT:
  1995. a_jmp_cond(list, OC_B, l_target);
  1996. OC_GTE:
  1997. a_jmp_cond(list, OC_AE, l_target);
  1998. OC_LTE:
  1999. a_jmp_cond(list, OC_BE, l_target);
  2000. OC_NE:
  2001. a_jmp_cond(list, OC_NE, l_target);
  2002. OC_BE:
  2003. a_jmp_cond(list, OC_BE, l_target);
  2004. OC_B:
  2005. a_jmp_cond(list, OC_B, l_target);
  2006. OC_AE:
  2007. a_jmp_cond(list, OC_AE, l_target);
  2008. OC_A:
  2009. a_jmp_cond(list, OC_A, l_target);
  2010. else
  2011. internalerror(2014010306);
  2012. end;
  2013. end;
  2014. procedure tcg8086.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  2015. var
  2016. ai : taicpu;
  2017. hreg16 : tregister;
  2018. hl_skip: TAsmLabel;
  2019. invf: TResFlags;
  2020. tmpsize: TCgSize;
  2021. tmpopsize: topsize;
  2022. begin
  2023. { optimized case for the carry flag, using ADC/RCL }
  2024. if f in [F_C,F_B,F_FB] then
  2025. begin
  2026. case size of
  2027. OS_8,OS_S8:
  2028. begin
  2029. tmpsize:=OS_8;
  2030. tmpopsize:=S_B;
  2031. end;
  2032. OS_16,OS_S16,OS_32,OS_S32:
  2033. begin
  2034. tmpsize:=OS_16;
  2035. tmpopsize:=S_W;
  2036. end;
  2037. else
  2038. internalerror(2013123101);
  2039. end;
  2040. list.concat(Taicpu.op_const_reg(A_MOV, tmpopsize, 0, reg));
  2041. hl_skip:=nil;
  2042. if f=F_FB then
  2043. begin
  2044. current_asmdata.getjumplabel(hl_skip);
  2045. ai:=Taicpu.op_sym(A_Jcc,S_NO,hl_skip);
  2046. ai.SetCondition(C_P);
  2047. ai.is_jmp:=true;
  2048. list.concat(ai);
  2049. end;
  2050. { RCL is faster than ADC on 8086/8088. On the 80286, it is
  2051. equally fast and it also has the same size. In these cases,
  2052. we still prefer it over ADC, because it's a better choice in
  2053. case the register is spilled. }
  2054. if (cs_opt_size in current_settings.optimizerswitches) or
  2055. (current_settings.optimizecputype<=cpu_286) then
  2056. list.concat(Taicpu.op_const_reg(A_RCL, tmpopsize, 1, reg))
  2057. else
  2058. { ADC is much faster on the 386. }
  2059. list.concat(Taicpu.op_reg_reg(A_ADC, tmpopsize, reg, reg));
  2060. if f=F_FB then
  2061. a_label(list,hl_skip);
  2062. a_load_reg_reg(list,tmpsize,size,reg,reg);
  2063. end
  2064. { optimized case for the inverted carry flag, using SBB }
  2065. else if f in [F_NC,F_AE,F_FAE] then
  2066. begin
  2067. case size of
  2068. OS_8,OS_S8:
  2069. begin
  2070. tmpsize:=OS_8;
  2071. list.concat(Taicpu.op_const_reg(A_MOV, S_B, 1, reg));
  2072. list.concat(Taicpu.op_const_reg(A_SBB, S_B, 0, reg));
  2073. end;
  2074. OS_16,OS_S16,OS_32,OS_S32:
  2075. begin
  2076. tmpsize:=OS_16;
  2077. list.concat(Taicpu.op_const_reg(A_MOV, S_W, 1, reg));
  2078. list.concat(Taicpu.op_const_reg(A_SBB, S_W, 0, reg));
  2079. end;
  2080. else
  2081. internalerror(2013123101);
  2082. end;
  2083. a_load_reg_reg(list,tmpsize,size,reg,reg);
  2084. end
  2085. else
  2086. begin
  2087. invf := f;
  2088. inverse_flags(invf);
  2089. case size of
  2090. OS_8,OS_S8:
  2091. begin
  2092. tmpsize:=OS_8;
  2093. list.concat(Taicpu.op_const_reg(A_MOV, S_B, 0, reg));
  2094. end;
  2095. OS_16,OS_S16,OS_32,OS_S32:
  2096. begin
  2097. tmpsize:=OS_16;
  2098. list.concat(Taicpu.op_const_reg(A_MOV, S_W, 0, reg));
  2099. end;
  2100. else
  2101. internalerror(2013123101);
  2102. end;
  2103. current_asmdata.getjumplabel(hl_skip);
  2104. { we can't just forward invf to a_jmp_flags for FA,FAE,FB and FBE, because
  2105. in the case of NaNs:
  2106. not(F_FA )<>F_FBE
  2107. not(F_FAE)<>F_FB
  2108. not(F_FB )<>F_FAE
  2109. not(F_FBE)<>F_FA
  2110. }
  2111. case f of
  2112. F_FA:
  2113. invf:=FPUFlags2Flags[invf];
  2114. F_FAE,F_FB:
  2115. { F_FAE and F_FB are handled above, using ADC/RCL/SBB }
  2116. internalerror(2015102101);
  2117. F_FBE:
  2118. begin
  2119. ai:=Taicpu.op_sym(A_Jcc,S_NO,hl_skip);
  2120. ai.SetCondition(C_P);
  2121. ai.is_jmp:=true;
  2122. list.concat(ai);
  2123. invf:=FPUFlags2Flags[invf];
  2124. end;
  2125. end;
  2126. a_jmp_flags(list,invf,hl_skip);
  2127. { 16-bit INC is shorter than 8-bit }
  2128. hreg16:=makeregsize(list,reg,OS_16);
  2129. list.concat(Taicpu.op_reg(A_INC, S_W, hreg16));
  2130. makeregsize(list,hreg16,tmpsize);
  2131. a_label(list,hl_skip);
  2132. a_load_reg_reg(list,tmpsize,size,reg,reg);
  2133. end;
  2134. end;
  2135. procedure tcg8086.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  2136. var
  2137. tmpreg : tregister;
  2138. tmpregsize: TCgSize;
  2139. tmpref: treference;
  2140. begin
  2141. if size in [OS_8,OS_S8,OS_16,OS_S16] then
  2142. tmpregsize:=size
  2143. else
  2144. tmpregsize:=OS_16;
  2145. tmpreg:=getintregister(list,tmpregsize);
  2146. g_flags2reg(list,tmpregsize,f,tmpreg);
  2147. tmpref:=ref;
  2148. make_simple_ref(list,tmpref);
  2149. if size in [OS_64,OS_S64] then
  2150. begin
  2151. a_load_reg_ref(list,tmpregsize,OS_32,tmpreg,tmpref);
  2152. inc(tmpref.offset,4);
  2153. a_load_const_ref(list,OS_32,0,tmpref);
  2154. end
  2155. else
  2156. a_load_reg_ref(list,tmpregsize,size,tmpreg,tmpref);
  2157. end;
  2158. procedure tcg8086.g_stackpointer_alloc(list : TAsmList;localsize: longint);
  2159. begin
  2160. if cs_check_stack in current_settings.localswitches then
  2161. begin
  2162. cg.getcpuregister(list,NR_AX);
  2163. cg.a_load_const_reg(list,OS_16, localsize,NR_AX);
  2164. cg.a_call_name(list,'FPC_STACKCHECK_I8086',false);
  2165. cg.ungetcpuregister(list, NR_AX);
  2166. end;
  2167. if localsize>0 then
  2168. list.concat(Taicpu.Op_const_reg(A_SUB,S_W,localsize,NR_STACK_POINTER_REG));
  2169. end;
  2170. procedure tcg8086.g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);
  2171. var
  2172. stacksize : longint;
  2173. ret_instr: TAsmOp;
  2174. sp_moved : boolean;
  2175. procedure maybe_move_sp;
  2176. var
  2177. ref : treference;
  2178. begin
  2179. if sp_moved then
  2180. exit;
  2181. if not(pi_has_open_array_parameter in current_procinfo.flags) then
  2182. exit;
  2183. { Restore SP position before SP change }
  2184. if current_settings.x86memorymodel=mm_huge then
  2185. stacksize:=stacksize + 2;
  2186. reference_reset_base(ref,NR_BP,-stacksize,2,[]);
  2187. list.concat(Taicpu.op_ref_reg(A_LEA,S_W,ref,NR_SP));
  2188. sp_moved:=true;
  2189. end;
  2190. begin
  2191. if is_proc_far(current_procinfo.procdef) then
  2192. ret_instr:=A_RETF
  2193. else
  2194. ret_instr:=A_RET;
  2195. { MMX needs to call EMMS }
  2196. if assigned(rg[R_MMXREGISTER]) and
  2197. (rg[R_MMXREGISTER].uses_registers) then
  2198. list.concat(Taicpu.op_none(A_EMMS,S_NO));
  2199. sp_moved:=false;
  2200. { remove stackframe }
  2201. if not nostackframe then
  2202. begin
  2203. stacksize:=current_procinfo.calc_stackframe_size;
  2204. if (target_info.stackalign>4) and
  2205. ((stacksize <> 0) or
  2206. (pi_do_call in current_procinfo.flags) or
  2207. { can't detect if a call in this case -> use nostackframe }
  2208. { if you (think you) know what you are doing }
  2209. (po_assembler in current_procinfo.procdef.procoptions)) then
  2210. stacksize := align(stacksize+sizeof(aint),target_info.stackalign) - sizeof(aint);
  2211. if (po_exports in current_procinfo.procdef.procoptions) and
  2212. (target_info.system=system_i8086_win16) then
  2213. begin
  2214. maybe_move_sp;
  2215. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DI));
  2216. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_SI));
  2217. end;
  2218. if ((current_settings.x86memorymodel=mm_huge) and
  2219. not (po_interrupt in current_procinfo.procdef.procoptions)) or
  2220. ((po_exports in current_procinfo.procdef.procoptions) and
  2221. (target_info.system=system_i8086_win16)) then
  2222. begin
  2223. maybe_move_sp;
  2224. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  2225. end;
  2226. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  2227. begin
  2228. if (stacksize<>0) then
  2229. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,stacksize,current_procinfo.framepointer);
  2230. end
  2231. else
  2232. begin
  2233. generate_leave(list);
  2234. if ((ts_x86_far_procs_push_odd_bp in current_settings.targetswitches) or
  2235. ((po_exports in current_procinfo.procdef.procoptions) and
  2236. (target_info.system=system_i8086_win16))) and
  2237. is_proc_far(current_procinfo.procdef) then
  2238. cg.a_op_const_reg(list,OP_SUB,OS_ADDR,1,current_procinfo.framepointer);
  2239. end;
  2240. list.concat(tai_regalloc.dealloc(current_procinfo.framepointer,nil));
  2241. end;
  2242. { return from interrupt }
  2243. if po_interrupt in current_procinfo.procdef.procoptions then
  2244. begin
  2245. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_ES));
  2246. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  2247. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DI));
  2248. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_SI));
  2249. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DX));
  2250. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_CX));
  2251. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_BX));
  2252. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_AX));
  2253. list.concat(Taicpu.Op_none(A_IRET,S_NO));
  2254. end
  2255. { Routines with the poclearstack flag set use only a ret }
  2256. else if (current_procinfo.procdef.proccalloption in clearstack_pocalls) and
  2257. (not paramanager.use_fixed_stack) then
  2258. begin
  2259. { complex return values are removed from stack in C code PM }
  2260. { but not on win32 }
  2261. { and not for safecall with hidden exceptions, because the result }
  2262. { wich contains the exception is passed in EAX }
  2263. if (target_info.system <> system_i386_win32) and
  2264. not ((current_procinfo.procdef.proccalloption = pocall_safecall) and
  2265. (tf_safecall_exceptions in target_info.flags)) and
  2266. paramanager.ret_in_param(current_procinfo.procdef.returndef,
  2267. current_procinfo.procdef) then
  2268. list.concat(Taicpu.Op_const(ret_instr,S_W,sizeof(aint)))
  2269. else
  2270. list.concat(Taicpu.Op_none(ret_instr,S_NO));
  2271. end
  2272. { ... also routines with parasize=0 }
  2273. else if (parasize=0) then
  2274. list.concat(Taicpu.Op_none(ret_instr,S_NO))
  2275. else
  2276. begin
  2277. { parameters are limited to 65535 bytes because ret allows only imm16 }
  2278. if (parasize>65535) then
  2279. CGMessage(cg_e_parasize_too_big);
  2280. list.concat(Taicpu.Op_const(ret_instr,S_W,parasize));
  2281. end;
  2282. end;
  2283. procedure tcg8086.g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:tcgint;destreg:tregister);
  2284. var
  2285. power : longint;
  2286. opsize : topsize;
  2287. saved_ds: Boolean;
  2288. begin
  2289. { get stack space }
  2290. getcpuregister(list,NR_DI);
  2291. a_load_loc_reg(list,OS_INT,lenloc,NR_DI);
  2292. list.concat(Taicpu.op_reg(A_INC,S_W,NR_DI));
  2293. { Now DI contains (high+1). }
  2294. include(current_procinfo.flags, pi_has_open_array_parameter);
  2295. { special case handling for elesize=2:
  2296. set CX = (high+1) instead of CX = (high+1)*elesize.
  2297. This allows us to avoid the SHR later. }
  2298. if elesize=2 then
  2299. begin
  2300. { Now DI contains (high+1). Copy it to CX for later use. }
  2301. getcpuregister(list,NR_CX);
  2302. list.concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_DI,NR_CX));
  2303. end;
  2304. { DI := DI * elesize }
  2305. if (elesize<>1) then
  2306. begin
  2307. if ispowerof2(elesize, power) then
  2308. a_op_const_reg(list,OP_SHL,OS_16,power,NR_DI)
  2309. else
  2310. a_op_const_reg(list,OP_IMUL,OS_16,elesize,NR_DI);
  2311. end;
  2312. if elesize<>2 then
  2313. begin
  2314. { Now DI contains (high+1)*elesize. Copy it to CX for later use. }
  2315. getcpuregister(list,NR_CX);
  2316. list.concat(Taicpu.op_reg_reg(A_MOV,S_W,NR_DI,NR_CX));
  2317. end;
  2318. { If we were probing pages, EDI=(size mod pagesize) and ESP is decremented
  2319. by (size div pagesize)*pagesize, otherwise EDI=size.
  2320. Either way, subtracting EDI from ESP will set ESP to desired final value. }
  2321. list.concat(Taicpu.op_reg_reg(A_SUB,S_W,NR_DI,NR_SP));
  2322. { align stack on 2 bytes }
  2323. list.concat(Taicpu.op_const_reg(A_AND,S_W,aint($fffe),NR_SP));
  2324. { load destination, don't use a_load_reg_reg, that will add a move instruction
  2325. that can confuse the reg allocator }
  2326. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_SP,NR_DI));
  2327. {$ifdef volatile_es}
  2328. list.concat(taicpu.op_reg(A_PUSH,S_W,NR_SS));
  2329. list.concat(taicpu.op_reg(A_POP,S_W,NR_ES));
  2330. {$endif volatile_es}
  2331. { Allocate SI and load it with source }
  2332. getcpuregister(list,NR_SI);
  2333. if ((ref.segment=NR_NO) and (segment_regs_equal(NR_SS,NR_DS) or (ref.base<>NR_BP))) or
  2334. (is_segment_reg(ref.segment) and segment_regs_equal(ref.segment,NR_DS)) then
  2335. begin
  2336. hlcg.a_loadaddr_ref_reg(list,voidnearpointertype,voidnearpointertype,ref,NR_SI);
  2337. saved_ds:=false;
  2338. end
  2339. else
  2340. begin
  2341. hlcg.a_loadaddr_ref_reg(list,voidnearpointertype,voidnearpointertype,ref,NR_SI);
  2342. list.concat(taicpu.op_reg(A_PUSH,S_W,NR_DS));
  2343. saved_ds:=true;
  2344. if ref.segment<>NR_NO then
  2345. list.concat(taicpu.op_reg(A_PUSH,S_W,ref.segment))
  2346. else if ref.base=NR_BP then
  2347. list.concat(taicpu.op_reg(A_PUSH,S_W,NR_SS))
  2348. else
  2349. internalerror(2014040403);
  2350. list.concat(taicpu.op_reg(A_POP,S_W,NR_DS));
  2351. end;
  2352. { calculate size }
  2353. opsize:=S_B;
  2354. if elesize=2 then
  2355. begin
  2356. opsize:=S_W;
  2357. { CX is already number of words, so no need to SHL/SHR }
  2358. end
  2359. else if (elesize and 1)=0 then
  2360. begin
  2361. opsize:=S_W;
  2362. { CX is number of bytes, convert to words }
  2363. list.concat(Taicpu.op_const_reg(A_SHR,S_W,1,NR_CX))
  2364. end;
  2365. if ts_cld in current_settings.targetswitches then
  2366. list.concat(Taicpu.op_none(A_CLD,S_NO));
  2367. if (opsize=S_B) and not (cs_opt_size in current_settings.optimizerswitches) then
  2368. begin
  2369. { SHR CX,1 moves the lowest (odd/even) bit to the carry flag }
  2370. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2371. list.concat(Taicpu.op_const_reg(A_SHR,S_W,1,NR_CX));
  2372. list.concat(Taicpu.op_none(A_REP,S_NO));
  2373. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2374. { ADC CX,CX will set CX to 1 if the number of bytes was odd }
  2375. list.concat(Taicpu.op_reg_reg(A_ADC,S_W,NR_CX,NR_CX));
  2376. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2377. list.concat(Taicpu.op_none(A_REP,S_NO));
  2378. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2379. end
  2380. else
  2381. begin
  2382. list.concat(Taicpu.op_none(A_REP,S_NO));
  2383. case opsize of
  2384. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  2385. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  2386. end;
  2387. end;
  2388. ungetcpuregister(list,NR_DI);
  2389. ungetcpuregister(list,NR_CX);
  2390. ungetcpuregister(list,NR_SI);
  2391. if saved_ds then
  2392. list.concat(taicpu.op_reg(A_POP,S_W,NR_DS));
  2393. { patch the new address, but don't use a_load_reg_reg, that will add a move instruction
  2394. that can confuse the reg allocator }
  2395. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_SP,destreg));
  2396. if current_settings.x86memorymodel in x86_far_data_models then
  2397. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_SS,GetNextReg(destreg)));
  2398. end;
  2399. procedure tcg8086.g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  2400. begin
  2401. { Nothing to do }
  2402. end;
  2403. procedure tcg8086.get_32bit_ops(op: TOpCG; out op1, op2: TAsmOp);
  2404. begin
  2405. case op of
  2406. OP_ADD :
  2407. begin
  2408. op1:=A_ADD;
  2409. op2:=A_ADC;
  2410. end;
  2411. OP_SUB :
  2412. begin
  2413. op1:=A_SUB;
  2414. op2:=A_SBB;
  2415. end;
  2416. OP_XOR :
  2417. begin
  2418. op1:=A_XOR;
  2419. op2:=A_XOR;
  2420. end;
  2421. OP_OR :
  2422. begin
  2423. op1:=A_OR;
  2424. op2:=A_OR;
  2425. end;
  2426. OP_AND :
  2427. begin
  2428. op1:=A_AND;
  2429. op2:=A_AND;
  2430. end;
  2431. else
  2432. internalerror(200203241);
  2433. end;
  2434. end;
  2435. procedure tcg8086.add_move_instruction(instr: Taicpu);
  2436. begin
  2437. { HACK: when regvars are on, don't notify the register allocator of any
  2438. direct moves to BX, so it doesn't try to coalesce them. Currently,
  2439. direct moves to BX are only used when returning an int64 value in
  2440. AX:BX:CX:DX. This hack fixes a common issue with functions, returning
  2441. int64, for example:
  2442. function RandomFrom(const AValues: array of Int64): Int64;
  2443. begin
  2444. result:=AValues[random(High(AValues)+1)];
  2445. end;
  2446. push bp
  2447. mov bp,sp
  2448. ; Var AValues located in register ireg20w
  2449. ; Var $highAVALUES located in register ireg21w
  2450. ; Var $result located in register ireg33w:ireg32w:ireg31w:ireg30w
  2451. mov ireg20w,word [bp+6]
  2452. mov ireg21w,word [bp+4]
  2453. ; [3] result:=AValues[random(High(AValues)+1)];
  2454. mov ireg22w,ireg21w
  2455. inc ireg22w
  2456. mov ax,ireg22w
  2457. cwd
  2458. mov ireg23w,ax
  2459. mov ireg24w,dx
  2460. push ireg24w
  2461. push ireg23w
  2462. call SYSTEM_$$_RANDOM$LONGINT$$LONGINT
  2463. mov ireg25w,ax
  2464. mov ireg26w,dx
  2465. mov ireg27w,ireg25w
  2466. mov ireg28w,ireg27w
  2467. mov ireg29w,ireg28w
  2468. mov cl,3
  2469. shl ireg29w,cl
  2470. ; Var $result located in register ireg32w:ireg30w
  2471. mov ireg30w,word [ireg20w+ireg29w]
  2472. mov ireg31w,word [ireg20w+ireg29w+2]
  2473. mov ireg32w,word [ireg20w+ireg29w+4] ; problematic section start
  2474. mov ireg33w,word [ireg20w+ireg29w+6]
  2475. ; [4] end;
  2476. mov bx,ireg32w ; problematic section end
  2477. mov ax,ireg33w
  2478. mov dx,ireg30w
  2479. mov cx,ireg31w
  2480. mov sp,bp
  2481. pop bp
  2482. ret 4
  2483. the problem arises, because the register allocator tries to coalesce
  2484. mov bx,ireg32w
  2485. however, in the references [ireg20w+ireg29w+const], due to the
  2486. constraints of i8086, ireg20w can only be BX (or BP, which isn't available
  2487. to the register allocator, because it's used as a base pointer) }
  2488. if (cs_opt_regvar in current_settings.optimizerswitches) and
  2489. (instr.opcode=A_MOV) and (instr.ops=2) and
  2490. (instr.oper[1]^.typ=top_reg) and (getsupreg(instr.oper[1]^.reg)=RS_BX) then
  2491. exit
  2492. else
  2493. inherited add_move_instruction(instr);
  2494. end;
  2495. procedure tcg8086.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2496. var
  2497. hsym : tsym;
  2498. href : treference;
  2499. paraloc : Pcgparalocation;
  2500. return_address_size: Integer;
  2501. begin
  2502. if current_settings.x86memorymodel in x86_far_code_models then
  2503. return_address_size:=4
  2504. else
  2505. return_address_size:=2;
  2506. { calculate the parameter info for the procdef }
  2507. procdef.init_paraloc_info(callerside);
  2508. hsym:=tsym(procdef.parast.Find('self'));
  2509. if not(assigned(hsym) and
  2510. (hsym.typ=paravarsym)) then
  2511. internalerror(200305251);
  2512. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2513. with paraloc^ do
  2514. begin
  2515. case loc of
  2516. LOC_REGISTER:
  2517. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2518. LOC_REFERENCE:
  2519. begin
  2520. { offset in the wrapper needs to be adjusted for the stored
  2521. return address }
  2522. if (reference.index<>NR_BP) and (reference.index<>NR_BX) and (reference.index<>NR_DI)
  2523. and (reference.index<>NR_SI) then
  2524. begin
  2525. list.concat(taicpu.op_reg(A_PUSH,S_W,NR_DI));
  2526. list.concat(taicpu.op_reg_reg(A_MOV,S_W,reference.index,NR_DI));
  2527. if reference.index=NR_SP then
  2528. reference_reset_base(href,NR_DI,reference.offset+return_address_size+2,sizeof(pint),[])
  2529. else
  2530. reference_reset_base(href,NR_DI,reference.offset+return_address_size,sizeof(pint),[]);
  2531. href.segment:=NR_SS;
  2532. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  2533. list.concat(taicpu.op_reg(A_POP,S_W,NR_DI));
  2534. end
  2535. else
  2536. begin
  2537. reference_reset_base(href,reference.index,reference.offset+return_address_size,sizeof(pint),[]);
  2538. href.segment:=NR_SS;
  2539. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  2540. end;
  2541. end
  2542. else
  2543. internalerror(200309189);
  2544. end;
  2545. paraloc:=next;
  2546. end;
  2547. end;
  2548. { ************* 64bit operations ************ }
  2549. procedure tcg64f8086.get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  2550. begin
  2551. case op of
  2552. OP_ADD :
  2553. begin
  2554. op1:=A_ADD;
  2555. op2:=A_ADC;
  2556. end;
  2557. OP_SUB :
  2558. begin
  2559. op1:=A_SUB;
  2560. op2:=A_SBB;
  2561. end;
  2562. OP_XOR :
  2563. begin
  2564. op1:=A_XOR;
  2565. op2:=A_XOR;
  2566. end;
  2567. OP_OR :
  2568. begin
  2569. op1:=A_OR;
  2570. op2:=A_OR;
  2571. end;
  2572. OP_AND :
  2573. begin
  2574. op1:=A_AND;
  2575. op2:=A_AND;
  2576. end;
  2577. else
  2578. internalerror(200203241);
  2579. end;
  2580. end;
  2581. procedure tcg64f8086.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  2582. var
  2583. op1,op2 : TAsmOp;
  2584. tempref : treference;
  2585. begin
  2586. if not(op in [OP_NEG,OP_NOT]) then
  2587. begin
  2588. get_64bit_ops(op,op1,op2);
  2589. tempref:=ref;
  2590. tcgx86(cg).make_simple_ref(list,tempref);
  2591. if op in [OP_ADD,OP_SUB] then
  2592. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2593. list.concat(taicpu.op_ref_reg(op1,S_W,tempref,reg.reglo));
  2594. inc(tempref.offset,2);
  2595. list.concat(taicpu.op_ref_reg(op2,S_W,tempref,GetNextReg(reg.reglo)));
  2596. inc(tempref.offset,2);
  2597. list.concat(taicpu.op_ref_reg(op2,S_W,tempref,reg.reghi));
  2598. inc(tempref.offset,2);
  2599. list.concat(taicpu.op_ref_reg(op2,S_W,tempref,GetNextReg(reg.reghi)));
  2600. if op in [OP_ADD,OP_SUB] then
  2601. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2602. end
  2603. else
  2604. begin
  2605. a_load64_ref_reg(list,ref,reg);
  2606. a_op64_reg_reg(list,op,size,reg,reg);
  2607. end;
  2608. end;
  2609. procedure tcg64f8086.a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64; const ref: treference);
  2610. var
  2611. op1,op2 : TAsmOp;
  2612. tempref : treference;
  2613. begin
  2614. case op of
  2615. OP_NOT:
  2616. begin
  2617. tempref:=ref;
  2618. tcgx86(cg).make_simple_ref(list,tempref);
  2619. list.concat(taicpu.op_ref(A_NOT,S_W,tempref));
  2620. inc(tempref.offset,2);
  2621. list.concat(taicpu.op_ref(A_NOT,S_W,tempref));
  2622. inc(tempref.offset,2);
  2623. list.concat(taicpu.op_ref(A_NOT,S_W,tempref));
  2624. inc(tempref.offset,2);
  2625. list.concat(taicpu.op_ref(A_NOT,S_W,tempref));
  2626. end;
  2627. OP_NEG:
  2628. begin
  2629. tempref:=ref;
  2630. tcgx86(cg).make_simple_ref(list,tempref);
  2631. inc(tempref.offset,6);
  2632. list.concat(taicpu.op_ref(A_NOT,S_W,tempref));
  2633. dec(tempref.offset,2);
  2634. list.concat(taicpu.op_ref(A_NOT,S_W,tempref));
  2635. dec(tempref.offset,2);
  2636. list.concat(taicpu.op_ref(A_NOT,S_W,tempref));
  2637. dec(tempref.offset,2);
  2638. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2639. list.concat(taicpu.op_ref(A_NEG,S_W,tempref));
  2640. inc(tempref.offset,2);
  2641. list.concat(taicpu.op_const_ref(A_SBB,S_W,-1,tempref));
  2642. inc(tempref.offset,2);
  2643. list.concat(taicpu.op_const_ref(A_SBB,S_W,-1,tempref));
  2644. inc(tempref.offset,2);
  2645. list.concat(taicpu.op_const_ref(A_SBB,S_W,-1,tempref));
  2646. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2647. end;
  2648. else
  2649. begin
  2650. get_64bit_ops(op,op1,op2);
  2651. tempref:=ref;
  2652. tcgx86(cg).make_simple_ref(list,tempref);
  2653. if op in [OP_ADD,OP_SUB] then
  2654. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2655. list.concat(taicpu.op_reg_ref(op1,S_W,reg.reglo,tempref));
  2656. inc(tempref.offset,2);
  2657. list.concat(taicpu.op_reg_ref(op2,S_W,GetNextReg(reg.reglo),tempref));
  2658. inc(tempref.offset,2);
  2659. list.concat(taicpu.op_reg_ref(op2,S_W,reg.reghi,tempref));
  2660. inc(tempref.offset,2);
  2661. list.concat(taicpu.op_reg_ref(op2,S_W,GetNextReg(reg.reghi),tempref));
  2662. if op in [OP_ADD,OP_SUB] then
  2663. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2664. end;
  2665. end;
  2666. end;
  2667. procedure tcg64f8086.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  2668. var
  2669. op1,op2 : TAsmOp;
  2670. l2, l3: TAsmLabel;
  2671. ai: taicpu;
  2672. begin
  2673. case op of
  2674. OP_NEG :
  2675. begin
  2676. if (regsrc.reglo<>regdst.reglo) then
  2677. a_load64_reg_reg(list,regsrc,regdst);
  2678. cg.a_op_reg_reg(list,OP_NOT,OS_32,regdst.reghi,regdst.reghi);
  2679. list.concat(taicpu.op_reg(A_NOT,S_W,GetNextReg(regdst.reglo)));
  2680. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2681. list.concat(taicpu.op_reg(A_NEG,S_W,regdst.reglo));
  2682. list.concat(taicpu.op_const_reg(A_SBB,S_W,-1,GetNextReg(regdst.reglo)));
  2683. list.concat(taicpu.op_const_reg(A_SBB,S_W,-1,regdst.reghi));
  2684. list.concat(taicpu.op_const_reg(A_SBB,S_W,-1,GetNextReg(regdst.reghi)));
  2685. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2686. exit;
  2687. end;
  2688. OP_NOT :
  2689. begin
  2690. if (regsrc.reglo<>regdst.reglo) then
  2691. a_load64_reg_reg(list,regsrc,regdst);
  2692. cg.a_op_reg_reg(list,OP_NOT,OS_32,regdst.reglo,regdst.reglo);
  2693. cg.a_op_reg_reg(list,OP_NOT,OS_32,regdst.reghi,regdst.reghi);
  2694. exit;
  2695. end;
  2696. OP_SHR,OP_SHL,OP_SAR:
  2697. begin
  2698. { load right operators in a register }
  2699. cg.getcpuregister(list,NR_CX);
  2700. cg.a_load_reg_reg(list,OS_16,OS_16,regsrc.reglo,NR_CX);
  2701. current_asmdata.getjumplabel(l2);
  2702. current_asmdata.getjumplabel(l3);
  2703. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2704. list.concat(taicpu.op_const_reg(A_AND,S_W,63,NR_CX));
  2705. cg.a_jmp_flags(list,F_E,l3);
  2706. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2707. cg.a_label(list,l2);
  2708. case op of
  2709. OP_SHL:
  2710. begin
  2711. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2712. list.concat(taicpu.op_const_reg(A_SHL,S_W,1,regdst.reglo));
  2713. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(regdst.reglo)));
  2714. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,regdst.reghi));
  2715. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(regdst.reghi)));
  2716. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2717. end;
  2718. OP_SHR,OP_SAR:
  2719. begin
  2720. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2721. cg.a_op_const_reg(list,op,OS_16,1,GetNextReg(regdst.reghi));
  2722. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,regdst.reghi));
  2723. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,GetNextReg(regdst.reglo)));
  2724. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,regdst.reglo));
  2725. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2726. end;
  2727. end;
  2728. ai:=Taicpu.Op_Sym(A_LOOP,S_W,l2);
  2729. ai.is_jmp := True;
  2730. list.Concat(ai);
  2731. cg.a_label(list,l3);
  2732. cg.ungetcpuregister(list,NR_CX);
  2733. exit;
  2734. end;
  2735. end;
  2736. get_64bit_ops(op,op1,op2);
  2737. if op in [OP_ADD,OP_SUB] then
  2738. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2739. list.concat(taicpu.op_reg_reg(op1,S_W,regsrc.reglo,regdst.reglo));
  2740. list.concat(taicpu.op_reg_reg(op2,S_W,GetNextReg(regsrc.reglo),GetNextReg(regdst.reglo)));
  2741. list.concat(taicpu.op_reg_reg(op2,S_W,regsrc.reghi,regdst.reghi));
  2742. list.concat(taicpu.op_reg_reg(op2,S_W,GetNextReg(regsrc.reghi),GetNextReg(regdst.reghi)));
  2743. if op in [OP_ADD,OP_SUB] then
  2744. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2745. end;
  2746. procedure tcg64f8086.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  2747. var
  2748. op1,op2 : TAsmOp;
  2749. loop_start: TAsmLabel;
  2750. ai: taicpu;
  2751. begin
  2752. case op of
  2753. OP_AND,OP_OR,OP_XOR:
  2754. begin
  2755. cg.a_op_const_reg(list,op,OS_32,tcgint(lo(value)),reg.reglo);
  2756. cg.a_op_const_reg(list,op,OS_32,tcgint(hi(value)),reg.reghi);
  2757. end;
  2758. OP_ADD, OP_SUB:
  2759. begin
  2760. get_64bit_ops(op,op1,op2);
  2761. if (value and $ffffffffffff) = 0 then
  2762. begin
  2763. { use a_op_const_reg to allow the use of inc/dec }
  2764. cg.a_op_const_reg(list,op,OS_16,aint((value shr 48) and $ffff),GetNextReg(reg.reghi));
  2765. end
  2766. // can't use a_op_const_ref because this may use dec/inc
  2767. else if (value and $ffffffff) = 0 then
  2768. begin
  2769. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2770. list.concat(taicpu.op_const_reg(op1,S_W,aint((value shr 32) and $ffff),reg.reghi));
  2771. list.concat(taicpu.op_const_reg(op2,S_W,aint((value shr 48) and $ffff),GetNextReg(reg.reghi)));
  2772. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2773. end
  2774. else if (value and $ffff) = 0 then
  2775. begin
  2776. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2777. list.concat(taicpu.op_const_reg(op1,S_W,aint((value shr 16) and $ffff),GetNextReg(reg.reglo)));
  2778. list.concat(taicpu.op_const_reg(op2,S_W,aint((value shr 32) and $ffff),reg.reghi));
  2779. list.concat(taicpu.op_const_reg(op2,S_W,aint((value shr 48) and $ffff),GetNextReg(reg.reghi)));
  2780. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2781. end
  2782. else
  2783. begin
  2784. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2785. list.concat(taicpu.op_const_reg(op1,S_W,aint(value and $ffff),reg.reglo));
  2786. list.concat(taicpu.op_const_reg(op2,S_W,aint((value shr 16) and $ffff),GetNextReg(reg.reglo)));
  2787. list.concat(taicpu.op_const_reg(op2,S_W,aint((value shr 32) and $ffff),reg.reghi));
  2788. list.concat(taicpu.op_const_reg(op2,S_W,aint((value shr 48) and $ffff),GetNextReg(reg.reghi)));
  2789. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2790. end;
  2791. end;
  2792. OP_SHR,OP_SHL,OP_SAR:
  2793. begin
  2794. value:=value and 63;
  2795. case value of
  2796. 0:
  2797. { ultra hyper fast shift by 0 };
  2798. 1:
  2799. case op of
  2800. OP_SHL:
  2801. begin
  2802. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2803. list.concat(taicpu.op_const_reg(A_SHL,S_W,1,reg.reglo));
  2804. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(reg.reglo)));
  2805. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,reg.reghi));
  2806. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(reg.reghi)));
  2807. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2808. end;
  2809. OP_SHR,OP_SAR:
  2810. begin
  2811. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2812. cg.a_op_const_reg(list,op,OS_16,1,GetNextReg(reg.reghi));
  2813. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg.reghi));
  2814. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,GetNextReg(reg.reglo)));
  2815. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg.reglo));
  2816. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2817. end;
  2818. end;
  2819. 2..15:
  2820. begin
  2821. cg.getcpuregister(list,NR_CX);
  2822. cg.a_load_const_reg(list,OS_16,value,NR_CX);
  2823. current_asmdata.getjumplabel(loop_start);
  2824. cg.a_label(list,loop_start);
  2825. case op of
  2826. OP_SHL:
  2827. begin
  2828. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2829. list.concat(taicpu.op_const_reg(A_SHL,S_W,1,reg.reglo));
  2830. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(reg.reglo)));
  2831. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,reg.reghi));
  2832. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(reg.reghi)));
  2833. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2834. end;
  2835. OP_SHR,OP_SAR:
  2836. begin
  2837. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2838. cg.a_op_const_reg(list,op,OS_16,1,GetNextReg(reg.reghi));
  2839. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg.reghi));
  2840. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,GetNextReg(reg.reglo)));
  2841. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg.reglo));
  2842. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2843. end;
  2844. end;
  2845. ai:=Taicpu.Op_Sym(A_LOOP,S_W,loop_start);
  2846. ai.is_jmp := True;
  2847. list.Concat(ai);
  2848. cg.ungetcpuregister(list,NR_CX);
  2849. end;
  2850. 16,17:
  2851. begin
  2852. case op of
  2853. OP_SHL:
  2854. begin
  2855. cg.a_load_reg_reg(list,OS_16,OS_16,reg.reghi,GetNextReg(reg.reghi));
  2856. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reglo),reg.reghi);
  2857. cg.a_load_reg_reg(list,OS_16,OS_16,reg.reglo,GetNextReg(reg.reglo));
  2858. cg.a_op_reg_reg(list,OP_XOR,OS_16,reg.reglo,reg.reglo);
  2859. end;
  2860. OP_SHR:
  2861. begin
  2862. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reglo),reg.reglo);
  2863. cg.a_load_reg_reg(list,OS_16,OS_16,reg.reghi,GetNextReg(reg.reglo));
  2864. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reghi),reg.reghi);
  2865. cg.a_op_reg_reg(list,OP_XOR,OS_16,GetNextReg(reg.reghi),GetNextReg(reg.reghi));
  2866. end;
  2867. OP_SAR:
  2868. begin
  2869. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reglo),reg.reglo);
  2870. cg.a_load_reg_reg(list,OS_16,OS_16,reg.reghi,GetNextReg(reg.reglo));
  2871. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reghi),reg.reghi);
  2872. cg.a_op_const_reg(list,OP_SAR,OS_16,15,GetNextReg(reg.reghi));
  2873. end;
  2874. end;
  2875. if value=17 then
  2876. case op of
  2877. OP_SHL:
  2878. begin
  2879. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2880. list.concat(taicpu.op_const_reg(A_SHL,S_W,1,GetNextReg(reg.reglo)));
  2881. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,reg.reghi));
  2882. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(reg.reghi)));
  2883. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2884. end;
  2885. OP_SHR,OP_SAR:
  2886. begin
  2887. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2888. cg.a_op_const_reg(list,op,OS_16,1,reg.reghi);
  2889. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,GetNextReg(reg.reglo)));
  2890. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg.reglo));
  2891. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2892. end;
  2893. end;
  2894. end;
  2895. 18..31:
  2896. begin
  2897. case op of
  2898. OP_SHL:
  2899. begin
  2900. cg.a_load_reg_reg(list,OS_16,OS_16,reg.reghi,GetNextReg(reg.reghi));
  2901. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reglo),reg.reghi);
  2902. cg.a_load_reg_reg(list,OS_16,OS_16,reg.reglo,GetNextReg(reg.reglo));
  2903. cg.a_op_reg_reg(list,OP_XOR,OS_16,reg.reglo,reg.reglo);
  2904. end;
  2905. OP_SHR:
  2906. begin
  2907. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reglo),reg.reglo);
  2908. cg.a_load_reg_reg(list,OS_16,OS_16,reg.reghi,GetNextReg(reg.reglo));
  2909. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reghi),reg.reghi);
  2910. cg.a_op_reg_reg(list,OP_XOR,OS_16,GetNextReg(reg.reghi),GetNextReg(reg.reghi));
  2911. end;
  2912. OP_SAR:
  2913. begin
  2914. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reglo),reg.reglo);
  2915. cg.a_load_reg_reg(list,OS_16,OS_16,reg.reghi,GetNextReg(reg.reglo));
  2916. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reghi),reg.reghi);
  2917. cg.a_op_const_reg(list,OP_SAR,OS_16,15,GetNextReg(reg.reghi));
  2918. end;
  2919. end;
  2920. cg.getcpuregister(list,NR_CX);
  2921. cg.a_load_const_reg(list,OS_16,value-16,NR_CX);
  2922. current_asmdata.getjumplabel(loop_start);
  2923. cg.a_label(list,loop_start);
  2924. case op of
  2925. OP_SHL:
  2926. begin
  2927. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2928. list.concat(taicpu.op_const_reg(A_SHL,S_W,1,GetNextReg(reg.reglo)));
  2929. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,reg.reghi));
  2930. list.concat(taicpu.op_const_reg(A_RCL,S_W,1,GetNextReg(reg.reghi)));
  2931. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2932. end;
  2933. OP_SHR,OP_SAR:
  2934. begin
  2935. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2936. cg.a_op_const_reg(list,op,OS_16,1,reg.reghi);
  2937. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,GetNextReg(reg.reglo)));
  2938. list.concat(taicpu.op_const_reg(A_RCR,S_W,1,reg.reglo));
  2939. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2940. end;
  2941. end;
  2942. ai:=Taicpu.Op_Sym(A_LOOP,S_W,loop_start);
  2943. ai.is_jmp := True;
  2944. list.Concat(ai);
  2945. cg.ungetcpuregister(list,NR_CX);
  2946. end;
  2947. 32..47:
  2948. case op of
  2949. OP_SHL:
  2950. begin
  2951. cg.a_op_const_reg_reg(list,OP_SHL,OS_32,value-32,reg.reglo,reg.reghi);
  2952. cg.a_op_reg_reg(list,OP_XOR,OS_16,reg.reglo,reg.reglo);
  2953. cg.a_op_reg_reg(list,OP_XOR,OS_16,GetNextReg(reg.reglo),GetNextReg(reg.reglo));
  2954. end;
  2955. OP_SHR:
  2956. begin
  2957. cg.a_op_const_reg_reg(list,OP_SHR,OS_32,value-32,reg.reghi,reg.reglo);
  2958. cg.a_op_reg_reg(list,OP_XOR,OS_16,reg.reghi,reg.reghi);
  2959. cg.a_op_reg_reg(list,OP_XOR,OS_16,GetNextReg(reg.reghi),GetNextReg(reg.reghi));
  2960. end;
  2961. OP_SAR:
  2962. begin
  2963. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,value-32,reg.reghi,reg.reglo);
  2964. cg.a_op_const_reg_reg(list,OP_SAR,OS_16,15-(value-32),GetNextReg(reg.reglo),reg.reghi);
  2965. cg.a_load_reg_reg(list,OS_16,OS_16,reg.reghi,GetNextReg(reg.reghi));
  2966. end;
  2967. end;
  2968. 48..63:
  2969. case op of
  2970. OP_SHL:
  2971. begin
  2972. cg.a_load_reg_reg(list,OS_16,OS_16,reg.reglo,GetNextReg(reg.reghi));
  2973. cg.a_op_reg_reg(list,OP_XOR,OS_16,reg.reglo,reg.reglo);
  2974. cg.a_op_reg_reg(list,OP_XOR,OS_16,GetNextReg(reg.reglo),GetNextReg(reg.reglo));
  2975. cg.a_op_reg_reg(list,OP_XOR,OS_16,reg.reghi,reg.reghi);
  2976. cg.a_op_const_reg(list,OP_SHL,OS_16,value-48,GetNextReg(reg.reghi));
  2977. end;
  2978. OP_SHR:
  2979. begin
  2980. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reghi),reg.reglo);
  2981. cg.a_op_reg_reg(list,OP_XOR,OS_16,GetNextReg(reg.reghi),GetNextReg(reg.reghi));
  2982. cg.a_op_reg_reg(list,OP_XOR,OS_16,reg.reghi,reg.reghi);
  2983. cg.a_op_reg_reg(list,OP_XOR,OS_16,GetNextReg(reg.reglo),GetNextReg(reg.reglo));
  2984. cg.a_op_const_reg(list,OP_SHR,OS_16,value-48,reg.reglo);
  2985. end;
  2986. OP_SAR:
  2987. if value=63 then
  2988. begin
  2989. cg.a_op_const_reg(list,OP_SAR,OS_16,15,GetNextReg(reg.reghi));
  2990. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reghi),reg.reghi);
  2991. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reghi),GetNextReg(reg.reglo));
  2992. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reghi),reg.reglo);
  2993. end
  2994. else
  2995. begin
  2996. cg.a_op_const_reg_reg(list,OP_SAR,OS_16,value-48,GetNextReg(reg.reghi),reg.reglo);
  2997. cg.a_op_const_reg_reg(list,OP_SAR,OS_16,15-(value-48),reg.reglo,GetNextReg(reg.reglo));
  2998. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reglo),reg.reghi);
  2999. cg.a_load_reg_reg(list,OS_16,OS_16,GetNextReg(reg.reglo),GetNextReg(reg.reghi));
  3000. end;
  3001. end;
  3002. end;
  3003. end;
  3004. else
  3005. internalerror(200204021);
  3006. end;
  3007. end;
  3008. procedure tcg64f8086.a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);
  3009. var
  3010. op1,op2 : TAsmOp;
  3011. tempref : treference;
  3012. begin
  3013. tempref:=ref;
  3014. tcgx86(cg).make_simple_ref(list,tempref);
  3015. case op of
  3016. OP_AND,OP_OR,OP_XOR:
  3017. begin
  3018. cg.a_op_const_ref(list,op,OS_32,tcgint(lo(value)),tempref);
  3019. inc(tempref.offset,4);
  3020. cg.a_op_const_ref(list,op,OS_32,tcgint(hi(value)),tempref);
  3021. end;
  3022. OP_ADD, OP_SUB:
  3023. begin
  3024. get_64bit_ops(op,op1,op2);
  3025. if (value and $ffffffffffff) = 0 then
  3026. begin
  3027. inc(tempref.offset,6);
  3028. { use a_op_const_ref to allow the use of inc/dec }
  3029. cg.a_op_const_ref(list,op,OS_16,aint((value shr 48) and $ffff),tempref);
  3030. end
  3031. // can't use a_op_const_ref because this may use dec/inc
  3032. else if (value and $ffffffff) = 0 then
  3033. begin
  3034. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3035. inc(tempref.offset,4);
  3036. list.concat(taicpu.op_const_ref(op1,S_W,aint((value shr 32) and $ffff),tempref));
  3037. inc(tempref.offset,2);
  3038. list.concat(taicpu.op_const_ref(op2,S_W,aint((value shr 48) and $ffff),tempref));
  3039. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3040. end
  3041. else if (value and $ffff) = 0 then
  3042. begin
  3043. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3044. inc(tempref.offset,2);
  3045. list.concat(taicpu.op_const_ref(op1,S_W,aint((value shr 16) and $ffff),tempref));
  3046. inc(tempref.offset,2);
  3047. list.concat(taicpu.op_const_ref(op2,S_W,aint((value shr 32) and $ffff),tempref));
  3048. inc(tempref.offset,2);
  3049. list.concat(taicpu.op_const_ref(op2,S_W,aint((value shr 48) and $ffff),tempref));
  3050. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3051. end
  3052. else
  3053. begin
  3054. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3055. list.concat(taicpu.op_const_ref(op1,S_W,aint(value and $ffff),tempref));
  3056. inc(tempref.offset,2);
  3057. list.concat(taicpu.op_const_ref(op2,S_W,aint((value shr 16) and $ffff),tempref));
  3058. inc(tempref.offset,2);
  3059. list.concat(taicpu.op_const_ref(op2,S_W,aint((value shr 32) and $ffff),tempref));
  3060. inc(tempref.offset,2);
  3061. list.concat(taicpu.op_const_ref(op2,S_W,aint((value shr 48) and $ffff),tempref));
  3062. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3063. end;
  3064. end;
  3065. else
  3066. internalerror(200204022);
  3067. end;
  3068. end;
  3069. procedure create_codegen;
  3070. begin
  3071. cg := tcg8086.create;
  3072. cg64 := tcg64f8086.create;
  3073. end;
  3074. end.