cgcpu.pas 68 KB

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