cgx86.pas 75 KB

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