cgx86.pas 72 KB

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