cgx86.pas 65 KB

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