cgx86.pas 74 KB

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