cgx86.pas 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  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. globtype,
  25. cgbase,cgutils,cgobj,
  26. aasmbase,aasmtai,aasmcpu,
  27. cpubase,cpuinfo,rgobj,rgx86,rgcpu,
  28. symconst,symtype;
  29. type
  30. tcgx86 = class(tcg)
  31. rgfpu : Trgx86fpu;
  32. procedure done_register_allocators;override;
  33. function getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;override;
  34. function getmmxregister(list:Taasmoutput):Tregister;
  35. procedure getcpuregister(list:Taasmoutput;r:Tregister);override;
  36. procedure ungetcpuregister(list:Taasmoutput;r:Tregister);override;
  37. procedure alloccpuregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);override;
  38. procedure dealloccpuregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);override;
  39. function uses_registers(rt:Tregistertype):boolean;override;
  40. procedure add_reg_instruction(instr:Tai;r:tregister);override;
  41. procedure dec_fpu_stack;
  42. procedure inc_fpu_stack;
  43. procedure a_call_name(list : taasmoutput;const s : string);override;
  44. procedure a_call_reg(list : taasmoutput;reg : tregister);override;
  45. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  46. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference); override;
  47. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  48. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  49. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  50. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  51. size: tcgsize; a: aint; src, dst: tregister); override;
  52. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  53. size: tcgsize; src1, src2, dst: tregister); override;
  54. { move instructions }
  55. procedure a_load_const_reg(list : taasmoutput; tosize: tcgsize; a : aint;reg : tregister);override;
  56. procedure a_load_const_ref(list : taasmoutput; tosize: tcgsize; a : aint;const ref : treference);override;
  57. procedure a_load_reg_ref(list : taasmoutput;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);override;
  58. procedure a_load_ref_reg(list : taasmoutput;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);override;
  59. procedure a_load_reg_reg(list : taasmoutput;fromsize,tosize: tcgsize;reg1,reg2 : tregister);override;
  60. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  61. { fpu move instructions }
  62. procedure a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister); override;
  63. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  64. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  65. { vector register move instructions }
  66. procedure a_loadmm_reg_reg(list: taasmoutput; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  67. procedure a_loadmm_ref_reg(list: taasmoutput; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  68. procedure a_loadmm_reg_ref(list: taasmoutput; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  69. procedure a_opmm_ref_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  70. procedure a_opmm_reg_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);override;
  71. { comparison operations }
  72. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  73. l : tasmlabel);override;
  74. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  75. l : tasmlabel);override;
  76. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  77. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister; l : tasmlabel); override;
  78. procedure a_cmp_reg_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); override;
  79. procedure a_jmp_name(list : taasmoutput;const s : string);override;
  80. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  81. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  82. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); override;
  83. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref: TReference); override;
  84. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint);override;
  85. { entry/exit code helpers }
  86. procedure g_releasevaluepara_openarray(list : taasmoutput;const l:tlocation);override;
  87. procedure g_profilecode(list : taasmoutput);override;
  88. procedure g_stackpointer_alloc(list : taasmoutput;localsize : longint);override;
  89. procedure g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);override;
  90. procedure g_overflowcheck(list: taasmoutput; const l:tlocation;def:tdef);override;
  91. protected
  92. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  93. procedure check_register_size(size:tcgsize;reg:tregister);
  94. procedure opmm_loc_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  95. private
  96. procedure sizes2load(s1,s2 : tcgsize;var op: tasmop; var s3: topsize);
  97. procedure make_simple_ref(list:taasmoutput;var ref: treference);
  98. procedure floatload(list: taasmoutput; t : tcgsize;const ref : treference);
  99. procedure floatstore(list: taasmoutput; t : tcgsize;const ref : treference);
  100. procedure floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  101. procedure floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  102. end;
  103. function use_sse(def : tdef) : boolean;
  104. const
  105. {$ifdef x86_64}
  106. TCGSize2OpSize: Array[tcgsize] of topsize =
  107. (S_NO,S_B,S_W,S_L,S_Q,S_T,S_B,S_W,S_L,S_Q,S_Q,
  108. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  109. S_NO,S_NO,S_NO,S_MD,S_T,
  110. S_NO,S_NO,S_NO,S_NO,S_T);
  111. {$else x86_64}
  112. TCGSize2OpSize: Array[tcgsize] of topsize =
  113. (S_NO,S_B,S_W,S_L,S_L,S_T,S_B,S_W,S_L,S_L,S_L,
  114. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  115. S_NO,S_NO,S_NO,S_MD,S_T,
  116. S_NO,S_NO,S_NO,S_NO,S_T);
  117. {$endif x86_64}
  118. {$ifndef NOTARGETWIN32}
  119. winstackpagesize = 4096;
  120. {$endif NOTARGETWIN32}
  121. implementation
  122. uses
  123. globals,verbose,systems,cutils,
  124. dwarf,
  125. symdef,defutil,paramgr,procinfo;
  126. const
  127. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_ADD,A_AND,A_DIV,
  128. A_IDIV,A_MUL, A_IMUL, A_NEG,A_NOT,A_OR,
  129. A_SAR,A_SHL,A_SHR,A_SUB,A_XOR);
  130. TOpCmp2AsmCond: Array[topcmp] of TAsmCond = (C_NONE,
  131. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  132. function use_sse(def : tdef) : boolean;
  133. begin
  134. use_sse:=(is_single(def) and (aktfputype in sse_singlescalar)) or
  135. (is_double(def) and (aktfputype in sse_doublescalar));
  136. end;
  137. procedure Tcgx86.done_register_allocators;
  138. begin
  139. rg[R_INTREGISTER].free;
  140. rg[R_MMREGISTER].free;
  141. rg[R_MMXREGISTER].free;
  142. rgfpu.free;
  143. inherited done_register_allocators;
  144. end;
  145. function Tcgx86.getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;
  146. begin
  147. result:=rgfpu.getregisterfpu(list);
  148. end;
  149. function Tcgx86.getmmxregister(list:Taasmoutput):Tregister;
  150. begin
  151. if not assigned(rg[R_MMXREGISTER]) then
  152. internalerror(200312124);
  153. result:=rg[R_MMXREGISTER].getregister(list,R_SUBNONE);
  154. end;
  155. procedure Tcgx86.getcpuregister(list:Taasmoutput;r:Tregister);
  156. begin
  157. if getregtype(r)=R_FPUREGISTER then
  158. internalerror(2003121210)
  159. else
  160. inherited getcpuregister(list,r);
  161. end;
  162. procedure tcgx86.ungetcpuregister(list:Taasmoutput;r:Tregister);
  163. begin
  164. if getregtype(r)=R_FPUREGISTER then
  165. rgfpu.ungetregisterfpu(list,r)
  166. else
  167. inherited ungetcpuregister(list,r);
  168. end;
  169. procedure Tcgx86.alloccpuregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);
  170. begin
  171. if rt<>R_FPUREGISTER then
  172. inherited alloccpuregisters(list,rt,r);
  173. end;
  174. procedure Tcgx86.dealloccpuregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);
  175. begin
  176. if rt<>R_FPUREGISTER then
  177. inherited dealloccpuregisters(list,rt,r);
  178. end;
  179. function Tcgx86.uses_registers(rt:Tregistertype):boolean;
  180. begin
  181. if rt=R_FPUREGISTER then
  182. result:=false
  183. else
  184. result:=inherited uses_registers(rt);
  185. end;
  186. procedure tcgx86.add_reg_instruction(instr:Tai;r:tregister);
  187. begin
  188. if getregtype(r)<>R_FPUREGISTER then
  189. inherited add_reg_instruction(instr,r);
  190. end;
  191. procedure tcgx86.dec_fpu_stack;
  192. begin
  193. dec(rgfpu.fpuvaroffset);
  194. end;
  195. procedure tcgx86.inc_fpu_stack;
  196. begin
  197. inc(rgfpu.fpuvaroffset);
  198. end;
  199. {****************************************************************************
  200. This is private property, keep out! :)
  201. ****************************************************************************}
  202. procedure tcgx86.sizes2load(s1,s2 : tcgsize; var op: tasmop; var s3: topsize);
  203. begin
  204. case s2 of
  205. OS_8,OS_S8 :
  206. if S1 in [OS_8,OS_S8] then
  207. s3 := S_B
  208. else
  209. internalerror(200109221);
  210. OS_16,OS_S16:
  211. case s1 of
  212. OS_8,OS_S8:
  213. s3 := S_BW;
  214. OS_16,OS_S16:
  215. s3 := S_W;
  216. else
  217. internalerror(200109222);
  218. end;
  219. OS_32,OS_S32:
  220. case s1 of
  221. OS_8,OS_S8:
  222. s3 := S_BL;
  223. OS_16,OS_S16:
  224. s3 := S_WL;
  225. OS_32,OS_S32:
  226. s3 := S_L;
  227. else
  228. internalerror(200109223);
  229. end;
  230. {$ifdef x86_64}
  231. OS_64,OS_S64:
  232. case s1 of
  233. OS_8:
  234. s3 := S_BL;
  235. OS_S8:
  236. s3 := S_BQ;
  237. OS_16:
  238. s3 := S_WL;
  239. OS_S16:
  240. s3 := S_WQ;
  241. OS_32:
  242. s3 := S_L;
  243. OS_S32:
  244. s3 := S_LQ;
  245. OS_64,OS_S64:
  246. s3 := S_Q;
  247. else
  248. internalerror(200304302);
  249. end;
  250. {$endif x86_64}
  251. else
  252. internalerror(200109227);
  253. end;
  254. if s3 in [S_B,S_W,S_L,S_Q] then
  255. op := A_MOV
  256. else if s1 in [OS_8,OS_16,OS_32,OS_64] then
  257. op := A_MOVZX
  258. else
  259. {$ifdef x86_64}
  260. if s3 in [S_LQ] then
  261. op := A_MOVSXD
  262. else
  263. {$endif x86_64}
  264. op := A_MOVSX;
  265. end;
  266. procedure tcgx86.make_simple_ref(list:taasmoutput;var ref: treference);
  267. {$ifdef x86_64}
  268. var
  269. hreg : tregister;
  270. {$endif x86_64}
  271. begin
  272. {$ifdef x86_64}
  273. { Only 32bit is allowed }
  274. if ((ref.offset<low(longint)) or (ref.offset>high(longint))) then
  275. begin
  276. { Load constant value to register }
  277. hreg:=GetAddressRegister(list);
  278. list.concat(taicpu.op_const_reg(A_MOV,S_Q,ref.offset,hreg));
  279. ref.offset:=0;
  280. {if assigned(ref.symbol) then
  281. begin
  282. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_Q,ref.symbol,0,hreg));
  283. ref.symbol:=nil;
  284. end;}
  285. { Add register to reference }
  286. if ref.index=NR_NO then
  287. ref.index:=hreg
  288. else
  289. begin
  290. if ref.scalefactor<>0 then
  291. begin
  292. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.base,hreg));
  293. ref.base:=hreg;
  294. end
  295. else
  296. begin
  297. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.index,hreg));
  298. ref.index:=hreg;
  299. end;
  300. end;
  301. end;
  302. {$endif x86_64}
  303. end;
  304. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  305. begin
  306. case t of
  307. OS_F32 :
  308. begin
  309. op:=A_FLD;
  310. s:=S_FS;
  311. end;
  312. OS_F64 :
  313. begin
  314. op:=A_FLD;
  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. tmpref : treference;
  336. begin
  337. tmpref:=ref;
  338. make_simple_ref(list,tmpref);
  339. floatloadops(t,op,s);
  340. list.concat(Taicpu.Op_ref(op,s,tmpref));
  341. inc_fpu_stack;
  342. end;
  343. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  344. begin
  345. case t of
  346. OS_F32 :
  347. begin
  348. op:=A_FSTP;
  349. s:=S_FS;
  350. end;
  351. OS_F64 :
  352. begin
  353. op:=A_FSTP;
  354. s:=S_FL;
  355. end;
  356. OS_F80 :
  357. begin
  358. op:=A_FSTP;
  359. s:=S_FX;
  360. end;
  361. OS_C64 :
  362. begin
  363. op:=A_FISTP;
  364. s:=S_IQ;
  365. end;
  366. else
  367. internalerror(200204042);
  368. end;
  369. end;
  370. procedure tcgx86.floatstore(list: taasmoutput; t : tcgsize;const ref : treference);
  371. var
  372. op : tasmop;
  373. s : topsize;
  374. tmpref : treference;
  375. begin
  376. tmpref:=ref;
  377. make_simple_ref(list,tmpref);
  378. floatstoreops(t,op,s);
  379. list.concat(Taicpu.Op_ref(op,s,tmpref));
  380. dec_fpu_stack;
  381. end;
  382. procedure tcgx86.check_register_size(size:tcgsize;reg:tregister);
  383. begin
  384. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  385. internalerror(200306031);
  386. end;
  387. {****************************************************************************
  388. Assembler code
  389. ****************************************************************************}
  390. procedure tcgx86.a_jmp_name(list : taasmoutput;const s : string);
  391. begin
  392. list.concat(taicpu.op_sym(A_JMP,S_NO,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION)));
  393. end;
  394. procedure tcgx86.a_jmp_always(list : taasmoutput;l: tasmlabel);
  395. begin
  396. a_jmp_cond(list, OC_NONE, l);
  397. end;
  398. procedure tcgx86.a_call_name(list : taasmoutput;const s : string);
  399. begin
  400. list.concat(taicpu.op_sym(A_CALL,S_NO,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION)));
  401. end;
  402. procedure tcgx86.a_call_reg(list : taasmoutput;reg : tregister);
  403. begin
  404. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  405. end;
  406. {********************** load instructions ********************}
  407. procedure tcgx86.a_load_const_reg(list : taasmoutput; tosize: TCGSize; a : aint; reg : TRegister);
  408. begin
  409. check_register_size(tosize,reg);
  410. { the optimizer will change it to "xor reg,reg" when loading zero, }
  411. { no need to do it here too (JM) }
  412. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg))
  413. end;
  414. procedure tcgx86.a_load_const_ref(list : taasmoutput; tosize: tcgsize; a : aint;const ref : treference);
  415. var
  416. tmpref : treference;
  417. begin
  418. tmpref:=ref;
  419. make_simple_ref(list,tmpref);
  420. {$ifdef x86_64}
  421. { x86_64 only supports signed 32 bits constants directly }
  422. if (tosize in [OS_S64,OS_64]) and
  423. ((a<low(longint)) or (a>high(longint))) then
  424. begin
  425. a_load_const_ref(list,OS_32,longint(a and $ffffffff),tmpref);
  426. inc(tmpref.offset,4);
  427. a_load_const_ref(list,OS_32,longint(a shr 32),tmpref);
  428. end
  429. else
  430. {$endif x86_64}
  431. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  432. end;
  433. procedure tcgx86.a_load_reg_ref(list : taasmoutput; fromsize,tosize: TCGSize; reg : tregister;const ref : treference);
  434. var
  435. op: tasmop;
  436. s: topsize;
  437. tmpsize : tcgsize;
  438. tmpreg : tregister;
  439. tmpref : treference;
  440. begin
  441. tmpref:=ref;
  442. make_simple_ref(list,tmpref);
  443. check_register_size(fromsize,reg);
  444. sizes2load(fromsize,tosize,op,s);
  445. case s of
  446. {$ifdef x86_64}
  447. S_BQ,S_WQ,S_LQ,
  448. {$endif x86_64}
  449. S_BW,S_BL,S_WL :
  450. begin
  451. tmpreg:=getintregister(list,tosize);
  452. {$ifdef x86_64}
  453. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  454. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  455. 64 bit (FK) }
  456. if s in [S_BL,S_WL,S_L] then
  457. begin
  458. tmpreg:=makeregsize(list,tmpreg,OS_32);
  459. tmpsize:=OS_32;
  460. end
  461. else
  462. {$endif x86_64}
  463. tmpsize:=tosize;
  464. list.concat(taicpu.op_reg_reg(op,s,reg,tmpreg));
  465. a_load_reg_ref(list,tmpsize,tosize,tmpreg,tmpref);
  466. end;
  467. else
  468. list.concat(taicpu.op_reg_ref(op,s,reg,tmpref));
  469. end;
  470. end;
  471. procedure tcgx86.a_load_ref_reg(list : taasmoutput;fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  472. var
  473. op: tasmop;
  474. s: topsize;
  475. tmpref : treference;
  476. begin
  477. tmpref:=ref;
  478. make_simple_ref(list,tmpref);
  479. check_register_size(tosize,reg);
  480. sizes2load(fromsize,tosize,op,s);
  481. {$ifdef x86_64}
  482. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  483. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  484. 64 bit (FK) }
  485. if s in [S_BL,S_WL,S_L] then
  486. reg:=makeregsize(list,reg,OS_32);
  487. {$endif x86_64}
  488. list.concat(taicpu.op_ref_reg(op,s,tmpref,reg));
  489. end;
  490. procedure tcgx86.a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  491. var
  492. op: tasmop;
  493. s: topsize;
  494. instr:Taicpu;
  495. begin
  496. check_register_size(fromsize,reg1);
  497. check_register_size(tosize,reg2);
  498. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  499. begin
  500. reg1:=makeregsize(list,reg1,tosize);
  501. s:=tcgsize2opsize[tosize];
  502. op:=A_MOV;
  503. end
  504. else
  505. sizes2load(fromsize,tosize,op,s);
  506. {$ifdef x86_64}
  507. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  508. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  509. 64 bit (FK) }
  510. if s in [S_BL,S_WL,S_L] then
  511. reg2:=makeregsize(list,reg2,OS_32);
  512. {$endif x86_64}
  513. if (reg1<>reg2) then
  514. begin
  515. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  516. { Notify the register allocator that we have written a move instruction so
  517. it can try to eliminate it. }
  518. add_move_instruction(instr);
  519. list.concat(instr);
  520. end;
  521. end;
  522. procedure tcgx86.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  523. var
  524. tmpref : treference;
  525. begin
  526. with ref do
  527. if (base=NR_NO) and (index=NR_NO) then
  528. begin
  529. if assigned(ref.symbol) then
  530. begin
  531. tmpref:=ref;
  532. tmpref.refaddr:=ADDR_FULL;
  533. list.concat(Taicpu.op_ref_reg(A_MOV,tcgsize2opsize[OS_ADDR],tmpref,r));
  534. end
  535. else
  536. a_load_const_reg(list,OS_ADDR,offset,r);
  537. end
  538. else if (base=NR_NO) and (index<>NR_NO) and
  539. (offset=0) and (scalefactor=0) and (symbol=nil) then
  540. a_load_reg_reg(list,OS_ADDR,OS_ADDR,index,r)
  541. else if (base<>NR_NO) and (index=NR_NO) and
  542. (offset=0) and (symbol=nil) then
  543. a_load_reg_reg(list,OS_ADDR,OS_ADDR,base,r)
  544. else
  545. begin
  546. tmpref:=ref;
  547. make_simple_ref(list,tmpref);
  548. list.concat(taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  549. end;
  550. end;
  551. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  552. { R_ST means "the current value at the top of the fpu stack" (JM) }
  553. procedure tcgx86.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  554. begin
  555. if (reg1<>NR_ST) then
  556. begin
  557. list.concat(taicpu.op_reg(A_FLD,S_NO,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  558. inc_fpu_stack;
  559. end;
  560. if (reg2<>NR_ST) then
  561. begin
  562. list.concat(taicpu.op_reg(A_FSTP,S_NO,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  563. dec_fpu_stack;
  564. end;
  565. end;
  566. procedure tcgx86.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  567. begin
  568. floatload(list,size,ref);
  569. if (reg<>NR_ST) then
  570. a_loadfpu_reg_reg(list,size,NR_ST,reg);
  571. end;
  572. procedure tcgx86.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  573. begin
  574. if reg<>NR_ST then
  575. a_loadfpu_reg_reg(list,size,reg,NR_ST);
  576. floatstore(list,size,ref);
  577. end;
  578. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  579. const
  580. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  581. (A_MOVSS,A_CVTSS2SD,A_NONE,A_NONE,A_NONE),
  582. (A_CVTSD2SS,A_MOVSD,A_NONE,A_NONE,A_NONE),
  583. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  584. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  585. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  586. begin
  587. result:=convertop[fromsize,tosize];
  588. if result=A_NONE then
  589. internalerror(200312205);
  590. end;
  591. procedure tcgx86.a_loadmm_reg_reg(list: taasmoutput; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  592. var
  593. instr : taicpu;
  594. begin
  595. if shuffle=nil then
  596. begin
  597. if fromsize=tosize then
  598. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2)
  599. else
  600. internalerror(200312202);
  601. end
  602. else if shufflescalar(shuffle) then
  603. instr:=taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg1,reg2)
  604. else
  605. internalerror(200312201);
  606. case get_scalar_mm_op(fromsize,tosize) of
  607. A_MOVSS,
  608. A_MOVSD,
  609. A_MOVQ:
  610. add_move_instruction(instr);
  611. end;
  612. list.concat(instr);
  613. end;
  614. procedure tcgx86.a_loadmm_ref_reg(list: taasmoutput; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  615. var
  616. tmpref : treference;
  617. begin
  618. tmpref:=ref;
  619. make_simple_ref(list,tmpref);
  620. if shuffle=nil then
  621. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,reg))
  622. else if shufflescalar(shuffle) then
  623. list.concat(taicpu.op_ref_reg(get_scalar_mm_op(fromsize,tosize),S_NO,tmpref,reg))
  624. else
  625. internalerror(200312252);
  626. end;
  627. procedure tcgx86.a_loadmm_reg_ref(list: taasmoutput; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  628. var
  629. hreg : tregister;
  630. tmpref : treference;
  631. begin
  632. tmpref:=ref;
  633. make_simple_ref(list,tmpref);
  634. if shuffle=nil then
  635. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,tmpref))
  636. else if shufflescalar(shuffle) then
  637. begin
  638. if tosize<>fromsize then
  639. begin
  640. hreg:=getmmregister(list,tosize);
  641. list.concat(taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg,hreg));
  642. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize),S_NO,hreg,tmpref));
  643. end
  644. else
  645. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize),S_NO,reg,tmpref));
  646. end
  647. else
  648. internalerror(200312252);
  649. end;
  650. procedure tcgx86.a_opmm_ref_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  651. var
  652. l : tlocation;
  653. begin
  654. l.loc:=LOC_REFERENCE;
  655. l.reference:=ref;
  656. l.size:=size;
  657. opmm_loc_reg(list,op,size,l,reg,shuffle);
  658. end;
  659. procedure tcgx86.a_opmm_reg_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  660. var
  661. l : tlocation;
  662. begin
  663. l.loc:=LOC_MMREGISTER;
  664. l.register:=src;
  665. l.size:=size;
  666. opmm_loc_reg(list,op,size,l,dst,shuffle);
  667. end;
  668. procedure tcgx86.opmm_loc_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  669. const
  670. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  671. ( { scalar }
  672. ( { OS_F32 }
  673. A_NOP,A_ADDSS,A_NOP,A_DIVSS,A_NOP,A_NOP,A_MULSS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBSS,A_NOP
  674. ),
  675. ( { OS_F64 }
  676. A_NOP,A_ADDSD,A_NOP,A_DIVSD,A_NOP,A_NOP,A_MULSD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBSD,A_NOP
  677. )
  678. ),
  679. ( { vectorized/packed }
  680. { because the logical packed single instructions have shorter op codes, we use always
  681. these
  682. }
  683. ( { OS_F32 }
  684. A_NOP,A_ADDPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_XORPS
  685. ),
  686. ( { OS_F64 }
  687. A_NOP,A_ADDPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_XORPD
  688. )
  689. )
  690. );
  691. var
  692. resultreg : tregister;
  693. asmop : tasmop;
  694. begin
  695. { this is an internally used procedure so the parameters have
  696. some constrains
  697. }
  698. if loc.size<>size then
  699. internalerror(200312213);
  700. resultreg:=dst;
  701. { deshuffle }
  702. //!!!
  703. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  704. begin
  705. end
  706. else if (shuffle=nil) then
  707. asmop:=opmm2asmop[1,size,op]
  708. else if shufflescalar(shuffle) then
  709. begin
  710. asmop:=opmm2asmop[0,size,op];
  711. { no scalar operation available? }
  712. if asmop=A_NOP then
  713. begin
  714. { do vectorized and shuffle finally }
  715. //!!!
  716. end;
  717. end
  718. else
  719. internalerror(200312211);
  720. if asmop=A_NOP then
  721. internalerror(200312215);
  722. case loc.loc of
  723. LOC_CREFERENCE,LOC_REFERENCE:
  724. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  725. LOC_CMMREGISTER,LOC_MMREGISTER:
  726. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  727. else
  728. internalerror(200312214);
  729. end;
  730. { shuffle }
  731. if resultreg<>dst then
  732. begin
  733. internalerror(200312212);
  734. end;
  735. end;
  736. procedure tcgx86.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  737. var
  738. opcode : tasmop;
  739. power : longint;
  740. {$ifdef x86_64}
  741. tmpreg : tregister;
  742. {$endif x86_64}
  743. begin
  744. {$ifdef x86_64}
  745. { x86_64 only supports signed 32 bits constants directly }
  746. if (size in [OS_S64,OS_64]) and
  747. ((a<low(longint)) or (a>high(longint))) then
  748. begin
  749. tmpreg:=getintregister(list,size);
  750. a_load_const_reg(list,size,a,tmpreg);
  751. a_op_reg_reg(list,op,size,tmpreg,reg);
  752. exit;
  753. end;
  754. {$endif x86_64}
  755. check_register_size(size,reg);
  756. case op of
  757. OP_DIV, OP_IDIV:
  758. begin
  759. if ispowerof2(int64(a),power) then
  760. begin
  761. case op of
  762. OP_DIV:
  763. opcode := A_SHR;
  764. OP_IDIV:
  765. opcode := A_SAR;
  766. end;
  767. list.concat(taicpu.op_const_reg(opcode,TCgSize2OpSize[size],power,reg));
  768. exit;
  769. end;
  770. { the rest should be handled specifically in the code }
  771. { generator because of the silly register usage restraints }
  772. internalerror(200109224);
  773. end;
  774. OP_MUL,OP_IMUL:
  775. begin
  776. if not(cs_check_overflow in aktlocalswitches) and
  777. ispowerof2(int64(a),power) then
  778. begin
  779. list.concat(taicpu.op_const_reg(A_SHL,TCgSize2OpSize[size],power,reg));
  780. exit;
  781. end;
  782. if op = OP_IMUL then
  783. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  784. else
  785. { OP_MUL should be handled specifically in the code }
  786. { generator because of the silly register usage restraints }
  787. internalerror(200109225);
  788. end;
  789. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  790. if not(cs_check_overflow in aktlocalswitches) and
  791. (a = 1) and
  792. (op in [OP_ADD,OP_SUB]) then
  793. if op = OP_ADD then
  794. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  795. else
  796. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  797. else if (a = 0) then
  798. if (op <> OP_AND) then
  799. exit
  800. else
  801. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],0,reg))
  802. else if (aword(a) = high(aword)) and
  803. (op in [OP_AND,OP_OR,OP_XOR]) then
  804. begin
  805. case op of
  806. OP_AND:
  807. exit;
  808. OP_OR:
  809. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],aint(high(aword)),reg));
  810. OP_XOR:
  811. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg));
  812. end
  813. end
  814. else
  815. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  816. OP_SHL,OP_SHR,OP_SAR:
  817. begin
  818. if (a and 31) <> 0 Then
  819. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  820. if (a shr 5) <> 0 Then
  821. internalerror(68991);
  822. end
  823. else internalerror(68992);
  824. end;
  825. end;
  826. procedure tcgx86.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference);
  827. var
  828. opcode: tasmop;
  829. power: longint;
  830. {$ifdef x86_64}
  831. tmpreg : tregister;
  832. {$endif x86_64}
  833. tmpref : treference;
  834. begin
  835. tmpref:=ref;
  836. make_simple_ref(list,tmpref);
  837. {$ifdef x86_64}
  838. { x86_64 only supports signed 32 bits constants directly }
  839. if (size in [OS_S64,OS_64]) and
  840. ((a<low(longint)) or (a>high(longint))) then
  841. begin
  842. tmpreg:=getintregister(list,size);
  843. a_load_const_reg(list,size,a,tmpreg);
  844. a_op_reg_ref(list,op,size,tmpreg,tmpref);
  845. exit;
  846. end;
  847. {$endif x86_64}
  848. Case Op of
  849. OP_DIV, OP_IDIV:
  850. Begin
  851. if ispowerof2(int64(a),power) then
  852. begin
  853. case op of
  854. OP_DIV:
  855. opcode := A_SHR;
  856. OP_IDIV:
  857. opcode := A_SAR;
  858. end;
  859. list.concat(taicpu.op_const_ref(opcode,
  860. TCgSize2OpSize[size],power,tmpref));
  861. exit;
  862. end;
  863. { the rest should be handled specifically in the code }
  864. { generator because of the silly register usage restraints }
  865. internalerror(200109231);
  866. End;
  867. OP_MUL,OP_IMUL:
  868. begin
  869. if not(cs_check_overflow in aktlocalswitches) and
  870. ispowerof2(int64(a),power) then
  871. begin
  872. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  873. power,tmpref));
  874. exit;
  875. end;
  876. { can't multiply a memory location directly with a constant }
  877. if op = OP_IMUL then
  878. inherited a_op_const_ref(list,op,size,a,tmpref)
  879. else
  880. { OP_MUL should be handled specifically in the code }
  881. { generator because of the silly register usage restraints }
  882. internalerror(200109232);
  883. end;
  884. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  885. if not(cs_check_overflow in aktlocalswitches) and
  886. (a = 1) and
  887. (op in [OP_ADD,OP_SUB]) then
  888. if op = OP_ADD then
  889. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  890. else
  891. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  892. else if (a = 0) then
  893. if (op <> OP_AND) then
  894. exit
  895. else
  896. a_load_const_ref(list,size,0,tmpref)
  897. else if (aword(a) = high(aword)) and
  898. (op in [OP_AND,OP_OR,OP_XOR]) then
  899. begin
  900. case op of
  901. OP_AND:
  902. exit;
  903. OP_OR:
  904. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],aint(high(aword)),tmpref));
  905. OP_XOR:
  906. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref));
  907. end
  908. end
  909. else
  910. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  911. TCgSize2OpSize[size],a,tmpref));
  912. OP_SHL,OP_SHR,OP_SAR:
  913. begin
  914. if (a and 31) <> 0 then
  915. list.concat(taicpu.op_const_ref(
  916. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  917. if (a shr 5) <> 0 Then
  918. internalerror(68991);
  919. end
  920. else internalerror(68992);
  921. end;
  922. end;
  923. procedure tcgx86.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  924. var
  925. dstsize: topsize;
  926. instr:Taicpu;
  927. begin
  928. check_register_size(size,src);
  929. check_register_size(size,dst);
  930. dstsize := tcgsize2opsize[size];
  931. case op of
  932. OP_NEG,OP_NOT:
  933. begin
  934. if src<>dst then
  935. a_load_reg_reg(list,size,size,src,dst);
  936. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  937. end;
  938. OP_MUL,OP_DIV,OP_IDIV:
  939. { special stuff, needs separate handling inside code }
  940. { generator }
  941. internalerror(200109233);
  942. OP_SHR,OP_SHL,OP_SAR:
  943. begin
  944. getcpuregister(list,NR_CL);
  945. a_load_reg_reg(list,OS_8,OS_8,makeregsize(list,src,OS_8),NR_CL);
  946. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,src));
  947. ungetcpuregister(list,NR_CL);
  948. end;
  949. else
  950. begin
  951. if reg2opsize(src) <> dstsize then
  952. internalerror(200109226);
  953. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  954. list.concat(instr);
  955. end;
  956. end;
  957. end;
  958. procedure tcgx86.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  959. var
  960. tmpref : treference;
  961. begin
  962. tmpref:=ref;
  963. make_simple_ref(list,tmpref);
  964. check_register_size(size,reg);
  965. case op of
  966. OP_NEG,OP_NOT,OP_IMUL:
  967. begin
  968. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  969. end;
  970. OP_MUL,OP_DIV,OP_IDIV:
  971. { special stuff, needs separate handling inside code }
  972. { generator }
  973. internalerror(200109239);
  974. else
  975. begin
  976. reg := makeregsize(list,reg,size);
  977. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  978. end;
  979. end;
  980. end;
  981. procedure tcgx86.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  982. var
  983. tmpref : treference;
  984. begin
  985. tmpref:=ref;
  986. make_simple_ref(list,tmpref);
  987. check_register_size(size,reg);
  988. case op of
  989. OP_NEG,OP_NOT:
  990. begin
  991. if reg<>NR_NO then
  992. internalerror(200109237);
  993. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  994. end;
  995. OP_IMUL:
  996. begin
  997. { this one needs a load/imul/store, which is the default }
  998. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  999. end;
  1000. OP_MUL,OP_DIV,OP_IDIV:
  1001. { special stuff, needs separate handling inside code }
  1002. { generator }
  1003. internalerror(200109238);
  1004. else
  1005. begin
  1006. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  1007. end;
  1008. end;
  1009. end;
  1010. procedure tcgx86.a_op_const_reg_reg(list: taasmoutput; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister);
  1011. var
  1012. tmpref: treference;
  1013. power: longint;
  1014. {$ifdef x86_64}
  1015. tmpreg : tregister;
  1016. {$endif x86_64}
  1017. begin
  1018. {$ifdef x86_64}
  1019. { x86_64 only supports signed 32 bits constants directly }
  1020. if (size in [OS_S64,OS_64]) and
  1021. ((a<low(longint)) or (a>high(longint))) then
  1022. begin
  1023. tmpreg:=getintregister(list,size);
  1024. a_load_const_reg(list,size,a,tmpreg);
  1025. a_op_reg_reg_reg(list,op,size,tmpreg,src,dst);
  1026. exit;
  1027. end;
  1028. {$endif x86_64}
  1029. check_register_size(size,src);
  1030. check_register_size(size,dst);
  1031. if tcgsize2size[size]<>tcgsize2size[OS_INT] then
  1032. begin
  1033. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1034. exit;
  1035. end;
  1036. { if we get here, we have to do a 32 bit calculation, guaranteed }
  1037. case op of
  1038. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  1039. OP_SAR:
  1040. { can't do anything special for these }
  1041. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1042. OP_IMUL:
  1043. begin
  1044. if not(cs_check_overflow in aktlocalswitches) and
  1045. ispowerof2(int64(a),power) then
  1046. { can be done with a shift }
  1047. begin
  1048. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1049. exit;
  1050. end;
  1051. list.concat(taicpu.op_const_reg_reg(A_IMUL,tcgsize2opsize[size],a,src,dst));
  1052. end;
  1053. OP_ADD, OP_SUB:
  1054. if (a = 0) then
  1055. a_load_reg_reg(list,size,size,src,dst)
  1056. else
  1057. begin
  1058. reference_reset(tmpref);
  1059. tmpref.base := src;
  1060. tmpref.offset := longint(a);
  1061. if op = OP_SUB then
  1062. tmpref.offset := -tmpref.offset;
  1063. list.concat(taicpu.op_ref_reg(A_LEA,tcgsize2opsize[size],tmpref,dst));
  1064. end
  1065. else internalerror(200112302);
  1066. end;
  1067. end;
  1068. procedure tcgx86.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;size: tcgsize; src1, src2, dst: tregister);
  1069. var
  1070. tmpref: treference;
  1071. begin
  1072. check_register_size(size,src1);
  1073. check_register_size(size,src2);
  1074. check_register_size(size,dst);
  1075. if tcgsize2size[size]<>tcgsize2size[OS_INT] then
  1076. begin
  1077. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1078. exit;
  1079. end;
  1080. { if we get here, we have to do a 32 bit calculation, guaranteed }
  1081. Case Op of
  1082. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  1083. OP_SAR,OP_SUB,OP_NOT,OP_NEG:
  1084. { can't do anything special for these }
  1085. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1086. OP_IMUL:
  1087. list.concat(taicpu.op_reg_reg_reg(A_IMUL,tcgsize2opsize[size],src1,src2,dst));
  1088. OP_ADD:
  1089. begin
  1090. reference_reset(tmpref);
  1091. tmpref.base := src1;
  1092. tmpref.index := src2;
  1093. tmpref.scalefactor := 1;
  1094. list.concat(taicpu.op_ref_reg(A_LEA,tcgsize2opsize[size],tmpref,dst));
  1095. end
  1096. else internalerror(200112303);
  1097. end;
  1098. end;
  1099. {*************** compare instructructions ****************}
  1100. procedure tcgx86.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  1101. l : tasmlabel);
  1102. {$ifdef x86_64}
  1103. var
  1104. tmpreg : tregister;
  1105. {$endif x86_64}
  1106. begin
  1107. {$ifdef x86_64}
  1108. { x86_64 only supports signed 32 bits constants directly }
  1109. if (size in [OS_S64,OS_64]) and
  1110. ((a<low(longint)) or (a>high(longint))) then
  1111. begin
  1112. tmpreg:=getintregister(list,size);
  1113. a_load_const_reg(list,size,a,tmpreg);
  1114. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1115. exit;
  1116. end;
  1117. {$endif x86_64}
  1118. if (a = 0) then
  1119. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  1120. else
  1121. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  1122. a_jmp_cond(list,cmp_op,l);
  1123. end;
  1124. procedure tcgx86.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  1125. l : tasmlabel);
  1126. var
  1127. {$ifdef x86_64}
  1128. tmpreg : tregister;
  1129. {$endif x86_64}
  1130. tmpref : treference;
  1131. begin
  1132. tmpref:=ref;
  1133. make_simple_ref(list,tmpref);
  1134. {$ifdef x86_64}
  1135. { x86_64 only supports signed 32 bits constants directly }
  1136. if (size in [OS_S64,OS_64]) and
  1137. ((a<low(longint)) or (a>high(longint))) then
  1138. begin
  1139. tmpreg:=getintregister(list,size);
  1140. a_load_const_reg(list,size,a,tmpreg);
  1141. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  1142. exit;
  1143. end;
  1144. {$endif x86_64}
  1145. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  1146. a_jmp_cond(list,cmp_op,l);
  1147. end;
  1148. procedure tcgx86.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  1149. reg1,reg2 : tregister;l : tasmlabel);
  1150. begin
  1151. check_register_size(size,reg1);
  1152. check_register_size(size,reg2);
  1153. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  1154. a_jmp_cond(list,cmp_op,l);
  1155. end;
  1156. procedure tcgx86.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  1157. var
  1158. tmpref : treference;
  1159. begin
  1160. tmpref:=ref;
  1161. make_simple_ref(list,tmpref);
  1162. check_register_size(size,reg);
  1163. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  1164. a_jmp_cond(list,cmp_op,l);
  1165. end;
  1166. procedure tcgx86.a_cmp_reg_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  1167. var
  1168. tmpref : treference;
  1169. begin
  1170. tmpref:=ref;
  1171. make_simple_ref(list,tmpref);
  1172. check_register_size(size,reg);
  1173. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  1174. a_jmp_cond(list,cmp_op,l);
  1175. end;
  1176. procedure tcgx86.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  1177. var
  1178. ai : taicpu;
  1179. begin
  1180. if cond=OC_None then
  1181. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1182. else
  1183. begin
  1184. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  1185. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1186. end;
  1187. ai.is_jmp:=true;
  1188. list.concat(ai);
  1189. end;
  1190. procedure tcgx86.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  1191. var
  1192. ai : taicpu;
  1193. begin
  1194. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  1195. ai.SetCondition(flags_to_cond(f));
  1196. ai.is_jmp := true;
  1197. list.concat(ai);
  1198. end;
  1199. procedure tcgx86.g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister);
  1200. var
  1201. ai : taicpu;
  1202. hreg : tregister;
  1203. begin
  1204. hreg:=makeregsize(list,reg,OS_8);
  1205. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  1206. ai.setcondition(flags_to_cond(f));
  1207. list.concat(ai);
  1208. if (reg<>hreg) then
  1209. a_load_reg_reg(list,OS_8,size,hreg,reg);
  1210. end;
  1211. procedure tcgx86.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref: TReference);
  1212. var
  1213. ai : taicpu;
  1214. tmpref : treference;
  1215. begin
  1216. tmpref:=ref;
  1217. make_simple_ref(list,tmpref);
  1218. if not(size in [OS_8,OS_S8]) then
  1219. a_load_const_ref(list,size,0,tmpref);
  1220. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  1221. ai.setcondition(flags_to_cond(f));
  1222. list.concat(ai);
  1223. end;
  1224. { ************* concatcopy ************ }
  1225. procedure Tcgx86.g_concatcopy(list:Taasmoutput;const source,dest:Treference;len:aint);
  1226. const
  1227. {$ifdef cpu64bit}
  1228. REGCX=NR_RCX;
  1229. REGSI=NR_RSI;
  1230. REGDI=NR_RDI;
  1231. {$else cpu64bit}
  1232. REGCX=NR_ECX;
  1233. REGSI=NR_ESI;
  1234. REGDI=NR_EDI;
  1235. {$endif cpu64bit}
  1236. type copymode=(copy_move,copy_mmx,copy_string);
  1237. var srcref,dstref:Treference;
  1238. r,r0,r1,r2,r3:Tregister;
  1239. helpsize:aint;
  1240. copysize:byte;
  1241. cgsize:Tcgsize;
  1242. cm:copymode;
  1243. begin
  1244. cm:=copy_move;
  1245. helpsize:=12;
  1246. if cs_littlesize in aktglobalswitches then
  1247. helpsize:=8;
  1248. if (cs_mmx in aktlocalswitches) and
  1249. not(pi_uses_fpu in current_procinfo.flags) and
  1250. ((len=8) or (len=16) or (len=24) or (len=32)) then
  1251. cm:=copy_mmx;
  1252. if (len>helpsize) then
  1253. cm:=copy_string;
  1254. if (cs_littlesize in aktglobalswitches) and
  1255. not((len<=16) and (cm=copy_mmx)) then
  1256. cm:=copy_string;
  1257. case cm of
  1258. copy_move:
  1259. begin
  1260. dstref:=dest;
  1261. srcref:=source;
  1262. copysize:=sizeof(aint);
  1263. cgsize:=int_cgsize(copysize);
  1264. while len<>0 do
  1265. begin
  1266. if len<2 then
  1267. begin
  1268. copysize:=1;
  1269. cgsize:=OS_8;
  1270. end
  1271. else if len<4 then
  1272. begin
  1273. copysize:=2;
  1274. cgsize:=OS_16;
  1275. end
  1276. else if len<8 then
  1277. begin
  1278. copysize:=4;
  1279. cgsize:=OS_32;
  1280. end;
  1281. dec(len,copysize);
  1282. r:=getintregister(list,cgsize);
  1283. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1284. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1285. inc(srcref.offset,copysize);
  1286. inc(dstref.offset,copysize);
  1287. end;
  1288. end;
  1289. copy_mmx:
  1290. begin
  1291. dstref:=dest;
  1292. srcref:=source;
  1293. r0:=getmmxregister(list);
  1294. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  1295. if len>=16 then
  1296. begin
  1297. inc(srcref.offset,8);
  1298. r1:=getmmxregister(list);
  1299. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  1300. end;
  1301. if len>=24 then
  1302. begin
  1303. inc(srcref.offset,8);
  1304. r2:=getmmxregister(list);
  1305. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  1306. end;
  1307. if len>=32 then
  1308. begin
  1309. inc(srcref.offset,8);
  1310. r3:=getmmxregister(list);
  1311. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  1312. end;
  1313. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  1314. if len>=16 then
  1315. begin
  1316. inc(dstref.offset,8);
  1317. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  1318. end;
  1319. if len>=24 then
  1320. begin
  1321. inc(dstref.offset,8);
  1322. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  1323. end;
  1324. if len>=32 then
  1325. begin
  1326. inc(dstref.offset,8);
  1327. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  1328. end;
  1329. end
  1330. else {copy_string, should be a good fallback in case of unhandled}
  1331. begin
  1332. getcpuregister(list,REGDI);
  1333. a_loadaddr_ref_reg(list,dest,REGDI);
  1334. getcpuregister(list,REGSI);
  1335. a_loadaddr_ref_reg(list,source,REGSI);
  1336. getcpuregister(list,REGCX);
  1337. list.concat(Taicpu.op_none(A_CLD,S_NO));
  1338. if cs_littlesize in aktglobalswitches then
  1339. begin
  1340. a_load_const_reg(list,OS_INT,len,REGCX);
  1341. list.concat(Taicpu.op_none(A_REP,S_NO));
  1342. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1343. end
  1344. else
  1345. begin
  1346. helpsize:=len div sizeof(aint);
  1347. len:=len mod sizeof(aint);
  1348. if helpsize>1 then
  1349. begin
  1350. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  1351. list.concat(Taicpu.op_none(A_REP,S_NO));
  1352. end;
  1353. if helpsize>0 then
  1354. begin
  1355. {$ifdef cpu64bit}
  1356. if sizeof(aint)=8 then
  1357. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  1358. else
  1359. {$endif cpu64bit}
  1360. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1361. end;
  1362. if len>=4 then
  1363. begin
  1364. dec(len,4);
  1365. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1366. end;
  1367. if len>=2 then
  1368. begin
  1369. dec(len,2);
  1370. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  1371. end;
  1372. if len=1 then
  1373. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1374. end;
  1375. ungetcpuregister(list,REGCX);
  1376. ungetcpuregister(list,REGSI);
  1377. ungetcpuregister(list,REGDI);
  1378. end;
  1379. end;
  1380. end;
  1381. {****************************************************************************
  1382. Entry/Exit Code Helpers
  1383. ****************************************************************************}
  1384. procedure tcgx86.g_releasevaluepara_openarray(list : taasmoutput;const l:tlocation);
  1385. begin
  1386. { Nothing to release }
  1387. end;
  1388. procedure tcgx86.g_profilecode(list : taasmoutput);
  1389. var
  1390. pl : tasmlabel;
  1391. mcountprefix : String[4];
  1392. begin
  1393. case target_info.system of
  1394. {$ifndef NOTARGETWIN32}
  1395. system_i386_win32,
  1396. {$endif}
  1397. system_i386_freebsd,
  1398. system_i386_netbsd,
  1399. // system_i386_openbsd,
  1400. system_i386_wdosx :
  1401. begin
  1402. Case target_info.system Of
  1403. system_i386_freebsd : mcountprefix:='.';
  1404. system_i386_netbsd : mcountprefix:='__';
  1405. // system_i386_openbsd : mcountprefix:='.';
  1406. else
  1407. mcountPrefix:='';
  1408. end;
  1409. objectlibrary.getaddrlabel(pl);
  1410. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(aint));
  1411. list.concat(Tai_label.Create(pl));
  1412. list.concat(Tai_const.Create_32bit(0));
  1413. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1414. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1415. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  1416. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount');
  1417. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  1418. end;
  1419. system_i386_linux:
  1420. a_call_name(list,target_info.Cprefix+'mcount');
  1421. system_i386_go32v2,system_i386_watcom:
  1422. begin
  1423. a_call_name(list,'MCOUNT');
  1424. end;
  1425. system_x86_64_linux:
  1426. begin
  1427. a_call_name(list,'mcount');
  1428. end;
  1429. end;
  1430. end;
  1431. procedure tcgx86.g_stackpointer_alloc(list : taasmoutput;localsize : longint);
  1432. {$ifdef i386}
  1433. {$ifndef NOTARGETWIN32}
  1434. var
  1435. href : treference;
  1436. i : integer;
  1437. again : tasmlabel;
  1438. {$endif NOTARGETWIN32}
  1439. {$endif i386}
  1440. begin
  1441. if localsize>0 then
  1442. begin
  1443. {$ifdef i386}
  1444. {$ifndef NOTARGETWIN32}
  1445. { windows guards only a few pages for stack growing, }
  1446. { so we have to access every page first }
  1447. if (target_info.system=system_i386_win32) and
  1448. (localsize>=winstackpagesize) then
  1449. begin
  1450. if localsize div winstackpagesize<=5 then
  1451. begin
  1452. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,NR_ESP));
  1453. for i:=1 to localsize div winstackpagesize do
  1454. begin
  1455. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize);
  1456. list.concat(Taicpu.op_const_ref(A_MOV,S_L,0,href));
  1457. end;
  1458. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1459. end
  1460. else
  1461. begin
  1462. objectlibrary.getlabel(again);
  1463. getcpuregister(list,NR_EDI);
  1464. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  1465. a_label(list,again);
  1466. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  1467. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1468. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI));
  1469. a_jmp_cond(list,OC_NE,again);
  1470. ungetcpuregister(list,NR_EDI);
  1471. list.concat(Taicpu.op_const_reg(A_SUB,S_L,localsize mod winstackpagesize,NR_ESP));
  1472. end
  1473. end
  1474. else
  1475. {$endif NOTARGETWIN32}
  1476. {$endif i386}
  1477. list.concat(Taicpu.Op_const_reg(A_SUB,tcgsize2opsize[OS_ADDR],localsize,NR_STACK_POINTER_REG));
  1478. end;
  1479. end;
  1480. procedure tcgx86.g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);
  1481. begin
  1482. {$ifdef i386}
  1483. { interrupt support for i386 }
  1484. if (po_interrupt in current_procinfo.procdef.procoptions) then
  1485. begin
  1486. { .... also the segment registers }
  1487. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  1488. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  1489. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  1490. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  1491. { save the registers of an interrupt procedure }
  1492. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  1493. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  1494. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1495. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  1496. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  1497. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  1498. end;
  1499. {$endif i386}
  1500. { save old framepointer }
  1501. if not nostackframe then
  1502. begin
  1503. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1504. CGmessage(cg_d_stackframe_omited)
  1505. else
  1506. begin
  1507. list.concat(tai_regalloc.alloc(NR_FRAME_POINTER_REG,nil));
  1508. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  1509. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  1510. { Return address and FP are both on stack }
  1511. dwarfcfi.cfa_def_cfa_offset(list,2*sizeof(aint));
  1512. dwarfcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(aint)));
  1513. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG));
  1514. dwarfcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  1515. end;
  1516. { allocate stackframe space }
  1517. if localsize<>0 then
  1518. begin
  1519. cg.g_stackpointer_alloc(list,localsize);
  1520. end;
  1521. end;
  1522. { allocate PIC register }
  1523. if (cs_create_pic in aktmoduleswitches) and
  1524. (tf_pic_uses_got in target_info.flags) then
  1525. begin
  1526. a_call_name(list,'FPC_GETEIPINEBX');
  1527. list.concat(taicpu.op_sym_ofs_reg(A_ADD,tcgsize2opsize[OS_ADDR],objectlibrary.newasmsymbol('_GLOBAL_OFFSET_TABLE_',AB_EXTERNAL,AT_DATA),0,NR_PIC_OFFSET_REG));
  1528. list.concat(tai_regalloc.alloc(NR_PIC_OFFSET_REG,nil));
  1529. end;
  1530. end;
  1531. { produces if necessary overflowcode }
  1532. procedure tcgx86.g_overflowcheck(list: taasmoutput; const l:tlocation;def:tdef);
  1533. var
  1534. hl : tasmlabel;
  1535. ai : taicpu;
  1536. cond : TAsmCond;
  1537. begin
  1538. if not(cs_check_overflow in aktlocalswitches) then
  1539. exit;
  1540. objectlibrary.getlabel(hl);
  1541. if not ((def.deftype=pointerdef) or
  1542. ((def.deftype=orddef) and
  1543. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1544. bool8bit,bool16bit,bool32bit]))) then
  1545. cond:=C_NO
  1546. else
  1547. cond:=C_NB;
  1548. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  1549. ai.SetCondition(cond);
  1550. ai.is_jmp:=true;
  1551. list.concat(ai);
  1552. a_call_name(list,'FPC_OVERFLOW');
  1553. a_label(list,hl);
  1554. end;
  1555. end.
  1556. {
  1557. $Log$
  1558. Revision 1.142 2005-01-25 18:48:15 peter
  1559. * tf_pic_uses_got added
  1560. Revision 1.141 2005/01/08 16:00:55 florian
  1561. * fixed loadaddr; I wonder how it ever worked
  1562. Revision 1.140 2004/12/12 10:50:35 florian
  1563. * fixed operand size calculation for sse operands
  1564. + all nasm assembler targets to help page output added
  1565. Revision 1.139 2004/11/08 20:23:29 florian
  1566. * fixed open arrays when using register variables
  1567. Revision 1.138 2004/11/02 20:50:54 florian
  1568. + added profiler call for x86_64
  1569. Revision 1.137 2004/11/02 18:23:16 florian
  1570. * fixed -<sse register>
  1571. * information about simple moves for sse is given to the register allocator
  1572. Revision 1.136 2004/11/01 23:30:11 peter
  1573. * support > 32bit accesses for x86_64
  1574. * rewrote array size checking to support 64bit
  1575. Revision 1.135 2004/11/01 15:42:47 florian
  1576. * cvt*2* can't write to memory location, fixed
  1577. Revision 1.134 2004/11/01 10:30:06 peter
  1578. * fixed uninited var in a_load_reg_ref
  1579. Revision 1.133 2004/10/31 21:45:04 peter
  1580. * generic tlocation
  1581. * move tlocation to cgutils
  1582. Revision 1.132 2004/10/25 15:36:47 peter
  1583. * save standard registers moved to tcgobj
  1584. Revision 1.131 2004/10/24 20:10:08 peter
  1585. * -Or fixes
  1586. Revision 1.130 2004/10/24 11:44:28 peter
  1587. * small regvar fixes
  1588. * loadref parameter removed from concatcopy,incrrefcount,etc
  1589. Revision 1.129 2004/10/06 19:27:35 jonas
  1590. * regvar fixes from Peter
  1591. Revision 1.128 2004/10/05 20:41:02 peter
  1592. * more spilling rewrites
  1593. Revision 1.127 2004/10/04 20:46:22 peter
  1594. * spilling code rewritten for x86. It now used the generic
  1595. spilling routines. Special x86 optimization still needs
  1596. to be added.
  1597. * Spilling fixed when both operands needed to be spilled
  1598. * Cleanup of spilling routine, do_spill_readwritten removed
  1599. Revision 1.126 2004/10/03 12:42:22 florian
  1600. * made sqrt, sqr and abs internal for the sparc
  1601. Revision 1.125 2004/09/25 14:23:55 peter
  1602. * ungetregister is now only used for cpuregisters, renamed to
  1603. ungetcpuregister
  1604. * renamed (get|unget)explicitregister(s) to ..cpuregister
  1605. * removed location-release/reference_release
  1606. Revision 1.124 2004/06/20 08:55:32 florian
  1607. * logs truncated
  1608. Revision 1.123 2004/06/16 20:07:11 florian
  1609. * dwarf branch merged
  1610. Revision 1.122 2004/05/22 23:34:28 peter
  1611. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  1612. Revision 1.121 2004/04/28 15:19:03 florian
  1613. + syscall directive support for MorphOS added
  1614. Revision 1.120 2004/04/09 14:36:05 peter
  1615. * A_MOVSL renamed to A_MOVSD
  1616. Revision 1.119.2.22 2004/05/28 20:29:50 florian
  1617. * fixed currency trouble on x86-64
  1618. }