cgx86.pas 81 KB

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