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