cgx86.pas 69 KB

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