cgx86.pas 72 KB

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