cgx86.pas 73 KB

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