cgx86.pas 72 KB

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