cgx86.pas 65 KB

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