cgx86.pas 70 KB

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