cgx86.pas 72 KB

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