cgx86.pas 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the common parts of the code generator for the i386 and the x86-64.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { This unit implements the common parts of the code generator for the i386 and the x86-64.
  19. }
  20. unit cgx86;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cgbase,cgobj,
  25. aasmbase,aasmtai,aasmcpu,
  26. cpubase,cpuinfo,rgobj,rgcpu,
  27. symconst,symtype;
  28. type
  29. tcgx86 = class(tcg)
  30. rgint,
  31. rgmm : trgcpu;
  32. rgfpu : Trgx86fpu;
  33. procedure init_register_allocators;override;
  34. procedure done_register_allocators;override;
  35. function getintregister(list:Taasmoutput;size:Tcgsize):Tregister;override;
  36. function getaddressregister(list:Taasmoutput):Tregister;override;
  37. function getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;override;
  38. function getmmregister(list:Taasmoutput;size:Tcgsize):Tregister;override;
  39. procedure getexplicitregister(list:Taasmoutput;r:Tregister);override;
  40. procedure ungetregister(list:Taasmoutput;r:Tregister);override;
  41. procedure ungetreference(list:Taasmoutput;const r:Treference);override;
  42. procedure allocexplicitregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);override;
  43. procedure deallocexplicitregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);override;
  44. function uses_registers(rt:Tregistertype):boolean;override;
  45. procedure add_move_instruction(instr:Taicpu);override;
  46. procedure dec_fpu_stack;
  47. procedure inc_fpu_stack;
  48. procedure do_register_allocation(list:Taasmoutput;headertai:tai);override;
  49. { passing parameters, per default the parameter is pushed }
  50. { nr gives the number of the parameter (enumerated from }
  51. { left to right), this allows to move the parameter to }
  52. { register, if the cpu supports register calling }
  53. { conventions }
  54. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);override;
  55. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  56. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  57. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  58. procedure a_call_name(list : taasmoutput;const s : string);override;
  59. procedure a_call_reg(list : taasmoutput;reg : tregister);override;
  60. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister); override;
  61. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference); override;
  62. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  63. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  64. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  65. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  66. size: tcgsize; a: aword; src, dst: tregister); override;
  67. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  68. size: tcgsize; src1, src2, dst: tregister); override;
  69. { move instructions }
  70. procedure a_load_const_reg(list : taasmoutput; tosize: tcgsize; a : aword;reg : tregister);override;
  71. procedure a_load_const_ref(list : taasmoutput; tosize: tcgsize; a : aword;const ref : treference);override;
  72. procedure a_load_reg_ref(list : taasmoutput;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);override;
  73. procedure a_load_ref_reg(list : taasmoutput;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);override;
  74. procedure a_load_reg_reg(list : taasmoutput;fromsize,tosize: tcgsize;reg1,reg2 : tregister);override;
  75. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  76. { fpu move instructions }
  77. procedure a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister); override;
  78. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  79. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  80. { vector register move instructions }
  81. procedure a_loadmm_reg_reg(list: taasmoutput; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  82. procedure a_loadmm_ref_reg(list: taasmoutput; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  83. procedure a_loadmm_reg_ref(list: taasmoutput; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  84. { comparison operations }
  85. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  86. l : tasmlabel);override;
  87. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  88. l : tasmlabel);override;
  89. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  90. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister; l : tasmlabel); override;
  91. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  92. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  93. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); override;
  94. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref: TReference); override;
  95. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  96. procedure g_exception_reason_save(list : taasmoutput; const href : treference);override;
  97. procedure g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aword);override;
  98. procedure g_exception_reason_load(list : taasmoutput; const href : treference);override;
  99. { entry/exit code helpers }
  100. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);override;
  101. procedure g_interrupt_stackframe_entry(list : taasmoutput);override;
  102. procedure g_interrupt_stackframe_exit(list : taasmoutput;accused,acchiused:boolean);override;
  103. procedure g_profilecode(list : taasmoutput);override;
  104. procedure g_stackpointer_alloc(list : taasmoutput;localsize : longint);override;
  105. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  106. procedure g_restore_frame_pointer(list : taasmoutput);override;
  107. procedure g_return_from_proc(list : taasmoutput;parasize : aword);override;
  108. procedure g_save_standard_registers(list:Taasmoutput);override;
  109. procedure g_restore_standard_registers(list:Taasmoutput);override;
  110. procedure g_save_all_registers(list : taasmoutput);override;
  111. procedure g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);override;
  112. procedure g_overflowcheck(list: taasmoutput; const l:tlocation;def:tdef);override;
  113. protected
  114. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  115. procedure check_register_size(size:tcgsize;reg:tregister);
  116. private
  117. procedure sizes2load(s1,s2 : tcgsize;var op: tasmop; var s3: topsize);
  118. procedure floatload(list: taasmoutput; t : tcgsize;const ref : treference);
  119. procedure floatstore(list: taasmoutput; t : tcgsize;const ref : treference);
  120. procedure floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  121. procedure floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  122. end;
  123. const
  124. TCGSize2OpSize: Array[tcgsize] of topsize =
  125. (S_NO,S_B,S_W,S_L,S_L,S_B,S_W,S_L,S_L,
  126. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  127. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO);
  128. implementation
  129. uses
  130. {$ifdef MEMDEBUG}
  131. cclasses,
  132. {$endif MEMDEBUG}
  133. globtype,globals,verbose,systems,cutils,
  134. symdef,paramgr,tgobj,procinfo;
  135. {$ifndef NOTARGETWIN32}
  136. const
  137. winstackpagesize = 4096;
  138. {$endif NOTARGETWIN32}
  139. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_ADD,A_AND,A_DIV,
  140. A_IDIV,A_MUL, A_IMUL, A_NEG,A_NOT,A_OR,
  141. A_SAR,A_SHL,A_SHR,A_SUB,A_XOR);
  142. TOpCmp2AsmCond: Array[topcmp] of TAsmCond = (C_NONE,
  143. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  144. procedure Tcgx86.init_register_allocators;
  145. begin
  146. if cs_create_pic in aktmoduleswitches then
  147. rgint:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_EAX,RS_EDX,RS_ECX,RS_ESI,RS_EDI],first_int_imreg,[RS_EBP,RS_EBX])
  148. else
  149. rgint:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_EAX,RS_EDX,RS_ECX,RS_EBX,RS_ESI,RS_EDI],first_int_imreg,[RS_EBP]);
  150. rgmm:=trgcpu.create(R_MMREGISTER,R_SUBNONE,[RS_MM0,RS_MM1,RS_MM2,RS_MM3,RS_MM4,RS_MM5,RS_MM6,RS_MM7],first_sse_imreg,[]);
  151. rgfpu:=Trgx86fpu.create;
  152. end;
  153. procedure Tcgx86.done_register_allocators;
  154. {$ifdef MEMDEBUG}
  155. var
  156. d : tmemdebug;
  157. {$endif}
  158. begin
  159. {$ifdef MEMDEBUG}
  160. d:=tmemdebug.create(current_procinfo.procdef.procsym.name+'-rgobj');
  161. {$endif}
  162. rgint.free;
  163. rgmm.free;
  164. rgfpu.free;
  165. {$ifdef MEMDEBUG}
  166. d.free;
  167. {$endif}
  168. end;
  169. function Tcgx86.getintregister(list:Taasmoutput;size:Tcgsize):Tregister;
  170. begin
  171. result:=rgint.getregister(list,cgsize2subreg(size));
  172. end;
  173. function Tcgx86.getaddressregister(list:Taasmoutput):Tregister;
  174. begin
  175. result:=rgint.getregister(list,R_SUBWHOLE);
  176. end;
  177. function Tcgx86.getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;
  178. begin
  179. result:=trgx86fpu(rgfpu).getregisterfpu(list);
  180. end;
  181. function Tcgx86.getmmregister(list:Taasmoutput;size:Tcgsize):Tregister;
  182. begin
  183. result:=rgmm.getregister(list,R_SUBNONE);
  184. end;
  185. procedure Tcgx86.getexplicitregister(list:Taasmoutput;r:Tregister);
  186. begin
  187. case getregtype(r) of
  188. R_INTREGISTER :
  189. rgint.getexplicitregister(list,r);
  190. R_SSEREGISTER :
  191. rgmm.getexplicitregister(list,r);
  192. else
  193. internalerror(200310091);
  194. end;
  195. end;
  196. procedure tcgx86.ungetregister(list:Taasmoutput;r:Tregister);
  197. begin
  198. case getregtype(r) of
  199. R_INTREGISTER :
  200. rgint.ungetregister(list,r);
  201. R_FPUREGISTER :
  202. rgfpu.ungetregisterfpu(list,r);
  203. R_SSEREGISTER :
  204. rgmm.ungetregister(list,r);
  205. else
  206. internalerror(200310091);
  207. end;
  208. end;
  209. procedure tcgx86.ungetreference(list:Taasmoutput;const r:Treference);
  210. begin
  211. if r.base<>NR_NO then
  212. rgint.ungetregister(list,r.base);
  213. if r.index<>NR_NO then
  214. rgint.ungetregister(list,r.index);
  215. end;
  216. procedure Tcgx86.allocexplicitregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);
  217. begin
  218. case rt of
  219. R_INTREGISTER :
  220. rgint.allocexplicitregisters(list,r);
  221. R_SSEREGISTER :
  222. rgmm.allocexplicitregisters(list,r);
  223. else
  224. internalerror(200310092);
  225. end;
  226. end;
  227. procedure Tcgx86.deallocexplicitregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);
  228. begin
  229. case rt of
  230. R_INTREGISTER :
  231. rgint.deallocexplicitregisters(list,r);
  232. R_SSEREGISTER :
  233. rgmm.deallocexplicitregisters(list,r);
  234. else
  235. internalerror(200310093);
  236. end;
  237. end;
  238. function Tcgx86.uses_registers(rt:Tregistertype):boolean;
  239. begin
  240. case rt of
  241. R_INTREGISTER :
  242. result:=rgint.uses_registers;
  243. R_SSEREGISTER :
  244. result:=rgmm.uses_registers;
  245. else
  246. internalerror(200310094);
  247. end;
  248. end;
  249. procedure Tcgx86.add_move_instruction(instr:Taicpu);
  250. begin
  251. rgint.add_move_instruction(instr);
  252. end;
  253. procedure tcgx86.dec_fpu_stack;
  254. begin
  255. dec(rgfpu.fpuvaroffset);
  256. end;
  257. procedure tcgx86.inc_fpu_stack;
  258. begin
  259. inc(rgfpu.fpuvaroffset);
  260. end;
  261. procedure Tcgx86.do_register_allocation(list:Taasmoutput;headertai:tai);
  262. begin
  263. { Int }
  264. rgint.do_register_allocation(list,headertai);
  265. rgint.translate_registers(list);
  266. { SSE }
  267. rgmm.do_register_allocation(list,headertai);
  268. rgmm.translate_registers(list);
  269. end;
  270. {****************************************************************************
  271. This is private property, keep out! :)
  272. ****************************************************************************}
  273. procedure tcgx86.sizes2load(s1,s2 : tcgsize; var op: tasmop; var s3: topsize);
  274. begin
  275. case s2 of
  276. OS_8,OS_S8 :
  277. if S1 in [OS_8,OS_S8] then
  278. s3 := S_B
  279. else internalerror(200109221);
  280. OS_16,OS_S16:
  281. case s1 of
  282. OS_8,OS_S8:
  283. s3 := S_BW;
  284. OS_16,OS_S16:
  285. s3 := S_W;
  286. else
  287. internalerror(200109222);
  288. end;
  289. OS_32,OS_S32:
  290. case s1 of
  291. OS_8,OS_S8:
  292. s3 := S_BL;
  293. OS_16,OS_S16:
  294. s3 := S_WL;
  295. OS_32,OS_S32:
  296. s3 := S_L;
  297. else
  298. internalerror(200109223);
  299. end;
  300. {$ifdef x86_64}
  301. OS_64,OS_S64:
  302. case s1 of
  303. OS_8,OS_S8:
  304. s3 := S_BQ;
  305. OS_16,OS_S16:
  306. s3 := S_WQ;
  307. OS_32,OS_S32:
  308. s3 := S_LQ;
  309. OS_64,OS_S64:
  310. s3 := S_Q;
  311. else
  312. internalerror(200304302);
  313. end;
  314. {$endif x86_64}
  315. else
  316. internalerror(200109227);
  317. end;
  318. if s3 in [S_B,S_W,S_L,S_Q] then
  319. op := A_MOV
  320. else if s1 in [OS_8,OS_16,OS_32,OS_64] then
  321. op := A_MOVZX
  322. else
  323. op := A_MOVSX;
  324. end;
  325. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  326. begin
  327. case t of
  328. OS_F32 :
  329. begin
  330. op:=A_FLD;
  331. s:=S_FS;
  332. end;
  333. OS_F64 :
  334. begin
  335. op:=A_FLD;
  336. { ???? }
  337. s:=S_FL;
  338. end;
  339. OS_F80 :
  340. begin
  341. op:=A_FLD;
  342. s:=S_FX;
  343. end;
  344. OS_C64 :
  345. begin
  346. op:=A_FILD;
  347. s:=S_IQ;
  348. end;
  349. else
  350. internalerror(200204041);
  351. end;
  352. end;
  353. procedure tcgx86.floatload(list: taasmoutput; t : tcgsize;const ref : treference);
  354. var
  355. op : tasmop;
  356. s : topsize;
  357. begin
  358. floatloadops(t,op,s);
  359. list.concat(Taicpu.Op_ref(op,s,ref));
  360. inc_fpu_stack;
  361. end;
  362. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  363. begin
  364. case t of
  365. OS_F32 :
  366. begin
  367. op:=A_FSTP;
  368. s:=S_FS;
  369. end;
  370. OS_F64 :
  371. begin
  372. op:=A_FSTP;
  373. s:=S_FL;
  374. end;
  375. OS_F80 :
  376. begin
  377. op:=A_FSTP;
  378. s:=S_FX;
  379. end;
  380. OS_C64 :
  381. begin
  382. op:=A_FISTP;
  383. s:=S_IQ;
  384. end;
  385. else
  386. internalerror(200204042);
  387. end;
  388. end;
  389. procedure tcgx86.floatstore(list: taasmoutput; t : tcgsize;const ref : treference);
  390. var
  391. op : tasmop;
  392. s : topsize;
  393. begin
  394. floatstoreops(t,op,s);
  395. list.concat(Taicpu.Op_ref(op,s,ref));
  396. dec_fpu_stack;
  397. end;
  398. procedure tcgx86.check_register_size(size:tcgsize;reg:tregister);
  399. begin
  400. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  401. internalerror(200306031);
  402. end;
  403. {****************************************************************************
  404. Assembler code
  405. ****************************************************************************}
  406. { currently does nothing }
  407. procedure tcgx86.a_jmp_always(list : taasmoutput;l: tasmlabel);
  408. begin
  409. a_jmp_cond(list, OC_NONE, l);
  410. end;
  411. { we implement the following routines because otherwise we can't }
  412. { instantiate the class since it's abstract }
  413. procedure tcgx86.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);
  414. begin
  415. check_register_size(size,r);
  416. if (locpara.loc=LOC_REFERENCE) and
  417. (locpara.reference.index=NR_STACK_POINTER_REG) then
  418. begin
  419. case size of
  420. OS_8,OS_S8,
  421. OS_16,OS_S16:
  422. begin
  423. if locpara.alignment = 2 then
  424. list.concat(taicpu.op_reg(A_PUSH,S_W,makeregsize(r,OS_16)))
  425. else
  426. list.concat(taicpu.op_reg(A_PUSH,S_L,makeregsize(r,OS_32)));
  427. end;
  428. OS_32,OS_S32:
  429. begin
  430. if getsubreg(r)<>R_SUBD then
  431. internalerror(7843);
  432. list.concat(taicpu.op_reg(A_PUSH,S_L,r));
  433. end
  434. else
  435. internalerror(2002032212);
  436. end;
  437. end
  438. else
  439. inherited a_param_reg(list,size,r,locpara);
  440. end;
  441. procedure tcgx86.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  442. begin
  443. if (locpara.loc=LOC_REFERENCE) and
  444. (locpara.reference.index=NR_STACK_POINTER_REG) then
  445. begin
  446. case size of
  447. OS_8,OS_S8,OS_16,OS_S16:
  448. begin
  449. if locpara.alignment = 2 then
  450. list.concat(taicpu.op_const(A_PUSH,S_W,a))
  451. else
  452. list.concat(taicpu.op_const(A_PUSH,S_L,a));
  453. end;
  454. OS_32,OS_S32:
  455. list.concat(taicpu.op_const(A_PUSH,S_L,a));
  456. else
  457. internalerror(2002032213);
  458. end;
  459. end
  460. else
  461. inherited a_param_const(list,size,a,locpara);
  462. end;
  463. procedure tcgx86.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  464. var
  465. pushsize : tcgsize;
  466. tmpreg : tregister;
  467. begin
  468. if (locpara.loc=LOC_REFERENCE) and
  469. (locpara.reference.index=NR_STACK_POINTER_REG) then
  470. begin
  471. case size of
  472. OS_8,OS_S8,
  473. OS_16,OS_S16:
  474. begin
  475. if locpara.alignment = 2 then
  476. pushsize:=OS_16
  477. else
  478. pushsize:=OS_32;
  479. tmpreg:=getintregister(list,pushsize);
  480. a_load_ref_reg(list,size,pushsize,r,tmpreg);
  481. list.concat(taicpu.op_reg(A_PUSH,TCgsize2opsize[pushsize],tmpreg));
  482. ungetregister(list,tmpreg);
  483. end;
  484. OS_32,OS_S32:
  485. list.concat(taicpu.op_ref(A_PUSH,S_L,r));
  486. {$ifdef cpu64bit}
  487. OS_64,OS_S64:
  488. list.concat(taicpu.op_ref(A_PUSH,S_Q,r));
  489. {$endif cpu64bit}
  490. else
  491. internalerror(2002032214);
  492. end;
  493. end
  494. else
  495. inherited a_param_ref(list,size,r,locpara);
  496. end;
  497. procedure tcgx86.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  498. var
  499. tmpreg : tregister;
  500. begin
  501. if (r.segment<>NR_NO) then
  502. CGMessage(cg_e_cant_use_far_pointer_there);
  503. if (locpara.loc=LOC_REFERENCE) and
  504. (locpara.reference.index=NR_STACK_POINTER_REG) then
  505. begin
  506. if (r.base=NR_NO) and (r.index=NR_NO) then
  507. begin
  508. if assigned(r.symbol) then
  509. list.concat(Taicpu.Op_sym_ofs(A_PUSH,S_L,r.symbol,r.offset))
  510. else
  511. list.concat(Taicpu.Op_const(A_PUSH,S_L,r.offset));
  512. end
  513. else if (r.base=NR_NO) and (r.index<>NR_NO) and
  514. (r.offset=0) and (r.scalefactor=0) and (r.symbol=nil) then
  515. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r.index))
  516. else if (r.base<>NR_NO) and (r.index=NR_NO) and
  517. (r.offset=0) and (r.symbol=nil) then
  518. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r.base))
  519. else
  520. begin
  521. tmpreg:=getaddressregister(list);
  522. a_loadaddr_ref_reg(list,r,tmpreg);
  523. ungetregister(list,tmpreg);
  524. list.concat(taicpu.op_reg(A_PUSH,S_L,tmpreg));
  525. end;
  526. end
  527. else
  528. inherited a_paramaddr_ref(list,r,locpara);
  529. end;
  530. procedure tcgx86.a_call_name(list : taasmoutput;const s : string);
  531. begin
  532. list.concat(taicpu.op_sym(A_CALL,S_NO,objectlibrary.newasmsymbol(s)));
  533. end;
  534. procedure tcgx86.a_call_reg(list : taasmoutput;reg : tregister);
  535. begin
  536. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  537. end;
  538. {********************** load instructions ********************}
  539. procedure tcgx86.a_load_const_reg(list : taasmoutput; tosize: TCGSize; a : aword; reg : TRegister);
  540. begin
  541. check_register_size(tosize,reg);
  542. { the optimizer will change it to "xor reg,reg" when loading zero, }
  543. { no need to do it here too (JM) }
  544. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg))
  545. end;
  546. procedure tcgx86.a_load_const_ref(list : taasmoutput; tosize: tcgsize; a : aword;const ref : treference);
  547. begin
  548. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,ref));
  549. end;
  550. procedure tcgx86.a_load_reg_ref(list : taasmoutput; fromsize,tosize: TCGSize; reg : tregister;const ref : treference);
  551. var
  552. op: tasmop;
  553. s: topsize;
  554. tmpreg : tregister;
  555. begin
  556. check_register_size(fromsize,reg);
  557. sizes2load(fromsize,tosize,op,s);
  558. case s of
  559. S_BW,S_BL,S_WL
  560. {$ifdef x86_64}
  561. ,S_BQ,S_WQ,S_LQ
  562. {$endif x86_64}
  563. :
  564. begin
  565. tmpreg:=getintregister(list,tosize);
  566. list.concat(taicpu.op_reg_reg(op,s,reg,tmpreg));
  567. a_load_reg_ref(list,tosize,tosize,tmpreg,ref);
  568. ungetregister(list,tmpreg);
  569. end;
  570. else
  571. list.concat(taicpu.op_reg_ref(op,s,reg,ref));
  572. end;
  573. end;
  574. procedure tcgx86.a_load_ref_reg(list : taasmoutput;fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  575. var
  576. op: tasmop;
  577. s: topsize;
  578. begin
  579. check_register_size(tosize,reg);
  580. sizes2load(fromsize,tosize,op,s);
  581. list.concat(taicpu.op_ref_reg(op,s,ref,reg));
  582. end;
  583. procedure tcgx86.a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  584. var
  585. op: tasmop;
  586. s: topsize;
  587. eq:boolean;
  588. instr:Taicpu;
  589. begin
  590. check_register_size(fromsize,reg1);
  591. check_register_size(tosize,reg2);
  592. sizes2load(fromsize,tosize,op,s);
  593. eq:=getsupreg(reg1)=getsupreg(reg2);
  594. if eq then
  595. begin
  596. { "mov reg1, reg1" doesn't make sense }
  597. if op = A_MOV then
  598. exit;
  599. end;
  600. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  601. {Notify the register allocator that we have written a move instruction so
  602. it can try to eliminate it.}
  603. Tcgx86(cg).rgint.add_move_instruction(instr);
  604. list.concat(instr);
  605. end;
  606. procedure tcgx86.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  607. begin
  608. if (ref.base=NR_NO) and (ref.index=NR_NO) then
  609. begin
  610. if assigned(ref.symbol) then
  611. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,ref.symbol,ref.offset,r))
  612. else
  613. a_load_const_reg(list,OS_INT,ref.offset,r);
  614. end
  615. else if (ref.base=NR_NO) and (ref.index<>NR_NO) and
  616. (ref.offset=0) and (ref.scalefactor=0) and (ref.symbol=nil) then
  617. a_load_reg_reg(list,OS_INT,OS_INT,ref.index,r)
  618. else if (ref.base<>NR_NO) and (ref.index=NR_NO) and
  619. (ref.offset=0) and (ref.symbol=nil) then
  620. a_load_reg_reg(list,OS_INT,OS_INT,ref.base,r)
  621. else
  622. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,r));
  623. end;
  624. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  625. { R_ST means "the current value at the top of the fpu stack" (JM) }
  626. procedure tcgx86.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  627. begin
  628. if (reg1<>NR_ST) then
  629. begin
  630. list.concat(taicpu.op_reg(A_FLD,S_NO,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  631. inc_fpu_stack;
  632. end;
  633. if (reg2<>NR_ST) then
  634. begin
  635. list.concat(taicpu.op_reg(A_FSTP,S_NO,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  636. dec_fpu_stack;
  637. end;
  638. end;
  639. procedure tcgx86.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  640. begin
  641. floatload(list,size,ref);
  642. if (reg<>NR_ST) then
  643. a_loadfpu_reg_reg(list,size,NR_ST,reg);
  644. end;
  645. procedure tcgx86.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  646. begin
  647. if reg<>NR_ST then
  648. a_loadfpu_reg_reg(list,size,reg,NR_ST);
  649. floatstore(list,size,ref);
  650. end;
  651. procedure tcgx86.a_loadmm_reg_reg(list: taasmoutput; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  652. begin
  653. list.concat(taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2));
  654. end;
  655. procedure tcgx86.a_loadmm_ref_reg(list: taasmoutput; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  656. begin
  657. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,ref,reg));
  658. end;
  659. procedure tcgx86.a_loadmm_reg_ref(list: taasmoutput; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  660. begin
  661. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,ref));
  662. end;
  663. procedure tcgx86.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister);
  664. var
  665. opcode: tasmop;
  666. power: longint;
  667. begin
  668. check_register_size(size,reg);
  669. case op of
  670. OP_DIV, OP_IDIV:
  671. begin
  672. if ispowerof2(a,power) then
  673. begin
  674. case op of
  675. OP_DIV:
  676. opcode := A_SHR;
  677. OP_IDIV:
  678. opcode := A_SAR;
  679. end;
  680. list.concat(taicpu.op_const_reg(opcode,TCgSize2OpSize[size],power,reg));
  681. exit;
  682. end;
  683. { the rest should be handled specifically in the code }
  684. { generator because of the silly register usage restraints }
  685. internalerror(200109224);
  686. end;
  687. OP_MUL,OP_IMUL:
  688. begin
  689. if not(cs_check_overflow in aktlocalswitches) and
  690. ispowerof2(a,power) then
  691. begin
  692. list.concat(taicpu.op_const_reg(A_SHL,TCgSize2OpSize[size],power,reg));
  693. exit;
  694. end;
  695. if op = OP_IMUL then
  696. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  697. else
  698. { OP_MUL should be handled specifically in the code }
  699. { generator because of the silly register usage restraints }
  700. internalerror(200109225);
  701. end;
  702. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  703. if not(cs_check_overflow in aktlocalswitches) and
  704. (a = 1) and
  705. (op in [OP_ADD,OP_SUB]) then
  706. if op = OP_ADD then
  707. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  708. else
  709. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  710. else if (a = 0) then
  711. if (op <> OP_AND) then
  712. exit
  713. else
  714. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],0,reg))
  715. else if (a = high(aword)) and
  716. (op in [OP_AND,OP_OR,OP_XOR]) then
  717. begin
  718. case op of
  719. OP_AND:
  720. exit;
  721. OP_OR:
  722. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],high(aword),reg));
  723. OP_XOR:
  724. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg));
  725. end
  726. end
  727. else
  728. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  729. OP_SHL,OP_SHR,OP_SAR:
  730. begin
  731. if (a and 31) <> 0 Then
  732. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  733. if (a shr 5) <> 0 Then
  734. internalerror(68991);
  735. end
  736. else internalerror(68992);
  737. end;
  738. end;
  739. procedure tcgx86.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; const ref: TReference);
  740. var
  741. opcode: tasmop;
  742. power: longint;
  743. begin
  744. Case Op of
  745. OP_DIV, OP_IDIV:
  746. Begin
  747. if ispowerof2(a,power) then
  748. begin
  749. case op of
  750. OP_DIV:
  751. opcode := A_SHR;
  752. OP_IDIV:
  753. opcode := A_SAR;
  754. end;
  755. list.concat(taicpu.op_const_ref(opcode,
  756. TCgSize2OpSize[size],power,ref));
  757. exit;
  758. end;
  759. { the rest should be handled specifically in the code }
  760. { generator because of the silly register usage restraints }
  761. internalerror(200109231);
  762. End;
  763. OP_MUL,OP_IMUL:
  764. begin
  765. if not(cs_check_overflow in aktlocalswitches) and
  766. ispowerof2(a,power) then
  767. begin
  768. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  769. power,ref));
  770. exit;
  771. end;
  772. { can't multiply a memory location directly with a constant }
  773. if op = OP_IMUL then
  774. inherited a_op_const_ref(list,op,size,a,ref)
  775. else
  776. { OP_MUL should be handled specifically in the code }
  777. { generator because of the silly register usage restraints }
  778. internalerror(200109232);
  779. end;
  780. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  781. if not(cs_check_overflow in aktlocalswitches) and
  782. (a = 1) and
  783. (op in [OP_ADD,OP_SUB]) then
  784. if op = OP_ADD then
  785. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],ref))
  786. else
  787. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],ref))
  788. else if (a = 0) then
  789. if (op <> OP_AND) then
  790. exit
  791. else
  792. a_load_const_ref(list,size,0,ref)
  793. else if (a = high(aword)) and
  794. (op in [OP_AND,OP_OR,OP_XOR]) then
  795. begin
  796. case op of
  797. OP_AND:
  798. exit;
  799. OP_OR:
  800. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],high(aword),ref));
  801. OP_XOR:
  802. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],ref));
  803. end
  804. end
  805. else
  806. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  807. TCgSize2OpSize[size],a,ref));
  808. OP_SHL,OP_SHR,OP_SAR:
  809. begin
  810. if (a and 31) <> 0 then
  811. list.concat(taicpu.op_const_ref(
  812. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,ref));
  813. if (a shr 5) <> 0 Then
  814. internalerror(68991);
  815. end
  816. else internalerror(68992);
  817. end;
  818. end;
  819. procedure tcgx86.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  820. var
  821. dstsize: topsize;
  822. instr:Taicpu;
  823. begin
  824. check_register_size(size,src);
  825. check_register_size(size,dst);
  826. dstsize := tcgsize2opsize[size];
  827. case op of
  828. OP_NEG,OP_NOT:
  829. begin
  830. if src<>dst then
  831. a_load_reg_reg(list,size,size,src,dst);
  832. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  833. end;
  834. OP_MUL,OP_DIV,OP_IDIV:
  835. { special stuff, needs separate handling inside code }
  836. { generator }
  837. internalerror(200109233);
  838. OP_SHR,OP_SHL,OP_SAR:
  839. begin
  840. getexplicitregister(list,NR_CL);
  841. a_load_reg_reg(list,size,OS_8,dst,NR_CL);
  842. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],S_B,src,NR_CL));
  843. ungetregister(list,NR_CL);
  844. end;
  845. else
  846. begin
  847. if reg2opsize(src) <> dstsize then
  848. internalerror(200109226);
  849. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  850. list.concat(instr);
  851. end;
  852. end;
  853. end;
  854. procedure tcgx86.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  855. begin
  856. check_register_size(size,reg);
  857. case op of
  858. OP_NEG,OP_NOT,OP_IMUL:
  859. begin
  860. inherited a_op_ref_reg(list,op,size,ref,reg);
  861. end;
  862. OP_MUL,OP_DIV,OP_IDIV:
  863. { special stuff, needs separate handling inside code }
  864. { generator }
  865. internalerror(200109239);
  866. else
  867. begin
  868. reg := makeregsize(reg,size);
  869. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],ref,reg));
  870. end;
  871. end;
  872. end;
  873. procedure tcgx86.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  874. begin
  875. check_register_size(size,reg);
  876. case op of
  877. OP_NEG,OP_NOT:
  878. begin
  879. if reg<>NR_NO then
  880. internalerror(200109237);
  881. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],ref));
  882. end;
  883. OP_IMUL:
  884. begin
  885. { this one needs a load/imul/store, which is the default }
  886. inherited a_op_ref_reg(list,op,size,ref,reg);
  887. end;
  888. OP_MUL,OP_DIV,OP_IDIV:
  889. { special stuff, needs separate handling inside code }
  890. { generator }
  891. internalerror(200109238);
  892. else
  893. begin
  894. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,ref));
  895. end;
  896. end;
  897. end;
  898. procedure tcgx86.a_op_const_reg_reg(list: taasmoutput; op: TOpCg; size: tcgsize; a: aword; src, dst: tregister);
  899. var
  900. tmpref: treference;
  901. power: longint;
  902. begin
  903. check_register_size(size,src);
  904. check_register_size(size,dst);
  905. if not (size in [OS_32,OS_S32]) then
  906. begin
  907. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  908. exit;
  909. end;
  910. { if we get here, we have to do a 32 bit calculation, guaranteed }
  911. case op of
  912. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  913. OP_SAR:
  914. { can't do anything special for these }
  915. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  916. OP_IMUL:
  917. begin
  918. if not(cs_check_overflow in aktlocalswitches) and
  919. ispowerof2(a,power) then
  920. { can be done with a shift }
  921. begin
  922. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  923. exit;
  924. end;
  925. list.concat(taicpu.op_const_reg_reg(A_IMUL,S_L,a,src,dst));
  926. end;
  927. OP_ADD, OP_SUB:
  928. if (a = 0) then
  929. a_load_reg_reg(list,size,size,src,dst)
  930. else
  931. begin
  932. reference_reset(tmpref);
  933. tmpref.base := src;
  934. tmpref.offset := longint(a);
  935. if op = OP_SUB then
  936. tmpref.offset := -tmpref.offset;
  937. list.concat(taicpu.op_ref_reg(A_LEA,S_L,tmpref,dst));
  938. end
  939. else internalerror(200112302);
  940. end;
  941. end;
  942. procedure tcgx86.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;size: tcgsize; src1, src2, dst: tregister);
  943. var
  944. tmpref: treference;
  945. begin
  946. check_register_size(size,src1);
  947. check_register_size(size,src2);
  948. check_register_size(size,dst);
  949. if not(size in [OS_32,OS_S32]) then
  950. begin
  951. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  952. exit;
  953. end;
  954. { if we get here, we have to do a 32 bit calculation, guaranteed }
  955. Case Op of
  956. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  957. OP_SAR,OP_SUB,OP_NOT,OP_NEG:
  958. { can't do anything special for these }
  959. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  960. OP_IMUL:
  961. list.concat(taicpu.op_reg_reg_reg(A_IMUL,S_L,src1,src2,dst));
  962. OP_ADD:
  963. begin
  964. reference_reset(tmpref);
  965. tmpref.base := src1;
  966. tmpref.index := src2;
  967. tmpref.scalefactor := 1;
  968. list.concat(taicpu.op_ref_reg(A_LEA,S_L,tmpref,dst));
  969. end
  970. else internalerror(200112303);
  971. end;
  972. end;
  973. {*************** compare instructructions ****************}
  974. procedure tcgx86.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  975. l : tasmlabel);
  976. begin
  977. if (a = 0) then
  978. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  979. else
  980. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  981. a_jmp_cond(list,cmp_op,l);
  982. end;
  983. procedure tcgx86.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;const ref : treference;
  984. l : tasmlabel);
  985. begin
  986. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,ref));
  987. a_jmp_cond(list,cmp_op,l);
  988. end;
  989. procedure tcgx86.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  990. reg1,reg2 : tregister;l : tasmlabel);
  991. begin
  992. check_register_size(size,reg1);
  993. check_register_size(size,reg2);
  994. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  995. a_jmp_cond(list,cmp_op,l);
  996. end;
  997. procedure tcgx86.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  998. begin
  999. check_register_size(size,reg);
  1000. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],ref,reg));
  1001. a_jmp_cond(list,cmp_op,l);
  1002. end;
  1003. procedure tcgx86.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  1004. var
  1005. ai : taicpu;
  1006. begin
  1007. if cond=OC_None then
  1008. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1009. else
  1010. begin
  1011. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  1012. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1013. end;
  1014. ai.is_jmp:=true;
  1015. list.concat(ai);
  1016. end;
  1017. procedure tcgx86.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  1018. var
  1019. ai : taicpu;
  1020. begin
  1021. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  1022. ai.SetCondition(flags_to_cond(f));
  1023. ai.is_jmp := true;
  1024. list.concat(ai);
  1025. end;
  1026. procedure tcgx86.g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister);
  1027. var
  1028. ai : taicpu;
  1029. hreg : tregister;
  1030. begin
  1031. hreg:=makeregsize(reg,OS_8);
  1032. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  1033. ai.setcondition(flags_to_cond(f));
  1034. list.concat(ai);
  1035. if (reg<>hreg) then
  1036. a_load_reg_reg(list,OS_8,size,hreg,reg);
  1037. end;
  1038. procedure tcgx86.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref: TReference);
  1039. var
  1040. ai : taicpu;
  1041. begin
  1042. if not(size in [OS_8,OS_S8]) then
  1043. a_load_const_ref(list,size,0,ref);
  1044. ai:=Taicpu.op_ref(A_SETcc,S_B,ref);
  1045. ai.setcondition(flags_to_cond(f));
  1046. list.concat(ai);
  1047. end;
  1048. { ************* concatcopy ************ }
  1049. procedure Tcgx86.g_concatcopy(list:Taasmoutput;const source,dest:Treference;
  1050. len:aword;delsource,loadref:boolean);
  1051. var srcref,dstref:Treference;
  1052. r:Tregister;
  1053. helpsize:aword;
  1054. copysize:byte;
  1055. cgsize:Tcgsize;
  1056. begin
  1057. helpsize:=12;
  1058. if cs_littlesize in aktglobalswitches then
  1059. helpsize:=8;
  1060. if not loadref and (len<=helpsize) then
  1061. begin
  1062. dstref:=dest;
  1063. srcref:=source;
  1064. copysize:=4;
  1065. cgsize:=OS_32;
  1066. while len<>0 do
  1067. begin
  1068. if len<2 then
  1069. begin
  1070. copysize:=1;
  1071. cgsize:=OS_8;
  1072. end
  1073. else if len<4 then
  1074. begin
  1075. copysize:=2;
  1076. cgsize:=OS_16;
  1077. end;
  1078. dec(len,copysize);
  1079. if (len=0) and delsource then
  1080. reference_release(list,source);
  1081. r:=getintregister(list,cgsize);
  1082. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1083. ungetregister(list,r);
  1084. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1085. inc(srcref.offset,copysize);
  1086. inc(dstref.offset,copysize);
  1087. end;
  1088. end
  1089. else
  1090. begin
  1091. getexplicitregister(list,NR_EDI);
  1092. a_loadaddr_ref_reg(list,dest,NR_EDI);
  1093. getexplicitregister(list,NR_ESI);
  1094. if loadref then
  1095. a_load_ref_reg(list,OS_ADDR,OS_ADDR,source,NR_ESI)
  1096. else
  1097. begin
  1098. a_loadaddr_ref_reg(list,source,NR_ESI);
  1099. if delsource then
  1100. begin
  1101. srcref:=source;
  1102. { Don't release ESI register yet, it's needed
  1103. by the movsl }
  1104. if (srcref.base=NR_ESI) then
  1105. srcref.base:=NR_NO
  1106. else if (srcref.index=NR_ESI) then
  1107. srcref.index:=NR_NO;
  1108. reference_release(list,srcref);
  1109. end;
  1110. end;
  1111. getexplicitregister(list,NR_ECX);
  1112. list.concat(Taicpu.op_none(A_CLD,S_NO));
  1113. if cs_littlesize in aktglobalswitches then
  1114. begin
  1115. a_load_const_reg(list,OS_INT,len,NR_ECX);
  1116. list.concat(Taicpu.op_none(A_REP,S_NO));
  1117. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1118. end
  1119. else
  1120. begin
  1121. helpsize:=len shr 2;
  1122. len:=len and 3;
  1123. if helpsize>1 then
  1124. begin
  1125. a_load_const_reg(list,OS_INT,helpsize,NR_ECX);
  1126. list.concat(Taicpu.op_none(A_REP,S_NO));
  1127. end;
  1128. if helpsize>0 then
  1129. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1130. if len>1 then
  1131. begin
  1132. dec(len,2);
  1133. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  1134. end;
  1135. if len=1 then
  1136. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1137. end;
  1138. ungetregister(list,NR_ECX);
  1139. ungetregister(list,NR_ESI);
  1140. ungetregister(list,NR_EDI);
  1141. end;
  1142. if delsource then
  1143. tg.ungetiftemp(list,source);
  1144. end;
  1145. procedure tcgx86.g_exception_reason_save(list : taasmoutput; const href : treference);
  1146. begin
  1147. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1148. end;
  1149. procedure tcgx86.g_exception_reason_save_const(list : taasmoutput;const href : treference; a: aword);
  1150. begin
  1151. list.concat(Taicpu.op_const(A_PUSH,S_L,a));
  1152. end;
  1153. procedure tcgx86.g_exception_reason_load(list : taasmoutput; const href : treference);
  1154. begin
  1155. list.concat(Taicpu.op_reg(A_POP,S_L,NR_EAX));
  1156. end;
  1157. {****************************************************************************
  1158. Entry/Exit Code Helpers
  1159. ****************************************************************************}
  1160. procedure tcgx86.g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);
  1161. var
  1162. power,len : longint;
  1163. opsize : topsize;
  1164. {$ifndef __NOWINPECOFF__}
  1165. again,ok : tasmlabel;
  1166. {$endif}
  1167. begin
  1168. { get stack space }
  1169. getexplicitregister(list,NR_EDI);
  1170. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,NR_EDI));
  1171. list.concat(Taicpu.op_reg(A_INC,S_L,NR_EDI));
  1172. if (elesize<>1) then
  1173. begin
  1174. if ispowerof2(elesize, power) then
  1175. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,NR_EDI))
  1176. else
  1177. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,NR_EDI));
  1178. end;
  1179. {$ifndef __NOWINPECOFF__}
  1180. { windows guards only a few pages for stack growing, }
  1181. { so we have to access every page first }
  1182. if target_info.system=system_i386_win32 then
  1183. begin
  1184. objectlibrary.getlabel(again);
  1185. objectlibrary.getlabel(ok);
  1186. a_label(list,again);
  1187. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,NR_EDI));
  1188. a_jmp_cond(list,OC_B,ok);
  1189. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  1190. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  1191. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,NR_EDI));
  1192. a_jmp_always(list,again);
  1193. a_label(list,ok);
  1194. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,NR_EDI,NR_ESP));
  1195. ungetregister(list,NR_EDI);
  1196. { now reload EDI }
  1197. getexplicitregister(list,NR_EDI);
  1198. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,NR_EDI));
  1199. list.concat(Taicpu.op_reg(A_INC,S_L,NR_EDI));
  1200. if (elesize<>1) then
  1201. begin
  1202. if ispowerof2(elesize, power) then
  1203. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,NR_EDI))
  1204. else
  1205. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,NR_EDI));
  1206. end;
  1207. end
  1208. else
  1209. {$endif __NOWINPECOFF__}
  1210. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,NR_EDI,NR_ESP));
  1211. { align stack on 4 bytes }
  1212. list.concat(Taicpu.op_const_reg(A_AND,S_L,$fffffff4,NR_ESP));
  1213. { load destination }
  1214. a_load_reg_reg(list,OS_INT,OS_INT,NR_ESP,NR_EDI);
  1215. { Allocate other registers }
  1216. getexplicitregister(list,NR_ECX);
  1217. getexplicitregister(list,NR_ESI);
  1218. { load count }
  1219. a_load_ref_reg(list,OS_INT,OS_INT,lenref,NR_ECX);
  1220. { load source }
  1221. a_load_ref_reg(list,OS_INT,OS_INT,ref,NR_ESI);
  1222. { scheduled .... }
  1223. list.concat(Taicpu.op_reg(A_INC,S_L,NR_ECX));
  1224. { calculate size }
  1225. len:=elesize;
  1226. opsize:=S_B;
  1227. if (len and 3)=0 then
  1228. begin
  1229. opsize:=S_L;
  1230. len:=len shr 2;
  1231. end
  1232. else
  1233. if (len and 1)=0 then
  1234. begin
  1235. opsize:=S_W;
  1236. len:=len shr 1;
  1237. end;
  1238. if ispowerof2(len, power) then
  1239. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,NR_ECX))
  1240. else
  1241. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,NR_ECX));
  1242. list.concat(Taicpu.op_none(A_REP,S_NO));
  1243. case opsize of
  1244. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1245. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1246. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1247. end;
  1248. ungetregister(list,NR_EDI);
  1249. ungetregister(list,NR_ECX);
  1250. ungetregister(list,NR_ESI);
  1251. { patch the new address }
  1252. a_load_reg_ref(list,OS_INT,OS_INT,NR_ESP,ref);
  1253. end;
  1254. procedure tcgx86.g_interrupt_stackframe_entry(list : taasmoutput);
  1255. begin
  1256. { .... also the segment registers }
  1257. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  1258. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  1259. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  1260. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  1261. { save the registers of an interrupt procedure }
  1262. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  1263. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  1264. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1265. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  1266. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  1267. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  1268. end;
  1269. procedure tcgx86.g_interrupt_stackframe_exit(list : taasmoutput;accused,acchiused:boolean);
  1270. begin
  1271. if accused then
  1272. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,NR_ESP))
  1273. else
  1274. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EAX));
  1275. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EBX));
  1276. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_ECX));
  1277. if acchiused then
  1278. list.concat(Taicpu.Op_const_reg(A_ADD,S_L,4,NR_ESP))
  1279. else
  1280. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  1281. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_ESI));
  1282. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDI));
  1283. { .... also the segment registers }
  1284. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  1285. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_ES));
  1286. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_FS));
  1287. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_GS));
  1288. { this restores the flags }
  1289. list.concat(Taicpu.Op_none(A_IRET,S_NO));
  1290. end;
  1291. procedure tcgx86.g_profilecode(list : taasmoutput);
  1292. var
  1293. pl : tasmlabel;
  1294. begin
  1295. case target_info.system of
  1296. {$ifndef NOTARGETWIN32}
  1297. system_i386_win32,
  1298. {$endif}
  1299. system_i386_freebsd,
  1300. system_i386_wdosx,
  1301. system_i386_linux:
  1302. begin
  1303. objectlibrary.getaddrlabel(pl);
  1304. list.concat(Tai_section.Create(sec_data));
  1305. list.concat(Tai_align.Create(4));
  1306. list.concat(Tai_label.Create(pl));
  1307. list.concat(Tai_const.Create_32bit(0));
  1308. list.concat(Tai_section.Create(sec_code));
  1309. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  1310. a_call_name(list,target_info.Cprefix+'mcount');
  1311. supregset_include(rgint.used_in_proc,RS_EDX);
  1312. end;
  1313. system_i386_go32v2,system_i386_watcom:
  1314. begin
  1315. a_call_name(list,'MCOUNT');
  1316. end;
  1317. end;
  1318. end;
  1319. procedure tcgx86.g_stackpointer_alloc(list : taasmoutput;localsize : longint);
  1320. var
  1321. href : treference;
  1322. i : integer;
  1323. again : tasmlabel;
  1324. begin
  1325. if localsize>0 then
  1326. begin
  1327. {$ifndef NOTARGETWIN32}
  1328. { windows guards only a few pages for stack growing, }
  1329. { so we have to access every page first }
  1330. if (target_info.system=system_i386_win32) and
  1331. (localsize>=winstackpagesize) then
  1332. begin
  1333. if localsize div winstackpagesize<=5 then
  1334. begin
  1335. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,NR_ESP));
  1336. for i:=1 to localsize div winstackpagesize do
  1337. begin
  1338. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize);
  1339. list.concat(Taicpu.op_const_ref(A_MOV,S_L,0,href));
  1340. end;
  1341. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1342. end
  1343. else
  1344. begin
  1345. objectlibrary.getlabel(again);
  1346. getexplicitregister(list,NR_EDI);
  1347. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  1348. a_label(list,again);
  1349. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  1350. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1351. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI));
  1352. a_jmp_cond(list,OC_NE,again);
  1353. ungetregister(list,NR_EDI);
  1354. list.concat(Taicpu.op_const_reg(A_SUB,S_L,localsize mod winstackpagesize,NR_ESP));
  1355. end
  1356. end
  1357. else
  1358. {$endif NOTARGETWIN32}
  1359. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize,NR_ESP));
  1360. end;
  1361. end;
  1362. procedure tcgx86.g_stackframe_entry(list : taasmoutput;localsize : longint);
  1363. begin
  1364. list.concat(tai_regalloc.alloc(NR_EBP));
  1365. include(rgint.preserved_by_proc,RS_EBP);
  1366. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EBP));
  1367. list.concat(Taicpu.op_reg_reg(A_MOV,S_L,NR_ESP,NR_EBP));
  1368. if localsize>0 then
  1369. g_stackpointer_alloc(list,localsize);
  1370. if cs_create_pic in aktmoduleswitches then
  1371. begin
  1372. a_call_name(list,'FPC_GETEIPINEBX');
  1373. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_L,objectlibrary.newasmsymboldata('_GLOBAL_OFFSET_TABLE_'),0,NR_EBX));
  1374. list.concat(tai_regalloc.alloc(NR_EBX));
  1375. end;
  1376. end;
  1377. procedure tcgx86.g_restore_frame_pointer(list : taasmoutput);
  1378. begin
  1379. if cs_create_pic in aktmoduleswitches then
  1380. list.concat(tai_regalloc.dealloc(NR_EBX));
  1381. list.concat(tai_regalloc.dealloc(NR_EBP));
  1382. list.concat(Taicpu.op_none(A_LEAVE,S_NO));
  1383. end;
  1384. procedure tcgx86.g_return_from_proc(list : taasmoutput;parasize : aword);
  1385. begin
  1386. { Routines with the poclearstack flag set use only a ret }
  1387. { also routines with parasize=0 }
  1388. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1389. begin
  1390. { complex return values are removed from stack in C code PM }
  1391. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,
  1392. current_procinfo.procdef.proccalloption) then
  1393. list.concat(Taicpu.Op_const(A_RET,S_NO,4))
  1394. else
  1395. list.concat(Taicpu.Op_none(A_RET,S_NO));
  1396. end
  1397. else if (parasize=0) then
  1398. list.concat(Taicpu.Op_none(A_RET,S_NO))
  1399. else
  1400. begin
  1401. { parameters are limited to 65535 bytes because }
  1402. { ret allows only imm16 }
  1403. if (parasize>65535) then
  1404. CGMessage(cg_e_parasize_too_big);
  1405. list.concat(Taicpu.Op_const(A_RET,S_NO,parasize));
  1406. end;
  1407. end;
  1408. procedure tcgx86.g_save_standard_registers(list:Taasmoutput);
  1409. var
  1410. href : treference;
  1411. size : longint;
  1412. begin
  1413. { Get temp }
  1414. size:=0;
  1415. if supregset_in(rgint.used_in_proc,RS_EBX) then
  1416. inc(size,POINTER_SIZE);
  1417. if supregset_in(rgint.used_in_proc,RS_ESI) then
  1418. inc(size,POINTER_SIZE);
  1419. if supregset_in(rgint.used_in_proc,RS_EDI) then
  1420. inc(size,POINTER_SIZE);
  1421. if size>0 then
  1422. begin
  1423. tg.GetTemp(list,size,tt_noreuse,current_procinfo.save_regs_ref);
  1424. { Copy registers to temp }
  1425. href:=current_procinfo.save_regs_ref;
  1426. if supregset_in(rgint.used_in_proc,RS_EBX) then
  1427. begin
  1428. a_load_reg_ref(list,OS_ADDR,OS_ADDR,NR_EBX,href);
  1429. inc(href.offset,POINTER_SIZE);
  1430. end;
  1431. if supregset_in(rgint.used_in_proc,RS_ESI) then
  1432. begin
  1433. a_load_reg_ref(list,OS_ADDR,OS_ADDR,NR_ESI,href);
  1434. inc(href.offset,POINTER_SIZE);
  1435. end;
  1436. if supregset_in(rgint.used_in_proc,RS_EDI) then
  1437. begin
  1438. a_load_reg_ref(list,OS_ADDR,OS_ADDR,NR_EDI,href);
  1439. inc(href.offset,POINTER_SIZE);
  1440. end;
  1441. end;
  1442. include(rgint.preserved_by_proc,RS_EBX);
  1443. include(rgint.preserved_by_proc,RS_ESI);
  1444. include(rgint.preserved_by_proc,RS_EDI);
  1445. end;
  1446. procedure tcgx86.g_restore_standard_registers(list:Taasmoutput);
  1447. var
  1448. href : treference;
  1449. begin
  1450. { Copy registers from temp }
  1451. href:=current_procinfo.save_regs_ref;
  1452. if supregset_in(rgint.used_in_proc,RS_EBX) then
  1453. begin
  1454. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_EBX);
  1455. inc(href.offset,POINTER_SIZE);
  1456. end;
  1457. if supregset_in(rgint.used_in_proc,RS_ESI) then
  1458. begin
  1459. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_ESI);
  1460. inc(href.offset,POINTER_SIZE);
  1461. end;
  1462. if supregset_in(rgint.used_in_proc,RS_EDI) then
  1463. begin
  1464. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_EDI);
  1465. inc(href.offset,POINTER_SIZE);
  1466. end;
  1467. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1468. end;
  1469. procedure tcgx86.g_save_all_registers(list : taasmoutput);
  1470. begin
  1471. list.concat(Taicpu.Op_none(A_PUSHA,S_L));
  1472. tg.GetTemp(list,POINTER_SIZE,tt_noreuse,current_procinfo.save_regs_ref);
  1473. a_load_reg_ref(list,OS_ADDR,OS_ADDR,NR_ESP,current_procinfo.save_regs_ref);
  1474. end;
  1475. procedure tcgx86.g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);
  1476. var
  1477. href : treference;
  1478. begin
  1479. a_load_ref_reg(list,OS_ADDR,OS_ADDR,current_procinfo.save_regs_ref,NR_ESP);
  1480. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1481. if acchiused then
  1482. begin
  1483. reference_reset_base(href,NR_ESP,20);
  1484. list.concat(Taicpu.Op_reg_ref(A_MOV,S_L,NR_EDX,href));
  1485. end;
  1486. if accused then
  1487. begin
  1488. reference_reset_base(href,NR_ESP,28);
  1489. list.concat(Taicpu.Op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1490. end;
  1491. list.concat(Taicpu.Op_none(A_POPA,S_L));
  1492. { We add a NOP because of the 386DX CPU bugs with POPAD }
  1493. list.concat(taicpu.op_none(A_NOP,S_L));
  1494. end;
  1495. { produces if necessary overflowcode }
  1496. procedure tcgx86.g_overflowcheck(list: taasmoutput; const l:tlocation;def:tdef);
  1497. var
  1498. hl : tasmlabel;
  1499. ai : taicpu;
  1500. cond : TAsmCond;
  1501. begin
  1502. if not(cs_check_overflow in aktlocalswitches) then
  1503. exit;
  1504. objectlibrary.getlabel(hl);
  1505. if not ((def.deftype=pointerdef) or
  1506. ((def.deftype=orddef) and
  1507. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1508. bool8bit,bool16bit,bool32bit]))) then
  1509. cond:=C_NO
  1510. else
  1511. cond:=C_NB;
  1512. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  1513. ai.SetCondition(cond);
  1514. ai.is_jmp:=true;
  1515. list.concat(ai);
  1516. a_call_name(list,'FPC_OVERFLOW');
  1517. a_label(list,hl);
  1518. end;
  1519. end.
  1520. {
  1521. $Log$
  1522. Revision 1.80 2003-10-17 14:38:32 peter
  1523. * 64k registers supported
  1524. * fixed some memory leaks
  1525. Revision 1.79 2003/10/14 00:30:48 florian
  1526. + some code for PIC support added
  1527. Revision 1.78 2003/10/13 01:23:13 florian
  1528. * some ideas for mm support implemented
  1529. Revision 1.77 2003/10/11 16:06:42 florian
  1530. * fixed some MMX<->SSE
  1531. * started to fix ppc, needs an overhaul
  1532. + stabs info improve for spilling, not sure if it works correctly/completly
  1533. - MMX_SUPPORT removed from Makefile.fpc
  1534. Revision 1.76 2003/10/10 17:48:14 peter
  1535. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1536. * tregisteralloctor renamed to trgobj
  1537. * removed rgobj from a lot of units
  1538. * moved location_* and reference_* to cgobj
  1539. * first things for mmx register allocation
  1540. Revision 1.75 2003/10/09 21:31:37 daniel
  1541. * Register allocator splitted, ans abstract now
  1542. Revision 1.74 2003/10/07 16:09:03 florian
  1543. * x86 supports only mem/reg to reg for movsx and movzx
  1544. Revision 1.73 2003/10/07 15:17:07 peter
  1545. * inline supported again, LOC_REFERENCEs are used to pass the
  1546. parameters
  1547. * inlineparasymtable,inlinelocalsymtable removed
  1548. * exitlabel inserting fixed
  1549. Revision 1.72 2003/10/03 22:00:33 peter
  1550. * parameter alignment fixes
  1551. Revision 1.71 2003/10/03 14:45:37 peter
  1552. * save ESP after pusha and restore before popa for save all registers
  1553. Revision 1.70 2003/10/01 20:34:51 peter
  1554. * procinfo unit contains tprocinfo
  1555. * cginfo renamed to cgbase
  1556. * moved cgmessage to verbose
  1557. * fixed ppc and sparc compiles
  1558. Revision 1.69 2003/09/30 19:53:47 peter
  1559. * fix pushw reg
  1560. Revision 1.68 2003/09/29 20:58:56 peter
  1561. * optimized releasing of registers
  1562. Revision 1.67 2003/09/28 13:37:19 peter
  1563. * a_call_ref removed
  1564. Revision 1.66 2003/09/25 21:29:16 peter
  1565. * change push/pop in getreg/ungetreg
  1566. Revision 1.65 2003/09/25 13:13:32 florian
  1567. * more x86-64 fixes
  1568. Revision 1.64 2003/09/11 11:55:00 florian
  1569. * improved arm code generation
  1570. * move some protected and private field around
  1571. * the temp. register for register parameters/arguments are now released
  1572. before the move to the parameter register is done. This improves
  1573. the code in a lot of cases.
  1574. Revision 1.63 2003/09/09 21:03:17 peter
  1575. * basics for x86 register calling
  1576. Revision 1.62 2003/09/09 20:59:27 daniel
  1577. * Adding register allocation order
  1578. Revision 1.61 2003/09/07 22:09:35 peter
  1579. * preparations for different default calling conventions
  1580. * various RA fixes
  1581. Revision 1.60 2003/09/05 17:41:13 florian
  1582. * merged Wiktor's Watcom patches in 1.1
  1583. Revision 1.59 2003/09/03 15:55:02 peter
  1584. * NEWRA branch merged
  1585. Revision 1.58.2.5 2003/08/31 20:40:50 daniel
  1586. * Fixed add_edges_used
  1587. Revision 1.58.2.4 2003/08/31 15:46:26 peter
  1588. * more updates for tregister
  1589. Revision 1.58.2.3 2003/08/29 17:29:00 peter
  1590. * next batch of updates
  1591. Revision 1.58.2.2 2003/08/28 18:35:08 peter
  1592. * tregister changed to cardinal
  1593. Revision 1.58.2.1 2003/08/27 21:06:34 peter
  1594. * more updates
  1595. Revision 1.58 2003/08/20 19:28:21 daniel
  1596. * Small NOTARGETWIN32 conditional tweak
  1597. Revision 1.57 2003/07/03 18:59:25 peter
  1598. * loadfpu_reg_reg size specifier
  1599. Revision 1.56 2003/06/14 14:53:50 jonas
  1600. * fixed newra cycle for x86
  1601. * added constants for indicating source and destination operands of the
  1602. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  1603. Revision 1.55 2003/06/13 21:19:32 peter
  1604. * current_procdef removed, use current_procinfo.procdef instead
  1605. Revision 1.54 2003/06/12 18:31:18 peter
  1606. * fix newra cycle for i386
  1607. Revision 1.53 2003/06/07 10:24:10 peter
  1608. * fixed copyvaluepara for left-to-right pushing
  1609. Revision 1.52 2003/06/07 10:06:55 jonas
  1610. * fixed cycling problem
  1611. Revision 1.51 2003/06/03 21:11:09 peter
  1612. * cg.a_load_* get a from and to size specifier
  1613. * makeregsize only accepts newregister
  1614. * i386 uses generic tcgnotnode,tcgunaryminus
  1615. Revision 1.50 2003/06/03 13:01:59 daniel
  1616. * Register allocator finished
  1617. Revision 1.49 2003/06/01 21:38:07 peter
  1618. * getregisterfpu size parameter added
  1619. * op_const_reg size parameter added
  1620. * sparc updates
  1621. Revision 1.48 2003/05/30 23:57:08 peter
  1622. * more sparc cleanup
  1623. * accumulator removed, splitted in function_return_reg (called) and
  1624. function_result_reg (caller)
  1625. Revision 1.47 2003/05/22 21:33:31 peter
  1626. * removed some unit dependencies
  1627. Revision 1.46 2003/05/16 14:33:31 peter
  1628. * regvar fixes
  1629. Revision 1.45 2003/05/15 18:58:54 peter
  1630. * removed selfpointer_offset, vmtpointer_offset
  1631. * tvarsym.adjusted_address
  1632. * address in localsymtable is now in the real direction
  1633. * removed some obsolete globals
  1634. Revision 1.44 2003/04/30 20:53:32 florian
  1635. * error when address of an abstract method is taken
  1636. * fixed some x86-64 problems
  1637. * merged some more x86-64 and i386 code
  1638. Revision 1.43 2003/04/27 11:21:36 peter
  1639. * aktprocdef renamed to current_procinfo.procdef
  1640. * procinfo renamed to current_procinfo
  1641. * procinfo will now be stored in current_module so it can be
  1642. cleaned up properly
  1643. * gen_main_procsym changed to create_main_proc and release_main_proc
  1644. to also generate a tprocinfo structure
  1645. * fixed unit implicit initfinal
  1646. Revision 1.42 2003/04/23 14:42:08 daniel
  1647. * Further register allocator work. Compiler now smaller with new
  1648. allocator than without.
  1649. * Somebody forgot to adjust ppu version number
  1650. Revision 1.41 2003/04/23 09:51:16 daniel
  1651. * Removed usage of edi in a lot of places when new register allocator used
  1652. + Added newra versions of g_concatcopy and secondadd_float
  1653. Revision 1.40 2003/04/22 13:47:08 peter
  1654. * fixed C style array of const
  1655. * fixed C array passing
  1656. * fixed left to right with high parameters
  1657. Revision 1.39 2003/04/22 10:09:35 daniel
  1658. + Implemented the actual register allocator
  1659. + Scratch registers unavailable when new register allocator used
  1660. + maybe_save/maybe_restore unavailable when new register allocator used
  1661. Revision 1.38 2003/04/17 16:48:21 daniel
  1662. * Added some code to keep track of move instructions in register
  1663. allocator
  1664. Revision 1.37 2003/03/28 19:16:57 peter
  1665. * generic constructor working for i386
  1666. * remove fixed self register
  1667. * esi added as address register for i386
  1668. Revision 1.36 2003/03/18 18:17:46 peter
  1669. * reg2opsize()
  1670. Revision 1.35 2003/03/13 19:52:23 jonas
  1671. * and more new register allocator fixes (in the i386 code generator this
  1672. time). At least now the ppc cross compiler can compile the linux
  1673. system unit again, but I haven't tested it.
  1674. Revision 1.34 2003/02/27 16:40:32 daniel
  1675. * Fixed ie 200301234 problem on Win32 target
  1676. Revision 1.33 2003/02/26 21:15:43 daniel
  1677. * Fixed the optimizer
  1678. Revision 1.32 2003/02/19 22:00:17 daniel
  1679. * Code generator converted to new register notation
  1680. - Horribily outdated todo.txt removed
  1681. Revision 1.31 2003/01/21 10:41:13 daniel
  1682. * Fixed another 200301081
  1683. Revision 1.30 2003/01/13 23:00:18 daniel
  1684. * Fixed internalerror
  1685. Revision 1.29 2003/01/13 14:54:34 daniel
  1686. * Further work to convert codegenerator register convention;
  1687. internalerror bug fixed.
  1688. Revision 1.28 2003/01/09 20:41:00 daniel
  1689. * Converted some code in cgx86.pas to new register numbering
  1690. Revision 1.27 2003/01/08 18:43:58 daniel
  1691. * Tregister changed into a record
  1692. Revision 1.26 2003/01/05 13:36:53 florian
  1693. * x86-64 compiles
  1694. + very basic support for float128 type (x86-64 only)
  1695. Revision 1.25 2003/01/02 16:17:50 peter
  1696. * align stack on 4 bytes in copyvalueopenarray
  1697. Revision 1.24 2002/12/24 15:56:50 peter
  1698. * stackpointer_alloc added for adjusting ESP. Win32 needs
  1699. this for the pageprotection
  1700. Revision 1.23 2002/11/25 18:43:34 carl
  1701. - removed the invalid if <> checking (Delphi is strange on this)
  1702. + implemented abstract warning on instance creation of class with
  1703. abstract methods.
  1704. * some error message cleanups
  1705. Revision 1.22 2002/11/25 17:43:29 peter
  1706. * splitted defbase in defutil,symutil,defcmp
  1707. * merged isconvertable and is_equal into compare_defs(_ext)
  1708. * made operator search faster by walking the list only once
  1709. Revision 1.21 2002/11/18 17:32:01 peter
  1710. * pass proccalloption to ret_in_xxx and push_xxx functions
  1711. Revision 1.20 2002/11/09 21:18:31 carl
  1712. * flags2reg() was not extending the byte register to the correct result size
  1713. Revision 1.19 2002/10/16 19:01:43 peter
  1714. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1715. implicit exception frames for procedures with initialized variables
  1716. and for constructors. The default is on for compatibility
  1717. Revision 1.18 2002/10/05 12:43:30 carl
  1718. * fixes for Delphi 6 compilation
  1719. (warning : Some features do not work under Delphi)
  1720. Revision 1.17 2002/09/17 18:54:06 jonas
  1721. * a_load_reg_reg() now has two size parameters: source and dest. This
  1722. allows some optimizations on architectures that don't encode the
  1723. register size in the register name.
  1724. Revision 1.16 2002/09/16 19:08:47 peter
  1725. * support references without registers and symbol in paramref_addr. It
  1726. pushes only the offset
  1727. Revision 1.15 2002/09/16 18:06:29 peter
  1728. * move CGSize2Opsize to interface
  1729. Revision 1.14 2002/09/01 14:42:41 peter
  1730. * removevaluepara added to fix the stackpointer so restoring of
  1731. saved registers works
  1732. Revision 1.13 2002/09/01 12:09:27 peter
  1733. + a_call_reg, a_call_loc added
  1734. * removed exprasmlist references
  1735. Revision 1.12 2002/08/17 09:23:50 florian
  1736. * first part of procinfo rewrite
  1737. Revision 1.11 2002/08/16 14:25:00 carl
  1738. * issameref() to test if two references are the same (then emit no opcodes)
  1739. + ret_in_reg to replace ret_in_acc
  1740. (fix some register allocation bugs at the same time)
  1741. + save_std_register now has an extra parameter which is the
  1742. usedinproc registers
  1743. Revision 1.10 2002/08/15 08:13:54 carl
  1744. - a_load_sym_ofs_reg removed
  1745. * loadvmt now calls loadaddr_ref_reg instead
  1746. Revision 1.9 2002/08/11 14:32:33 peter
  1747. * renamed current_library to objectlibrary
  1748. Revision 1.8 2002/08/11 13:24:20 peter
  1749. * saving of asmsymbols in ppu supported
  1750. * asmsymbollist global is removed and moved into a new class
  1751. tasmlibrarydata that will hold the info of a .a file which
  1752. corresponds with a single module. Added librarydata to tmodule
  1753. to keep the library info stored for the module. In the future the
  1754. objectfiles will also be stored to the tasmlibrarydata class
  1755. * all getlabel/newasmsymbol and friends are moved to the new class
  1756. Revision 1.7 2002/08/10 10:06:04 jonas
  1757. * fixed stupid bug of mine in g_flags2reg() when optimizations are on
  1758. Revision 1.6 2002/08/09 19:18:27 carl
  1759. * fix generic exception handling
  1760. Revision 1.5 2002/08/04 19:52:04 carl
  1761. + updated exception routines
  1762. Revision 1.4 2002/07/27 19:53:51 jonas
  1763. + generic implementation of tcg.g_flags2ref()
  1764. * tcg.flags2xxx() now also needs a size parameter
  1765. Revision 1.3 2002/07/26 21:15:46 florian
  1766. * rewrote the system handling
  1767. Revision 1.2 2002/07/21 16:55:34 jonas
  1768. * fixed bug in op_const_reg_reg() for imul
  1769. Revision 1.1 2002/07/20 19:28:47 florian
  1770. * splitting of i386\cgcpu.pas into x86\cgx86.pas and i386\cgcpu.pas
  1771. cgx86.pas will contain the common code for i386 and x86_64
  1772. }