cgx86.pas 64 KB

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