cgx86.pas 70 KB

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