cgx86.pas 73 KB

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