cgx86.pas 65 KB

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