cgx86.pas 71 KB

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