cgx86.pas 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  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. baseno,indexno:boolean;
  317. begin
  318. if r.segment.enum<>R_NO then
  319. CGMessage(cg_e_cant_use_far_pointer_there);
  320. baseno:=(r.base.enum=R_NO) or ((r.base.enum=R_INTREGISTER) and (r.base.number=NR_NO));
  321. indexno:=(r.index.enum=R_NO) or ((r.index.enum=R_INTREGISTER) and (r.index.number=NR_NO));
  322. if baseno and indexno then
  323. begin
  324. if assigned(r.symbol) then
  325. list.concat(Taicpu.Op_sym_ofs(A_PUSH,S_L,r.symbol,r.offset))
  326. else
  327. list.concat(Taicpu.Op_const(A_PUSH,S_L,r.offset));
  328. end
  329. else if baseno and not indexno and
  330. (r.offset=0) and (r.scalefactor=0) and (r.symbol=nil) then
  331. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r.index))
  332. else if not baseno and indexno and
  333. (r.offset=0) and (r.symbol=nil) then
  334. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r.base))
  335. else
  336. begin
  337. tmpreg := get_scratch_reg_address(list);
  338. a_loadaddr_ref_reg(list,r,tmpreg);
  339. list.concat(taicpu.op_reg(A_PUSH,S_L,tmpreg));
  340. free_scratch_reg(list,tmpreg);
  341. end;
  342. end;
  343. procedure tcgx86.a_call_name(list : taasmoutput;const s : string);
  344. begin
  345. list.concat(taicpu.op_sym(A_CALL,S_NO,objectlibrary.newasmsymbol(s)));
  346. end;
  347. procedure tcgx86.a_call_ref(list : taasmoutput;const ref : treference);
  348. begin
  349. list.concat(taicpu.op_ref(A_CALL,S_NO,ref));
  350. end;
  351. procedure tcgx86.a_call_reg(list : taasmoutput;reg : tregister);
  352. begin
  353. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  354. end;
  355. {********************** load instructions ********************}
  356. procedure tcgx86.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  357. begin
  358. { the optimizer will change it to "xor reg,reg" when loading zero, }
  359. { no need to do it here too (JM) }
  360. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[size],a,reg))
  361. end;
  362. procedure tcgx86.a_load_const_ref(list : taasmoutput; size: tcgsize; a : aword;const ref : treference);
  363. begin
  364. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[size],a,ref));
  365. end;
  366. procedure tcgx86.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref : treference);
  367. begin
  368. list.concat(taicpu.op_reg_ref(A_MOV,TCGSize2OpSize[size],reg,
  369. ref));
  370. End;
  371. procedure tcgx86.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref: treference;reg : tregister);
  372. var
  373. op: tasmop;
  374. o,s: topsize;
  375. begin
  376. if reg.enum=R_INTREGISTER then
  377. o:=subreg2opsize[reg.number and $ff]
  378. else
  379. o:=reg2opsize[reg.enum];
  380. sizes2load(size,o,op,s);
  381. list.concat(taicpu.op_ref_reg(op,s,ref,reg));
  382. end;
  383. procedure tcgx86.a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  384. var
  385. op: tasmop;
  386. s: topsize;
  387. eq:boolean;
  388. begin
  389. if (reg1.enum=R_INTREGISTER) and (reg2.enum=R_INTREGISTER) then
  390. begin
  391. sizes2load(fromsize,subreg2opsize[reg2.number and $ff],op,s);
  392. eq:=(reg1.number shr 8)=(reg2.number shr 8);
  393. end
  394. else if (reg1.enum<lastreg) and (reg2.enum<lastreg) then
  395. begin
  396. sizes2load(fromsize,reg2opsize[reg2.enum],op,s);
  397. eq:=(rg.makeregsize(reg1,OS_INT).enum = rg.makeregsize(reg2,OS_INT).enum);
  398. end
  399. else
  400. internalerror(200301081);
  401. if eq then
  402. begin
  403. { "mov reg1, reg1" doesn't make sense }
  404. if op = A_MOV then
  405. exit;
  406. { optimize movzx with "and ffff,<reg>" operation }
  407. if (op = A_MOVZX) then
  408. begin
  409. case fromsize of
  410. OS_8:
  411. begin
  412. list.concat(taicpu.op_const_reg(A_AND,reg2opsize[reg2.enum],255,reg2));
  413. exit;
  414. end;
  415. OS_16:
  416. begin
  417. list.concat(taicpu.op_const_reg(A_AND,reg2opsize[reg2.enum],65535,reg2));
  418. exit;
  419. end;
  420. end;
  421. end;
  422. end;
  423. list.concat(taicpu.op_reg_reg(op,s,reg1,reg2));
  424. end;
  425. procedure tcgx86.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  426. begin
  427. if assigned(ref.symbol) and
  428. (ref.base.enum=R_NO) and
  429. (ref.index.enum=R_NO) then
  430. list.concat(taicpu.op_sym_ofs_reg(A_MOV,S_L,ref.symbol,ref.offset,r))
  431. else
  432. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,r));
  433. end;
  434. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  435. { R_ST means "the current value at the top of the fpu stack" (JM) }
  436. procedure tcgx86.a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  437. begin
  438. if (reg1.enum <> R_ST) then
  439. begin
  440. list.concat(taicpu.op_reg(A_FLD,S_NO,
  441. trgcpu(rg).correct_fpuregister(reg1,trgcpu(rg).fpuvaroffset)));
  442. inc(trgcpu(rg).fpuvaroffset);
  443. end;
  444. if (reg2.enum <> R_ST) then
  445. begin
  446. list.concat(taicpu.op_reg(A_FSTP,S_NO,
  447. trgcpu(rg).correct_fpuregister(reg2,trgcpu(rg).fpuvaroffset)));
  448. dec(trgcpu(rg).fpuvaroffset);
  449. end;
  450. end;
  451. procedure tcgx86.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  452. var rst:Tregister;
  453. begin
  454. rst.enum:=R_ST;
  455. floatload(list,size,ref);
  456. if reg.enum>lastreg then
  457. internalerror(200301081);
  458. if (reg.enum <> R_ST) then
  459. a_loadfpu_reg_reg(list,rst,reg);
  460. end;
  461. procedure tcgx86.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  462. var rst:Tregister;
  463. begin
  464. rst.enum:=R_ST;
  465. if reg.enum>lastreg then
  466. internalerror(200301081);
  467. if reg.enum <> R_ST then
  468. a_loadfpu_reg_reg(list,reg,rst);
  469. floatstore(list,size,ref);
  470. end;
  471. procedure tcgx86.a_loadmm_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  472. begin
  473. list.concat(taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2));
  474. end;
  475. procedure tcgx86.a_loadmm_ref_reg(list: taasmoutput; const ref: treference; reg: tregister);
  476. begin
  477. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,ref,reg));
  478. end;
  479. procedure tcgx86.a_loadmm_reg_ref(list: taasmoutput; reg: tregister; const ref: treference);
  480. begin
  481. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,ref));
  482. end;
  483. procedure tcgx86.a_parammm_reg(list: taasmoutput; reg: tregister);
  484. var
  485. href : treference;
  486. r : Tregister;
  487. begin
  488. r.enum:=R_ESP;
  489. list.concat(taicpu.op_const_reg(A_SUB,S_L,8,r));
  490. reference_reset_base(href,r,0);
  491. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,href));
  492. end;
  493. procedure tcgx86.a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister);
  494. var
  495. opcode: tasmop;
  496. power: longint;
  497. begin
  498. if reg.enum>lastreg then
  499. internalerror(200301081);
  500. Case Op of
  501. OP_DIV, OP_IDIV:
  502. Begin
  503. if ispowerof2(a,power) then
  504. begin
  505. case op of
  506. OP_DIV:
  507. opcode := A_SHR;
  508. OP_IDIV:
  509. opcode := A_SAR;
  510. end;
  511. list.concat(taicpu.op_const_reg(opcode,reg2opsize[reg.enum],power,
  512. reg));
  513. exit;
  514. end;
  515. { the rest should be handled specifically in the code }
  516. { generator because of the silly register usage restraints }
  517. internalerror(200109224);
  518. End;
  519. OP_MUL,OP_IMUL:
  520. begin
  521. if not(cs_check_overflow in aktlocalswitches) and
  522. ispowerof2(a,power) then
  523. begin
  524. list.concat(taicpu.op_const_reg(A_SHL,reg2opsize[reg.enum],power,
  525. reg));
  526. exit;
  527. end;
  528. if op = OP_IMUL then
  529. list.concat(taicpu.op_const_reg(A_IMUL,reg2opsize[reg.enum],
  530. a,reg))
  531. else
  532. { OP_MUL should be handled specifically in the code }
  533. { generator because of the silly register usage restraints }
  534. internalerror(200109225);
  535. end;
  536. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  537. if not(cs_check_overflow in aktlocalswitches) and
  538. (a = 1) and
  539. (op in [OP_ADD,OP_SUB]) then
  540. if op = OP_ADD then
  541. list.concat(taicpu.op_reg(A_INC,reg2opsize[reg.enum],reg))
  542. else
  543. list.concat(taicpu.op_reg(A_DEC,reg2opsize[reg.enum],reg))
  544. else if (a = 0) then
  545. if (op <> OP_AND) then
  546. exit
  547. else
  548. list.concat(taicpu.op_const_reg(A_MOV,reg2opsize[reg.enum],0,reg))
  549. else if (a = high(aword)) and
  550. (op in [OP_AND,OP_OR,OP_XOR]) then
  551. begin
  552. case op of
  553. OP_AND:
  554. exit;
  555. OP_OR:
  556. list.concat(taicpu.op_const_reg(A_MOV,reg2opsize[reg.enum],high(aword),reg));
  557. OP_XOR:
  558. list.concat(taicpu.op_reg(A_NOT,reg2opsize[reg.enum],reg));
  559. end
  560. end
  561. else
  562. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],reg2opsize[reg.enum],
  563. a,reg));
  564. OP_SHL,OP_SHR,OP_SAR:
  565. begin
  566. if (a and 31) <> 0 Then
  567. list.concat(taicpu.op_const_reg(
  568. TOpCG2AsmOp[op],reg2opsize[reg.enum],a and 31,reg));
  569. if (a shr 5) <> 0 Then
  570. internalerror(68991);
  571. end
  572. else internalerror(68992);
  573. end;
  574. end;
  575. procedure tcgx86.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  576. var
  577. opcode: tasmop;
  578. power: longint;
  579. begin
  580. Case Op of
  581. OP_DIV, OP_IDIV:
  582. Begin
  583. if ispowerof2(a,power) then
  584. begin
  585. case op of
  586. OP_DIV:
  587. opcode := A_SHR;
  588. OP_IDIV:
  589. opcode := A_SAR;
  590. end;
  591. list.concat(taicpu.op_const_ref(opcode,
  592. TCgSize2OpSize[size],power,ref));
  593. exit;
  594. end;
  595. { the rest should be handled specifically in the code }
  596. { generator because of the silly register usage restraints }
  597. internalerror(200109231);
  598. End;
  599. OP_MUL,OP_IMUL:
  600. begin
  601. if not(cs_check_overflow in aktlocalswitches) and
  602. ispowerof2(a,power) then
  603. begin
  604. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  605. power,ref));
  606. exit;
  607. end;
  608. { can't multiply a memory location directly with a constant }
  609. if op = OP_IMUL then
  610. inherited a_op_const_ref(list,op,size,a,ref)
  611. else
  612. { OP_MUL should be handled specifically in the code }
  613. { generator because of the silly register usage restraints }
  614. internalerror(200109232);
  615. end;
  616. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  617. if not(cs_check_overflow in aktlocalswitches) and
  618. (a = 1) and
  619. (op in [OP_ADD,OP_SUB]) then
  620. if op = OP_ADD then
  621. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],ref))
  622. else
  623. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],ref))
  624. else if (a = 0) then
  625. if (op <> OP_AND) then
  626. exit
  627. else
  628. a_load_const_ref(list,size,0,ref)
  629. else if (a = high(aword)) and
  630. (op in [OP_AND,OP_OR,OP_XOR]) then
  631. begin
  632. case op of
  633. OP_AND:
  634. exit;
  635. OP_OR:
  636. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],high(aword),ref));
  637. OP_XOR:
  638. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],ref));
  639. end
  640. end
  641. else
  642. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  643. TCgSize2OpSize[size],a,ref));
  644. OP_SHL,OP_SHR,OP_SAR:
  645. begin
  646. if (a and 31) <> 0 Then
  647. list.concat(taicpu.op_const_ref(
  648. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,ref));
  649. if (a shr 5) <> 0 Then
  650. internalerror(68991);
  651. end
  652. else internalerror(68992);
  653. end;
  654. end;
  655. procedure tcgx86.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  656. var
  657. regloadsize: tcgsize;
  658. dstsize: topsize;
  659. tmpreg : tregister;
  660. popecx : boolean;
  661. r:Tregister;
  662. begin
  663. if src.enum>lastreg then
  664. internalerror(200301081);
  665. if dst.enum>lastreg then
  666. internalerror(200301081);
  667. r.enum:=R_INTREGISTER;
  668. dstsize := tcgsize2opsize[size];
  669. dst := rg.makeregsize(dst,size);
  670. case op of
  671. OP_NEG,OP_NOT:
  672. begin
  673. if src.enum <> R_NO then
  674. internalerror(200112291);
  675. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  676. end;
  677. OP_MUL,OP_DIV,OP_IDIV:
  678. { special stuff, needs separate handling inside code }
  679. { generator }
  680. internalerror(200109233);
  681. OP_SHR,OP_SHL,OP_SAR:
  682. begin
  683. tmpreg.enum := R_NO;
  684. popecx := false;
  685. { we need cl to hold the shift count, so if the destination }
  686. { is ecx, save it to a temp for now }
  687. if dst.enum in [R_ECX,R_CX,R_CL] then
  688. begin
  689. case reg2opsize[dst.enum] of
  690. S_B: regloadsize := OS_8;
  691. S_W: regloadsize := OS_16;
  692. else regloadsize := OS_32;
  693. end;
  694. tmpreg := get_scratch_reg_int(list);
  695. a_load_reg_reg(list,regloadsize,regloadsize,src,tmpreg);
  696. end;
  697. if not(src.enum in [R_ECX,R_CX,R_CL]) then
  698. begin
  699. { is ecx still free (it's also free if it was allocated }
  700. { to dst, since we've moved dst somewhere else already) }
  701. r.number:=NR_ECX;
  702. if not((dst.enum = R_ECX) or
  703. ((R_ECX in rg.unusedregsint) and
  704. { this will always be true, it's just here to }
  705. { allocate ecx }
  706. (rg.getexplicitregisterint(list,R_ECX).enum = R_ECX))) then
  707. begin
  708. list.concat(taicpu.op_reg(A_PUSH,S_L,r));
  709. popecx := true;
  710. end;
  711. a_load_reg_reg(list,OS_32,OS_32,rg.makeregsize(src,OS_32),r);
  712. end
  713. else
  714. src.enum := R_CL;
  715. { do the shift }
  716. r.number:=NR_CL;
  717. if tmpreg.enum = R_NO then
  718. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,
  719. r,dst))
  720. else
  721. begin
  722. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],S_L,
  723. r,tmpreg));
  724. { move result back to the destination }
  725. r.number:=NR_ECX;
  726. a_load_reg_reg(list,OS_32,OS_32,tmpreg,r);
  727. free_scratch_reg(list,tmpreg);
  728. end;
  729. r.number:=NR_ECX;
  730. if popecx then
  731. list.concat(taicpu.op_reg(A_POP,S_L,r))
  732. else if not (dst.enum in [R_ECX,R_CX,R_CL]) then
  733. rg.ungetregisterint(list,r);
  734. end;
  735. else
  736. begin
  737. if reg2opsize[src.enum] <> dstsize then
  738. internalerror(200109226);
  739. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,
  740. src,dst));
  741. end;
  742. end;
  743. end;
  744. procedure tcgx86.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  745. begin
  746. case op of
  747. OP_NEG,OP_NOT,OP_IMUL:
  748. begin
  749. inherited a_op_ref_reg(list,op,size,ref,reg);
  750. end;
  751. OP_MUL,OP_DIV,OP_IDIV:
  752. { special stuff, needs separate handling inside code }
  753. { generator }
  754. internalerror(200109239);
  755. else
  756. begin
  757. reg := rg.makeregsize(reg,size);
  758. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],ref,reg));
  759. end;
  760. end;
  761. end;
  762. procedure tcgx86.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  763. var
  764. opsize: topsize;
  765. begin
  766. if reg.enum>lastreg then
  767. internalerror(200201081);
  768. case op of
  769. OP_NEG,OP_NOT:
  770. begin
  771. if reg.enum <> R_NO then
  772. internalerror(200109237);
  773. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],ref));
  774. end;
  775. OP_IMUL:
  776. begin
  777. { this one needs a load/imul/store, which is the default }
  778. inherited a_op_ref_reg(list,op,size,ref,reg);
  779. end;
  780. OP_MUL,OP_DIV,OP_IDIV:
  781. { special stuff, needs separate handling inside code }
  782. { generator }
  783. internalerror(200109238);
  784. else
  785. begin
  786. opsize := tcgsize2opsize[size];
  787. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],opsize,reg,ref));
  788. end;
  789. end;
  790. end;
  791. procedure tcgx86.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  792. size: tcgsize; a: aword; src, dst: tregister);
  793. var
  794. tmpref: treference;
  795. power: longint;
  796. opsize: topsize;
  797. begin
  798. if src.enum>lastreg then
  799. internalerror(200201081);
  800. if dst.enum>lastreg then
  801. internalerror(200201081);
  802. opsize := reg2opsize[src.enum];
  803. if (opsize <> S_L) or
  804. not (size in [OS_32,OS_S32]) then
  805. begin
  806. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  807. exit;
  808. end;
  809. { if we get here, we have to do a 32 bit calculation, guaranteed }
  810. Case Op of
  811. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  812. OP_SAR:
  813. { can't do anything special for these }
  814. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  815. OP_IMUL:
  816. begin
  817. if not(cs_check_overflow in aktlocalswitches) and
  818. ispowerof2(a,power) then
  819. { can be done with a shift }
  820. begin
  821. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  822. exit;
  823. end;
  824. list.concat(taicpu.op_const_reg_reg(A_IMUL,S_L,a,src,dst));
  825. end;
  826. OP_ADD, OP_SUB:
  827. if (a = 0) then
  828. a_load_reg_reg(list,size,size,src,dst)
  829. else
  830. begin
  831. reference_reset(tmpref);
  832. tmpref.base := src;
  833. tmpref.offset := longint(a);
  834. if op = OP_SUB then
  835. tmpref.offset := -tmpref.offset;
  836. list.concat(taicpu.op_ref_reg(A_LEA,S_L,tmpref,dst));
  837. end
  838. else internalerror(200112302);
  839. end;
  840. end;
  841. procedure tcgx86.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  842. size: tcgsize; src1, src2, dst: tregister);
  843. var
  844. tmpref: treference;
  845. opsize: topsize;
  846. begin
  847. if src1.enum>lastreg then
  848. internalerror(200201081);
  849. if src2.enum>lastreg then
  850. internalerror(200201081);
  851. if dst.enum>lastreg then
  852. internalerror(200201081);
  853. opsize := reg2opsize[src1.enum];
  854. if (opsize <> S_L) or
  855. (reg2opsize[src2.enum] <> S_L) or
  856. not (size in [OS_32,OS_S32]) then
  857. begin
  858. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  859. exit;
  860. end;
  861. { if we get here, we have to do a 32 bit calculation, guaranteed }
  862. Case Op of
  863. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  864. OP_SAR,OP_SUB,OP_NOT,OP_NEG:
  865. { can't do anything special for these }
  866. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  867. OP_IMUL:
  868. list.concat(taicpu.op_reg_reg_reg(A_IMUL,S_L,src1,src2,dst));
  869. OP_ADD:
  870. begin
  871. reference_reset(tmpref);
  872. tmpref.base := src1;
  873. tmpref.index := src2;
  874. tmpref.scalefactor := 1;
  875. list.concat(taicpu.op_ref_reg(A_LEA,S_L,tmpref,dst));
  876. end
  877. else internalerror(200112303);
  878. end;
  879. end;
  880. {*************** compare instructructions ****************}
  881. procedure tcgx86.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  882. l : tasmlabel);
  883. begin
  884. if reg.enum>lastreg then
  885. internalerror(200101081);
  886. if (a = 0) then
  887. list.concat(taicpu.op_reg_reg(A_TEST,reg2opsize[reg.enum],reg,reg))
  888. else
  889. list.concat(taicpu.op_const_reg(A_CMP,reg2opsize[reg.enum],a,reg));
  890. a_jmp_cond(list,cmp_op,l);
  891. end;
  892. procedure tcgx86.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  893. l : tasmlabel);
  894. begin
  895. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,ref));
  896. a_jmp_cond(list,cmp_op,l);
  897. end;
  898. procedure tcgx86.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  899. reg1,reg2 : tregister;l : tasmlabel);
  900. begin
  901. if reg1.enum>lastreg then
  902. internalerror(200101081);
  903. if reg2.enum>lastreg then
  904. internalerror(200101081);
  905. if reg2opsize[reg1.enum] <> reg2opsize[reg2.enum] then
  906. internalerror(200109226);
  907. list.concat(taicpu.op_reg_reg(A_CMP,reg2opsize[reg1.enum],reg1,reg2));
  908. a_jmp_cond(list,cmp_op,l);
  909. end;
  910. procedure tcgx86.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  911. begin
  912. reg := rg.makeregsize(reg,size);
  913. list.concat(taicpu.op_ref_reg(A_CMP,tcgsize2opsize[size],ref,reg));
  914. a_jmp_cond(list,cmp_op,l);
  915. end;
  916. procedure tcgx86.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  917. var
  918. ai : taicpu;
  919. begin
  920. if cond=OC_None then
  921. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  922. else
  923. begin
  924. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  925. ai.SetCondition(TOpCmp2AsmCond[cond]);
  926. end;
  927. ai.is_jmp:=true;
  928. list.concat(ai);
  929. end;
  930. procedure tcgx86.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  931. var
  932. ai : taicpu;
  933. begin
  934. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  935. ai.SetCondition(flags_to_cond(f));
  936. ai.is_jmp := true;
  937. list.concat(ai);
  938. end;
  939. procedure tcgx86.g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister);
  940. var
  941. ai : taicpu;
  942. hreg : tregister;
  943. begin
  944. if reg.enum>lastreg then
  945. internalerror(200201081);
  946. hreg := rg.makeregsize(reg,OS_8);
  947. ai:=Taicpu.Op_reg(A_Setcc,S_B,hreg);
  948. ai.SetCondition(flags_to_cond(f));
  949. list.concat(ai);
  950. if (reg.enum <> hreg.enum) then
  951. a_load_reg_reg(list,OS_8,size,hreg,reg);
  952. end;
  953. procedure tcgx86.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref: TReference);
  954. var
  955. ai : taicpu;
  956. begin
  957. if not(size in [OS_8,OS_S8]) then
  958. a_load_const_ref(list,size,0,ref);
  959. ai:=Taicpu.Op_ref(A_Setcc,S_B,ref);
  960. ai.SetCondition(flags_to_cond(f));
  961. list.concat(ai);
  962. end;
  963. { ************* concatcopy ************ }
  964. procedure tcgx86.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  965. var
  966. ecxpushed : boolean;
  967. helpsize : longint;
  968. i : byte;
  969. reg8,reg32 : tregister;
  970. srcref,dstref : treference;
  971. swap : boolean;
  972. r : Tregister;
  973. procedure maybepushecx;
  974. var r:Tregister;
  975. begin
  976. r.enum:=R_INTREGISTER;
  977. r.number:=NR_ECX;
  978. if not(R_ECX in rg.unusedregsint) then
  979. begin
  980. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  981. ecxpushed:=true;
  982. end
  983. else rg.getexplicitregisterint(list,R_ECX);
  984. end;
  985. begin
  986. if (not loadref) and
  987. ((len<=8) or
  988. (not(cs_littlesize in aktglobalswitches ) and (len<=12))) then
  989. begin
  990. r.enum:=R_INTREGISTER;
  991. helpsize:=len shr 2;
  992. rg.getexplicitregisterint(list,R_EDI);
  993. dstref:=dest;
  994. srcref:=source;
  995. for i:=1 to helpsize do
  996. begin
  997. r.number:=NR_EDI;
  998. a_load_ref_reg(list,OS_32,srcref,r);
  999. If (len = 4) and delsource then
  1000. reference_release(list,source);
  1001. a_load_reg_ref(list,OS_32,r,dstref);
  1002. inc(srcref.offset,4);
  1003. inc(dstref.offset,4);
  1004. dec(len,4);
  1005. end;
  1006. if len>1 then
  1007. begin
  1008. r.number:=NR_DI;
  1009. a_load_ref_reg(list,OS_16,srcref,r);
  1010. If (len = 2) and delsource then
  1011. reference_release(list,source);
  1012. a_load_reg_ref(list,OS_16,r,dstref);
  1013. inc(srcref.offset,2);
  1014. inc(dstref.offset,2);
  1015. dec(len,2);
  1016. end;
  1017. r.enum:=R_EDI;
  1018. rg.ungetregisterint(list,r);
  1019. r.enum:=R_INTREGISTER;
  1020. reg8.enum:=R_INTREGISTER;
  1021. reg32.enum:=R_INTREGISTER;
  1022. if len>0 then
  1023. begin
  1024. { and now look for an 8 bit register }
  1025. swap:=false;
  1026. if R_EAX in rg.unusedregsint then reg8:=rg.makeregsize(rg.getexplicitregisterint(list,R_EAX),OS_8)
  1027. else if R_EDX in rg.unusedregsint then reg8:=rg.makeregsize(rg.getexplicitregisterint(list,R_EDX),OS_8)
  1028. else if R_EBX in rg.unusedregsint then reg8:=rg.makeregsize(rg.getexplicitregisterint(list,R_EBX),OS_8)
  1029. else if R_ECX in rg.unusedregsint then reg8:=rg.makeregsize(rg.getexplicitregisterint(list,R_ECX),OS_8)
  1030. else
  1031. begin
  1032. swap:=true;
  1033. { we need only to check 3 registers, because }
  1034. { one is always not index or base }
  1035. if (dest.base.enum<>R_EAX) and (dest.index.enum<>R_EAX) then
  1036. begin
  1037. reg8.number:=NR_AL;
  1038. reg32.number:=NR_EAX;
  1039. end
  1040. else if (dest.base.enum<>R_EBX) and (dest.index.enum<>R_EBX) then
  1041. begin
  1042. reg8.number:=NR_BL;
  1043. reg32.number:=NR_EBX;
  1044. end
  1045. else if (dest.base.enum<>R_ECX) and (dest.index.enum<>R_ECX) then
  1046. begin
  1047. reg8.number:=NR_CL;
  1048. reg32.number:=NR_ECX;
  1049. end;
  1050. end;
  1051. if swap then
  1052. { was earlier XCHG, of course nonsense }
  1053. begin
  1054. rg.getexplicitregisterint(list,R_EDI);
  1055. r.number:=NR_EDI;
  1056. a_load_reg_reg(list,OS_32,OS_32,reg32,r);
  1057. end;
  1058. a_load_ref_reg(list,OS_8,srcref,reg8);
  1059. If delsource and (len=1) then
  1060. reference_release(list,source);
  1061. a_load_reg_ref(list,OS_8,reg8,dstref);
  1062. if swap then
  1063. begin
  1064. r.number:=NR_EDI;
  1065. a_load_reg_reg(list,OS_32,OS_32,r,reg32);
  1066. r.enum:=R_EDI;
  1067. rg.ungetregisterint(list,r);
  1068. end
  1069. else
  1070. begin
  1071. if reg8.number=NR_AL then
  1072. reg8.enum:=R_AL
  1073. else if reg8.number=NR_BL then
  1074. reg8.enum:=R_BL
  1075. else if reg8.number=NR_CL then
  1076. reg8.enum:=R_CL;
  1077. rg.ungetregister(list,reg8);
  1078. end;
  1079. end;
  1080. end
  1081. else
  1082. begin
  1083. r.enum:=R_INTREGISTER;
  1084. r.number:=NR_EDI;
  1085. rg.getexplicitregisterint(list,R_EDI);
  1086. a_loadaddr_ref_reg(list,dest,r);
  1087. r.number:=NR_ESI;
  1088. list.concat(tai_regalloc.alloc(r));
  1089. if loadref then
  1090. a_load_ref_reg(list,OS_ADDR,source,r)
  1091. else
  1092. begin
  1093. a_loadaddr_ref_reg(list,source,r);
  1094. if delsource then
  1095. reference_release(list,source);
  1096. end;
  1097. list.concat(Taicpu.Op_none(A_CLD,S_NO));
  1098. ecxpushed:=false;
  1099. r.number:=NR_ECX;
  1100. if cs_littlesize in aktglobalswitches then
  1101. begin
  1102. maybepushecx;
  1103. a_load_const_reg(list,OS_INT,len,r);
  1104. list.concat(Taicpu.Op_none(A_REP,S_NO));
  1105. list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1106. end
  1107. else
  1108. begin
  1109. helpsize:=len shr 2;
  1110. len:=len and 3;
  1111. if helpsize>1 then
  1112. begin
  1113. maybepushecx;
  1114. a_load_const_reg(list,OS_INT,helpsize,r);
  1115. list.concat(Taicpu.Op_none(A_REP,S_NO));
  1116. end;
  1117. if helpsize>0 then
  1118. list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1119. if len>1 then
  1120. begin
  1121. dec(len,2);
  1122. list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1123. end;
  1124. if len=1 then
  1125. list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1126. end;
  1127. r.enum:=R_EDI;
  1128. rg.ungetregisterint(list,r);
  1129. r.enum:=R_INTREGISTER;
  1130. r.number:=NR_ESI;
  1131. list.concat(tai_regalloc.dealloc(r));
  1132. if ecxpushed then
  1133. begin
  1134. r.number:=NR_ECX;
  1135. list.concat(Taicpu.Op_reg(A_POP,S_L,r))
  1136. end
  1137. else
  1138. begin
  1139. r.enum:=R_ECX;
  1140. rg.ungetregisterint(list,r);
  1141. end;
  1142. { loading SELF-reference again }
  1143. g_maybe_loadself(list);
  1144. end;
  1145. if delsource then
  1146. tg.ungetiftemp(list,source);
  1147. end;
  1148. procedure tcgx86.g_exception_reason_save(list : taasmoutput; const href : treference);
  1149. var r:Tregister;
  1150. begin
  1151. r.enum:=R_INTREGISTER;
  1152. r.number:=NR_EAX;
  1153. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1154. end;
  1155. procedure tcgx86.g_exception_reason_save_const(list : taasmoutput;const href : treference; a: aword);
  1156. begin
  1157. list.concat(Taicpu.op_const(A_PUSH,S_L,a));
  1158. end;
  1159. procedure tcgx86.g_exception_reason_load(list : taasmoutput; const href : treference);
  1160. var r:Tregister;
  1161. begin
  1162. r.enum:=R_INTREGISTER;
  1163. r.number:=NR_EAX;
  1164. list.concat(Taicpu.op_reg(A_POP,S_L,r));
  1165. end;
  1166. {****************************************************************************
  1167. Entry/Exit Code Helpers
  1168. ****************************************************************************}
  1169. procedure tcgx86.g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);
  1170. var
  1171. lenref : treference;
  1172. power,len : longint;
  1173. opsize : topsize;
  1174. {$ifndef __NOWINPECOFF__}
  1175. again,ok : tasmlabel;
  1176. {$endif}
  1177. r,r2,rsp:Tregister;
  1178. begin
  1179. lenref:=ref;
  1180. inc(lenref.offset,4);
  1181. { get stack space }
  1182. r.enum:=R_EDI;
  1183. rsp.enum:=R_ESP;
  1184. rg.getexplicitregisterint(list,R_EDI);
  1185. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1186. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1187. if (elesize<>1) then
  1188. begin
  1189. if ispowerof2(elesize, power) then
  1190. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1191. else
  1192. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1193. end;
  1194. {$ifndef __NOWINPECOFF__}
  1195. { windows guards only a few pages for stack growing, }
  1196. { so we have to access every page first }
  1197. if target_info.system=system_i386_win32 then
  1198. begin
  1199. objectlibrary.getlabel(again);
  1200. objectlibrary.getlabel(ok);
  1201. a_label(list,again);
  1202. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,r));
  1203. a_jmp_cond(list,OC_B,ok);
  1204. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1205. r2.enum:=R_EAX;
  1206. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1207. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,r));
  1208. a_jmp_always(list,again);
  1209. a_label(list,ok);
  1210. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1211. rg.ungetregisterint(list,r);
  1212. { now reload EDI }
  1213. rg.getexplicitregisterint(list,R_EDI);
  1214. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1215. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1216. if (elesize<>1) then
  1217. begin
  1218. if ispowerof2(elesize, power) then
  1219. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1220. else
  1221. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1222. end;
  1223. end
  1224. else
  1225. {$endif __NOWINPECOFF__}
  1226. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1227. { align stack on 4 bytes }
  1228. list.concat(Taicpu.op_const_reg(A_AND,S_L,$fffffff4,rsp));
  1229. { load destination }
  1230. list.concat(Taicpu.op_reg_reg(A_MOV,S_L,rsp,r));
  1231. { don't destroy the registers! }
  1232. r2.enum:=R_ECX;
  1233. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1234. r2.enum:=R_ESI;
  1235. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1236. { load count }
  1237. r2.enum:=R_ECX;
  1238. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r2));
  1239. { load source }
  1240. r2.enum:=R_ESI;
  1241. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,ref,r2));
  1242. { scheduled .... }
  1243. r2.enum:=R_ECX;
  1244. list.concat(Taicpu.op_reg(A_INC,S_L,r2));
  1245. { calculate size }
  1246. len:=elesize;
  1247. opsize:=S_B;
  1248. if (len and 3)=0 then
  1249. begin
  1250. opsize:=S_L;
  1251. len:=len shr 2;
  1252. end
  1253. else
  1254. if (len and 1)=0 then
  1255. begin
  1256. opsize:=S_W;
  1257. len:=len shr 1;
  1258. end;
  1259. if ispowerof2(len, power) then
  1260. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r2))
  1261. else
  1262. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,r2));
  1263. list.concat(Taicpu.op_none(A_REP,S_NO));
  1264. case opsize of
  1265. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1266. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1267. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1268. end;
  1269. rg.ungetregisterint(list,r);
  1270. r2.enum:=R_ESI;
  1271. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1272. r2.enum:=R_ECX;
  1273. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1274. { patch the new address }
  1275. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,rsp,ref));
  1276. end;
  1277. procedure tcgx86.g_removevaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);
  1278. var
  1279. lenref : treference;
  1280. power,len : longint;
  1281. r,rsp:Tregister;
  1282. begin
  1283. lenref:=ref;
  1284. inc(lenref.offset,4);
  1285. { caluclate size and adjust stack space }
  1286. rg.getexplicitregisterint(list,R_EDI);
  1287. r.enum:=R_EDI;
  1288. rsp.enum:=R_ESP;
  1289. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1290. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1291. if (elesize<>1) then
  1292. begin
  1293. if ispowerof2(elesize, power) then
  1294. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1295. else
  1296. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1297. end;
  1298. list.concat(Taicpu.op_reg_reg(A_ADD,S_L,r,rsp));
  1299. end;
  1300. procedure tcgx86.g_interrupt_stackframe_entry(list : taasmoutput);
  1301. var r:Tregister;
  1302. begin
  1303. r.enum:=R_INTREGISTER;
  1304. r.number:=NR_GS;
  1305. { .... also the segment registers }
  1306. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1307. r.number:=NR_FS;
  1308. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1309. r.number:=NR_ES;
  1310. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1311. r.number:=NR_DS;
  1312. list.concat(Taicpu.Op_reg(A_PUSH,S_W,r));
  1313. { save the registers of an interrupt procedure }
  1314. r.number:=NR_EDI;
  1315. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1316. r.number:=NR_ESI;
  1317. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1318. r.number:=NR_EDX;
  1319. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1320. r.number:=NR_ECX;
  1321. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1322. r.number:=NR_EBX;
  1323. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1324. r.number:=NR_EAX;
  1325. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  1326. end;
  1327. procedure tcgx86.g_interrupt_stackframe_exit(list : taasmoutput;selfused,accused,acchiused:boolean);
  1328. var r:Tregister;
  1329. begin
  1330. r.enum:=R_INTREGISTER;
  1331. if accused then
  1332. begin
  1333. r.number:=NR_ESP;
  1334. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,r))
  1335. end
  1336. else
  1337. begin
  1338. r.number:=NR_EAX;
  1339. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1340. end;
  1341. r.number:=NR_EBX;
  1342. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1343. r.number:=NR_ECX;
  1344. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1345. if acchiused then
  1346. begin
  1347. r.number:=NR_ESP;
  1348. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,r))
  1349. end
  1350. else
  1351. begin
  1352. r.number:=NR_EDX;
  1353. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1354. end;
  1355. if selfused then
  1356. begin
  1357. r.number:=NR_ESP;
  1358. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,r))
  1359. end
  1360. else
  1361. begin
  1362. r.number:=NR_ESI;
  1363. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1364. end;
  1365. r.number:=NR_EDI;
  1366. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  1367. { .... also the segment registers }
  1368. r.number:=NR_DS;
  1369. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1370. r.number:=NR_ES;
  1371. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1372. r.number:=NR_FS;
  1373. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1374. r.number:=NR_GS;
  1375. list.concat(Taicpu.Op_reg(A_POP,S_W,r));
  1376. { this restores the flags }
  1377. list.concat(Taicpu.Op_none(A_IRET,S_NO));
  1378. end;
  1379. procedure tcgx86.g_profilecode(list : taasmoutput);
  1380. var
  1381. pl : tasmlabel;
  1382. r : Tregister;
  1383. begin
  1384. case target_info.system of
  1385. system_i386_win32,
  1386. system_i386_freebsd,
  1387. system_i386_wdosx,
  1388. system_i386_linux:
  1389. begin
  1390. objectlibrary.getaddrlabel(pl);
  1391. list.concat(Tai_section.Create(sec_data));
  1392. list.concat(Tai_align.Create(4));
  1393. list.concat(Tai_label.Create(pl));
  1394. list.concat(Tai_const.Create_32bit(0));
  1395. list.concat(Tai_section.Create(sec_code));
  1396. r.enum:=R_INTREGISTER;
  1397. r.number:=NR_EDX;
  1398. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,r));
  1399. a_call_name(list,target_info.Cprefix+'mcount');
  1400. include(rg.usedinproc,R_EDX);
  1401. end;
  1402. system_i386_go32v2:
  1403. begin
  1404. a_call_name(list,'MCOUNT');
  1405. end;
  1406. end;
  1407. end;
  1408. procedure tcgx86.g_stackpointer_alloc(list : taasmoutput;localsize : longint);
  1409. var
  1410. href : treference;
  1411. i : integer;
  1412. again : tasmlabel;
  1413. r,rsp : Tregister;
  1414. begin
  1415. r.enum:=R_INTREGISTER;
  1416. rsp.enum:=R_INTREGISTER;
  1417. rsp.number:=NR_ESP;
  1418. if localsize>0 then
  1419. begin
  1420. {$ifndef NOTARGETWIN32}
  1421. { windows guards only a few pages for stack growing, }
  1422. { so we have to access every page first }
  1423. if (target_info.system=system_i386_win32) and
  1424. (localsize>=winstackpagesize) then
  1425. begin
  1426. if localsize div winstackpagesize<=5 then
  1427. begin
  1428. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,rsp));
  1429. for i:=1 to localsize div winstackpagesize do
  1430. begin
  1431. reference_reset_base(href,rsp,localsize-i*winstackpagesize);
  1432. list.concat(Taicpu.op_const_ref(A_MOV,S_L,0,href));
  1433. end;
  1434. r.number:=NR_EAX;
  1435. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1436. end
  1437. else
  1438. begin
  1439. objectlibrary.getlabel(again);
  1440. r.number:=NR_EDI;
  1441. rg.getexplicitregisterint(list,R_EDI);
  1442. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,r));
  1443. a_label(list,again);
  1444. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1445. r.number:=NR_EAX;
  1446. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1447. r.number:=NR_EDI;
  1448. list.concat(Taicpu.op_reg(A_DEC,S_L,r));
  1449. a_jmp_cond(list,OC_NE,again);
  1450. r.enum:=R_EDI;
  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.31 2003-01-21 10:41:13 daniel
  1672. * Fixed another 200301081
  1673. Revision 1.30 2003/01/13 23:00:18 daniel
  1674. * Fixed internalerror
  1675. Revision 1.29 2003/01/13 14:54:34 daniel
  1676. * Further work to convert codegenerator register convention;
  1677. internalerror bug fixed.
  1678. Revision 1.28 2003/01/09 20:41:00 daniel
  1679. * Converted some code in cgx86.pas to new register numbering
  1680. Revision 1.27 2003/01/08 18:43:58 daniel
  1681. * Tregister changed into a record
  1682. Revision 1.26 2003/01/05 13:36:53 florian
  1683. * x86-64 compiles
  1684. + very basic support for float128 type (x86-64 only)
  1685. Revision 1.25 2003/01/02 16:17:50 peter
  1686. * align stack on 4 bytes in copyvalueopenarray
  1687. Revision 1.24 2002/12/24 15:56:50 peter
  1688. * stackpointer_alloc added for adjusting ESP. Win32 needs
  1689. this for the pageprotection
  1690. Revision 1.23 2002/11/25 18:43:34 carl
  1691. - removed the invalid if <> checking (Delphi is strange on this)
  1692. + implemented abstract warning on instance creation of class with
  1693. abstract methods.
  1694. * some error message cleanups
  1695. Revision 1.22 2002/11/25 17:43:29 peter
  1696. * splitted defbase in defutil,symutil,defcmp
  1697. * merged isconvertable and is_equal into compare_defs(_ext)
  1698. * made operator search faster by walking the list only once
  1699. Revision 1.21 2002/11/18 17:32:01 peter
  1700. * pass proccalloption to ret_in_xxx and push_xxx functions
  1701. Revision 1.20 2002/11/09 21:18:31 carl
  1702. * flags2reg() was not extending the byte register to the correct result size
  1703. Revision 1.19 2002/10/16 19:01:43 peter
  1704. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1705. implicit exception frames for procedures with initialized variables
  1706. and for constructors. The default is on for compatibility
  1707. Revision 1.18 2002/10/05 12:43:30 carl
  1708. * fixes for Delphi 6 compilation
  1709. (warning : Some features do not work under Delphi)
  1710. Revision 1.17 2002/09/17 18:54:06 jonas
  1711. * a_load_reg_reg() now has two size parameters: source and dest. This
  1712. allows some optimizations on architectures that don't encode the
  1713. register size in the register name.
  1714. Revision 1.16 2002/09/16 19:08:47 peter
  1715. * support references without registers and symbol in paramref_addr. It
  1716. pushes only the offset
  1717. Revision 1.15 2002/09/16 18:06:29 peter
  1718. * move CGSize2Opsize to interface
  1719. Revision 1.14 2002/09/01 14:42:41 peter
  1720. * removevaluepara added to fix the stackpointer so restoring of
  1721. saved registers works
  1722. Revision 1.13 2002/09/01 12:09:27 peter
  1723. + a_call_reg, a_call_loc added
  1724. * removed exprasmlist references
  1725. Revision 1.12 2002/08/17 09:23:50 florian
  1726. * first part of procinfo rewrite
  1727. Revision 1.11 2002/08/16 14:25:00 carl
  1728. * issameref() to test if two references are the same (then emit no opcodes)
  1729. + ret_in_reg to replace ret_in_acc
  1730. (fix some register allocation bugs at the same time)
  1731. + save_std_register now has an extra parameter which is the
  1732. usedinproc registers
  1733. Revision 1.10 2002/08/15 08:13:54 carl
  1734. - a_load_sym_ofs_reg removed
  1735. * loadvmt now calls loadaddr_ref_reg instead
  1736. Revision 1.9 2002/08/11 14:32:33 peter
  1737. * renamed current_library to objectlibrary
  1738. Revision 1.8 2002/08/11 13:24:20 peter
  1739. * saving of asmsymbols in ppu supported
  1740. * asmsymbollist global is removed and moved into a new class
  1741. tasmlibrarydata that will hold the info of a .a file which
  1742. corresponds with a single module. Added librarydata to tmodule
  1743. to keep the library info stored for the module. In the future the
  1744. objectfiles will also be stored to the tasmlibrarydata class
  1745. * all getlabel/newasmsymbol and friends are moved to the new class
  1746. Revision 1.7 2002/08/10 10:06:04 jonas
  1747. * fixed stupid bug of mine in g_flags2reg() when optimizations are on
  1748. Revision 1.6 2002/08/09 19:18:27 carl
  1749. * fix generic exception handling
  1750. Revision 1.5 2002/08/04 19:52:04 carl
  1751. + updated exception routines
  1752. Revision 1.4 2002/07/27 19:53:51 jonas
  1753. + generic implementation of tcg.g_flags2ref()
  1754. * tcg.flags2xxx() now also needs a size parameter
  1755. Revision 1.3 2002/07/26 21:15:46 florian
  1756. * rewrote the system handling
  1757. Revision 1.2 2002/07/21 16:55:34 jonas
  1758. * fixed bug in op_const_reg_reg() for imul
  1759. Revision 1.1 2002/07/20 19:28:47 florian
  1760. * splitting of i386\cgcpu.pas into x86\cgx86.pas and i386\cgcpu.pas
  1761. cgx86.pas will contain the common code for i386 and x86_64
  1762. }