cgx86.pas 71 KB

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