cgx86.pas 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  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_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); override;
  42. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference); override;
  43. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  44. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  45. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  46. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  47. size: tcgsize; a: aword; src, dst: tregister); override;
  48. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  49. size: tcgsize; src1, src2, dst: tregister); override;
  50. { move instructions }
  51. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  52. procedure a_load_const_ref(list : taasmoutput; size: tcgsize; a : aword;const ref : treference);override;
  53. procedure a_load_reg_ref(list : taasmoutput; size: tcgsize; reg : tregister;const ref : treference);override;
  54. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref : treference;reg : tregister);override;
  55. procedure a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);override;
  56. procedure a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: 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_new_exception(list : taasmoutput;var jmpbuf,envbuf,href : treference;
  80. a : aword; exceptlabel : tasmlabel);override;
  81. procedure g_free_exception(list : taasmoutput;var jmpbuf, envbuf, href : treference;
  82. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);override;
  83. procedure g_exception_reason_save(list : taasmoutput; const href : treference);override;
  84. procedure g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);override;
  85. procedure g_exception_reason_load(list : taasmoutput; const href : treference);override;
  86. class function reg_cgsize(const reg: tregister): tcgsize; override;
  87. { entry/exit code helpers }
  88. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);override;
  89. procedure g_interrupt_stackframe_entry(list : taasmoutput);override;
  90. procedure g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);override;
  91. procedure g_profilecode(list : taasmoutput);override;
  92. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  93. procedure g_restore_frame_pointer(list : taasmoutput);override;
  94. procedure g_return_from_proc(list : taasmoutput;parasize : aword);override;
  95. {$ifndef TEST_GENERIC}
  96. procedure g_call_constructor_helper(list : taasmoutput);override;
  97. procedure g_call_destructor_helper(list : taasmoutput);override;
  98. procedure g_call_fail_helper(list : taasmoutput);override;
  99. {$endif}
  100. procedure g_save_standard_registers(list : taasmoutput);override;
  101. procedure g_restore_standard_registers(list : taasmoutput);override;
  102. procedure g_save_all_registers(list : taasmoutput);override;
  103. procedure g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);override;
  104. procedure g_overflowcheck(list: taasmoutput; const p: tnode);override;
  105. private
  106. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  107. procedure sizes2load(s1 : tcgsize;s2 : topsize; var op: tasmop; var s3: topsize);
  108. procedure floatload(list: taasmoutput; t : tcgsize;const ref : treference);
  109. procedure floatstore(list: taasmoutput; t : tcgsize;const ref : treference);
  110. procedure floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  111. procedure floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  112. end;
  113. implementation
  114. uses
  115. globtype,globals,verbose,systems,cutils,
  116. symdef,symsym,defbase,paramgr,
  117. rgobj,tgobj,rgcpu;
  118. {$ifndef NOTARGETWIN32}
  119. const
  120. winstackpagesize = 4096;
  121. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_ADD,A_AND,A_DIV,
  122. A_IDIV,A_MUL, A_IMUL, A_NEG,A_NOT,A_OR,
  123. A_SAR,A_SHL,A_SHR,A_SUB,A_XOR);
  124. TOpCmp2AsmCond: Array[topcmp] of TAsmCond = (C_NONE,
  125. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  126. TCGSize2OpSize: Array[tcgsize] of topsize =
  127. (S_NO,S_B,S_W,S_L,S_L,S_B,S_W,S_L,S_L,
  128. S_FS,S_FL,S_FX,S_IQ,
  129. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO);
  130. {$endif NOTARGETWIN32}
  131. {****************************************************************************
  132. This is private property, keep out! :)
  133. ****************************************************************************}
  134. procedure tcgx86.sizes2load(s1 : tcgsize;s2: topsize; var op: tasmop; var s3: topsize);
  135. begin
  136. case s2 of
  137. S_B:
  138. if S1 in [OS_8,OS_S8] then
  139. s3 := S_B
  140. else internalerror(200109221);
  141. S_W:
  142. case s1 of
  143. OS_8,OS_S8:
  144. s3 := S_BW;
  145. OS_16,OS_S16:
  146. s3 := S_W;
  147. else internalerror(200109222);
  148. end;
  149. S_L:
  150. case s1 of
  151. OS_8,OS_S8:
  152. s3 := S_BL;
  153. OS_16,OS_S16:
  154. s3 := S_WL;
  155. OS_32,OS_S32:
  156. s3 := S_L;
  157. else internalerror(200109223);
  158. end;
  159. else internalerror(200109227);
  160. end;
  161. if s3 in [S_B,S_W,S_L] then
  162. op := A_MOV
  163. else if s1 in [OS_8,OS_16,OS_32] then
  164. op := A_MOVZX
  165. else
  166. op := A_MOVSX;
  167. end;
  168. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  169. begin
  170. case t of
  171. OS_F32 :
  172. begin
  173. op:=A_FLD;
  174. s:=S_FS;
  175. end;
  176. OS_F64 :
  177. begin
  178. op:=A_FLD;
  179. { ???? }
  180. s:=S_FL;
  181. end;
  182. OS_F80 :
  183. begin
  184. op:=A_FLD;
  185. s:=S_FX;
  186. end;
  187. OS_C64 :
  188. begin
  189. op:=A_FILD;
  190. s:=S_IQ;
  191. end;
  192. else
  193. internalerror(200204041);
  194. end;
  195. end;
  196. procedure tcgx86.floatload(list: taasmoutput; t : tcgsize;const ref : treference);
  197. var
  198. op : tasmop;
  199. s : topsize;
  200. begin
  201. floatloadops(t,op,s);
  202. list.concat(Taicpu.Op_ref(op,s,ref));
  203. inc(trgcpu(rg).fpuvaroffset);
  204. end;
  205. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  206. begin
  207. case t of
  208. OS_F32 :
  209. begin
  210. op:=A_FSTP;
  211. s:=S_FS;
  212. end;
  213. OS_F64 :
  214. begin
  215. op:=A_FSTP;
  216. s:=S_FL;
  217. end;
  218. OS_F80 :
  219. begin
  220. op:=A_FSTP;
  221. s:=S_FX;
  222. end;
  223. OS_C64 :
  224. begin
  225. op:=A_FISTP;
  226. s:=S_IQ;
  227. end;
  228. else
  229. internalerror(200204042);
  230. end;
  231. end;
  232. procedure tcgx86.floatstore(list: taasmoutput; t : tcgsize;const ref : treference);
  233. var
  234. op : tasmop;
  235. s : topsize;
  236. begin
  237. floatstoreops(t,op,s);
  238. list.concat(Taicpu.Op_ref(op,s,ref));
  239. dec(trgcpu(rg).fpuvaroffset);
  240. end;
  241. {****************************************************************************
  242. Assembler code
  243. ****************************************************************************}
  244. function tcgx86.reg_cgsize(const reg: tregister): tcgsize;
  245. const
  246. regsize_2_cgsize: array[S_B..S_L] of tcgsize = (OS_8,OS_16,OS_32);
  247. begin
  248. result := regsize_2_cgsize[reg2opsize[reg]];
  249. end;
  250. { currently does nothing }
  251. procedure tcgx86.a_jmp_always(list : taasmoutput;l: tasmlabel);
  252. begin
  253. a_jmp_cond(list, OC_NONE, l);
  254. end;
  255. { we implement the following routines because otherwise we can't }
  256. { instantiate the class since it's abstract }
  257. procedure tcgx86.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);
  258. begin
  259. case size of
  260. OS_8,OS_S8,
  261. OS_16,OS_S16:
  262. begin
  263. if target_info.alignment.paraalign = 2 then
  264. list.concat(taicpu.op_reg(A_PUSH,S_W,rg.makeregsize(r,OS_16)))
  265. else
  266. list.concat(taicpu.op_reg(A_PUSH,S_L,rg.makeregsize(r,OS_32)));
  267. end;
  268. OS_32,OS_S32:
  269. list.concat(taicpu.op_reg(A_PUSH,S_L,r));
  270. else
  271. internalerror(2002032212);
  272. end;
  273. end;
  274. procedure tcgx86.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  275. begin
  276. case size of
  277. OS_8,OS_S8,OS_16,OS_S16:
  278. begin
  279. if target_info.alignment.paraalign = 2 then
  280. list.concat(taicpu.op_const(A_PUSH,S_W,a))
  281. else
  282. list.concat(taicpu.op_const(A_PUSH,S_L,a));
  283. end;
  284. OS_32,OS_S32:
  285. list.concat(taicpu.op_const(A_PUSH,S_L,a));
  286. else
  287. internalerror(2002032213);
  288. end;
  289. end;
  290. procedure tcgx86.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  291. var
  292. tmpreg: tregister;
  293. begin
  294. case size of
  295. OS_8,OS_S8,
  296. OS_16,OS_S16:
  297. begin
  298. tmpreg := get_scratch_reg_address(list);
  299. a_load_ref_reg(list,size,r,tmpreg);
  300. if target_info.alignment.paraalign = 2 then
  301. list.concat(taicpu.op_reg(A_PUSH,S_W,rg.makeregsize(tmpreg,OS_16)))
  302. else
  303. list.concat(taicpu.op_reg(A_PUSH,S_L,tmpreg));
  304. free_scratch_reg(list,tmpreg);
  305. end;
  306. OS_32,OS_S32:
  307. list.concat(taicpu.op_ref(A_PUSH,S_L,r));
  308. else
  309. internalerror(2002032214);
  310. end;
  311. end;
  312. procedure tcgx86.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  313. var
  314. tmpreg: tregister;
  315. begin
  316. if r.segment<>R_NO then
  317. CGMessage(cg_e_cant_use_far_pointer_there);
  318. if (r.base=R_NO) and (r.index=R_NO) then
  319. list.concat(Taicpu.Op_sym_ofs(A_PUSH,S_L,r.symbol,r.offset))
  320. else if (r.base=R_NO) and (r.index<>R_NO) and
  321. (r.offset=0) and (r.scalefactor=0) and (r.symbol=nil) then
  322. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r.index))
  323. else if (r.base<>R_NO) and (r.index=R_NO) and
  324. (r.offset=0) and (r.symbol=nil) then
  325. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r.base))
  326. else
  327. begin
  328. tmpreg := get_scratch_reg_address(list);
  329. a_loadaddr_ref_reg(list,r,tmpreg);
  330. list.concat(taicpu.op_reg(A_PUSH,S_L,tmpreg));
  331. free_scratch_reg(list,tmpreg);
  332. end;
  333. end;
  334. procedure tcgx86.a_call_name(list : taasmoutput;const s : string);
  335. begin
  336. list.concat(taicpu.op_sym(A_CALL,S_NO,newasmsymbol(s)));
  337. end;
  338. procedure tcgx86.a_call_ref(list : taasmoutput;const ref : treference);
  339. begin
  340. list.concat(taicpu.op_ref(A_CALL,S_NO,ref));
  341. end;
  342. {********************** load instructions ********************}
  343. procedure tcgx86.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  344. begin
  345. { the optimizer will change it to "xor reg,reg" when loading zero, }
  346. { no need to do it here too (JM) }
  347. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[size],a,reg))
  348. end;
  349. procedure tcgx86.a_load_const_ref(list : taasmoutput; size: tcgsize; a : aword;const ref : treference);
  350. begin
  351. {$ifdef OPTLOAD0}
  352. { zero is often used several times in succession -> load it in a }
  353. { register and then store it to memory, so the optimizer can then }
  354. { remove the unnecessary loads of registers and you get smaller }
  355. { (and faster) code }
  356. if (a = 0) and
  357. (size in [OS_32,OS_S32]) then
  358. inherited a_load_const_ref(list,size,a,ref)
  359. else
  360. {$endif OPTLOAD0}
  361. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[size],a,ref));
  362. end;
  363. procedure tcgx86.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref : treference);
  364. begin
  365. list.concat(taicpu.op_reg_ref(A_MOV,TCGSize2OpSize[size],reg,
  366. ref));
  367. End;
  368. procedure tcgx86.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref: treference;reg : tregister);
  369. var
  370. op: tasmop;
  371. s: topsize;
  372. begin
  373. sizes2load(size,reg2opsize[reg],op,s);
  374. list.concat(taicpu.op_ref_reg(op,s,ref,reg));
  375. end;
  376. procedure tcgx86.a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);
  377. var
  378. op: tasmop;
  379. s: topsize;
  380. begin
  381. sizes2load(size,reg2opsize[reg2],op,s);
  382. if (rg.makeregsize(reg1,OS_INT) = rg.makeregsize(reg2,OS_INT)) then
  383. begin
  384. { "mov reg1, reg1" doesn't make sense }
  385. if op = A_MOV then
  386. exit;
  387. { optimize movzx with "and ffff,<reg>" operation }
  388. if (op = A_MOVZX) then
  389. begin
  390. case size of
  391. OS_8:
  392. begin
  393. list.concat(taicpu.op_const_reg(A_AND,reg2opsize[reg2],255,reg2));
  394. exit;
  395. end;
  396. OS_16:
  397. begin
  398. list.concat(taicpu.op_const_reg(A_AND,reg2opsize[reg2],65535,reg2));
  399. exit;
  400. end;
  401. end;
  402. end;
  403. end;
  404. list.concat(taicpu.op_reg_reg(op,s,reg1,reg2));
  405. end;
  406. procedure tcgx86.a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister);
  407. begin
  408. list.concat(taicpu.op_sym_ofs_reg(A_MOV,S_L,sym,ofs,reg));
  409. end;
  410. procedure tcgx86.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  411. begin
  412. if assigned(ref.symbol) and
  413. (ref.base=R_NO) and
  414. (ref.index=R_NO) then
  415. list.concat(taicpu.op_sym_ofs_reg(A_MOV,S_L,ref.symbol,ref.offset,r))
  416. else
  417. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,r));
  418. end;
  419. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  420. { R_ST means "the current value at the top of the fpu stack" (JM) }
  421. procedure tcgx86.a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  422. begin
  423. if (reg1 <> R_ST) then
  424. begin
  425. list.concat(taicpu.op_reg(A_FLD,S_NO,
  426. trgcpu(rg).correct_fpuregister(reg1,trgcpu(rg).fpuvaroffset)));
  427. inc(trgcpu(rg).fpuvaroffset);
  428. end;
  429. if (reg2 <> R_ST) then
  430. begin
  431. list.concat(taicpu.op_reg(A_FSTP,S_NO,
  432. trgcpu(rg).correct_fpuregister(reg2,trgcpu(rg).fpuvaroffset)));
  433. dec(trgcpu(rg).fpuvaroffset);
  434. end;
  435. end;
  436. procedure tcgx86.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  437. begin
  438. floatload(list,size,ref);
  439. if (reg <> R_ST) then
  440. a_loadfpu_reg_reg(list,R_ST,reg);
  441. end;
  442. procedure tcgx86.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  443. begin
  444. if reg <> R_ST then
  445. a_loadfpu_reg_reg(list,reg,R_ST);
  446. floatstore(list,size,ref);
  447. end;
  448. procedure tcgx86.a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  449. begin
  450. list.concat(taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2));
  451. end;
  452. procedure tcgx86.a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister);
  453. begin
  454. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,ref,reg));
  455. end;
  456. procedure tcgx86.a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference);
  457. begin
  458. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,ref));
  459. end;
  460. procedure tcgx86.a_parammm_reg(list: taasmoutput; reg: tregister);
  461. var
  462. href : treference;
  463. begin
  464. list.concat(taicpu.op_const_reg(A_SUB,S_L,8,R_ESP));
  465. reference_reset_base(href,R_ESP,0);
  466. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,href));
  467. end;
  468. procedure tcgx86.a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister);
  469. var
  470. opcode: tasmop;
  471. power: longint;
  472. begin
  473. Case Op of
  474. OP_DIV, OP_IDIV:
  475. Begin
  476. if ispowerof2(a,power) then
  477. begin
  478. case op of
  479. OP_DIV:
  480. opcode := A_SHR;
  481. OP_IDIV:
  482. opcode := A_SAR;
  483. end;
  484. list.concat(taicpu.op_const_reg(opcode,reg2opsize[reg],power,
  485. reg));
  486. exit;
  487. end;
  488. { the rest should be handled specifically in the code }
  489. { generator because of the silly register usage restraints }
  490. internalerror(200109224);
  491. End;
  492. OP_MUL,OP_IMUL:
  493. begin
  494. if not(cs_check_overflow in aktlocalswitches) and
  495. ispowerof2(a,power) then
  496. begin
  497. list.concat(taicpu.op_const_reg(A_SHL,reg2opsize[reg],power,
  498. reg));
  499. exit;
  500. end;
  501. if op = OP_IMUL then
  502. list.concat(taicpu.op_const_reg(A_IMUL,reg2opsize[reg],
  503. a,reg))
  504. else
  505. { OP_MUL should be handled specifically in the code }
  506. { generator because of the silly register usage restraints }
  507. internalerror(200109225);
  508. end;
  509. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  510. if not(cs_check_overflow in aktlocalswitches) and
  511. (a = 1) and
  512. (op in [OP_ADD,OP_SUB]) then
  513. if op = OP_ADD then
  514. list.concat(taicpu.op_reg(A_INC,reg2opsize[reg],reg))
  515. else
  516. list.concat(taicpu.op_reg(A_DEC,reg2opsize[reg],reg))
  517. else if (a = 0) then
  518. if (op <> OP_AND) then
  519. exit
  520. else
  521. list.concat(taicpu.op_const_reg(A_MOV,reg2opsize[reg],0,reg))
  522. else if (a = high(aword)) and
  523. (op in [OP_AND,OP_OR,OP_XOR]) then
  524. begin
  525. case op of
  526. OP_AND:
  527. exit;
  528. OP_OR:
  529. list.concat(taicpu.op_const_reg(A_MOV,reg2opsize[reg],high(aword),reg));
  530. OP_XOR:
  531. list.concat(taicpu.op_reg(A_NOT,reg2opsize[reg],reg));
  532. end
  533. end
  534. else
  535. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],reg2opsize[reg],
  536. a,reg));
  537. OP_SHL,OP_SHR,OP_SAR:
  538. begin
  539. if (a and 31) <> 0 Then
  540. list.concat(taicpu.op_const_reg(
  541. TOpCG2AsmOp[op],reg2opsize[reg],a and 31,reg));
  542. if (a shr 5) <> 0 Then
  543. internalerror(68991);
  544. end
  545. else internalerror(68992);
  546. end;
  547. end;
  548. procedure tcgx86.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  549. var
  550. opcode: tasmop;
  551. power: longint;
  552. begin
  553. Case Op of
  554. OP_DIV, OP_IDIV:
  555. Begin
  556. if ispowerof2(a,power) then
  557. begin
  558. case op of
  559. OP_DIV:
  560. opcode := A_SHR;
  561. OP_IDIV:
  562. opcode := A_SAR;
  563. end;
  564. list.concat(taicpu.op_const_ref(opcode,
  565. TCgSize2OpSize[size],power,ref));
  566. exit;
  567. end;
  568. { the rest should be handled specifically in the code }
  569. { generator because of the silly register usage restraints }
  570. internalerror(200109231);
  571. End;
  572. OP_MUL,OP_IMUL:
  573. begin
  574. if not(cs_check_overflow in aktlocalswitches) and
  575. ispowerof2(a,power) then
  576. begin
  577. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  578. power,ref));
  579. exit;
  580. end;
  581. { can't multiply a memory location directly with a constant }
  582. if op = OP_IMUL then
  583. inherited a_op_const_ref(list,op,size,a,ref)
  584. else
  585. { OP_MUL should be handled specifically in the code }
  586. { generator because of the silly register usage restraints }
  587. internalerror(200109232);
  588. end;
  589. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  590. if not(cs_check_overflow in aktlocalswitches) and
  591. (a = 1) and
  592. (op in [OP_ADD,OP_SUB]) then
  593. if op = OP_ADD then
  594. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],ref))
  595. else
  596. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],ref))
  597. else if (a = 0) then
  598. if (op <> OP_AND) then
  599. exit
  600. else
  601. a_load_const_ref(list,size,0,ref)
  602. else if (a = high(aword)) and
  603. (op in [OP_AND,OP_OR,OP_XOR]) then
  604. begin
  605. case op of
  606. OP_AND:
  607. exit;
  608. OP_OR:
  609. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],high(aword),ref));
  610. OP_XOR:
  611. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],ref));
  612. end
  613. end
  614. else
  615. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  616. TCgSize2OpSize[size],a,ref));
  617. OP_SHL,OP_SHR,OP_SAR:
  618. begin
  619. if (a and 31) <> 0 Then
  620. list.concat(taicpu.op_const_ref(
  621. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,ref));
  622. if (a shr 5) <> 0 Then
  623. internalerror(68991);
  624. end
  625. else internalerror(68992);
  626. end;
  627. end;
  628. procedure tcgx86.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  629. var
  630. regloadsize: tcgsize;
  631. dstsize: topsize;
  632. tmpreg : tregister;
  633. popecx : boolean;
  634. begin
  635. dstsize := tcgsize2opsize[size];
  636. dst := rg.makeregsize(dst,size);
  637. case op of
  638. OP_NEG,OP_NOT:
  639. begin
  640. if src <> R_NO then
  641. internalerror(200112291);
  642. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  643. end;
  644. OP_MUL,OP_DIV,OP_IDIV:
  645. { special stuff, needs separate handling inside code }
  646. { generator }
  647. internalerror(200109233);
  648. OP_SHR,OP_SHL,OP_SAR:
  649. begin
  650. tmpreg := R_NO;
  651. popecx := false;
  652. { we need cl to hold the shift count, so if the destination }
  653. { is ecx, save it to a temp for now }
  654. if dst in [R_ECX,R_CX,R_CL] then
  655. begin
  656. case reg2opsize[dst] of
  657. S_B: regloadsize := OS_8;
  658. S_W: regloadsize := OS_16;
  659. else regloadsize := OS_32;
  660. end;
  661. tmpreg := get_scratch_reg_int(list);
  662. a_load_reg_reg(list,regloadsize,src,tmpreg);
  663. end;
  664. if not(src in [R_ECX,R_CX,R_CL]) then
  665. begin
  666. { is ecx still free (it's also free if it was allocated }
  667. { to dst, since we've moved dst somewhere else already) }
  668. if not((dst = R_ECX) or
  669. ((R_ECX in rg.unusedregsint) and
  670. { this will always be true, it's just here to }
  671. { allocate ecx }
  672. (rg.getexplicitregisterint(list,R_ECX) = R_ECX))) then
  673. begin
  674. list.concat(taicpu.op_reg(A_PUSH,S_L,R_ECX));
  675. popecx := true;
  676. end;
  677. a_load_reg_reg(list,OS_32,rg.makeregsize(src,OS_32),R_ECX);
  678. end
  679. else
  680. src := R_CL;
  681. { do the shift }
  682. if tmpreg = R_NO then
  683. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,
  684. R_CL,dst))
  685. else
  686. begin
  687. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],S_L,
  688. R_CL,tmpreg));
  689. { move result back to the destination }
  690. a_load_reg_reg(list,OS_32,tmpreg,R_ECX);
  691. free_scratch_reg(list,tmpreg);
  692. end;
  693. if popecx then
  694. list.concat(taicpu.op_reg(A_POP,S_L,R_ECX))
  695. else if not (dst in [R_ECX,R_CX,R_CL]) then
  696. rg.ungetregisterint(list,R_ECX);
  697. end;
  698. else
  699. begin
  700. if reg2opsize[src] <> dstsize then
  701. internalerror(200109226);
  702. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,
  703. src,dst));
  704. end;
  705. end;
  706. end;
  707. procedure tcgx86.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  708. begin
  709. case op of
  710. OP_NEG,OP_NOT,OP_IMUL:
  711. begin
  712. inherited a_op_ref_reg(list,op,size,ref,reg);
  713. end;
  714. OP_MUL,OP_DIV,OP_IDIV:
  715. { special stuff, needs separate handling inside code }
  716. { generator }
  717. internalerror(200109239);
  718. else
  719. begin
  720. reg := rg.makeregsize(reg,size);
  721. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],ref,reg));
  722. end;
  723. end;
  724. end;
  725. procedure tcgx86.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  726. var
  727. opsize: topsize;
  728. begin
  729. case op of
  730. OP_NEG,OP_NOT:
  731. begin
  732. if reg <> R_NO then
  733. internalerror(200109237);
  734. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],ref));
  735. end;
  736. OP_IMUL:
  737. begin
  738. { this one needs a load/imul/store, which is the default }
  739. inherited a_op_ref_reg(list,op,size,ref,reg);
  740. end;
  741. OP_MUL,OP_DIV,OP_IDIV:
  742. { special stuff, needs separate handling inside code }
  743. { generator }
  744. internalerror(200109238);
  745. else
  746. begin
  747. opsize := tcgsize2opsize[size];
  748. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],opsize,reg,ref));
  749. end;
  750. end;
  751. end;
  752. procedure tcgx86.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  753. size: tcgsize; a: aword; src, dst: tregister);
  754. var
  755. tmpref: treference;
  756. power: longint;
  757. opsize: topsize;
  758. begin
  759. opsize := reg2opsize[src];
  760. if (opsize <> S_L) or
  761. not (size in [OS_32,OS_S32]) then
  762. begin
  763. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  764. exit;
  765. end;
  766. { if we get here, we have to do a 32 bit calculation, guaranteed }
  767. Case Op of
  768. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  769. OP_SAR:
  770. { can't do anything special for these }
  771. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  772. OP_IMUL:
  773. begin
  774. if not(cs_check_overflow in aktlocalswitches) and
  775. ispowerof2(a,power) then
  776. { can be done with a shift }
  777. begin
  778. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  779. exit;
  780. end;
  781. list.concat(taicpu.op_const_reg_reg(A_IMUL,S_L,a,src,dst));
  782. end;
  783. OP_ADD, OP_SUB:
  784. if (a = 0) then
  785. a_load_reg_reg(list,size,src,dst)
  786. else
  787. begin
  788. reference_reset(tmpref);
  789. tmpref.base := src;
  790. tmpref.offset := longint(a);
  791. if op = OP_SUB then
  792. tmpref.offset := -tmpref.offset;
  793. list.concat(taicpu.op_ref_reg(A_LEA,S_L,tmpref,dst));
  794. end
  795. else internalerror(200112302);
  796. end;
  797. end;
  798. procedure tcgx86.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  799. size: tcgsize; src1, src2, dst: tregister);
  800. var
  801. tmpref: treference;
  802. opsize: topsize;
  803. begin
  804. opsize := reg2opsize[src1];
  805. if (opsize <> S_L) or
  806. (reg2opsize[src2] <> S_L) or
  807. not (size in [OS_32,OS_S32]) then
  808. begin
  809. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  810. exit;
  811. end;
  812. { if we get here, we have to do a 32 bit calculation, guaranteed }
  813. Case Op of
  814. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  815. OP_SAR,OP_SUB,OP_NOT,OP_NEG:
  816. { can't do anything special for these }
  817. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  818. OP_IMUL:
  819. list.concat(taicpu.op_reg_reg_reg(A_IMUL,S_L,src1,src2,dst));
  820. OP_ADD:
  821. begin
  822. reference_reset(tmpref);
  823. tmpref.base := src1;
  824. tmpref.index := src2;
  825. tmpref.scalefactor := 1;
  826. list.concat(taicpu.op_ref_reg(A_LEA,S_L,tmpref,dst));
  827. end
  828. else internalerror(200112303);
  829. end;
  830. end;
  831. {*************** compare instructructions ****************}
  832. procedure tcgx86.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  833. l : tasmlabel);
  834. begin
  835. if (a = 0) then
  836. list.concat(taicpu.op_reg_reg(A_TEST,reg2opsize[reg],reg,reg))
  837. else
  838. list.concat(taicpu.op_const_reg(A_CMP,reg2opsize[reg],a,reg));
  839. a_jmp_cond(list,cmp_op,l);
  840. end;
  841. procedure tcgx86.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  842. l : tasmlabel);
  843. begin
  844. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,ref));
  845. a_jmp_cond(list,cmp_op,l);
  846. end;
  847. procedure tcgx86.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  848. reg1,reg2 : tregister;l : tasmlabel);
  849. begin
  850. if reg2opsize[reg1] <> reg2opsize[reg2] then
  851. internalerror(200109226);
  852. list.concat(taicpu.op_reg_reg(A_CMP,reg2opsize[reg1],reg1,reg2));
  853. a_jmp_cond(list,cmp_op,l);
  854. end;
  855. procedure tcgx86.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  856. begin
  857. reg := rg.makeregsize(reg,size);
  858. list.concat(taicpu.op_ref_reg(A_CMP,tcgsize2opsize[size],ref,reg));
  859. a_jmp_cond(list,cmp_op,l);
  860. end;
  861. procedure tcgx86.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  862. var
  863. ai : taicpu;
  864. begin
  865. if cond=OC_None then
  866. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  867. else
  868. begin
  869. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  870. ai.SetCondition(TOpCmp2AsmCond[cond]);
  871. end;
  872. ai.is_jmp:=true;
  873. list.concat(ai);
  874. end;
  875. procedure tcgx86.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  876. var
  877. ai : taicpu;
  878. begin
  879. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  880. ai.SetCondition(flags_to_cond(f));
  881. ai.is_jmp := true;
  882. list.concat(ai);
  883. end;
  884. procedure tcgx86.g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister);
  885. var
  886. ai : taicpu;
  887. hreg : tregister;
  888. begin
  889. if not(size in [OS_8,OS_S8]) then
  890. a_load_const_reg(list,size,0,reg);
  891. hreg := rg.makeregsize(reg,OS_8);
  892. ai:=Taicpu.Op_reg(A_Setcc,S_B,hreg);
  893. ai.SetCondition(flags_to_cond(f));
  894. list.concat(ai);
  895. end;
  896. procedure tcgx86.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref: TReference);
  897. var
  898. ai : taicpu;
  899. begin
  900. if not(size in [OS_8,OS_S8]) then
  901. a_load_const_ref(list,size,0,ref);
  902. ai:=Taicpu.Op_ref(A_Setcc,S_B,ref);
  903. ai.SetCondition(flags_to_cond(f));
  904. list.concat(ai);
  905. end;
  906. { ************* concatcopy ************ }
  907. procedure tcgx86.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  908. var
  909. ecxpushed : boolean;
  910. helpsize : longint;
  911. i : byte;
  912. reg8,reg32 : tregister;
  913. srcref,dstref : treference;
  914. swap : boolean;
  915. procedure maybepushecx;
  916. begin
  917. if not(R_ECX in rg.unusedregsint) then
  918. begin
  919. list.concat(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  920. ecxpushed:=true;
  921. end
  922. else rg.getexplicitregisterint(list,R_ECX);
  923. end;
  924. begin
  925. if (not loadref) and
  926. ((len<=8) or
  927. (not(cs_littlesize in aktglobalswitches ) and (len<=12))) then
  928. begin
  929. helpsize:=len shr 2;
  930. rg.getexplicitregisterint(list,R_EDI);
  931. dstref:=dest;
  932. srcref:=source;
  933. for i:=1 to helpsize do
  934. begin
  935. a_load_ref_reg(list,OS_32,srcref,R_EDI);
  936. If (len = 4) and delsource then
  937. reference_release(list,source);
  938. a_load_reg_ref(list,OS_32,R_EDI,dstref);
  939. inc(srcref.offset,4);
  940. inc(dstref.offset,4);
  941. dec(len,4);
  942. end;
  943. if len>1 then
  944. begin
  945. a_load_ref_reg(list,OS_16,srcref,R_DI);
  946. If (len = 2) and delsource then
  947. reference_release(list,source);
  948. a_load_reg_ref(list,OS_16,R_DI,dstref);
  949. inc(srcref.offset,2);
  950. inc(dstref.offset,2);
  951. dec(len,2);
  952. end;
  953. rg.ungetregisterint(list,R_EDI);
  954. if len>0 then
  955. begin
  956. { and now look for an 8 bit register }
  957. swap:=false;
  958. if R_EAX in rg.unusedregsint then reg8:=rg.makeregsize(rg.getexplicitregisterint(list,R_EAX),OS_8)
  959. else if R_EDX in rg.unusedregsint then reg8:=rg.makeregsize(rg.getexplicitregisterint(list,R_EDX),OS_8)
  960. else if R_EBX in rg.unusedregsint then reg8:=rg.makeregsize(rg.getexplicitregisterint(list,R_EBX),OS_8)
  961. else if R_ECX in rg.unusedregsint then reg8:=rg.makeregsize(rg.getexplicitregisterint(list,R_ECX),OS_8)
  962. else
  963. begin
  964. swap:=true;
  965. { we need only to check 3 registers, because }
  966. { one is always not index or base }
  967. if (dest.base<>R_EAX) and (dest.index<>R_EAX) then
  968. begin
  969. reg8:=R_AL;
  970. reg32:=R_EAX;
  971. end
  972. else if (dest.base<>R_EBX) and (dest.index<>R_EBX) then
  973. begin
  974. reg8:=R_BL;
  975. reg32:=R_EBX;
  976. end
  977. else if (dest.base<>R_ECX) and (dest.index<>R_ECX) then
  978. begin
  979. reg8:=R_CL;
  980. reg32:=R_ECX;
  981. end;
  982. end;
  983. if swap then
  984. { was earlier XCHG, of course nonsense }
  985. begin
  986. rg.getexplicitregisterint(list,R_EDI);
  987. a_load_reg_reg(list,OS_32,reg32,R_EDI);
  988. end;
  989. a_load_ref_reg(list,OS_8,srcref,reg8);
  990. If delsource and (len=1) then
  991. reference_release(list,source);
  992. a_load_reg_ref(list,OS_8,reg8,dstref);
  993. if swap then
  994. begin
  995. a_load_reg_reg(list,OS_32,R_EDI,reg32);
  996. rg.ungetregisterint(list,R_EDI);
  997. end
  998. else
  999. rg.ungetregister(list,reg8);
  1000. end;
  1001. end
  1002. else
  1003. begin
  1004. rg.getexplicitregisterint(list,R_EDI);
  1005. a_loadaddr_ref_reg(list,dest,R_EDI);
  1006. list.concat(tai_regalloc.Alloc(R_ESI));
  1007. if loadref then
  1008. a_load_ref_reg(list,OS_ADDR,source,R_ESI)
  1009. else
  1010. begin
  1011. a_loadaddr_ref_reg(list,source,R_ESI);
  1012. if delsource then
  1013. reference_release(list,source);
  1014. end;
  1015. list.concat(Taicpu.Op_none(A_CLD,S_NO));
  1016. ecxpushed:=false;
  1017. if cs_littlesize in aktglobalswitches then
  1018. begin
  1019. maybepushecx;
  1020. a_load_const_reg(list,OS_INT,len,R_ECX);
  1021. list.concat(Taicpu.Op_none(A_REP,S_NO));
  1022. list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1023. end
  1024. else
  1025. begin
  1026. helpsize:=len shr 2;
  1027. len:=len and 3;
  1028. if helpsize>1 then
  1029. begin
  1030. maybepushecx;
  1031. a_load_const_reg(list,OS_INT,helpsize,R_ECX);
  1032. list.concat(Taicpu.Op_none(A_REP,S_NO));
  1033. end;
  1034. if helpsize>0 then
  1035. list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1036. if len>1 then
  1037. begin
  1038. dec(len,2);
  1039. list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1040. end;
  1041. if len=1 then
  1042. list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1043. end;
  1044. rg.ungetregisterint(list,R_EDI);
  1045. list.concat(tai_regalloc.DeAlloc(R_ESI));
  1046. if ecxpushed then
  1047. list.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX))
  1048. else
  1049. rg.ungetregisterint(list,R_ECX);
  1050. { loading SELF-reference again }
  1051. g_maybe_loadself(list);
  1052. end;
  1053. if delsource then
  1054. tg.ungetiftemp(list,source);
  1055. end;
  1056. procedure tcgx86.g_new_exception(list : taasmoutput;var jmpbuf,envbuf,href : treference;
  1057. a : aword; exceptlabel : tasmlabel);
  1058. var
  1059. tmpreg : tregister;
  1060. tempbuf : treference;
  1061. begin
  1062. { allocate exception frame buffer }
  1063. cg.a_op_const_reg(list,OP_SUB,36,STACK_POINTER_REG);
  1064. tmpreg:=rg.getaddressregister(list);
  1065. cg.a_load_reg_reg(list,OS_ADDR,STACK_POINTER_REG,tmpreg);
  1066. reference_reset_base(tempbuf,tmpreg,0);
  1067. jmpbuf:=tempbuf;
  1068. envbuf:=tempbuf;
  1069. inc(envbuf.offset,12);
  1070. a_paramaddr_ref(list,jmpbuf,paramanager.getintparaloc(3));
  1071. a_paramaddr_ref(list,envbuf,paramanager.getintparaloc(2));
  1072. a_param_const(list,OS_S32,a,paramanager.getintparaloc(1));
  1073. a_call_name(list,'FPC_PUSHEXCEPTADDR');
  1074. a_reg_alloc(list,accumulator);
  1075. a_param_reg(list,OS_ADDR,accumulator,paramanager.getintparaloc(1));
  1076. a_reg_dealloc(list,accumulator);
  1077. a_call_name(list,'FPC_SETJMP');
  1078. list.concat(tai_regalloc.Alloc(accumulator));
  1079. list.concat(Taicpu.op_reg(A_PUSH,S_L,accumulator));
  1080. list.concat(tai_regalloc.DeAlloc(accumulator));
  1081. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,accumulator,exceptlabel);
  1082. reference_release(list,tempbuf);
  1083. end;
  1084. procedure tcgx86.g_free_exception(list : taasmoutput;var jmpbuf, envbuf, href : treference;
  1085. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  1086. begin
  1087. cg.a_call_name(list,'FPC_POPADDRSTACK');
  1088. if not onlyfree then
  1089. begin
  1090. a_reg_alloc(list,accumulator);
  1091. g_exception_reason_load(list,href);
  1092. a_reg_dealloc(list,accumulator);
  1093. cg.a_cmp_const_reg_label(list,OS_S32,OC_EQ,0,accumulator,endexceptlabel);
  1094. end;
  1095. end;
  1096. procedure tcgx86.g_exception_reason_save(list : taasmoutput; const href : treference);
  1097. begin
  1098. list.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1099. end;
  1100. procedure tcgx86.g_exception_reason_save_const(list : taasmoutput;const href : treference; a: aword);
  1101. begin
  1102. list.concat(Taicpu.op_const(A_PUSH,S_L,a));
  1103. end;
  1104. procedure tcgx86.g_exception_reason_load(list : taasmoutput; const href : treference);
  1105. begin
  1106. list.concat(Taicpu.op_reg(A_POP,S_L,R_EAX));
  1107. end;
  1108. {****************************************************************************
  1109. Entry/Exit Code Helpers
  1110. ****************************************************************************}
  1111. procedure tcgx86.g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);
  1112. var
  1113. lenref : treference;
  1114. power,len : longint;
  1115. opsize : topsize;
  1116. {$ifndef __NOWINPECOFF__}
  1117. again,ok : tasmlabel;
  1118. {$endif}
  1119. begin
  1120. lenref:=ref;
  1121. inc(lenref.offset,4);
  1122. { get stack space }
  1123. rg.getexplicitregisterint(list,R_EDI);
  1124. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,R_EDI));
  1125. list.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1126. if (elesize<>1) then
  1127. begin
  1128. if ispowerof2(elesize, power) then
  1129. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1130. else
  1131. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,R_EDI));
  1132. end;
  1133. {$ifndef __NOWINPECOFF__}
  1134. { windows guards only a few pages for stack growing, }
  1135. { so we have to access every page first }
  1136. if target_info.system=system_i386_win32 then
  1137. begin
  1138. getlabel(again);
  1139. getlabel(ok);
  1140. a_label(list,again);
  1141. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,R_EDI));
  1142. a_jmp_cond(list,OC_B,ok);
  1143. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  1144. list.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1145. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,R_EDI));
  1146. a_jmp_always(list,again);
  1147. a_label(list,ok);
  1148. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1149. rg.ungetregisterint(list,R_EDI);
  1150. { now reload EDI }
  1151. rg.getexplicitregisterint(list,R_EDI);
  1152. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,R_EDI));
  1153. list.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1154. if (elesize<>1) then
  1155. begin
  1156. if ispowerof2(elesize, power) then
  1157. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1158. else
  1159. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,R_EDI));
  1160. end;
  1161. end
  1162. else
  1163. {$endif __NOWINPECOFF__}
  1164. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1165. { load destination }
  1166. list.concat(Taicpu.op_reg_reg(A_MOV,S_L,R_ESP,R_EDI));
  1167. { don't destroy the registers! }
  1168. list.concat(Taicpu.op_reg(A_PUSH,S_L,R_ECX));
  1169. list.concat(Taicpu.op_reg(A_PUSH,S_L,R_ESI));
  1170. { load count }
  1171. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,R_ECX));
  1172. { load source }
  1173. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,ref,R_ESI));
  1174. { scheduled .... }
  1175. list.concat(Taicpu.op_reg(A_INC,S_L,R_ECX));
  1176. { calculate size }
  1177. len:=elesize;
  1178. opsize:=S_B;
  1179. if (len and 3)=0 then
  1180. begin
  1181. opsize:=S_L;
  1182. len:=len shr 2;
  1183. end
  1184. else
  1185. if (len and 1)=0 then
  1186. begin
  1187. opsize:=S_W;
  1188. len:=len shr 1;
  1189. end;
  1190. if ispowerof2(len, power) then
  1191. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_ECX))
  1192. else
  1193. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,R_ECX));
  1194. list.concat(Taicpu.op_none(A_REP,S_NO));
  1195. case opsize of
  1196. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1197. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1198. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1199. end;
  1200. rg.ungetregisterint(list,R_EDI);
  1201. list.concat(Taicpu.op_reg(A_POP,S_L,R_ESI));
  1202. list.concat(Taicpu.op_reg(A_POP,S_L,R_ECX));
  1203. { patch the new address }
  1204. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,R_ESP,ref));
  1205. end;
  1206. procedure tcgx86.g_interrupt_stackframe_entry(list : taasmoutput);
  1207. begin
  1208. { .... also the segment registers }
  1209. list.concat(Taicpu.Op_reg(A_PUSH,S_W,R_GS));
  1210. list.concat(Taicpu.Op_reg(A_PUSH,S_W,R_FS));
  1211. list.concat(Taicpu.Op_reg(A_PUSH,S_W,R_ES));
  1212. list.concat(Taicpu.Op_reg(A_PUSH,S_W,R_DS));
  1213. { save the registers of an interrupt procedure }
  1214. list.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1215. list.concat(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1216. list.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDX));
  1217. list.concat(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  1218. list.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1219. list.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EAX));
  1220. end;
  1221. procedure tcgx86.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1222. begin
  1223. if accused then
  1224. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,R_ESP))
  1225. else
  1226. list.concat(Taicpu.Op_reg(A_POP,S_L,R_EAX));
  1227. list.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  1228. list.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX));
  1229. if acchiused then
  1230. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,R_ESP))
  1231. else
  1232. list.concat(Taicpu.Op_reg(A_POP,S_L,R_EDX));
  1233. if selfused then
  1234. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,R_ESP))
  1235. else
  1236. list.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  1237. list.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  1238. { .... also the segment registers }
  1239. list.concat(Taicpu.Op_reg(A_POP,S_W,R_DS));
  1240. list.concat(Taicpu.Op_reg(A_POP,S_W,R_ES));
  1241. list.concat(Taicpu.Op_reg(A_POP,S_W,R_FS));
  1242. list.concat(Taicpu.Op_reg(A_POP,S_W,R_GS));
  1243. { this restores the flags }
  1244. list.concat(Taicpu.Op_none(A_IRET,S_NO));
  1245. end;
  1246. procedure tcgx86.g_profilecode(list : taasmoutput);
  1247. var
  1248. pl : tasmlabel;
  1249. begin
  1250. case target_info.system of
  1251. system_i386_win32,
  1252. system_i386_freebsd,
  1253. system_i386_wdosx,
  1254. system_i386_linux:
  1255. begin
  1256. getaddrlabel(pl);
  1257. list.concat(Tai_section.Create(sec_data));
  1258. list.concat(Tai_align.Create(4));
  1259. list.concat(Tai_label.Create(pl));
  1260. list.concat(Tai_const.Create_32bit(0));
  1261. list.concat(Tai_section.Create(sec_code));
  1262. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,R_EDX));
  1263. a_call_name(list,target_info.Cprefix+'mcount');
  1264. include(rg.usedinproc,R_EDX);
  1265. end;
  1266. system_i386_go32v2:
  1267. begin
  1268. a_call_name(list,'MCOUNT');
  1269. end;
  1270. end;
  1271. end;
  1272. procedure tcgx86.g_stackframe_entry(list : taasmoutput;localsize : longint);
  1273. var
  1274. href : treference;
  1275. i : integer;
  1276. again : tasmlabel;
  1277. begin
  1278. list.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EBP));
  1279. list.concat(Taicpu.Op_reg_reg(A_MOV,S_L,R_ESP,R_EBP));
  1280. if localsize>0 then
  1281. begin
  1282. {$ifndef NOTARGETWIN32}
  1283. { windows guards only a few pages for stack growing, }
  1284. { so we have to access every page first }
  1285. if (target_info.system=system_i386_win32) and
  1286. (localsize>=winstackpagesize) then
  1287. begin
  1288. if localsize div winstackpagesize<=5 then
  1289. begin
  1290. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,R_ESP));
  1291. for i:=1 to localsize div winstackpagesize do
  1292. begin
  1293. reference_reset_base(href,R_ESP,localsize-i*winstackpagesize);
  1294. list.concat(Taicpu.op_const_ref(A_MOV,S_L,0,href));
  1295. end;
  1296. list.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1297. end
  1298. else
  1299. begin
  1300. getlabel(again);
  1301. rg.getexplicitregisterint(list,R_EDI);
  1302. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,R_EDI));
  1303. a_label(list,again);
  1304. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  1305. list.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1306. list.concat(Taicpu.op_reg(A_DEC,S_L,R_EDI));
  1307. a_jmp_cond(list,OC_NE,again);
  1308. rg.ungetregisterint(list,R_EDI);
  1309. list.concat(Taicpu.op_const_reg(A_SUB,S_L,localsize mod winstackpagesize,R_ESP));
  1310. end
  1311. end
  1312. else
  1313. {$endif NOTARGETWIN32}
  1314. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize,R_ESP));
  1315. end;
  1316. end;
  1317. procedure tcgx86.g_restore_frame_pointer(list : taasmoutput);
  1318. begin
  1319. list.concat(Taicpu.Op_none(A_LEAVE,S_NO));
  1320. end;
  1321. procedure tcgx86.g_return_from_proc(list : taasmoutput;parasize : aword);
  1322. begin
  1323. { Routines with the poclearstack flag set use only a ret }
  1324. { also routines with parasize=0 }
  1325. if (po_clearstack in aktprocdef.procoptions) then
  1326. begin
  1327. { complex return values are removed from stack in C code PM }
  1328. if paramanager.ret_in_param(aktprocdef.rettype.def) then
  1329. list.concat(Taicpu.Op_const(A_RET,S_NO,4))
  1330. else
  1331. list.concat(Taicpu.Op_none(A_RET,S_NO));
  1332. end
  1333. else if (parasize=0) then
  1334. list.concat(Taicpu.Op_none(A_RET,S_NO))
  1335. else
  1336. begin
  1337. { parameters are limited to 65535 bytes because }
  1338. { ret allows only imm16 }
  1339. if (parasize>65535) then
  1340. CGMessage(cg_e_parasize_too_big);
  1341. list.concat(Taicpu.Op_const(A_RET,S_NO,parasize));
  1342. end;
  1343. end;
  1344. {$ifndef TEST_GENERIC}
  1345. procedure tcgx86.g_call_constructor_helper(list : taasmoutput);
  1346. begin
  1347. if is_class(procinfo^._class) then
  1348. begin
  1349. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1350. a_call_name(list,'FPC_NEW_CLASS');
  1351. list.concat(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1352. end
  1353. else if is_object(procinfo^._class) then
  1354. begin
  1355. rg.getexplicitregisterint(list,R_EDI);
  1356. a_load_const_reg(list,OS_ADDR,procinfo^._class.vmt_offset,R_EDI);
  1357. a_call_name(list,'FPC_HELP_CONSTRUCTOR');
  1358. list.concat(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1359. end
  1360. else
  1361. internalerror(200006161);
  1362. end;
  1363. procedure tcgx86.g_call_destructor_helper(list : taasmoutput);
  1364. var
  1365. nofinal : tasmlabel;
  1366. href : treference;
  1367. begin
  1368. if is_class(procinfo^._class) then
  1369. begin
  1370. a_call_name(list,'FPC_DISPOSE_CLASS')
  1371. end
  1372. else if is_object(procinfo^._class) then
  1373. begin
  1374. { must the object be finalized ? }
  1375. if procinfo^._class.needs_inittable then
  1376. begin
  1377. getlabel(nofinal);
  1378. reference_reset_base(href,R_EBP,8);
  1379. a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nofinal);
  1380. reference_reset_base(href,R_ESI,0);
  1381. g_finalize(list,procinfo^._class,href,false);
  1382. a_label(list,nofinal);
  1383. end;
  1384. rg.getexplicitregisterint(list,R_EDI);
  1385. a_load_const_reg(list,OS_ADDR,procinfo^._class.vmt_offset,R_EDI);
  1386. rg.ungetregisterint(list,R_EDI);
  1387. a_call_name(list,'FPC_HELP_DESTRUCTOR')
  1388. end
  1389. else
  1390. internalerror(200006162);
  1391. end;
  1392. procedure tcgx86.g_call_fail_helper(list : taasmoutput);
  1393. var
  1394. href : treference;
  1395. begin
  1396. if is_class(procinfo^._class) then
  1397. begin
  1398. reference_reset_base(href,procinfo^.framepointer,8);
  1399. a_load_ref_reg(list,OS_ADDR,href,R_ESI);
  1400. a_call_name(list,'FPC_HELP_FAIL_CLASS');
  1401. end
  1402. else if is_object(procinfo^._class) then
  1403. begin
  1404. reference_reset_base(href,procinfo^.framepointer,12);
  1405. a_load_ref_reg(list,OS_ADDR,href,R_ESI);
  1406. rg.getexplicitregisterint(list,R_EDI);
  1407. a_load_const_reg(list,OS_ADDR,procinfo^._class.vmt_offset,R_EDI);
  1408. a_call_name(list,'FPC_HELP_FAIL');
  1409. rg.ungetregisterint(list,R_EDI);
  1410. end
  1411. else
  1412. internalerror(200006163);
  1413. end;
  1414. {$endif}
  1415. procedure tcgx86.g_save_standard_registers(list : taasmoutput);
  1416. begin
  1417. if (R_EBX in aktprocdef.usedregisters) then
  1418. list.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1419. list.concat(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1420. list.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1421. end;
  1422. procedure tcgx86.g_restore_standard_registers(list : taasmoutput);
  1423. begin
  1424. list.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  1425. list.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  1426. if (R_EBX in aktprocdef.usedregisters) then
  1427. list.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  1428. end;
  1429. procedure tcgx86.g_save_all_registers(list : taasmoutput);
  1430. begin
  1431. list.concat(Taicpu.Op_none(A_PUSHA,S_L));
  1432. end;
  1433. procedure tcgx86.g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);
  1434. var
  1435. href : treference;
  1436. begin
  1437. if selfused then
  1438. begin
  1439. reference_reset_base(href,R_ESP,4);
  1440. list.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_ESI,href));
  1441. end;
  1442. if acchiused then
  1443. begin
  1444. reference_reset_base(href,R_ESP,20);
  1445. list.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDX,href));
  1446. end;
  1447. if accused then
  1448. begin
  1449. reference_reset_base(href,R_ESP,28);
  1450. list.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EAX,href));
  1451. end;
  1452. list.concat(Taicpu.Op_none(A_POPA,S_L));
  1453. { We add a NOP because of the 386DX CPU bugs with POPAD }
  1454. list.concat(taicpu.op_none(A_NOP,S_L));
  1455. end;
  1456. { produces if necessary overflowcode }
  1457. procedure tcgx86.g_overflowcheck(list: taasmoutput; const p: tnode);
  1458. var
  1459. hl : tasmlabel;
  1460. ai : taicpu;
  1461. cond : TAsmCond;
  1462. begin
  1463. if not(cs_check_overflow in aktlocalswitches) then
  1464. exit;
  1465. getlabel(hl);
  1466. if not ((p.resulttype.def.deftype=pointerdef) or
  1467. ((p.resulttype.def.deftype=orddef) and
  1468. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1469. bool8bit,bool16bit,bool32bit]))) then
  1470. cond:=C_NO
  1471. else
  1472. cond:=C_NB;
  1473. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  1474. ai.SetCondition(cond);
  1475. ai.is_jmp:=true;
  1476. list.concat(ai);
  1477. a_call_name(list,'FPC_OVERFLOW');
  1478. a_label(list,hl);
  1479. end;
  1480. end.
  1481. {
  1482. $Log$
  1483. Revision 1.5 2002-08-04 19:52:04 carl
  1484. + updated exception routines
  1485. Revision 1.4 2002/07/27 19:53:51 jonas
  1486. + generic implementation of tcg.g_flags2ref()
  1487. * tcg.flags2xxx() now also needs a size parameter
  1488. Revision 1.3 2002/07/26 21:15:46 florian
  1489. * rewrote the system handling
  1490. Revision 1.2 2002/07/21 16:55:34 jonas
  1491. * fixed bug in op_const_reg_reg() for imul
  1492. Revision 1.1 2002/07/20 19:28:47 florian
  1493. * splitting of i386\cgcpu.pas into x86\cgx86.pas and i386\cgcpu.pas
  1494. cgx86.pas will contain the common code for i386 and x86_64
  1495. }