cgx86.pas 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140
  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,cpupara,
  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; 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;selfused,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;selfused,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. regsize_2_cgsize: array[S_B..S_L] of tcgsize = (OS_8,OS_16,OS_32);
  256. begin
  257. result := regsize_2_cgsize[reg2opsize(reg)];
  258. end;
  259. { currently does nothing }
  260. procedure tcgx86.a_jmp_always(list : taasmoutput;l: tasmlabel);
  261. begin
  262. a_jmp_cond(list, OC_NONE, l);
  263. end;
  264. { we implement the following routines because otherwise we can't }
  265. { instantiate the class since it's abstract }
  266. procedure tcgx86.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);
  267. begin
  268. case size of
  269. OS_8,OS_S8,
  270. OS_16,OS_S16:
  271. begin
  272. if target_info.alignment.paraalign = 2 then
  273. r.number:=(r.number and not $ff) or R_SUBW
  274. else
  275. r.number:=(r.number and not $ff) or R_SUBD;
  276. list.concat(taicpu.op_reg(A_PUSH,S_L,r));
  277. end;
  278. OS_32,OS_S32:
  279. begin
  280. if r.number and $ff<>R_SUBD then
  281. internalerror(7843);
  282. list.concat(taicpu.op_reg(A_PUSH,S_L,r));
  283. end
  284. else
  285. internalerror(2002032212);
  286. end;
  287. end;
  288. procedure tcgx86.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  289. begin
  290. case size of
  291. OS_8,OS_S8,OS_16,OS_S16:
  292. begin
  293. if target_info.alignment.paraalign = 2 then
  294. list.concat(taicpu.op_const(A_PUSH,S_W,a))
  295. else
  296. list.concat(taicpu.op_const(A_PUSH,S_L,a));
  297. end;
  298. OS_32,OS_S32:
  299. list.concat(taicpu.op_const(A_PUSH,S_L,a));
  300. else
  301. internalerror(2002032213);
  302. end;
  303. end;
  304. procedure tcgx86.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  305. var
  306. tmpreg: tregister;
  307. begin
  308. case size of
  309. OS_8,OS_S8,
  310. OS_16,OS_S16:
  311. begin
  312. {$ifdef newra}
  313. if target_info.alignment.paraalign = 2 then
  314. tmpreg:=rg.getregisterint(list,OS_16)
  315. else
  316. tmpreg:=rg.getregisterint(list,OS_32);
  317. {$else}
  318. if target_info.alignment.paraalign = 2 then
  319. tmpreg:=get_scratch_reg_int(list,OS_16)
  320. else
  321. tmpreg:=get_scratch_reg_int(list,OS_32);
  322. {$endif}
  323. a_load_ref_reg(list,size,r,tmpreg);
  324. list.concat(taicpu.op_reg(A_PUSH,S_L,tmpreg));
  325. {$ifdef newra}
  326. rg.ungetregisterint(list,tmpreg);
  327. {$else}
  328. free_scratch_reg(list,tmpreg);
  329. {$endif}
  330. end;
  331. OS_32,OS_S32:
  332. list.concat(taicpu.op_ref(A_PUSH,S_L,r));
  333. OS_64,OS_S64:
  334. list.concat(taicpu.op_ref(A_PUSH,S_Q,r));
  335. else
  336. internalerror(2002032214);
  337. end;
  338. end;
  339. procedure tcgx86.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  340. var
  341. tmpreg: tregister;
  342. baseno,indexno:boolean;
  343. begin
  344. if not((r.segment.enum=R_NO) or ((r.segment.enum=R_INTREGISTER) and (r.segment.number=NR_NO))) then
  345. CGMessage(cg_e_cant_use_far_pointer_there);
  346. baseno:=(r.base.enum=R_NO) or ((r.base.enum=R_INTREGISTER) and (r.base.number=NR_NO));
  347. indexno:=(r.index.enum=R_NO) or ((r.index.enum=R_INTREGISTER) and (r.index.number=NR_NO));
  348. if baseno and indexno then
  349. begin
  350. if assigned(r.symbol) then
  351. list.concat(Taicpu.Op_sym_ofs(A_PUSH,S_L,r.symbol,r.offset))
  352. else
  353. list.concat(Taicpu.Op_const(A_PUSH,S_L,r.offset));
  354. end
  355. else if baseno and not indexno and
  356. (r.offset=0) and (r.scalefactor=0) and (r.symbol=nil) then
  357. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r.index))
  358. else if not baseno and indexno and
  359. (r.offset=0) and (r.symbol=nil) then
  360. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r.base))
  361. else
  362. begin
  363. {$ifdef newra}
  364. tmpreg:=rg.getaddressregister(list);
  365. {$else}
  366. tmpreg := get_scratch_reg_address(list);
  367. {$endif}
  368. a_loadaddr_ref_reg(list,r,tmpreg);
  369. list.concat(taicpu.op_reg(A_PUSH,S_L,tmpreg));
  370. {$ifdef newra}
  371. rg.ungetregisterint(list,tmpreg);
  372. {$else}
  373. free_scratch_reg(list,tmpreg);
  374. {$endif}
  375. end;
  376. end;
  377. procedure tcgx86.a_call_name(list : taasmoutput;const s : string);
  378. begin
  379. list.concat(taicpu.op_sym(A_CALL,S_NO,objectlibrary.newasmsymbol(s)));
  380. end;
  381. procedure tcgx86.a_call_ref(list : taasmoutput;const ref : treference);
  382. begin
  383. list.concat(taicpu.op_ref(A_CALL,S_NO,ref));
  384. end;
  385. procedure tcgx86.a_call_reg(list : taasmoutput;reg : tregister);
  386. begin
  387. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  388. end;
  389. {********************** load instructions ********************}
  390. procedure tcgx86.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  391. begin
  392. { the optimizer will change it to "xor reg,reg" when loading zero, }
  393. { no need to do it here too (JM) }
  394. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[size],a,reg))
  395. end;
  396. procedure tcgx86.a_load_const_ref(list : taasmoutput; size: tcgsize; a : aword;const ref : treference);
  397. begin
  398. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[size],a,ref));
  399. end;
  400. procedure tcgx86.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref : treference);
  401. begin
  402. list.concat(taicpu.op_reg_ref(A_MOV,TCGSize2OpSize[size],reg,
  403. ref));
  404. end;
  405. procedure tcgx86.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref: treference;reg : tregister);
  406. var
  407. op: tasmop;
  408. o,s: topsize;
  409. begin
  410. if reg.enum<>R_INTREGISTER then
  411. internalerror(200302058);
  412. o:=reg2opsize(reg);
  413. sizes2load(size,o,op,s);
  414. list.concat(taicpu.op_ref_reg(op,s,ref,reg));
  415. end;
  416. procedure tcgx86.a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  417. var
  418. op: tasmop;
  419. s: topsize;
  420. eq:boolean;
  421. instr:Taicpu;
  422. begin
  423. if (reg1.enum=R_INTREGISTER) and (reg2.enum=R_INTREGISTER) then
  424. begin
  425. sizes2load(fromsize,reg2opsize(reg2),op,s);
  426. eq:=(reg1.number shr 8)=(reg2.number shr 8);
  427. end
  428. else
  429. internalerror(200301081);
  430. if eq then
  431. begin
  432. { "mov reg1, reg1" doesn't make sense }
  433. if op = A_MOV then
  434. exit;
  435. end;
  436. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  437. {Notify the register allocator that we have written a move instruction so
  438. it can try to eliminate it.}
  439. {$ifdef newra}
  440. rg.add_move_instruction(instr);
  441. {$endif}
  442. list.concat(instr);
  443. end;
  444. procedure tcgx86.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  445. begin
  446. if ref.base.enum<>R_INTREGISTER then
  447. internalerror(200302102);
  448. if ref.index.enum<>R_INTREGISTER then
  449. internalerror(200302102);
  450. if assigned(ref.symbol) and
  451. (ref.base.number=NR_NO) and
  452. (ref.index.number=NR_NO) then
  453. list.concat(taicpu.op_sym_ofs_reg(A_MOV,S_L,ref.symbol,ref.offset,r))
  454. else
  455. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,r));
  456. end;
  457. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  458. { R_ST means "the current value at the top of the fpu stack" (JM) }
  459. procedure tcgx86.a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  460. begin
  461. if (reg1.enum <> R_ST) then
  462. begin
  463. list.concat(taicpu.op_reg(A_FLD,S_NO,
  464. trgcpu(rg).correct_fpuregister(reg1,trgcpu(rg).fpuvaroffset)));
  465. inc(trgcpu(rg).fpuvaroffset);
  466. end;
  467. if (reg2.enum <> R_ST) then
  468. begin
  469. list.concat(taicpu.op_reg(A_FSTP,S_NO,
  470. trgcpu(rg).correct_fpuregister(reg2,trgcpu(rg).fpuvaroffset)));
  471. dec(trgcpu(rg).fpuvaroffset);
  472. end;
  473. end;
  474. procedure tcgx86.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  475. var rst:Tregister;
  476. begin
  477. rst.enum:=R_ST;
  478. floatload(list,size,ref);
  479. if reg.enum>lastreg then
  480. internalerror(200301081);
  481. if (reg.enum <> R_ST) then
  482. a_loadfpu_reg_reg(list,rst,reg);
  483. end;
  484. procedure tcgx86.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  485. var rst:Tregister;
  486. begin
  487. rst.enum:=R_ST;
  488. if reg.enum>lastreg then
  489. internalerror(200301081);
  490. if reg.enum <> R_ST then
  491. a_loadfpu_reg_reg(list,reg,rst);
  492. floatstore(list,size,ref);
  493. end;
  494. procedure tcgx86.a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  495. begin
  496. list.concat(taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2));
  497. end;
  498. procedure tcgx86.a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister);
  499. begin
  500. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,ref,reg));
  501. end;
  502. procedure tcgx86.a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference);
  503. begin
  504. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,ref));
  505. end;
  506. procedure tcgx86.a_parammm_reg(list: taasmoutput; reg: tregister);
  507. var
  508. href : treference;
  509. r : Tregister;
  510. begin
  511. r.enum:=R_INTREGISTER;
  512. r.number:=NR_ESP;
  513. list.concat(taicpu.op_const_reg(A_SUB,S_L,8,r));
  514. reference_reset_base(href,r,0);
  515. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,href));
  516. end;
  517. procedure tcgx86.a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister);
  518. var
  519. opcode: tasmop;
  520. power: longint;
  521. begin
  522. if reg.enum<>R_INTREGISTER then
  523. internalerror(200302034);
  524. case op of
  525. OP_DIV, OP_IDIV:
  526. begin
  527. if ispowerof2(a,power) then
  528. begin
  529. case op of
  530. OP_DIV:
  531. opcode := A_SHR;
  532. OP_IDIV:
  533. opcode := A_SAR;
  534. end;
  535. list.concat(taicpu.op_const_reg(opcode,reg2opsize(reg),
  536. power,reg));
  537. exit;
  538. end;
  539. { the rest should be handled specifically in the code }
  540. { generator because of the silly register usage restraints }
  541. internalerror(200109224);
  542. end;
  543. OP_MUL,OP_IMUL:
  544. begin
  545. if not(cs_check_overflow in aktlocalswitches) and
  546. ispowerof2(a,power) then
  547. begin
  548. list.concat(taicpu.op_const_reg(A_SHL,reg2opsize(reg),
  549. power,reg));
  550. exit;
  551. end;
  552. if op = OP_IMUL then
  553. list.concat(taicpu.op_const_reg(A_IMUL,reg2opsize(reg),
  554. a,reg))
  555. else
  556. { OP_MUL should be handled specifically in the code }
  557. { generator because of the silly register usage restraints }
  558. internalerror(200109225);
  559. end;
  560. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  561. if not(cs_check_overflow in aktlocalswitches) and
  562. (a = 1) and
  563. (op in [OP_ADD,OP_SUB]) then
  564. if op = OP_ADD then
  565. list.concat(taicpu.op_reg(A_INC,reg2opsize(reg),reg))
  566. else
  567. list.concat(taicpu.op_reg(A_DEC,reg2opsize(reg),reg))
  568. else if (a = 0) then
  569. if (op <> OP_AND) then
  570. exit
  571. else
  572. list.concat(taicpu.op_const_reg(A_MOV,reg2opsize(reg),0,reg))
  573. else if (a = high(aword)) and
  574. (op in [OP_AND,OP_OR,OP_XOR]) then
  575. begin
  576. case op of
  577. OP_AND:
  578. exit;
  579. OP_OR:
  580. list.concat(taicpu.op_const_reg(A_MOV,reg2opsize(reg),high(aword),reg));
  581. OP_XOR:
  582. list.concat(taicpu.op_reg(A_NOT,reg2opsize(reg),reg));
  583. end
  584. end
  585. else
  586. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],reg2opsize(reg),
  587. a,reg));
  588. OP_SHL,OP_SHR,OP_SAR:
  589. begin
  590. if (a and 31) <> 0 Then
  591. list.concat(taicpu.op_const_reg(
  592. TOpCG2AsmOp[op],reg2opsize(reg),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. var
  803. opsize: topsize;
  804. begin
  805. if reg.enum<>R_INTREGISTER then
  806. internalerror(200302036);
  807. case op of
  808. OP_NEG,OP_NOT:
  809. begin
  810. if reg.number<>NR_NO then
  811. internalerror(200109237);
  812. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],ref));
  813. end;
  814. OP_IMUL:
  815. begin
  816. { this one needs a load/imul/store, which is the default }
  817. inherited a_op_ref_reg(list,op,size,ref,reg);
  818. end;
  819. OP_MUL,OP_DIV,OP_IDIV:
  820. { special stuff, needs separate handling inside code }
  821. { generator }
  822. internalerror(200109238);
  823. else
  824. begin
  825. opsize := tcgsize2opsize[size];
  826. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],opsize,reg,ref));
  827. end;
  828. end;
  829. end;
  830. procedure tcgx86.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  831. size: tcgsize; a: aword; src, dst: tregister);
  832. var
  833. tmpref: treference;
  834. power: longint;
  835. opsize: topsize;
  836. begin
  837. if src.enum<>R_INTREGISTER then
  838. internalerror(200302057);
  839. if dst.enum<>R_INTREGISTER then
  840. internalerror(200302057);
  841. opsize := reg2opsize(src);
  842. if (opsize <> S_L) or
  843. not (size in [OS_32,OS_S32]) then
  844. begin
  845. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  846. exit;
  847. end;
  848. { if we get here, we have to do a 32 bit calculation, guaranteed }
  849. case op of
  850. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  851. OP_SAR:
  852. { can't do anything special for these }
  853. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  854. OP_IMUL:
  855. begin
  856. if not(cs_check_overflow in aktlocalswitches) and
  857. ispowerof2(a,power) then
  858. { can be done with a shift }
  859. begin
  860. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  861. exit;
  862. end;
  863. list.concat(taicpu.op_const_reg_reg(A_IMUL,S_L,a,src,dst));
  864. end;
  865. OP_ADD, OP_SUB:
  866. if (a = 0) then
  867. a_load_reg_reg(list,size,size,src,dst)
  868. else
  869. begin
  870. reference_reset(tmpref);
  871. tmpref.base := src;
  872. tmpref.offset := longint(a);
  873. if op = OP_SUB then
  874. tmpref.offset := -tmpref.offset;
  875. list.concat(taicpu.op_ref_reg(A_LEA,S_L,tmpref,dst));
  876. end
  877. else internalerror(200112302);
  878. end;
  879. end;
  880. procedure tcgx86.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  881. size: tcgsize; src1, src2, dst: tregister);
  882. var
  883. tmpref: treference;
  884. opsize: topsize;
  885. begin
  886. if src1.enum>lastreg then
  887. internalerror(200201081);
  888. if src2.enum>lastreg then
  889. internalerror(200201081);
  890. if dst.enum>lastreg then
  891. internalerror(200201081);
  892. opsize := reg2opsize(src1);
  893. if (opsize <> S_L) or
  894. (reg2opsize(src2) <> S_L) or
  895. not (size in [OS_32,OS_S32]) then
  896. begin
  897. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  898. exit;
  899. end;
  900. { if we get here, we have to do a 32 bit calculation, guaranteed }
  901. Case Op of
  902. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  903. OP_SAR,OP_SUB,OP_NOT,OP_NEG:
  904. { can't do anything special for these }
  905. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  906. OP_IMUL:
  907. list.concat(taicpu.op_reg_reg_reg(A_IMUL,S_L,src1,src2,dst));
  908. OP_ADD:
  909. begin
  910. reference_reset(tmpref);
  911. tmpref.base := src1;
  912. tmpref.index := src2;
  913. tmpref.scalefactor := 1;
  914. list.concat(taicpu.op_ref_reg(A_LEA,S_L,tmpref,dst));
  915. end
  916. else internalerror(200112303);
  917. end;
  918. end;
  919. {*************** compare instructructions ****************}
  920. procedure tcgx86.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  921. l : tasmlabel);
  922. begin
  923. if reg.enum=R_INTREGISTER then
  924. begin
  925. if (a = 0) then
  926. list.concat(taicpu.op_reg_reg(A_TEST,reg2opsize(reg),reg,reg))
  927. else
  928. list.concat(taicpu.op_const_reg(A_CMP,reg2opsize(reg),a,reg));
  929. end
  930. else
  931. internalerror(200303131);
  932. a_jmp_cond(list,cmp_op,l);
  933. end;
  934. procedure tcgx86.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  935. l : tasmlabel);
  936. begin
  937. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,ref));
  938. a_jmp_cond(list,cmp_op,l);
  939. end;
  940. procedure tcgx86.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  941. reg1,reg2 : tregister;l : tasmlabel);
  942. begin
  943. if reg1.enum<>R_INTREGISTER then
  944. internalerror(200101081);
  945. if reg2.enum<>R_INTREGISTER then
  946. internalerror(200101081);
  947. if reg2opsize(reg1) <> reg2opsize(reg2) then
  948. internalerror(200109226);
  949. list.concat(taicpu.op_reg_reg(A_CMP,reg2opsize(reg1),reg1,reg2));
  950. a_jmp_cond(list,cmp_op,l);
  951. end;
  952. procedure tcgx86.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  953. begin
  954. if reg.enum<>R_INTREGISTER then
  955. internalerror(200302059);
  956. reg.number:=(reg.number and not $ff) or cgsize2subreg(size);
  957. list.concat(taicpu.op_ref_reg(A_CMP,tcgsize2opsize[size],ref,reg));
  958. a_jmp_cond(list,cmp_op,l);
  959. end;
  960. procedure tcgx86.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  961. var
  962. ai : taicpu;
  963. begin
  964. if cond=OC_None then
  965. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  966. else
  967. begin
  968. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  969. ai.SetCondition(TOpCmp2AsmCond[cond]);
  970. end;
  971. ai.is_jmp:=true;
  972. list.concat(ai);
  973. end;
  974. procedure tcgx86.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  975. var
  976. ai : taicpu;
  977. begin
  978. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  979. ai.SetCondition(flags_to_cond(f));
  980. ai.is_jmp := true;
  981. list.concat(ai);
  982. end;
  983. procedure tcgx86.g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister);
  984. var
  985. ai : taicpu;
  986. hreg : tregister;
  987. begin
  988. if reg.enum<>R_INTREGISTER then
  989. internalerror(200202031);
  990. hreg.enum:=R_INTREGISTER;
  991. hreg.number:=(reg.number and not $ff) or R_SUBL;
  992. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  993. ai.setcondition(flags_to_cond(f));
  994. list.concat(ai);
  995. if (reg.number <> hreg.number) then
  996. a_load_reg_reg(list,OS_8,size,hreg,reg);
  997. end;
  998. procedure tcgx86.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref: TReference);
  999. var
  1000. ai : taicpu;
  1001. begin
  1002. if not(size in [OS_8,OS_S8]) then
  1003. a_load_const_ref(list,size,0,ref);
  1004. ai:=Taicpu.op_ref(A_SETcc,S_B,ref);
  1005. ai.setcondition(flags_to_cond(f));
  1006. list.concat(ai);
  1007. end;
  1008. { ************* concatcopy ************ }
  1009. {$ifdef newra}
  1010. procedure Tcgx86.g_concatcopy(list:Taasmoutput;const source,dest:Treference;
  1011. len:aword;delsource,loadref:boolean);
  1012. var srcref,dstref:Treference;
  1013. srcreg,destreg,countreg,r:Tregister;
  1014. helpsize:aword;
  1015. copysize:byte;
  1016. cgsize:Tcgsize;
  1017. begin
  1018. helpsize:=12;
  1019. if cs_littlesize in aktglobalswitches then
  1020. helpsize:=8;
  1021. if not loadref and (len<=helpsize) then
  1022. begin
  1023. dstref:=dest;
  1024. srcref:=source;
  1025. copysize:=4;
  1026. cgsize:=OS_32;
  1027. while len<>0 do
  1028. begin
  1029. if len<2 then
  1030. begin
  1031. copysize:=1;
  1032. cgsize:=OS_8;
  1033. end
  1034. else if len<4 then
  1035. begin
  1036. copysize:=2;
  1037. cgsize:=OS_16;
  1038. end;
  1039. dec(len,copysize);
  1040. r:=rg.getregisterint(list,cgsize);
  1041. a_load_ref_reg(list,cgsize,srcref,r);
  1042. if (len=0) and delsource then
  1043. reference_release(list,source);
  1044. a_load_reg_ref(list,cgsize,r,dstref);
  1045. inc(srcref.offset,copysize);
  1046. inc(dstref.offset,copysize);
  1047. rg.ungetregisterint(list,r);
  1048. end;
  1049. end
  1050. else
  1051. begin
  1052. destreg:=rg.getexplicitregisterint(list,NR_EDI);
  1053. a_loadaddr_ref_reg(list,dest,destreg);
  1054. srcreg:=rg.getexplicitregisterint(list,NR_ESI);
  1055. if loadref then
  1056. a_load_ref_reg(list,OS_ADDR,source,srcreg)
  1057. else
  1058. begin
  1059. a_loadaddr_ref_reg(list,source,srcreg);
  1060. if delsource then
  1061. begin
  1062. srcref:=source;
  1063. { Don't release ESI register yet, it's needed
  1064. by the movsl }
  1065. if (srcref.base.number=NR_ESI) then
  1066. srcref.base.number:=NR_NO
  1067. else if (srcref.index.number=NR_ESI) then
  1068. srcref.index.number:=NR_NO;
  1069. reference_release(list,srcref);
  1070. end;
  1071. end;
  1072. countreg:=rg.getexplicitregisterint(list,NR_ECX);
  1073. list.concat(Taicpu.op_none(A_CLD,S_NO));
  1074. if cs_littlesize in aktglobalswitches then
  1075. begin
  1076. a_load_const_reg(list,OS_INT,len,countreg);
  1077. list.concat(Taicpu.op_none(A_REP,S_NO));
  1078. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1079. end
  1080. else
  1081. begin
  1082. helpsize:=len shr 2;
  1083. len:=len and 3;
  1084. if helpsize>1 then
  1085. begin
  1086. a_load_const_reg(list,OS_INT,helpsize,countreg);
  1087. list.concat(Taicpu.op_none(A_REP,S_NO));
  1088. end;
  1089. if helpsize>0 then
  1090. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1091. if len>1 then
  1092. begin
  1093. dec(len,2);
  1094. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  1095. end;
  1096. if len=1 then
  1097. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1098. end;
  1099. rg.ungetregisterint(list,countreg);
  1100. rg.ungetregisterint(list,srcreg);
  1101. rg.ungetregisterint(list,destreg);
  1102. end;
  1103. if delsource then
  1104. tg.ungetiftemp(list,source);
  1105. end;
  1106. {$else newra}
  1107. procedure tcgx86.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  1108. var
  1109. ecxpushed,esipushed : boolean;
  1110. helpsize : longint;
  1111. i : byte;
  1112. reg8,reg32 : tregister;
  1113. srcref,dstref : treference;
  1114. swap : boolean;
  1115. srcreg,destreg,r : Tregister;
  1116. function maybepush(r:tnewregister;var pushed:boolean):tregister;
  1117. begin
  1118. pushed:=false;
  1119. result.enum:=R_INTREGISTER;
  1120. result.number:=r;
  1121. if not((r shr 8) in rg.unusedregsint) then
  1122. begin
  1123. list.concat(Taicpu.Op_reg(A_PUSH,S_L,result));
  1124. pushed:=true;
  1125. end
  1126. else
  1127. rg.getexplicitregisterint(list,r);
  1128. end;
  1129. begin
  1130. ecxpushed:=false;
  1131. esipushed:=false;
  1132. if (not loadref) and
  1133. ((len<=8) or
  1134. (not(cs_littlesize in aktglobalswitches ) and (len<=12))) then
  1135. begin
  1136. helpsize:=len shr 2;
  1137. dstref:=dest;
  1138. srcref:=source;
  1139. for i:=1 to helpsize do
  1140. begin
  1141. r:=rg.getexplicitregisterint(list,NR_EDI);
  1142. a_load_ref_reg(list,OS_32,srcref,r);
  1143. If (len = 4) and delsource then
  1144. reference_release(list,source);
  1145. a_load_reg_ref(list,OS_32,r,dstref);
  1146. inc(srcref.offset,4);
  1147. inc(dstref.offset,4);
  1148. dec(len,4);
  1149. rg.ungetregisterint(list,r);
  1150. end;
  1151. if len>1 then
  1152. begin
  1153. r:=rg.getexplicitregisterint(list,NR_DI);
  1154. a_load_ref_reg(list,OS_16,srcref,r);
  1155. If (len = 2) and delsource then
  1156. reference_release(list,source);
  1157. a_load_reg_ref(list,OS_16,r,dstref);
  1158. inc(srcref.offset,2);
  1159. inc(dstref.offset,2);
  1160. dec(len,2);
  1161. rg.ungetregisterint(list,r);
  1162. end;
  1163. r.enum:=R_INTREGISTER;
  1164. reg8.enum:=R_INTREGISTER;
  1165. reg32.enum:=R_INTREGISTER;
  1166. if len>0 then
  1167. begin
  1168. { and now look for an 8 bit register }
  1169. swap:=false;
  1170. if RS_EAX in rg.unusedregsint then reg8:=rg.getexplicitregisterint(list,NR_AL)
  1171. else if RS_EDX in rg.unusedregsint then reg8:=rg.getexplicitregisterint(list,NR_DL)
  1172. else if RS_EBX in rg.unusedregsint then reg8:=rg.getexplicitregisterint(list,NR_BL)
  1173. else if RS_ECX in rg.unusedregsint then reg8:=rg.getexplicitregisterint(list,NR_CL)
  1174. else
  1175. begin
  1176. swap:=true;
  1177. { we need only to check 3 registers, because }
  1178. { one is always not index or base }
  1179. if (dest.base.number<>NR_EAX) and (dest.index.number<>NR_EAX) then
  1180. begin
  1181. reg8.number:=NR_AL;
  1182. reg32.number:=NR_EAX;
  1183. end
  1184. else if (dest.base.number<>NR_EBX) and (dest.index.number<>NR_EBX) then
  1185. begin
  1186. reg8.number:=NR_BL;
  1187. reg32.number:=NR_EBX;
  1188. end
  1189. else if (dest.base.number<>NR_ECX) and (dest.index.number<>NR_ECX) then
  1190. begin
  1191. reg8.number:=NR_CL;
  1192. reg32.number:=NR_ECX;
  1193. end;
  1194. end;
  1195. if swap then
  1196. { was earlier XCHG, of course nonsense }
  1197. begin
  1198. r:=rg.getexplicitregisterint(list,NR_EDI);
  1199. a_load_reg_reg(list,OS_32,OS_32,reg32,r);
  1200. end;
  1201. a_load_ref_reg(list,OS_8,srcref,reg8);
  1202. If delsource and (len=1) then
  1203. reference_release(list,source);
  1204. a_load_reg_ref(list,OS_8,reg8,dstref);
  1205. if swap then
  1206. begin
  1207. r.number:=NR_EDI;
  1208. a_load_reg_reg(list,OS_32,OS_32,r,reg32);
  1209. rg.ungetregisterint(list,r);
  1210. end
  1211. else
  1212. rg.ungetregisterint(list,reg8);
  1213. end;
  1214. end
  1215. else
  1216. begin
  1217. destreg:=rg.getexplicitregisterint(list,NR_EDI);
  1218. a_loadaddr_ref_reg(list,dest,destreg);
  1219. if loadref then
  1220. begin
  1221. srcreg:=maybepush(NR_ESI,esipushed);
  1222. a_load_ref_reg(list,OS_ADDR,source,srcreg)
  1223. end
  1224. else
  1225. begin
  1226. if delsource then
  1227. begin
  1228. if (source.base.number=NR_ESI) then
  1229. srcreg:=source.base
  1230. else if (source.index.number=NR_ESI) then
  1231. srcreg:=source.index
  1232. else
  1233. srcreg:=maybepush(NR_ESI,esipushed);
  1234. end
  1235. else
  1236. srcreg:=maybepush(NR_ESI,esipushed);
  1237. a_loadaddr_ref_reg(list,source,srcreg);
  1238. if delsource then
  1239. begin
  1240. srcref:=source;
  1241. { Don't release ESI register yet, it's needed
  1242. by the movsl }
  1243. if (srcref.base.number=NR_ESI) then
  1244. srcref.base.number:=NR_NO
  1245. else if (srcref.index.number=NR_ESI) then
  1246. srcref.index.number:=NR_NO;
  1247. reference_release(list,srcref);
  1248. end;
  1249. end;
  1250. list.concat(Taicpu.Op_none(A_CLD,S_NO));
  1251. ecxpushed:=false;
  1252. if cs_littlesize in aktglobalswitches then
  1253. begin
  1254. r:=maybepush(NR_ECX,ecxpushed);
  1255. a_load_const_reg(list,OS_INT,len,r);
  1256. list.concat(Taicpu.Op_none(A_REP,S_NO));
  1257. list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1258. end
  1259. else
  1260. begin
  1261. helpsize:=len shr 2;
  1262. len:=len and 3;
  1263. if helpsize>1 then
  1264. begin
  1265. r:=maybepush(NR_ECX,ecxpushed);
  1266. a_load_const_reg(list,OS_INT,helpsize,r);
  1267. list.concat(Taicpu.Op_none(A_REP,S_NO));
  1268. end;
  1269. if helpsize>0 then
  1270. list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1271. if len>1 then
  1272. begin
  1273. dec(len,2);
  1274. list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1275. end;
  1276. if len=1 then
  1277. list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1278. end;
  1279. r.enum:=R_INTREGISTER;
  1280. if ecxpushed then
  1281. begin
  1282. r.number:=NR_ECX;
  1283. list.concat(Taicpu.Op_reg(A_POP,S_L,r))
  1284. end
  1285. else
  1286. begin
  1287. r.number:=NR_ECX;
  1288. rg.ungetregisterint(list,r);
  1289. end;
  1290. if esipushed then
  1291. begin
  1292. r.number:=NR_ESI;
  1293. list.concat(Taicpu.Op_reg(A_POP,S_L,r))
  1294. end
  1295. else
  1296. begin
  1297. r.number:=NR_ESI;
  1298. rg.ungetregisterint(list,r);
  1299. end;
  1300. rg.ungetregisterint(list,destreg);
  1301. end;
  1302. if delsource then
  1303. tg.ungetiftemp(list,source);
  1304. end;
  1305. {$endif newra}
  1306. procedure tcgx86.g_exception_reason_save(list : taasmoutput; const href : treference);
  1307. var r:Tregister;
  1308. begin
  1309. r.enum:=R_INTREGISTER;
  1310. r.number:=NR_EAX;
  1311. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1312. end;
  1313. procedure tcgx86.g_exception_reason_save_const(list : taasmoutput;const href : treference; a: aword);
  1314. begin
  1315. list.concat(Taicpu.op_const(A_PUSH,S_L,a));
  1316. end;
  1317. procedure tcgx86.g_exception_reason_load(list : taasmoutput; const href : treference);
  1318. var r:Tregister;
  1319. begin
  1320. r.enum:=R_INTREGISTER;
  1321. r.number:=NR_EAX;
  1322. list.concat(Taicpu.op_reg(A_POP,S_L,r));
  1323. end;
  1324. {****************************************************************************
  1325. Entry/Exit Code Helpers
  1326. ****************************************************************************}
  1327. procedure tcgx86.g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);
  1328. var
  1329. lenref : treference;
  1330. power,len : longint;
  1331. opsize : topsize;
  1332. {$ifndef __NOWINPECOFF__}
  1333. again,ok : tasmlabel;
  1334. {$endif}
  1335. r,r2,rsp:Tregister;
  1336. begin
  1337. lenref:=ref;
  1338. inc(lenref.offset,4);
  1339. { get stack space }
  1340. r.enum:=R_INTREGISTER;
  1341. r.number:=NR_EDI;
  1342. rsp.enum:=R_INTREGISTER;
  1343. rsp.number:=NR_ESP;
  1344. r2.enum:=R_INTREGISTER;
  1345. rg.getexplicitregisterint(list,NR_EDI);
  1346. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1347. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1348. if (elesize<>1) then
  1349. begin
  1350. if ispowerof2(elesize, power) then
  1351. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1352. else
  1353. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1354. end;
  1355. {$ifndef __NOWINPECOFF__}
  1356. { windows guards only a few pages for stack growing, }
  1357. { so we have to access every page first }
  1358. if target_info.system=system_i386_win32 then
  1359. begin
  1360. objectlibrary.getlabel(again);
  1361. objectlibrary.getlabel(ok);
  1362. a_label(list,again);
  1363. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,r));
  1364. a_jmp_cond(list,OC_B,ok);
  1365. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1366. r2.number:=NR_EAX;
  1367. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1368. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,r));
  1369. a_jmp_always(list,again);
  1370. a_label(list,ok);
  1371. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1372. rg.ungetregisterint(list,r);
  1373. { now reload EDI }
  1374. rg.getexplicitregisterint(list,NR_EDI);
  1375. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1376. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1377. if (elesize<>1) then
  1378. begin
  1379. if ispowerof2(elesize, power) then
  1380. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1381. else
  1382. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1383. end;
  1384. end
  1385. else
  1386. {$endif __NOWINPECOFF__}
  1387. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1388. { align stack on 4 bytes }
  1389. list.concat(Taicpu.op_const_reg(A_AND,S_L,$fffffff4,rsp));
  1390. { load destination }
  1391. a_load_reg_reg(list,OS_INT,OS_INT,rsp,r);
  1392. { don't destroy the registers! }
  1393. r2.number:=NR_ECX;
  1394. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1395. r2.number:=NR_ESI;
  1396. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1397. { load count }
  1398. r2.number:=NR_ECX;
  1399. a_load_ref_reg(list,OS_INT,lenref,r2);
  1400. { load source }
  1401. r2.number:=NR_ESI;
  1402. a_load_ref_reg(list,OS_INT,ref,r2);
  1403. { scheduled .... }
  1404. r2.number:=NR_ECX;
  1405. list.concat(Taicpu.op_reg(A_INC,S_L,r2));
  1406. { calculate size }
  1407. len:=elesize;
  1408. opsize:=S_B;
  1409. if (len and 3)=0 then
  1410. begin
  1411. opsize:=S_L;
  1412. len:=len shr 2;
  1413. end
  1414. else
  1415. if (len and 1)=0 then
  1416. begin
  1417. opsize:=S_W;
  1418. len:=len shr 1;
  1419. end;
  1420. if ispowerof2(len, power) then
  1421. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r2))
  1422. else
  1423. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,r2));
  1424. list.concat(Taicpu.op_none(A_REP,S_NO));
  1425. case opsize of
  1426. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1427. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1428. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1429. end;
  1430. rg.ungetregisterint(list,r);
  1431. r2.number:=NR_ESI;
  1432. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1433. r2.number:=NR_ECX;
  1434. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1435. { patch the new address }
  1436. a_load_reg_ref(list,OS_INT,rsp,ref);
  1437. end;
  1438. procedure tcgx86.g_interrupt_stackframe_entry(list : taasmoutput);
  1439. var r:Tregister;
  1440. begin
  1441. r.enum:=R_INTREGISTER;
  1442. r.number:=NR_GS;
  1443. { .... also the segment registers }
  1444. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1445. r.number:=NR_FS;
  1446. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1447. r.number:=NR_ES;
  1448. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1449. r.number:=NR_DS;
  1450. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1451. { save the registers of an interrupt procedure }
  1452. r.number:=NR_EDI;
  1453. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1454. r.number:=NR_ESI;
  1455. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1456. r.number:=NR_EDX;
  1457. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1458. r.number:=NR_ECX;
  1459. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1460. r.number:=NR_EBX;
  1461. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1462. r.number:=NR_EAX;
  1463. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1464. end;
  1465. procedure tcgx86.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1466. var r:Tregister;
  1467. begin
  1468. r.enum:=R_INTREGISTER;
  1469. if accused then
  1470. begin
  1471. r.number:=NR_ESP;
  1472. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,r))
  1473. end
  1474. else
  1475. begin
  1476. r.number:=NR_EAX;
  1477. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1478. end;
  1479. r.number:=NR_EBX;
  1480. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1481. r.number:=NR_ECX;
  1482. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1483. if acchiused then
  1484. begin
  1485. r.number:=NR_ESP;
  1486. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,r))
  1487. end
  1488. else
  1489. begin
  1490. r.number:=NR_EDX;
  1491. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1492. end;
  1493. if selfused then
  1494. begin
  1495. r.number:=NR_ESP;
  1496. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,r))
  1497. end
  1498. else
  1499. begin
  1500. r.number:=NR_ESI;
  1501. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1502. end;
  1503. r.number:=NR_EDI;
  1504. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1505. { .... also the segment registers }
  1506. r.number:=NR_DS;
  1507. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1508. r.number:=NR_ES;
  1509. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1510. r.number:=NR_FS;
  1511. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1512. r.number:=NR_GS;
  1513. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1514. { this restores the flags }
  1515. list.concat(Taicpu.Op_none(A_IRET,S_NO));
  1516. end;
  1517. procedure tcgx86.g_profilecode(list : taasmoutput);
  1518. var
  1519. pl : tasmlabel;
  1520. r : Tregister;
  1521. begin
  1522. case target_info.system of
  1523. system_i386_win32,
  1524. system_i386_freebsd,
  1525. system_i386_wdosx,
  1526. system_i386_linux:
  1527. begin
  1528. objectlibrary.getaddrlabel(pl);
  1529. list.concat(Tai_section.Create(sec_data));
  1530. list.concat(Tai_align.Create(4));
  1531. list.concat(Tai_label.Create(pl));
  1532. list.concat(Tai_const.Create_32bit(0));
  1533. list.concat(Tai_section.Create(sec_code));
  1534. r.enum:=R_INTREGISTER;
  1535. r.number:=NR_EDX;
  1536. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,r));
  1537. a_call_name(list,target_info.Cprefix+'mcount');
  1538. include(rg.usedinproc,R_EDX);
  1539. end;
  1540. system_i386_go32v2:
  1541. begin
  1542. a_call_name(list,'MCOUNT');
  1543. end;
  1544. end;
  1545. end;
  1546. procedure tcgx86.g_stackpointer_alloc(list : taasmoutput;localsize : longint);
  1547. var
  1548. href : treference;
  1549. i : integer;
  1550. again : tasmlabel;
  1551. r,rsp : Tregister;
  1552. begin
  1553. r.enum:=R_INTREGISTER;
  1554. rsp.enum:=R_INTREGISTER;
  1555. rsp.number:=NR_ESP;
  1556. if localsize>0 then
  1557. begin
  1558. {$ifndef NOTARGETWIN32}
  1559. { windows guards only a few pages for stack growing, }
  1560. { so we have to access every page first }
  1561. if (target_info.system=system_i386_win32) and
  1562. (localsize>=winstackpagesize) then
  1563. begin
  1564. if localsize div winstackpagesize<=5 then
  1565. begin
  1566. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,rsp));
  1567. for i:=1 to localsize div winstackpagesize do
  1568. begin
  1569. reference_reset_base(href,rsp,localsize-i*winstackpagesize);
  1570. list.concat(Taicpu.op_const_ref(A_MOV,S_L,0,href));
  1571. end;
  1572. r.number:=NR_EAX;
  1573. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1574. end
  1575. else
  1576. begin
  1577. objectlibrary.getlabel(again);
  1578. r.number:=NR_EDI;
  1579. rg.getexplicitregisterint(list,NR_EDI);
  1580. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,r));
  1581. a_label(list,again);
  1582. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1583. r.number:=NR_EAX;
  1584. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1585. r.number:=NR_EDI;
  1586. list.concat(Taicpu.op_reg(A_DEC,S_L,r));
  1587. a_jmp_cond(list,OC_NE,again);
  1588. rg.ungetregisterint(list,r);
  1589. list.concat(Taicpu.op_const_reg(A_SUB,S_L,localsize mod winstackpagesize,rsp));
  1590. end
  1591. end
  1592. else
  1593. {$endif NOTARGETWIN32}
  1594. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize,rsp));
  1595. end;
  1596. end;
  1597. procedure tcgx86.g_stackframe_entry(list : taasmoutput;localsize : longint);
  1598. var r,rsp:Tregister;
  1599. begin
  1600. r.enum:=R_INTREGISTER;
  1601. r.number:=NR_EBP;
  1602. rsp.enum:=R_INTREGISTER;
  1603. rsp.number:=NR_ESP;
  1604. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1605. list.concat(Taicpu.Op_reg_reg(A_MOV,S_L,rsp,r));
  1606. if localsize>0 then
  1607. g_stackpointer_alloc(list,localsize);
  1608. end;
  1609. procedure tcgx86.g_restore_frame_pointer(list : taasmoutput);
  1610. begin
  1611. list.concat(Taicpu.Op_none(A_LEAVE,S_NO));
  1612. end;
  1613. procedure tcgx86.g_return_from_proc(list : taasmoutput;parasize : aword);
  1614. begin
  1615. { Routines with the poclearstack flag set use only a ret }
  1616. { also routines with parasize=0 }
  1617. if (po_clearstack in current_procdef.procoptions) then
  1618. begin
  1619. { complex return values are removed from stack in C code PM }
  1620. if paramanager.ret_in_param(current_procdef.rettype.def,current_procdef.proccalloption) then
  1621. list.concat(Taicpu.Op_const(A_RET,S_NO,4))
  1622. else
  1623. list.concat(Taicpu.Op_none(A_RET,S_NO));
  1624. end
  1625. else if (parasize=0) then
  1626. list.concat(Taicpu.Op_none(A_RET,S_NO))
  1627. else
  1628. begin
  1629. { parameters are limited to 65535 bytes because }
  1630. { ret allows only imm16 }
  1631. if (parasize>65535) then
  1632. CGMessage(cg_e_parasize_too_big);
  1633. list.concat(Taicpu.Op_const(A_RET,S_NO,parasize));
  1634. end;
  1635. end;
  1636. procedure tcgx86.g_save_standard_registers(list:Taasmoutput;usedinproc:Tsupregset);
  1637. var r:Tregister;
  1638. begin
  1639. r.enum:=R_INTREGISTER;
  1640. r.number:=NR_EBX;
  1641. if (RS_EBX in usedinproc) then
  1642. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1643. r.number:=NR_ESI;
  1644. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1645. r.number:=NR_EDI;
  1646. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1647. end;
  1648. procedure tcgx86.g_restore_standard_registers(list:Taasmoutput;usedinproc:Tsupregset);
  1649. var r:Tregister;
  1650. begin
  1651. r.enum:=R_INTREGISTER;
  1652. r.number:=NR_EDI;
  1653. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1654. r.number:=NR_ESI;
  1655. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1656. r.number:=NR_EBX;
  1657. if (RS_EBX in usedinproc) then
  1658. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1659. end;
  1660. procedure tcgx86.g_save_all_registers(list : taasmoutput);
  1661. begin
  1662. list.concat(Taicpu.Op_none(A_PUSHA,S_L));
  1663. end;
  1664. procedure tcgx86.g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);
  1665. var
  1666. href : treference;
  1667. r,rsp: Tregister;
  1668. begin
  1669. rsp.enum:=R_INTREGISTER;
  1670. rsp.number:=NR_ESP;
  1671. r.enum:=R_INTREGISTER;
  1672. if selfused then
  1673. begin
  1674. reference_reset_base(href,rsp,4);
  1675. r.number:=NR_ESI;
  1676. list.concat(Taicpu.Op_reg_ref(A_MOV,S_L,r,href));
  1677. end;
  1678. if acchiused then
  1679. begin
  1680. reference_reset_base(href,rsp,20);
  1681. r.number:=NR_EDX;
  1682. list.concat(Taicpu.Op_reg_ref(A_MOV,S_L,r,href));
  1683. end;
  1684. if accused then
  1685. begin
  1686. reference_reset_base(href,rsp,28);
  1687. r.number:=NR_EAX;
  1688. list.concat(Taicpu.Op_reg_ref(A_MOV,S_L,r,href));
  1689. end;
  1690. list.concat(Taicpu.Op_none(A_POPA,S_L));
  1691. { We add a NOP because of the 386DX CPU bugs with POPAD }
  1692. list.concat(taicpu.op_none(A_NOP,S_L));
  1693. end;
  1694. { produces if necessary overflowcode }
  1695. procedure tcgx86.g_overflowcheck(list: taasmoutput; const p: tnode);
  1696. var
  1697. hl : tasmlabel;
  1698. ai : taicpu;
  1699. cond : TAsmCond;
  1700. begin
  1701. if not(cs_check_overflow in aktlocalswitches) then
  1702. exit;
  1703. objectlibrary.getlabel(hl);
  1704. if not ((p.resulttype.def.deftype=pointerdef) or
  1705. ((p.resulttype.def.deftype=orddef) and
  1706. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1707. bool8bit,bool16bit,bool32bit]))) then
  1708. cond:=C_NO
  1709. else
  1710. cond:=C_NB;
  1711. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  1712. ai.SetCondition(cond);
  1713. ai.is_jmp:=true;
  1714. list.concat(ai);
  1715. a_call_name(list,'FPC_OVERFLOW');
  1716. a_label(list,hl);
  1717. end;
  1718. end.
  1719. {
  1720. $Log$
  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. }