cgx86.pas 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the common parts of the code generator for the i386 and the x86-64.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { This unit implements the common parts of the code generator for the i386 and the x86-64.
  19. }
  20. unit cgx86;
  21. {$i fpcdefs.inc}
  22. interface
  23. {$define TEST_GENERIC}
  24. uses
  25. cginfo,cgbase,cgobj,
  26. aasmbase,aasmtai,aasmcpu,
  27. cpubase,cpuinfo,
  28. node,symconst;
  29. type
  30. tcgx86 = class(tcg)
  31. { passing parameters, per default the parameter is pushed }
  32. { nr gives the number of the parameter (enumerated from }
  33. { left to right), this allows to move the parameter to }
  34. { register, if the cpu supports register calling }
  35. { conventions }
  36. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);override;
  37. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  38. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  39. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  40. procedure a_call_name(list : taasmoutput;const s : string);override;
  41. procedure a_call_ref(list : taasmoutput;const ref : treference);override;
  42. procedure a_call_reg(list : taasmoutput;reg : tregister);override;
  43. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister); override;
  44. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference); override;
  45. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  46. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  47. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  48. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  49. size: tcgsize; a: aword; src, dst: tregister); override;
  50. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  51. size: tcgsize; src1, src2, dst: tregister); override;
  52. { move instructions }
  53. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  54. procedure a_load_const_ref(list : taasmoutput; size: tcgsize; a : aword;const ref : treference);override;
  55. procedure a_load_reg_ref(list : taasmoutput; size: tcgsize; reg : tregister;const ref : treference);override;
  56. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref : treference;reg : tregister);override;
  57. procedure a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);override;
  58. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  59. { fpu move instructions }
  60. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); override;
  61. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  62. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  63. { vector register move instructions }
  64. procedure a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister); override;
  65. procedure a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister); override;
  66. procedure a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference); override;
  67. procedure a_parammm_reg(list: taasmoutput; reg: tregister); override;
  68. { comparison operations }
  69. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  70. l : tasmlabel);override;
  71. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  72. l : tasmlabel);override;
  73. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  74. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister; l : tasmlabel); override;
  75. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  76. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  77. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); override;
  78. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref: TReference); override;
  79. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  80. procedure g_exception_reason_save(list : taasmoutput; const href : treference);override;
  81. procedure g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);override;
  82. procedure g_exception_reason_load(list : taasmoutput; const href : treference);override;
  83. class function reg_cgsize(const reg: tregister): tcgsize; override;
  84. { entry/exit code helpers }
  85. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);override;
  86. procedure g_interrupt_stackframe_entry(list : taasmoutput);override;
  87. procedure g_interrupt_stackframe_exit(list : taasmoutput;accused,acchiused:boolean);override;
  88. procedure g_profilecode(list : taasmoutput);override;
  89. procedure g_stackpointer_alloc(list : taasmoutput;localsize : longint);override;
  90. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  91. procedure g_restore_frame_pointer(list : taasmoutput);override;
  92. procedure g_return_from_proc(list : taasmoutput;parasize : aword);override;
  93. procedure g_save_standard_registers(list:Taasmoutput;usedinproc:Tsupregset);override;
  94. procedure g_restore_standard_registers(list:Taasmoutput;usedinproc:Tsupregset);override;
  95. procedure g_save_all_registers(list : taasmoutput);override;
  96. procedure g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);override;
  97. procedure g_overflowcheck(list: taasmoutput; const p: tnode);override;
  98. private
  99. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  100. procedure sizes2load(s1 : tcgsize;s2 : topsize; var op: tasmop; var s3: topsize);
  101. procedure floatload(list: taasmoutput; t : tcgsize;const ref : treference);
  102. procedure floatstore(list: taasmoutput; t : tcgsize;const ref : treference);
  103. procedure floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  104. procedure floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  105. end;
  106. const
  107. TCGSize2OpSize: Array[tcgsize] of topsize =
  108. (S_NO,S_B,S_W,S_L,S_L,S_B,S_W,S_L,S_L,
  109. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  110. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO);
  111. implementation
  112. uses
  113. globtype,globals,verbose,systems,cutils,
  114. symdef,symsym,defutil,paramgr,
  115. rgobj,tgobj,rgcpu;
  116. {$ifndef NOTARGETWIN32}
  117. const
  118. winstackpagesize = 4096;
  119. {$endif NOTARGETWIN32}
  120. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_ADD,A_AND,A_DIV,
  121. A_IDIV,A_MUL, A_IMUL, A_NEG,A_NOT,A_OR,
  122. A_SAR,A_SHL,A_SHR,A_SUB,A_XOR);
  123. TOpCmp2AsmCond: Array[topcmp] of TAsmCond = (C_NONE,
  124. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  125. {****************************************************************************
  126. This is private property, keep out! :)
  127. ****************************************************************************}
  128. procedure tcgx86.sizes2load(s1 : tcgsize;s2: topsize; var op: tasmop; var s3: topsize);
  129. begin
  130. case s2 of
  131. S_B:
  132. if S1 in [OS_8,OS_S8] then
  133. s3 := S_B
  134. else internalerror(200109221);
  135. S_W:
  136. case s1 of
  137. OS_8,OS_S8:
  138. s3 := S_BW;
  139. OS_16,OS_S16:
  140. s3 := S_W;
  141. else internalerror(200109222);
  142. end;
  143. S_L:
  144. case s1 of
  145. OS_8,OS_S8:
  146. s3 := S_BL;
  147. OS_16,OS_S16:
  148. s3 := S_WL;
  149. OS_32,OS_S32:
  150. s3 := S_L;
  151. else internalerror(200109223);
  152. end;
  153. {$ifdef x86_64}
  154. S_D,
  155. S_Q:
  156. case s1 of
  157. OS_8,OS_S8:
  158. s3 := S_BQ;
  159. OS_16,OS_S16:
  160. s3 := S_WQ;
  161. OS_32,OS_S32:
  162. s3 := S_LQ;
  163. OS_64,OS_S64:
  164. s3 := S_Q;
  165. else internalerror(200304302);
  166. end;
  167. {$endif x86_64}
  168. else internalerror(200109227);
  169. end;
  170. if s3 in [S_B,S_W,S_L,S_Q] then
  171. op := A_MOV
  172. else if s1 in [OS_8,OS_16,OS_32,OS_64] then
  173. op := A_MOVZX
  174. else
  175. op := A_MOVSX;
  176. end;
  177. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  178. begin
  179. case t of
  180. OS_F32 :
  181. begin
  182. op:=A_FLD;
  183. s:=S_FS;
  184. end;
  185. OS_F64 :
  186. begin
  187. op:=A_FLD;
  188. { ???? }
  189. s:=S_FL;
  190. end;
  191. OS_F80 :
  192. begin
  193. op:=A_FLD;
  194. s:=S_FX;
  195. end;
  196. OS_C64 :
  197. begin
  198. op:=A_FILD;
  199. s:=S_IQ;
  200. end;
  201. else
  202. internalerror(200204041);
  203. end;
  204. end;
  205. procedure tcgx86.floatload(list: taasmoutput; t : tcgsize;const ref : treference);
  206. var
  207. op : tasmop;
  208. s : topsize;
  209. begin
  210. floatloadops(t,op,s);
  211. list.concat(Taicpu.Op_ref(op,s,ref));
  212. inc(trgcpu(rg).fpuvaroffset);
  213. end;
  214. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  215. begin
  216. case t of
  217. OS_F32 :
  218. begin
  219. op:=A_FSTP;
  220. s:=S_FS;
  221. end;
  222. OS_F64 :
  223. begin
  224. op:=A_FSTP;
  225. s:=S_FL;
  226. end;
  227. OS_F80 :
  228. begin
  229. op:=A_FSTP;
  230. s:=S_FX;
  231. end;
  232. OS_C64 :
  233. begin
  234. op:=A_FISTP;
  235. s:=S_IQ;
  236. end;
  237. else
  238. internalerror(200204042);
  239. end;
  240. end;
  241. procedure tcgx86.floatstore(list: taasmoutput; t : tcgsize;const ref : treference);
  242. var
  243. op : tasmop;
  244. s : topsize;
  245. begin
  246. floatstoreops(t,op,s);
  247. list.concat(Taicpu.Op_ref(op,s,ref));
  248. dec(trgcpu(rg).fpuvaroffset);
  249. end;
  250. {****************************************************************************
  251. Assembler code
  252. ****************************************************************************}
  253. class function tcgx86.reg_cgsize(const reg: tregister): tcgsize;
  254. const
  255. opsize_2_cgsize: array[topsize] of tcgsize = (OS_NO,
  256. OS_8,OS_16,OS_32,OS_NO,OS_NO,OS_NO,
  257. OS_32,OS_64,OS_64,
  258. OS_F32,OS_F64,OS_F80,OS_F32,OS_F64,OS_NO,OS_NO,
  259. OS_NO,OS_NO,OS_NO
  260. );
  261. begin
  262. result := opsize_2_cgsize[reg2opsize(reg)];
  263. end;
  264. { currently does nothing }
  265. procedure tcgx86.a_jmp_always(list : taasmoutput;l: tasmlabel);
  266. begin
  267. a_jmp_cond(list, OC_NONE, l);
  268. end;
  269. { we implement the following routines because otherwise we can't }
  270. { instantiate the class since it's abstract }
  271. procedure tcgx86.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);
  272. begin
  273. case size of
  274. OS_8,OS_S8,
  275. OS_16,OS_S16:
  276. begin
  277. if target_info.alignment.paraalign = 2 then
  278. r.number:=(r.number and not $ff) or R_SUBW
  279. else
  280. r.number:=(r.number and not $ff) or R_SUBD;
  281. list.concat(taicpu.op_reg(A_PUSH,S_L,r));
  282. end;
  283. OS_32,OS_S32:
  284. begin
  285. if r.number and $ff<>R_SUBD then
  286. internalerror(7843);
  287. list.concat(taicpu.op_reg(A_PUSH,S_L,r));
  288. end
  289. else
  290. internalerror(2002032212);
  291. end;
  292. end;
  293. procedure tcgx86.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  294. begin
  295. case size of
  296. OS_8,OS_S8,OS_16,OS_S16:
  297. begin
  298. if target_info.alignment.paraalign = 2 then
  299. list.concat(taicpu.op_const(A_PUSH,S_W,a))
  300. else
  301. list.concat(taicpu.op_const(A_PUSH,S_L,a));
  302. end;
  303. OS_32,OS_S32:
  304. list.concat(taicpu.op_const(A_PUSH,S_L,a));
  305. else
  306. internalerror(2002032213);
  307. end;
  308. end;
  309. procedure tcgx86.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  310. var
  311. tmpreg: tregister;
  312. begin
  313. case size of
  314. OS_8,OS_S8,
  315. OS_16,OS_S16:
  316. begin
  317. {$ifdef newra}
  318. if target_info.alignment.paraalign = 2 then
  319. tmpreg:=rg.getregisterint(list,OS_16)
  320. else
  321. tmpreg:=rg.getregisterint(list,OS_32);
  322. {$else}
  323. if target_info.alignment.paraalign = 2 then
  324. tmpreg:=get_scratch_reg_int(list,OS_16)
  325. else
  326. tmpreg:=get_scratch_reg_int(list,OS_32);
  327. {$endif}
  328. a_load_ref_reg(list,size,r,tmpreg);
  329. list.concat(taicpu.op_reg(A_PUSH,S_L,tmpreg));
  330. {$ifdef newra}
  331. rg.ungetregisterint(list,tmpreg);
  332. {$else}
  333. free_scratch_reg(list,tmpreg);
  334. {$endif}
  335. end;
  336. OS_32,OS_S32:
  337. list.concat(taicpu.op_ref(A_PUSH,S_L,r));
  338. OS_64,OS_S64:
  339. list.concat(taicpu.op_ref(A_PUSH,S_Q,r));
  340. else
  341. internalerror(2002032214);
  342. end;
  343. end;
  344. procedure tcgx86.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  345. var
  346. tmpreg: tregister;
  347. baseno,indexno:boolean;
  348. begin
  349. if not((r.segment.enum=R_NO) or ((r.segment.enum=R_INTREGISTER) and (r.segment.number=NR_NO))) then
  350. CGMessage(cg_e_cant_use_far_pointer_there);
  351. baseno:=(r.base.enum=R_NO) or ((r.base.enum=R_INTREGISTER) and (r.base.number=NR_NO));
  352. indexno:=(r.index.enum=R_NO) or ((r.index.enum=R_INTREGISTER) and (r.index.number=NR_NO));
  353. if baseno and indexno then
  354. begin
  355. if assigned(r.symbol) then
  356. list.concat(Taicpu.Op_sym_ofs(A_PUSH,S_L,r.symbol,r.offset))
  357. else
  358. list.concat(Taicpu.Op_const(A_PUSH,S_L,r.offset));
  359. end
  360. else if baseno and not indexno and
  361. (r.offset=0) and (r.scalefactor=0) and (r.symbol=nil) then
  362. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r.index))
  363. else if not baseno and indexno and
  364. (r.offset=0) and (r.symbol=nil) then
  365. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r.base))
  366. else
  367. begin
  368. {$ifdef newra}
  369. tmpreg:=rg.getaddressregister(list);
  370. {$else}
  371. tmpreg := get_scratch_reg_address(list);
  372. {$endif}
  373. a_loadaddr_ref_reg(list,r,tmpreg);
  374. list.concat(taicpu.op_reg(A_PUSH,S_L,tmpreg));
  375. {$ifdef newra}
  376. rg.ungetregisterint(list,tmpreg);
  377. {$else}
  378. free_scratch_reg(list,tmpreg);
  379. {$endif}
  380. end;
  381. end;
  382. procedure tcgx86.a_call_name(list : taasmoutput;const s : string);
  383. begin
  384. list.concat(taicpu.op_sym(A_CALL,S_NO,objectlibrary.newasmsymbol(s)));
  385. end;
  386. procedure tcgx86.a_call_ref(list : taasmoutput;const ref : treference);
  387. begin
  388. list.concat(taicpu.op_ref(A_CALL,S_NO,ref));
  389. end;
  390. procedure tcgx86.a_call_reg(list : taasmoutput;reg : tregister);
  391. begin
  392. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  393. end;
  394. {********************** load instructions ********************}
  395. procedure tcgx86.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  396. begin
  397. { the optimizer will change it to "xor reg,reg" when loading zero, }
  398. { no need to do it here too (JM) }
  399. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[size],a,reg))
  400. end;
  401. procedure tcgx86.a_load_const_ref(list : taasmoutput; size: tcgsize; a : aword;const ref : treference);
  402. begin
  403. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[size],a,ref));
  404. end;
  405. procedure tcgx86.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref : treference);
  406. begin
  407. list.concat(taicpu.op_reg_ref(A_MOV,TCGSize2OpSize[size],reg,
  408. ref));
  409. end;
  410. procedure tcgx86.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref: treference;reg : tregister);
  411. var
  412. op: tasmop;
  413. o,s: topsize;
  414. begin
  415. if reg.enum<>R_INTREGISTER then
  416. internalerror(200302058);
  417. o:=reg2opsize(reg);
  418. sizes2load(size,o,op,s);
  419. list.concat(taicpu.op_ref_reg(op,s,ref,reg));
  420. end;
  421. procedure tcgx86.a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  422. var
  423. op: tasmop;
  424. s: topsize;
  425. eq:boolean;
  426. instr:Taicpu;
  427. begin
  428. if (reg1.enum=R_INTREGISTER) and (reg2.enum=R_INTREGISTER) then
  429. begin
  430. sizes2load(fromsize,reg2opsize(reg2),op,s);
  431. eq:=(reg1.number shr 8)=(reg2.number shr 8);
  432. end
  433. else
  434. internalerror(200301081);
  435. if eq then
  436. begin
  437. { "mov reg1, reg1" doesn't make sense }
  438. if op = A_MOV then
  439. exit;
  440. end;
  441. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  442. {Notify the register allocator that we have written a move instruction so
  443. it can try to eliminate it.}
  444. {$ifdef newra}
  445. rg.add_move_instruction(instr);
  446. {$endif}
  447. list.concat(instr);
  448. end;
  449. procedure tcgx86.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  450. begin
  451. if ref.base.enum<>R_INTREGISTER then
  452. internalerror(200302102);
  453. if ref.index.enum<>R_INTREGISTER then
  454. internalerror(200302102);
  455. if assigned(ref.symbol) and
  456. (ref.base.number=NR_NO) and
  457. (ref.index.number=NR_NO) then
  458. list.concat(taicpu.op_sym_ofs_reg(A_MOV,S_L,ref.symbol,ref.offset,r))
  459. else
  460. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,r));
  461. end;
  462. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  463. { R_ST means "the current value at the top of the fpu stack" (JM) }
  464. procedure tcgx86.a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  465. begin
  466. if (reg1.enum <> R_ST) then
  467. begin
  468. list.concat(taicpu.op_reg(A_FLD,S_NO,
  469. trgcpu(rg).correct_fpuregister(reg1,trgcpu(rg).fpuvaroffset)));
  470. inc(trgcpu(rg).fpuvaroffset);
  471. end;
  472. if (reg2.enum <> R_ST) then
  473. begin
  474. list.concat(taicpu.op_reg(A_FSTP,S_NO,
  475. trgcpu(rg).correct_fpuregister(reg2,trgcpu(rg).fpuvaroffset)));
  476. dec(trgcpu(rg).fpuvaroffset);
  477. end;
  478. end;
  479. procedure tcgx86.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  480. var rst:Tregister;
  481. begin
  482. rst.enum:=R_ST;
  483. floatload(list,size,ref);
  484. if reg.enum>lastreg then
  485. internalerror(200301081);
  486. if (reg.enum <> R_ST) then
  487. a_loadfpu_reg_reg(list,rst,reg);
  488. end;
  489. procedure tcgx86.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  490. var rst:Tregister;
  491. begin
  492. rst.enum:=R_ST;
  493. if reg.enum>lastreg then
  494. internalerror(200301081);
  495. if reg.enum <> R_ST then
  496. a_loadfpu_reg_reg(list,reg,rst);
  497. floatstore(list,size,ref);
  498. end;
  499. procedure tcgx86.a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  500. begin
  501. list.concat(taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2));
  502. end;
  503. procedure tcgx86.a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister);
  504. begin
  505. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,ref,reg));
  506. end;
  507. procedure tcgx86.a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference);
  508. begin
  509. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,ref));
  510. end;
  511. procedure tcgx86.a_parammm_reg(list: taasmoutput; reg: tregister);
  512. var
  513. href : treference;
  514. r : Tregister;
  515. begin
  516. r.enum:=R_INTREGISTER;
  517. r.number:=NR_ESP;
  518. list.concat(taicpu.op_const_reg(A_SUB,S_L,8,r));
  519. reference_reset_base(href,r,0);
  520. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,href));
  521. end;
  522. procedure tcgx86.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister);
  523. var
  524. opcode: tasmop;
  525. power: longint;
  526. begin
  527. if reg.enum<>R_INTREGISTER then
  528. internalerror(200302034);
  529. case op of
  530. OP_DIV, OP_IDIV:
  531. begin
  532. if ispowerof2(a,power) then
  533. begin
  534. case op of
  535. OP_DIV:
  536. opcode := A_SHR;
  537. OP_IDIV:
  538. opcode := A_SAR;
  539. end;
  540. list.concat(taicpu.op_const_reg(opcode,TCgSize2OpSize[size],power,reg));
  541. exit;
  542. end;
  543. { the rest should be handled specifically in the code }
  544. { generator because of the silly register usage restraints }
  545. internalerror(200109224);
  546. end;
  547. OP_MUL,OP_IMUL:
  548. begin
  549. if not(cs_check_overflow in aktlocalswitches) and
  550. ispowerof2(a,power) then
  551. begin
  552. list.concat(taicpu.op_const_reg(A_SHL,TCgSize2OpSize[size],power,reg));
  553. exit;
  554. end;
  555. if op = OP_IMUL then
  556. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  557. else
  558. { OP_MUL should be handled specifically in the code }
  559. { generator because of the silly register usage restraints }
  560. internalerror(200109225);
  561. end;
  562. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  563. if not(cs_check_overflow in aktlocalswitches) and
  564. (a = 1) and
  565. (op in [OP_ADD,OP_SUB]) then
  566. if op = OP_ADD then
  567. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  568. else
  569. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  570. else if (a = 0) then
  571. if (op <> OP_AND) then
  572. exit
  573. else
  574. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],0,reg))
  575. else if (a = high(aword)) and
  576. (op in [OP_AND,OP_OR,OP_XOR]) then
  577. begin
  578. case op of
  579. OP_AND:
  580. exit;
  581. OP_OR:
  582. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],high(aword),reg));
  583. OP_XOR:
  584. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg));
  585. end
  586. end
  587. else
  588. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  589. OP_SHL,OP_SHR,OP_SAR:
  590. begin
  591. if (a and 31) <> 0 Then
  592. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  593. if (a shr 5) <> 0 Then
  594. internalerror(68991);
  595. end
  596. else internalerror(68992);
  597. end;
  598. end;
  599. procedure tcgx86.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  600. var
  601. opcode: tasmop;
  602. power: longint;
  603. begin
  604. Case Op of
  605. OP_DIV, OP_IDIV:
  606. Begin
  607. if ispowerof2(a,power) then
  608. begin
  609. case op of
  610. OP_DIV:
  611. opcode := A_SHR;
  612. OP_IDIV:
  613. opcode := A_SAR;
  614. end;
  615. list.concat(taicpu.op_const_ref(opcode,
  616. TCgSize2OpSize[size],power,ref));
  617. exit;
  618. end;
  619. { the rest should be handled specifically in the code }
  620. { generator because of the silly register usage restraints }
  621. internalerror(200109231);
  622. End;
  623. OP_MUL,OP_IMUL:
  624. begin
  625. if not(cs_check_overflow in aktlocalswitches) and
  626. ispowerof2(a,power) then
  627. begin
  628. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  629. power,ref));
  630. exit;
  631. end;
  632. { can't multiply a memory location directly with a constant }
  633. if op = OP_IMUL then
  634. inherited a_op_const_ref(list,op,size,a,ref)
  635. else
  636. { OP_MUL should be handled specifically in the code }
  637. { generator because of the silly register usage restraints }
  638. internalerror(200109232);
  639. end;
  640. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  641. if not(cs_check_overflow in aktlocalswitches) and
  642. (a = 1) and
  643. (op in [OP_ADD,OP_SUB]) then
  644. if op = OP_ADD then
  645. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],ref))
  646. else
  647. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],ref))
  648. else if (a = 0) then
  649. if (op <> OP_AND) then
  650. exit
  651. else
  652. a_load_const_ref(list,size,0,ref)
  653. else if (a = high(aword)) and
  654. (op in [OP_AND,OP_OR,OP_XOR]) then
  655. begin
  656. case op of
  657. OP_AND:
  658. exit;
  659. OP_OR:
  660. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],high(aword),ref));
  661. OP_XOR:
  662. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],ref));
  663. end
  664. end
  665. else
  666. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  667. TCgSize2OpSize[size],a,ref));
  668. OP_SHL,OP_SHR,OP_SAR:
  669. begin
  670. if (a and 31) <> 0 then
  671. list.concat(taicpu.op_const_ref(
  672. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,ref));
  673. if (a shr 5) <> 0 Then
  674. internalerror(68991);
  675. end
  676. else internalerror(68992);
  677. end;
  678. end;
  679. procedure tcgx86.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  680. var
  681. regloadsize: tcgsize;
  682. dstsize: topsize;
  683. tmpreg : tregister;
  684. {$ifndef newra}
  685. popecx : boolean;
  686. {$endif}
  687. r:Tregister;
  688. instr:Taicpu;
  689. begin
  690. if src.enum<>R_INTREGISTER then
  691. internalerror(200302025);
  692. if dst.enum<>R_INTREGISTER then
  693. internalerror(200302025);
  694. r.enum:=R_INTREGISTER;
  695. dstsize := tcgsize2opsize[size];
  696. dst.number:=(dst.number and not $ff) or cgsize2subreg(size);
  697. case op of
  698. OP_NEG,OP_NOT:
  699. begin
  700. if src.number <> NR_NO then
  701. internalerror(200112291);
  702. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  703. end;
  704. OP_MUL,OP_DIV,OP_IDIV:
  705. { special stuff, needs separate handling inside code }
  706. { generator }
  707. internalerror(200109233);
  708. OP_SHR,OP_SHL,OP_SAR:
  709. begin
  710. {$ifdef newra}
  711. tmpreg:=rg.getexplicitregisterint(list,NR_CL);
  712. a_load_reg_reg(list,size,OS_8,dst,tmpreg);
  713. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],S_B,src,
  714. tmpreg));
  715. rg.ungetregisterint(list,tmpreg);
  716. {$else newra}
  717. tmpreg.enum:=R_INTREGISTER;
  718. tmpreg.number:=NR_NO;
  719. popecx := false;
  720. { we need cl to hold the shift count, so if the destination }
  721. { is ecx, save it to a temp for now }
  722. if dst.number shr 8=RS_ECX then
  723. begin
  724. case dst.number and $ff of
  725. R_SUBL,R_SUBH:
  726. regloadsize:=OS_8;
  727. R_SUBW:
  728. regloadsize:=OS_16;
  729. else
  730. regloadsize:=OS_32;
  731. end;
  732. tmpreg := get_scratch_reg_int(list,OS_INT);
  733. a_load_reg_reg(list,regloadsize,regloadsize,src,tmpreg);
  734. end;
  735. if src.number shr 8<>RS_ECX then
  736. begin
  737. { is ecx still free (it's also free if it was allocated }
  738. { to dst, since we've moved dst somewhere else already) }
  739. r.number:=NR_ECX;
  740. if not((dst.number shr 8=RS_ECX) or
  741. ((RS_ECX in rg.unusedregsint) and
  742. { this will always be true, it's just here to }
  743. { allocate ecx }
  744. (rg.getexplicitregisterint(list,NR_ECX).number = NR_ECX))) then
  745. begin
  746. list.concat(taicpu.op_reg(A_PUSH,S_L,r));
  747. popecx := true;
  748. end;
  749. a_load_reg_reg(list,OS_32,OS_32,rg.makeregsize(src,OS_32),r);
  750. end
  751. else
  752. src.number := NR_CL;
  753. { do the shift }
  754. r.number:=NR_CL;
  755. if tmpreg.number = NR_NO then
  756. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,
  757. r,dst))
  758. else
  759. begin
  760. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],S_L,
  761. r,tmpreg));
  762. { move result back to the destination }
  763. r.number:=NR_ECX;
  764. a_load_reg_reg(list,OS_32,OS_32,tmpreg,r);
  765. free_scratch_reg(list,tmpreg);
  766. end;
  767. r.number:=NR_ECX;
  768. if popecx then
  769. list.concat(taicpu.op_reg(A_POP,S_L,r))
  770. else if not (dst.number shr 8=RS_ECX) then
  771. rg.ungetregisterint(list,r);
  772. {$endif newra}
  773. end;
  774. else
  775. begin
  776. if reg2opsize(src) <> dstsize then
  777. internalerror(200109226);
  778. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  779. list.concat(instr);
  780. end;
  781. end;
  782. end;
  783. procedure tcgx86.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  784. begin
  785. case op of
  786. OP_NEG,OP_NOT,OP_IMUL:
  787. begin
  788. inherited a_op_ref_reg(list,op,size,ref,reg);
  789. end;
  790. OP_MUL,OP_DIV,OP_IDIV:
  791. { special stuff, needs separate handling inside code }
  792. { generator }
  793. internalerror(200109239);
  794. else
  795. begin
  796. reg := rg.makeregsize(reg,size);
  797. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],ref,reg));
  798. end;
  799. end;
  800. end;
  801. procedure tcgx86.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  802. begin
  803. if reg.enum<>R_INTREGISTER then
  804. internalerror(200302036);
  805. case op of
  806. OP_NEG,OP_NOT:
  807. begin
  808. if reg.number<>NR_NO then
  809. internalerror(200109237);
  810. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],ref));
  811. end;
  812. OP_IMUL:
  813. begin
  814. { this one needs a load/imul/store, which is the default }
  815. inherited a_op_ref_reg(list,op,size,ref,reg);
  816. end;
  817. OP_MUL,OP_DIV,OP_IDIV:
  818. { special stuff, needs separate handling inside code }
  819. { generator }
  820. internalerror(200109238);
  821. else
  822. begin
  823. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,ref));
  824. end;
  825. end;
  826. end;
  827. procedure tcgx86.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  828. size: tcgsize; a: aword; src, dst: tregister);
  829. var
  830. tmpref: treference;
  831. power: longint;
  832. opsize: topsize;
  833. begin
  834. if src.enum<>R_INTREGISTER then
  835. internalerror(200302057);
  836. if dst.enum<>R_INTREGISTER then
  837. internalerror(200302057);
  838. opsize := reg2opsize(src);
  839. if (opsize <> S_L) or
  840. not (size in [OS_32,OS_S32]) then
  841. begin
  842. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  843. exit;
  844. end;
  845. { if we get here, we have to do a 32 bit calculation, guaranteed }
  846. case op of
  847. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  848. OP_SAR:
  849. { can't do anything special for these }
  850. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  851. OP_IMUL:
  852. begin
  853. if not(cs_check_overflow in aktlocalswitches) and
  854. ispowerof2(a,power) then
  855. { can be done with a shift }
  856. begin
  857. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  858. exit;
  859. end;
  860. list.concat(taicpu.op_const_reg_reg(A_IMUL,S_L,a,src,dst));
  861. end;
  862. OP_ADD, OP_SUB:
  863. if (a = 0) then
  864. a_load_reg_reg(list,size,size,src,dst)
  865. else
  866. begin
  867. reference_reset(tmpref);
  868. tmpref.base := src;
  869. tmpref.offset := longint(a);
  870. if op = OP_SUB then
  871. tmpref.offset := -tmpref.offset;
  872. list.concat(taicpu.op_ref_reg(A_LEA,S_L,tmpref,dst));
  873. end
  874. else internalerror(200112302);
  875. end;
  876. end;
  877. procedure tcgx86.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  878. size: tcgsize; src1, src2, dst: tregister);
  879. var
  880. tmpref: treference;
  881. opsize: topsize;
  882. begin
  883. if src1.enum>lastreg then
  884. internalerror(200201081);
  885. if src2.enum>lastreg then
  886. internalerror(200201081);
  887. if dst.enum>lastreg then
  888. internalerror(200201081);
  889. opsize := reg2opsize(src1);
  890. if (opsize <> S_L) or
  891. (reg2opsize(src2) <> S_L) or
  892. not (size in [OS_32,OS_S32]) then
  893. begin
  894. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  895. exit;
  896. end;
  897. { if we get here, we have to do a 32 bit calculation, guaranteed }
  898. Case Op of
  899. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  900. OP_SAR,OP_SUB,OP_NOT,OP_NEG:
  901. { can't do anything special for these }
  902. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  903. OP_IMUL:
  904. list.concat(taicpu.op_reg_reg_reg(A_IMUL,S_L,src1,src2,dst));
  905. OP_ADD:
  906. begin
  907. reference_reset(tmpref);
  908. tmpref.base := src1;
  909. tmpref.index := src2;
  910. tmpref.scalefactor := 1;
  911. list.concat(taicpu.op_ref_reg(A_LEA,S_L,tmpref,dst));
  912. end
  913. else internalerror(200112303);
  914. end;
  915. end;
  916. {*************** compare instructructions ****************}
  917. procedure tcgx86.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  918. l : tasmlabel);
  919. begin
  920. if reg.enum=R_INTREGISTER then
  921. begin
  922. if (a = 0) then
  923. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  924. else
  925. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  926. end
  927. else
  928. internalerror(200303131);
  929. a_jmp_cond(list,cmp_op,l);
  930. end;
  931. procedure tcgx86.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  932. l : tasmlabel);
  933. begin
  934. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,ref));
  935. a_jmp_cond(list,cmp_op,l);
  936. end;
  937. procedure tcgx86.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  938. reg1,reg2 : tregister;l : tasmlabel);
  939. begin
  940. if reg1.enum<>R_INTREGISTER then
  941. internalerror(200101081);
  942. if reg2.enum<>R_INTREGISTER then
  943. internalerror(200101081);
  944. if reg2opsize(reg1) <> reg2opsize(reg2) then
  945. internalerror(200109226);
  946. list.concat(taicpu.op_reg_reg(A_CMP,reg2opsize(reg1),reg1,reg2));
  947. a_jmp_cond(list,cmp_op,l);
  948. end;
  949. procedure tcgx86.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  950. begin
  951. if reg.enum<>R_INTREGISTER then
  952. internalerror(200302059);
  953. reg.number:=(reg.number and not $ff) or cgsize2subreg(size);
  954. list.concat(taicpu.op_ref_reg(A_CMP,tcgsize2opsize[size],ref,reg));
  955. a_jmp_cond(list,cmp_op,l);
  956. end;
  957. procedure tcgx86.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  958. var
  959. ai : taicpu;
  960. begin
  961. if cond=OC_None then
  962. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  963. else
  964. begin
  965. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  966. ai.SetCondition(TOpCmp2AsmCond[cond]);
  967. end;
  968. ai.is_jmp:=true;
  969. list.concat(ai);
  970. end;
  971. procedure tcgx86.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  972. var
  973. ai : taicpu;
  974. begin
  975. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  976. ai.SetCondition(flags_to_cond(f));
  977. ai.is_jmp := true;
  978. list.concat(ai);
  979. end;
  980. procedure tcgx86.g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister);
  981. var
  982. ai : taicpu;
  983. hreg : tregister;
  984. begin
  985. if reg.enum<>R_INTREGISTER then
  986. internalerror(200202031);
  987. hreg.enum:=R_INTREGISTER;
  988. hreg.number:=(reg.number and not $ff) or R_SUBL;
  989. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  990. ai.setcondition(flags_to_cond(f));
  991. list.concat(ai);
  992. if (reg.number <> hreg.number) then
  993. a_load_reg_reg(list,OS_8,size,hreg,reg);
  994. end;
  995. procedure tcgx86.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref: TReference);
  996. var
  997. ai : taicpu;
  998. begin
  999. if not(size in [OS_8,OS_S8]) then
  1000. a_load_const_ref(list,size,0,ref);
  1001. ai:=Taicpu.op_ref(A_SETcc,S_B,ref);
  1002. ai.setcondition(flags_to_cond(f));
  1003. list.concat(ai);
  1004. end;
  1005. { ************* concatcopy ************ }
  1006. {$ifdef newra}
  1007. procedure Tcgx86.g_concatcopy(list:Taasmoutput;const source,dest:Treference;
  1008. len:aword;delsource,loadref:boolean);
  1009. var srcref,dstref:Treference;
  1010. srcreg,destreg,countreg,r:Tregister;
  1011. helpsize:aword;
  1012. copysize:byte;
  1013. cgsize:Tcgsize;
  1014. begin
  1015. helpsize:=12;
  1016. if cs_littlesize in aktglobalswitches then
  1017. helpsize:=8;
  1018. if not loadref and (len<=helpsize) then
  1019. begin
  1020. dstref:=dest;
  1021. srcref:=source;
  1022. copysize:=4;
  1023. cgsize:=OS_32;
  1024. while len<>0 do
  1025. begin
  1026. if len<2 then
  1027. begin
  1028. copysize:=1;
  1029. cgsize:=OS_8;
  1030. end
  1031. else if len<4 then
  1032. begin
  1033. copysize:=2;
  1034. cgsize:=OS_16;
  1035. end;
  1036. dec(len,copysize);
  1037. r:=rg.getregisterint(list,cgsize);
  1038. a_load_ref_reg(list,cgsize,srcref,r);
  1039. if (len=0) and delsource then
  1040. reference_release(list,source);
  1041. a_load_reg_ref(list,cgsize,r,dstref);
  1042. inc(srcref.offset,copysize);
  1043. inc(dstref.offset,copysize);
  1044. rg.ungetregisterint(list,r);
  1045. end;
  1046. end
  1047. else
  1048. begin
  1049. destreg:=rg.getexplicitregisterint(list,NR_EDI);
  1050. a_loadaddr_ref_reg(list,dest,destreg);
  1051. srcreg:=rg.getexplicitregisterint(list,NR_ESI);
  1052. if loadref then
  1053. a_load_ref_reg(list,OS_ADDR,source,srcreg)
  1054. else
  1055. begin
  1056. a_loadaddr_ref_reg(list,source,srcreg);
  1057. if delsource then
  1058. begin
  1059. srcref:=source;
  1060. { Don't release ESI register yet, it's needed
  1061. by the movsl }
  1062. if (srcref.base.number=NR_ESI) then
  1063. srcref.base.number:=NR_NO
  1064. else if (srcref.index.number=NR_ESI) then
  1065. srcref.index.number:=NR_NO;
  1066. reference_release(list,srcref);
  1067. end;
  1068. end;
  1069. countreg:=rg.getexplicitregisterint(list,NR_ECX);
  1070. list.concat(Taicpu.op_none(A_CLD,S_NO));
  1071. if cs_littlesize in aktglobalswitches then
  1072. begin
  1073. a_load_const_reg(list,OS_INT,len,countreg);
  1074. list.concat(Taicpu.op_none(A_REP,S_NO));
  1075. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1076. end
  1077. else
  1078. begin
  1079. helpsize:=len shr 2;
  1080. len:=len and 3;
  1081. if helpsize>1 then
  1082. begin
  1083. a_load_const_reg(list,OS_INT,helpsize,countreg);
  1084. list.concat(Taicpu.op_none(A_REP,S_NO));
  1085. end;
  1086. if helpsize>0 then
  1087. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1088. if len>1 then
  1089. begin
  1090. dec(len,2);
  1091. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  1092. end;
  1093. if len=1 then
  1094. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1095. end;
  1096. rg.ungetregisterint(list,countreg);
  1097. rg.ungetregisterint(list,srcreg);
  1098. rg.ungetregisterint(list,destreg);
  1099. end;
  1100. if delsource then
  1101. tg.ungetiftemp(list,source);
  1102. end;
  1103. {$else newra}
  1104. procedure tcgx86.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  1105. var
  1106. ecxpushed,esipushed : boolean;
  1107. helpsize : longint;
  1108. i : byte;
  1109. reg8,reg32 : tregister;
  1110. srcref,dstref : treference;
  1111. swap : boolean;
  1112. srcreg,destreg,r : Tregister;
  1113. function maybepush(r:tnewregister;var pushed:boolean):tregister;
  1114. begin
  1115. pushed:=false;
  1116. result.enum:=R_INTREGISTER;
  1117. result.number:=r;
  1118. if not((r shr 8) in rg.unusedregsint) then
  1119. begin
  1120. list.concat(Taicpu.Op_reg(A_PUSH,S_L,result));
  1121. pushed:=true;
  1122. end
  1123. else
  1124. rg.getexplicitregisterint(list,r);
  1125. end;
  1126. begin
  1127. ecxpushed:=false;
  1128. esipushed:=false;
  1129. if (not loadref) and
  1130. ((len<=8) or
  1131. (not(cs_littlesize in aktglobalswitches ) and (len<=12))) then
  1132. begin
  1133. helpsize:=len shr 2;
  1134. dstref:=dest;
  1135. srcref:=source;
  1136. for i:=1 to helpsize do
  1137. begin
  1138. r:=rg.getexplicitregisterint(list,NR_EDI);
  1139. a_load_ref_reg(list,OS_32,srcref,r);
  1140. If (len = 4) and delsource then
  1141. reference_release(list,source);
  1142. a_load_reg_ref(list,OS_32,r,dstref);
  1143. inc(srcref.offset,4);
  1144. inc(dstref.offset,4);
  1145. dec(len,4);
  1146. rg.ungetregisterint(list,r);
  1147. end;
  1148. if len>1 then
  1149. begin
  1150. r:=rg.getexplicitregisterint(list,NR_DI);
  1151. a_load_ref_reg(list,OS_16,srcref,r);
  1152. If (len = 2) and delsource then
  1153. reference_release(list,source);
  1154. a_load_reg_ref(list,OS_16,r,dstref);
  1155. inc(srcref.offset,2);
  1156. inc(dstref.offset,2);
  1157. dec(len,2);
  1158. rg.ungetregisterint(list,r);
  1159. end;
  1160. r.enum:=R_INTREGISTER;
  1161. reg8.enum:=R_INTREGISTER;
  1162. reg32.enum:=R_INTREGISTER;
  1163. if len>0 then
  1164. begin
  1165. { and now look for an 8 bit register }
  1166. swap:=false;
  1167. if RS_EAX in rg.unusedregsint then reg8:=rg.getexplicitregisterint(list,NR_AL)
  1168. else if RS_EDX in rg.unusedregsint then reg8:=rg.getexplicitregisterint(list,NR_DL)
  1169. else if RS_EBX in rg.unusedregsint then reg8:=rg.getexplicitregisterint(list,NR_BL)
  1170. else if RS_ECX in rg.unusedregsint then reg8:=rg.getexplicitregisterint(list,NR_CL)
  1171. else
  1172. begin
  1173. swap:=true;
  1174. { we need only to check 3 registers, because }
  1175. { one is always not index or base }
  1176. if (dest.base.number<>NR_EAX) and (dest.index.number<>NR_EAX) then
  1177. begin
  1178. reg8.number:=NR_AL;
  1179. reg32.number:=NR_EAX;
  1180. end
  1181. else if (dest.base.number<>NR_EBX) and (dest.index.number<>NR_EBX) then
  1182. begin
  1183. reg8.number:=NR_BL;
  1184. reg32.number:=NR_EBX;
  1185. end
  1186. else if (dest.base.number<>NR_ECX) and (dest.index.number<>NR_ECX) then
  1187. begin
  1188. reg8.number:=NR_CL;
  1189. reg32.number:=NR_ECX;
  1190. end;
  1191. end;
  1192. if swap then
  1193. { was earlier XCHG, of course nonsense }
  1194. begin
  1195. r:=rg.getexplicitregisterint(list,NR_EDI);
  1196. a_load_reg_reg(list,OS_32,OS_32,reg32,r);
  1197. end;
  1198. a_load_ref_reg(list,OS_8,srcref,reg8);
  1199. If delsource and (len=1) then
  1200. reference_release(list,source);
  1201. a_load_reg_ref(list,OS_8,reg8,dstref);
  1202. if swap then
  1203. begin
  1204. r.number:=NR_EDI;
  1205. a_load_reg_reg(list,OS_32,OS_32,r,reg32);
  1206. rg.ungetregisterint(list,r);
  1207. end
  1208. else
  1209. rg.ungetregisterint(list,reg8);
  1210. end;
  1211. end
  1212. else
  1213. begin
  1214. destreg:=rg.getexplicitregisterint(list,NR_EDI);
  1215. a_loadaddr_ref_reg(list,dest,destreg);
  1216. if loadref then
  1217. begin
  1218. srcreg:=maybepush(NR_ESI,esipushed);
  1219. a_load_ref_reg(list,OS_ADDR,source,srcreg)
  1220. end
  1221. else
  1222. begin
  1223. if delsource then
  1224. begin
  1225. if (source.base.number=NR_ESI) then
  1226. srcreg:=source.base
  1227. else if (source.index.number=NR_ESI) then
  1228. srcreg:=source.index
  1229. else
  1230. srcreg:=maybepush(NR_ESI,esipushed);
  1231. end
  1232. else
  1233. srcreg:=maybepush(NR_ESI,esipushed);
  1234. a_loadaddr_ref_reg(list,source,srcreg);
  1235. if delsource then
  1236. begin
  1237. srcref:=source;
  1238. { Don't release ESI register yet, it's needed
  1239. by the movsl }
  1240. if (srcref.base.number=NR_ESI) then
  1241. srcref.base.number:=NR_NO
  1242. else if (srcref.index.number=NR_ESI) then
  1243. srcref.index.number:=NR_NO;
  1244. reference_release(list,srcref);
  1245. end;
  1246. end;
  1247. list.concat(Taicpu.Op_none(A_CLD,S_NO));
  1248. ecxpushed:=false;
  1249. if cs_littlesize in aktglobalswitches then
  1250. begin
  1251. r:=maybepush(NR_ECX,ecxpushed);
  1252. a_load_const_reg(list,OS_INT,len,r);
  1253. list.concat(Taicpu.Op_none(A_REP,S_NO));
  1254. list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1255. end
  1256. else
  1257. begin
  1258. helpsize:=len shr 2;
  1259. len:=len and 3;
  1260. if helpsize>1 then
  1261. begin
  1262. r:=maybepush(NR_ECX,ecxpushed);
  1263. a_load_const_reg(list,OS_INT,helpsize,r);
  1264. list.concat(Taicpu.Op_none(A_REP,S_NO));
  1265. end;
  1266. if helpsize>0 then
  1267. list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1268. if len>1 then
  1269. begin
  1270. dec(len,2);
  1271. list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1272. end;
  1273. if len=1 then
  1274. list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1275. end;
  1276. r.enum:=R_INTREGISTER;
  1277. if ecxpushed then
  1278. begin
  1279. r.number:=NR_ECX;
  1280. list.concat(Taicpu.Op_reg(A_POP,S_L,r))
  1281. end
  1282. else
  1283. begin
  1284. r.number:=NR_ECX;
  1285. rg.ungetregisterint(list,r);
  1286. end;
  1287. if esipushed then
  1288. begin
  1289. r.number:=NR_ESI;
  1290. list.concat(Taicpu.Op_reg(A_POP,S_L,r))
  1291. end
  1292. else
  1293. begin
  1294. r.number:=NR_ESI;
  1295. rg.ungetregisterint(list,r);
  1296. end;
  1297. rg.ungetregisterint(list,destreg);
  1298. end;
  1299. if delsource then
  1300. tg.ungetiftemp(list,source);
  1301. end;
  1302. {$endif newra}
  1303. procedure tcgx86.g_exception_reason_save(list : taasmoutput; const href : treference);
  1304. var r:Tregister;
  1305. begin
  1306. r.enum:=R_INTREGISTER;
  1307. r.number:=NR_EAX;
  1308. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1309. end;
  1310. procedure tcgx86.g_exception_reason_save_const(list : taasmoutput;const href : treference; a: aword);
  1311. begin
  1312. list.concat(Taicpu.op_const(A_PUSH,S_L,a));
  1313. end;
  1314. procedure tcgx86.g_exception_reason_load(list : taasmoutput; const href : treference);
  1315. var r:Tregister;
  1316. begin
  1317. r.enum:=R_INTREGISTER;
  1318. r.number:=NR_EAX;
  1319. list.concat(Taicpu.op_reg(A_POP,S_L,r));
  1320. end;
  1321. {****************************************************************************
  1322. Entry/Exit Code Helpers
  1323. ****************************************************************************}
  1324. procedure tcgx86.g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);
  1325. var
  1326. lenref : treference;
  1327. power,len : longint;
  1328. opsize : topsize;
  1329. {$ifndef __NOWINPECOFF__}
  1330. again,ok : tasmlabel;
  1331. {$endif}
  1332. r,r2,rsp:Tregister;
  1333. begin
  1334. lenref:=ref;
  1335. inc(lenref.offset,4);
  1336. { get stack space }
  1337. r.enum:=R_INTREGISTER;
  1338. r.number:=NR_EDI;
  1339. rsp.enum:=R_INTREGISTER;
  1340. rsp.number:=NR_ESP;
  1341. r2.enum:=R_INTREGISTER;
  1342. rg.getexplicitregisterint(list,NR_EDI);
  1343. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1344. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1345. if (elesize<>1) then
  1346. begin
  1347. if ispowerof2(elesize, power) then
  1348. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1349. else
  1350. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1351. end;
  1352. {$ifndef __NOWINPECOFF__}
  1353. { windows guards only a few pages for stack growing, }
  1354. { so we have to access every page first }
  1355. if target_info.system=system_i386_win32 then
  1356. begin
  1357. objectlibrary.getlabel(again);
  1358. objectlibrary.getlabel(ok);
  1359. a_label(list,again);
  1360. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,r));
  1361. a_jmp_cond(list,OC_B,ok);
  1362. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1363. r2.number:=NR_EAX;
  1364. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1365. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,r));
  1366. a_jmp_always(list,again);
  1367. a_label(list,ok);
  1368. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1369. rg.ungetregisterint(list,r);
  1370. { now reload EDI }
  1371. rg.getexplicitregisterint(list,NR_EDI);
  1372. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1373. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1374. if (elesize<>1) then
  1375. begin
  1376. if ispowerof2(elesize, power) then
  1377. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1378. else
  1379. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1380. end;
  1381. end
  1382. else
  1383. {$endif __NOWINPECOFF__}
  1384. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1385. { align stack on 4 bytes }
  1386. list.concat(Taicpu.op_const_reg(A_AND,S_L,$fffffff4,rsp));
  1387. { load destination }
  1388. a_load_reg_reg(list,OS_INT,OS_INT,rsp,r);
  1389. { don't destroy the registers! }
  1390. r2.number:=NR_ECX;
  1391. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1392. r2.number:=NR_ESI;
  1393. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1394. { load count }
  1395. r2.number:=NR_ECX;
  1396. a_load_ref_reg(list,OS_INT,lenref,r2);
  1397. { load source }
  1398. r2.number:=NR_ESI;
  1399. a_load_ref_reg(list,OS_INT,ref,r2);
  1400. { scheduled .... }
  1401. r2.number:=NR_ECX;
  1402. list.concat(Taicpu.op_reg(A_INC,S_L,r2));
  1403. { calculate size }
  1404. len:=elesize;
  1405. opsize:=S_B;
  1406. if (len and 3)=0 then
  1407. begin
  1408. opsize:=S_L;
  1409. len:=len shr 2;
  1410. end
  1411. else
  1412. if (len and 1)=0 then
  1413. begin
  1414. opsize:=S_W;
  1415. len:=len shr 1;
  1416. end;
  1417. if ispowerof2(len, power) then
  1418. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r2))
  1419. else
  1420. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,r2));
  1421. list.concat(Taicpu.op_none(A_REP,S_NO));
  1422. case opsize of
  1423. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1424. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1425. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1426. end;
  1427. rg.ungetregisterint(list,r);
  1428. r2.number:=NR_ESI;
  1429. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1430. r2.number:=NR_ECX;
  1431. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1432. { patch the new address }
  1433. a_load_reg_ref(list,OS_INT,rsp,ref);
  1434. end;
  1435. procedure tcgx86.g_interrupt_stackframe_entry(list : taasmoutput);
  1436. var r:Tregister;
  1437. begin
  1438. r.enum:=R_INTREGISTER;
  1439. r.number:=NR_GS;
  1440. { .... also the segment registers }
  1441. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1442. r.number:=NR_FS;
  1443. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1444. r.number:=NR_ES;
  1445. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1446. r.number:=NR_DS;
  1447. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1448. { save the registers of an interrupt procedure }
  1449. r.number:=NR_EDI;
  1450. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1451. r.number:=NR_ESI;
  1452. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1453. r.number:=NR_EDX;
  1454. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1455. r.number:=NR_ECX;
  1456. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1457. r.number:=NR_EBX;
  1458. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1459. r.number:=NR_EAX;
  1460. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1461. end;
  1462. procedure tcgx86.g_interrupt_stackframe_exit(list : taasmoutput;accused,acchiused:boolean);
  1463. var r:Tregister;
  1464. begin
  1465. r.enum:=R_INTREGISTER;
  1466. if accused then
  1467. begin
  1468. r.number:=NR_ESP;
  1469. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,r))
  1470. end
  1471. else
  1472. begin
  1473. r.number:=NR_EAX;
  1474. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1475. end;
  1476. r.number:=NR_EBX;
  1477. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1478. r.number:=NR_ECX;
  1479. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1480. if acchiused then
  1481. begin
  1482. r.number:=NR_ESP;
  1483. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,r))
  1484. end
  1485. else
  1486. begin
  1487. r.number:=NR_EDX;
  1488. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1489. end;
  1490. r.number:=NR_ESI;
  1491. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1492. r.number:=NR_EDI;
  1493. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1494. { .... also the segment registers }
  1495. r.number:=NR_DS;
  1496. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1497. r.number:=NR_ES;
  1498. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1499. r.number:=NR_FS;
  1500. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1501. r.number:=NR_GS;
  1502. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1503. { this restores the flags }
  1504. list.concat(Taicpu.Op_none(A_IRET,S_NO));
  1505. end;
  1506. procedure tcgx86.g_profilecode(list : taasmoutput);
  1507. var
  1508. pl : tasmlabel;
  1509. r : Tregister;
  1510. begin
  1511. case target_info.system of
  1512. system_i386_win32,
  1513. system_i386_freebsd,
  1514. system_i386_wdosx,
  1515. system_i386_linux:
  1516. begin
  1517. objectlibrary.getaddrlabel(pl);
  1518. list.concat(Tai_section.Create(sec_data));
  1519. list.concat(Tai_align.Create(4));
  1520. list.concat(Tai_label.Create(pl));
  1521. list.concat(Tai_const.Create_32bit(0));
  1522. list.concat(Tai_section.Create(sec_code));
  1523. r.enum:=R_INTREGISTER;
  1524. r.number:=NR_EDX;
  1525. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,r));
  1526. a_call_name(list,target_info.Cprefix+'mcount');
  1527. include(rg.usedinproc,R_EDX);
  1528. end;
  1529. system_i386_go32v2:
  1530. begin
  1531. a_call_name(list,'MCOUNT');
  1532. end;
  1533. end;
  1534. end;
  1535. procedure tcgx86.g_stackpointer_alloc(list : taasmoutput;localsize : longint);
  1536. var
  1537. href : treference;
  1538. i : integer;
  1539. again : tasmlabel;
  1540. r,rsp : Tregister;
  1541. begin
  1542. r.enum:=R_INTREGISTER;
  1543. rsp.enum:=R_INTREGISTER;
  1544. rsp.number:=NR_ESP;
  1545. if localsize>0 then
  1546. begin
  1547. {$ifndef NOTARGETWIN32}
  1548. { windows guards only a few pages for stack growing, }
  1549. { so we have to access every page first }
  1550. if (target_info.system=system_i386_win32) and
  1551. (localsize>=winstackpagesize) then
  1552. begin
  1553. if localsize div winstackpagesize<=5 then
  1554. begin
  1555. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,rsp));
  1556. for i:=1 to localsize div winstackpagesize do
  1557. begin
  1558. reference_reset_base(href,rsp,localsize-i*winstackpagesize);
  1559. list.concat(Taicpu.op_const_ref(A_MOV,S_L,0,href));
  1560. end;
  1561. r.number:=NR_EAX;
  1562. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1563. end
  1564. else
  1565. begin
  1566. objectlibrary.getlabel(again);
  1567. r.number:=NR_EDI;
  1568. rg.getexplicitregisterint(list,NR_EDI);
  1569. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,r));
  1570. a_label(list,again);
  1571. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1572. r.number:=NR_EAX;
  1573. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1574. r.number:=NR_EDI;
  1575. list.concat(Taicpu.op_reg(A_DEC,S_L,r));
  1576. a_jmp_cond(list,OC_NE,again);
  1577. rg.ungetregisterint(list,r);
  1578. list.concat(Taicpu.op_const_reg(A_SUB,S_L,localsize mod winstackpagesize,rsp));
  1579. end
  1580. end
  1581. else
  1582. {$endif NOTARGETWIN32}
  1583. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize,rsp));
  1584. end;
  1585. end;
  1586. procedure tcgx86.g_stackframe_entry(list : taasmoutput;localsize : longint);
  1587. var r,rsp:Tregister;
  1588. begin
  1589. r.enum:=R_INTREGISTER;
  1590. r.number:=NR_EBP;
  1591. rsp.enum:=R_INTREGISTER;
  1592. rsp.number:=NR_ESP;
  1593. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1594. list.concat(Taicpu.Op_reg_reg(A_MOV,S_L,rsp,r));
  1595. if localsize>0 then
  1596. g_stackpointer_alloc(list,localsize);
  1597. end;
  1598. procedure tcgx86.g_restore_frame_pointer(list : taasmoutput);
  1599. begin
  1600. list.concat(Taicpu.Op_none(A_LEAVE,S_NO));
  1601. end;
  1602. procedure tcgx86.g_return_from_proc(list : taasmoutput;parasize : aword);
  1603. begin
  1604. { Routines with the poclearstack flag set use only a ret }
  1605. { also routines with parasize=0 }
  1606. if (po_clearstack in current_procdef.procoptions) then
  1607. begin
  1608. { complex return values are removed from stack in C code PM }
  1609. if paramanager.ret_in_param(current_procdef.rettype.def,current_procdef.proccalloption) then
  1610. list.concat(Taicpu.Op_const(A_RET,S_NO,4))
  1611. else
  1612. list.concat(Taicpu.Op_none(A_RET,S_NO));
  1613. end
  1614. else if (parasize=0) then
  1615. list.concat(Taicpu.Op_none(A_RET,S_NO))
  1616. else
  1617. begin
  1618. { parameters are limited to 65535 bytes because }
  1619. { ret allows only imm16 }
  1620. if (parasize>65535) then
  1621. CGMessage(cg_e_parasize_too_big);
  1622. list.concat(Taicpu.Op_const(A_RET,S_NO,parasize));
  1623. end;
  1624. end;
  1625. procedure tcgx86.g_save_standard_registers(list:Taasmoutput;usedinproc:Tsupregset);
  1626. var r:Tregister;
  1627. begin
  1628. r.enum:=R_INTREGISTER;
  1629. r.number:=NR_EBX;
  1630. if (RS_EBX in usedinproc) then
  1631. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1632. r.number:=NR_ESI;
  1633. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1634. r.number:=NR_EDI;
  1635. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1636. end;
  1637. procedure tcgx86.g_restore_standard_registers(list:Taasmoutput;usedinproc:Tsupregset);
  1638. var r:Tregister;
  1639. begin
  1640. r.enum:=R_INTREGISTER;
  1641. r.number:=NR_EDI;
  1642. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1643. r.number:=NR_ESI;
  1644. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1645. r.number:=NR_EBX;
  1646. if (RS_EBX in usedinproc) then
  1647. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1648. end;
  1649. procedure tcgx86.g_save_all_registers(list : taasmoutput);
  1650. begin
  1651. list.concat(Taicpu.Op_none(A_PUSHA,S_L));
  1652. end;
  1653. procedure tcgx86.g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);
  1654. var
  1655. href : treference;
  1656. r,rsp: Tregister;
  1657. begin
  1658. rsp.enum:=R_INTREGISTER;
  1659. rsp.number:=NR_ESP;
  1660. r.enum:=R_INTREGISTER;
  1661. if acchiused then
  1662. begin
  1663. reference_reset_base(href,rsp,20);
  1664. r.number:=NR_EDX;
  1665. list.concat(Taicpu.Op_reg_ref(A_MOV,S_L,r,href));
  1666. end;
  1667. if accused then
  1668. begin
  1669. reference_reset_base(href,rsp,28);
  1670. r.number:=NR_EAX;
  1671. list.concat(Taicpu.Op_reg_ref(A_MOV,S_L,r,href));
  1672. end;
  1673. list.concat(Taicpu.Op_none(A_POPA,S_L));
  1674. { We add a NOP because of the 386DX CPU bugs with POPAD }
  1675. list.concat(taicpu.op_none(A_NOP,S_L));
  1676. end;
  1677. { produces if necessary overflowcode }
  1678. procedure tcgx86.g_overflowcheck(list: taasmoutput; const p: tnode);
  1679. var
  1680. hl : tasmlabel;
  1681. ai : taicpu;
  1682. cond : TAsmCond;
  1683. begin
  1684. if not(cs_check_overflow in aktlocalswitches) then
  1685. exit;
  1686. objectlibrary.getlabel(hl);
  1687. if not ((p.resulttype.def.deftype=pointerdef) or
  1688. ((p.resulttype.def.deftype=orddef) and
  1689. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1690. bool8bit,bool16bit,bool32bit]))) then
  1691. cond:=C_NO
  1692. else
  1693. cond:=C_NB;
  1694. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  1695. ai.SetCondition(cond);
  1696. ai.is_jmp:=true;
  1697. list.concat(ai);
  1698. a_call_name(list,'FPC_OVERFLOW');
  1699. a_label(list,hl);
  1700. end;
  1701. end.
  1702. {
  1703. $Log$
  1704. Revision 1.49 2003-06-01 21:38:07 peter
  1705. * getregisterfpu size parameter added
  1706. * op_const_reg size parameter added
  1707. * sparc updates
  1708. Revision 1.48 2003/05/30 23:57:08 peter
  1709. * more sparc cleanup
  1710. * accumulator removed, splitted in function_return_reg (called) and
  1711. function_result_reg (caller)
  1712. Revision 1.47 2003/05/22 21:33:31 peter
  1713. * removed some unit dependencies
  1714. Revision 1.46 2003/05/16 14:33:31 peter
  1715. * regvar fixes
  1716. Revision 1.45 2003/05/15 18:58:54 peter
  1717. * removed selfpointer_offset, vmtpointer_offset
  1718. * tvarsym.adjusted_address
  1719. * address in localsymtable is now in the real direction
  1720. * removed some obsolete globals
  1721. Revision 1.44 2003/04/30 20:53:32 florian
  1722. * error when address of an abstract method is taken
  1723. * fixed some x86-64 problems
  1724. * merged some more x86-64 and i386 code
  1725. Revision 1.43 2003/04/27 11:21:36 peter
  1726. * aktprocdef renamed to current_procdef
  1727. * procinfo renamed to current_procinfo
  1728. * procinfo will now be stored in current_module so it can be
  1729. cleaned up properly
  1730. * gen_main_procsym changed to create_main_proc and release_main_proc
  1731. to also generate a tprocinfo structure
  1732. * fixed unit implicit initfinal
  1733. Revision 1.42 2003/04/23 14:42:08 daniel
  1734. * Further register allocator work. Compiler now smaller with new
  1735. allocator than without.
  1736. * Somebody forgot to adjust ppu version number
  1737. Revision 1.41 2003/04/23 09:51:16 daniel
  1738. * Removed usage of edi in a lot of places when new register allocator used
  1739. + Added newra versions of g_concatcopy and secondadd_float
  1740. Revision 1.40 2003/04/22 13:47:08 peter
  1741. * fixed C style array of const
  1742. * fixed C array passing
  1743. * fixed left to right with high parameters
  1744. Revision 1.39 2003/04/22 10:09:35 daniel
  1745. + Implemented the actual register allocator
  1746. + Scratch registers unavailable when new register allocator used
  1747. + maybe_save/maybe_restore unavailable when new register allocator used
  1748. Revision 1.38 2003/04/17 16:48:21 daniel
  1749. * Added some code to keep track of move instructions in register
  1750. allocator
  1751. Revision 1.37 2003/03/28 19:16:57 peter
  1752. * generic constructor working for i386
  1753. * remove fixed self register
  1754. * esi added as address register for i386
  1755. Revision 1.36 2003/03/18 18:17:46 peter
  1756. * reg2opsize()
  1757. Revision 1.35 2003/03/13 19:52:23 jonas
  1758. * and more new register allocator fixes (in the i386 code generator this
  1759. time). At least now the ppc cross compiler can compile the linux
  1760. system unit again, but I haven't tested it.
  1761. Revision 1.34 2003/02/27 16:40:32 daniel
  1762. * Fixed ie 200301234 problem on Win32 target
  1763. Revision 1.33 2003/02/26 21:15:43 daniel
  1764. * Fixed the optimizer
  1765. Revision 1.32 2003/02/19 22:00:17 daniel
  1766. * Code generator converted to new register notation
  1767. - Horribily outdated todo.txt removed
  1768. Revision 1.31 2003/01/21 10:41:13 daniel
  1769. * Fixed another 200301081
  1770. Revision 1.30 2003/01/13 23:00:18 daniel
  1771. * Fixed internalerror
  1772. Revision 1.29 2003/01/13 14:54:34 daniel
  1773. * Further work to convert codegenerator register convention;
  1774. internalerror bug fixed.
  1775. Revision 1.28 2003/01/09 20:41:00 daniel
  1776. * Converted some code in cgx86.pas to new register numbering
  1777. Revision 1.27 2003/01/08 18:43:58 daniel
  1778. * Tregister changed into a record
  1779. Revision 1.26 2003/01/05 13:36:53 florian
  1780. * x86-64 compiles
  1781. + very basic support for float128 type (x86-64 only)
  1782. Revision 1.25 2003/01/02 16:17:50 peter
  1783. * align stack on 4 bytes in copyvalueopenarray
  1784. Revision 1.24 2002/12/24 15:56:50 peter
  1785. * stackpointer_alloc added for adjusting ESP. Win32 needs
  1786. this for the pageprotection
  1787. Revision 1.23 2002/11/25 18:43:34 carl
  1788. - removed the invalid if <> checking (Delphi is strange on this)
  1789. + implemented abstract warning on instance creation of class with
  1790. abstract methods.
  1791. * some error message cleanups
  1792. Revision 1.22 2002/11/25 17:43:29 peter
  1793. * splitted defbase in defutil,symutil,defcmp
  1794. * merged isconvertable and is_equal into compare_defs(_ext)
  1795. * made operator search faster by walking the list only once
  1796. Revision 1.21 2002/11/18 17:32:01 peter
  1797. * pass proccalloption to ret_in_xxx and push_xxx functions
  1798. Revision 1.20 2002/11/09 21:18:31 carl
  1799. * flags2reg() was not extending the byte register to the correct result size
  1800. Revision 1.19 2002/10/16 19:01:43 peter
  1801. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1802. implicit exception frames for procedures with initialized variables
  1803. and for constructors. The default is on for compatibility
  1804. Revision 1.18 2002/10/05 12:43:30 carl
  1805. * fixes for Delphi 6 compilation
  1806. (warning : Some features do not work under Delphi)
  1807. Revision 1.17 2002/09/17 18:54:06 jonas
  1808. * a_load_reg_reg() now has two size parameters: source and dest. This
  1809. allows some optimizations on architectures that don't encode the
  1810. register size in the register name.
  1811. Revision 1.16 2002/09/16 19:08:47 peter
  1812. * support references without registers and symbol in paramref_addr. It
  1813. pushes only the offset
  1814. Revision 1.15 2002/09/16 18:06:29 peter
  1815. * move CGSize2Opsize to interface
  1816. Revision 1.14 2002/09/01 14:42:41 peter
  1817. * removevaluepara added to fix the stackpointer so restoring of
  1818. saved registers works
  1819. Revision 1.13 2002/09/01 12:09:27 peter
  1820. + a_call_reg, a_call_loc added
  1821. * removed exprasmlist references
  1822. Revision 1.12 2002/08/17 09:23:50 florian
  1823. * first part of procinfo rewrite
  1824. Revision 1.11 2002/08/16 14:25:00 carl
  1825. * issameref() to test if two references are the same (then emit no opcodes)
  1826. + ret_in_reg to replace ret_in_acc
  1827. (fix some register allocation bugs at the same time)
  1828. + save_std_register now has an extra parameter which is the
  1829. usedinproc registers
  1830. Revision 1.10 2002/08/15 08:13:54 carl
  1831. - a_load_sym_ofs_reg removed
  1832. * loadvmt now calls loadaddr_ref_reg instead
  1833. Revision 1.9 2002/08/11 14:32:33 peter
  1834. * renamed current_library to objectlibrary
  1835. Revision 1.8 2002/08/11 13:24:20 peter
  1836. * saving of asmsymbols in ppu supported
  1837. * asmsymbollist global is removed and moved into a new class
  1838. tasmlibrarydata that will hold the info of a .a file which
  1839. corresponds with a single module. Added librarydata to tmodule
  1840. to keep the library info stored for the module. In the future the
  1841. objectfiles will also be stored to the tasmlibrarydata class
  1842. * all getlabel/newasmsymbol and friends are moved to the new class
  1843. Revision 1.7 2002/08/10 10:06:04 jonas
  1844. * fixed stupid bug of mine in g_flags2reg() when optimizations are on
  1845. Revision 1.6 2002/08/09 19:18:27 carl
  1846. * fix generic exception handling
  1847. Revision 1.5 2002/08/04 19:52:04 carl
  1848. + updated exception routines
  1849. Revision 1.4 2002/07/27 19:53:51 jonas
  1850. + generic implementation of tcg.g_flags2ref()
  1851. * tcg.flags2xxx() now also needs a size parameter
  1852. Revision 1.3 2002/07/26 21:15:46 florian
  1853. * rewrote the system handling
  1854. Revision 1.2 2002/07/21 16:55:34 jonas
  1855. * fixed bug in op_const_reg_reg() for imul
  1856. Revision 1.1 2002/07/20 19:28:47 florian
  1857. * splitting of i386\cgcpu.pas into x86\cgx86.pas and i386\cgcpu.pas
  1858. cgx86.pas will contain the common code for i386 and x86_64
  1859. }