ncgutil.pas 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Helper routines for all code generators
  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. unit ncgutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,cpuinfo,
  22. globtype,
  23. cpubase,cgbase,parabase,cgutils,
  24. aasmbase,aasmtai,aasmdata,aasmcpu,
  25. symconst,symbase,symdef,symsym,symtype,symtable
  26. {$ifndef cpu64bit}
  27. ,cg64f32
  28. {$endif cpu64bit}
  29. ;
  30. type
  31. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  32. pusedregvars = ^tusedregvars;
  33. tusedregvars = record
  34. intregvars, fpuregvars, mmregvars: Tsuperregisterworklist;
  35. end;
  36. {
  37. Not used currently, implemented because I thought we had to
  38. synchronise around if/then/else as well, but not needed. May
  39. still be useful for SSA once we get around to implementing
  40. that (JM)
  41. pusedregvarscommon = ^tusedregvarscommon;
  42. tusedregvarscommon = record
  43. allregvars, commonregvars, myregvars: tusedregvars;
  44. end;
  45. }
  46. procedure firstcomplex(p : tbinarynode);
  47. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  48. // procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  49. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  50. procedure location_force_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  51. procedure location_force_mem(list:TAsmList;var l:tlocation);
  52. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  53. { Retrieve the location of the data pointed to in location l, when the location is
  54. a register it is expected to contain the address of the data }
  55. procedure location_get_data_ref(list:TAsmList;const l:tlocation;var ref:treference;loadref:boolean);
  56. function maybe_pushfpu(list:TAsmList;needed : byte;var l:tlocation) : boolean;
  57. procedure alloc_proc_symbol(pd: tprocdef);
  58. procedure gen_proc_symbol(list:TAsmList);
  59. procedure gen_proc_symbol_end(list:TAsmList);
  60. procedure gen_proc_entry_code(list:TAsmList);
  61. procedure gen_proc_exit_code(list:TAsmList);
  62. procedure gen_stack_check_size_para(list:TAsmList);
  63. procedure gen_stack_check_call(list:TAsmList);
  64. procedure gen_save_used_regs(list:TAsmList);
  65. procedure gen_restore_used_regs(list:TAsmList);
  66. procedure gen_initialize_code(list:TAsmList);
  67. procedure gen_finalize_code(list:TAsmList);
  68. procedure gen_entry_code(list:TAsmList);
  69. procedure gen_exit_code(list:TAsmList);
  70. procedure gen_load_para_value(list:TAsmList);
  71. procedure gen_load_return_value(list:TAsmList);
  72. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  73. procedure gen_intf_wrappers(list:TAsmList;st:tsymtable);
  74. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  75. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  76. { adds the regvars used in n and its children to rv.allregvars,
  77. those which were already in rv.allregvars to rv.commonregvars and
  78. uses rv.myregvars as scratch (so that two uses of the same regvar
  79. in a single tree to make it appear in commonregvars). Useful to
  80. find out which regvars are used in two different node trees
  81. (e.g. in the "else" and "then" path, or in various case blocks }
  82. // procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  83. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  84. {#
  85. Allocate the buffers for exception management and setjmp environment.
  86. Return a pointer to these buffers, send them to the utility routine
  87. so they are registered, and then call setjmp.
  88. Then compare the result of setjmp with 0, and if not equal
  89. to zero, then jump to exceptlabel.
  90. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  91. It is to note that this routine may be called *after* the stackframe of a
  92. routine has been called, therefore on machines where the stack cannot
  93. be modified, all temps should be allocated on the heap instead of the
  94. stack.
  95. }
  96. const
  97. EXCEPT_BUF_SIZE = 3*sizeof(aint);
  98. type
  99. texceptiontemps=record
  100. jmpbuf,
  101. envbuf,
  102. reasonbuf : treference;
  103. end;
  104. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  105. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  106. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  107. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  108. procedure insertbssdata(sym : tglobalvarsym);
  109. procedure gen_alloc_symtable(list:TAsmList;st:tsymtable);
  110. procedure gen_free_symtable(list:TAsmList;st:tsymtable);
  111. { rtti and init/final }
  112. procedure generate_rtti(p:Ttypesym);
  113. procedure generate_inittable(p:tsym);
  114. procedure location_free(list: TAsmList; const location : TLocation);
  115. function getprocalign : shortint;
  116. procedure gen_pic_helpers(list : TAsmList);
  117. procedure gen_got_load(list : TAsmList);
  118. implementation
  119. uses
  120. version,
  121. cutils,cclasses,
  122. globals,systems,verbose,
  123. ppu,defutil,
  124. procinfo,paramgr,fmodule,
  125. regvars,dbgbase,
  126. pass_1,pass_2,
  127. nbas,ncon,nld,nutils,
  128. tgobj,cgobj
  129. {$ifdef powerpc}
  130. , cpupi
  131. {$endif}
  132. {$ifdef powerpc64}
  133. , cpupi
  134. {$endif}
  135. ;
  136. {*****************************************************************************
  137. Misc Helpers
  138. *****************************************************************************}
  139. procedure location_free(list: TAsmList; const location : TLocation);
  140. begin
  141. case location.loc of
  142. LOC_VOID:
  143. ;
  144. LOC_REGISTER,
  145. LOC_CREGISTER:
  146. begin
  147. if getsupreg(location.register)<first_int_imreg then
  148. cg.ungetcpuregister(list,location.register);
  149. end;
  150. LOC_FPUREGISTER,
  151. LOC_CFPUREGISTER:
  152. begin
  153. if getsupreg(location.register)<first_fpu_imreg then
  154. cg.ungetcpuregister(list,location.register);
  155. end;
  156. LOC_MMREGISTER,
  157. LOC_CMMREGISTER :
  158. begin
  159. if getsupreg(location.register)<first_mm_imreg then
  160. cg.ungetcpuregister(list,location.register);
  161. end;
  162. LOC_REFERENCE,
  163. LOC_CREFERENCE :
  164. begin
  165. if use_fixed_stack then
  166. location_freetemp(list,location);
  167. end;
  168. else
  169. internalerror(2004110211);
  170. end;
  171. end;
  172. { DO NOT RELY on the fact that the tnode is not yet swaped
  173. because of inlining code PM }
  174. procedure firstcomplex(p : tbinarynode);
  175. var
  176. hp : tnode;
  177. begin
  178. { always calculate boolean AND and OR from left to right }
  179. if (p.nodetype in [orn,andn]) and
  180. is_boolean(p.left.resulttype.def) then
  181. begin
  182. if nf_swaped in p.flags then
  183. internalerror(234234);
  184. end
  185. else
  186. if (
  187. (p.expectloc=LOC_FPUREGISTER) and
  188. (p.right.registersfpu > p.left.registersfpu)
  189. ) or
  190. (
  191. (
  192. (
  193. ((p.left.registersfpu = 0) and (p.right.registersfpu = 0)) or
  194. (p.expectloc<>LOC_FPUREGISTER)
  195. ) and
  196. (p.left.registersint<p.right.registersint)
  197. )
  198. ) then
  199. begin
  200. hp:=p.left;
  201. p.left:=p.right;
  202. p.right:=hp;
  203. if nf_swaped in p.flags then
  204. exclude(p.flags,nf_swaped)
  205. else
  206. include(p.flags,nf_swaped);
  207. end;
  208. end;
  209. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  210. {
  211. produces jumps to true respectively false labels using boolean expressions
  212. depending on whether the loading of regvars is currently being
  213. synchronized manually (such as in an if-node) or automatically (most of
  214. the other cases where this procedure is called), loadregvars can be
  215. "lr_load_regvars" or "lr_dont_load_regvars"
  216. }
  217. var
  218. opsize : tcgsize;
  219. storepos : tfileposinfo;
  220. begin
  221. if nf_error in p.flags then
  222. exit;
  223. storepos:=aktfilepos;
  224. aktfilepos:=p.fileinfo;
  225. if is_boolean(p.resulttype.def) then
  226. begin
  227. {$ifdef OLDREGVARS}
  228. if loadregvars = lr_load_regvars then
  229. load_all_regvars(list);
  230. {$endif OLDREGVARS}
  231. if is_constboolnode(p) then
  232. begin
  233. if tordconstnode(p).value<>0 then
  234. cg.a_jmp_always(list,current_procinfo.CurrTrueLabel)
  235. else
  236. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel)
  237. end
  238. else
  239. begin
  240. opsize:=def_cgsize(p.resulttype.def);
  241. case p.location.loc of
  242. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  243. begin
  244. {$ifdef OLDREGVARS}
  245. if (p.location.loc = LOC_CREGISTER) then
  246. load_regvar_reg(list,p.location.register);
  247. {$endif OLDREGVARS}
  248. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  249. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  250. end;
  251. LOC_JUMP:
  252. ;
  253. {$ifdef cpuflags}
  254. LOC_FLAGS :
  255. begin
  256. cg.a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  257. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  258. end;
  259. {$endif cpuflags}
  260. else
  261. begin
  262. printnode(output,p);
  263. internalerror(200308241);
  264. end;
  265. end;
  266. end;
  267. end
  268. else
  269. internalerror(200112305);
  270. aktfilepos:=storepos;
  271. end;
  272. (*
  273. This code needs fixing. It is not safe to use rgint; on the m68000 it
  274. would be rgaddr.
  275. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  276. begin
  277. case t.loc of
  278. LOC_REGISTER:
  279. begin
  280. { can't be a regvar, since it would be LOC_CREGISTER then }
  281. exclude(regs,getsupreg(t.register));
  282. if t.register64.reghi<>NR_NO then
  283. exclude(regs,getsupreg(t.register64.reghi));
  284. end;
  285. LOC_CREFERENCE,LOC_REFERENCE:
  286. begin
  287. if not(cs_opt_regvar in aktoptimizerswitches) or
  288. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  289. exclude(regs,getsupreg(t.reference.base));
  290. if not(cs_opt_regvar in aktoptimizerswitches) or
  291. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  292. exclude(regs,getsupreg(t.reference.index));
  293. end;
  294. end;
  295. end;
  296. *)
  297. {*****************************************************************************
  298. EXCEPTION MANAGEMENT
  299. *****************************************************************************}
  300. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  301. var
  302. srsym : ttypesym;
  303. begin
  304. if jmp_buf_size=-1 then
  305. begin
  306. srsym:=search_system_type('JMP_BUF');
  307. jmp_buf_size:=srsym.restype.def.size;
  308. end;
  309. tg.GetTemp(list,EXCEPT_BUF_SIZE,tt_persistent,t.envbuf);
  310. tg.GetTemp(list,jmp_buf_size,tt_persistent,t.jmpbuf);
  311. tg.GetTemp(list,sizeof(aint),tt_persistent,t.reasonbuf);
  312. end;
  313. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  314. begin
  315. tg.Ungettemp(list,t.jmpbuf);
  316. tg.ungettemp(list,t.envbuf);
  317. tg.ungettemp(list,t.reasonbuf);
  318. end;
  319. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  320. var
  321. paraloc1,paraloc2,paraloc3 : tcgpara;
  322. begin
  323. paraloc1.init;
  324. paraloc2.init;
  325. paraloc3.init;
  326. paramanager.getintparaloc(pocall_default,1,paraloc1);
  327. paramanager.getintparaloc(pocall_default,2,paraloc2);
  328. paramanager.getintparaloc(pocall_default,3,paraloc3);
  329. paramanager.allocparaloc(list,paraloc3);
  330. cg.a_paramaddr_ref(list,t.envbuf,paraloc3);
  331. paramanager.allocparaloc(list,paraloc2);
  332. cg.a_paramaddr_ref(list,t.jmpbuf,paraloc2);
  333. { push type of exceptionframe }
  334. paramanager.allocparaloc(list,paraloc1);
  335. cg.a_param_const(list,OS_S32,1,paraloc1);
  336. paramanager.freeparaloc(list,paraloc3);
  337. paramanager.freeparaloc(list,paraloc2);
  338. paramanager.freeparaloc(list,paraloc1);
  339. cg.allocallcpuregisters(list);
  340. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR');
  341. cg.deallocallcpuregisters(list);
  342. paramanager.getintparaloc(pocall_default,1,paraloc1);
  343. paramanager.allocparaloc(list,paraloc1);
  344. cg.a_param_reg(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  345. paramanager.freeparaloc(list,paraloc1);
  346. cg.allocallcpuregisters(list);
  347. cg.a_call_name(list,'FPC_SETJMP');
  348. cg.deallocallcpuregisters(list);
  349. cg.g_exception_reason_save(list, t.reasonbuf);
  350. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,cg.makeregsize(list,NR_FUNCTION_RESULT_REG,OS_S32),exceptlabel);
  351. paraloc1.done;
  352. paraloc2.done;
  353. paraloc3.done;
  354. end;
  355. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  356. begin
  357. cg.allocallcpuregisters(list);
  358. cg.a_call_name(list,'FPC_POPADDRSTACK');
  359. cg.deallocallcpuregisters(list);
  360. if not onlyfree then
  361. begin
  362. cg.g_exception_reason_load(list, t.reasonbuf);
  363. cg.a_cmp_const_reg_label(list,OS_INT,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  364. end;
  365. end;
  366. {*****************************************************************************
  367. TLocation
  368. *****************************************************************************}
  369. {$ifndef cpu64bit}
  370. { 32-bit version }
  371. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  372. var
  373. hregister,
  374. hregisterhi : tregister;
  375. hreg64 : tregister64;
  376. hl : tasmlabel;
  377. oldloc : tlocation;
  378. const_location: boolean;
  379. begin
  380. oldloc:=l;
  381. if dst_size=OS_NO then
  382. internalerror(200309144);
  383. { handle transformations to 64bit separate }
  384. if dst_size in [OS_64,OS_S64] then
  385. begin
  386. if not (l.size in [OS_64,OS_S64]) then
  387. begin
  388. { load a smaller size to OS_64 }
  389. if l.loc=LOC_REGISTER then
  390. begin
  391. hregister:=cg.makeregsize(list,l.register64.reglo,OS_32);
  392. cg.a_load_reg_reg(list,l.size,OS_32,l.register64.reglo,hregister);
  393. end
  394. else
  395. hregister:=cg.getintregister(list,OS_INT);
  396. { load value in low register }
  397. case l.loc of
  398. LOC_FLAGS :
  399. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  400. LOC_JUMP :
  401. begin
  402. cg.a_label(list,current_procinfo.CurrTrueLabel);
  403. cg.a_load_const_reg(list,OS_INT,1,hregister);
  404. current_asmdata.getjumplabel(hl);
  405. cg.a_jmp_always(list,hl);
  406. cg.a_label(list,current_procinfo.CurrFalseLabel);
  407. cg.a_load_const_reg(list,OS_INT,0,hregister);
  408. cg.a_label(list,hl);
  409. end;
  410. else
  411. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  412. end;
  413. { reset hi part, take care of the signed bit of the current value }
  414. hregisterhi:=cg.getintregister(list,OS_INT);
  415. if (l.size in [OS_S8,OS_S16,OS_S32]) then
  416. begin
  417. if l.loc=LOC_CONSTANT then
  418. begin
  419. if (longint(l.value)<0) then
  420. cg.a_load_const_reg(list,OS_32,aint($ffffffff),hregisterhi)
  421. else
  422. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  423. end
  424. else
  425. begin
  426. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  427. hregisterhi);
  428. end;
  429. end
  430. else
  431. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  432. location_reset(l,LOC_REGISTER,dst_size);
  433. l.register64.reglo:=hregister;
  434. l.register64.reghi:=hregisterhi;
  435. end
  436. else
  437. begin
  438. { 64bit to 64bit }
  439. if ((l.loc=LOC_CREGISTER) and maybeconst) then
  440. begin
  441. hregister:=l.register64.reglo;
  442. hregisterhi:=l.register64.reghi;
  443. const_location := true;
  444. end
  445. else
  446. begin
  447. hregister:=cg.getintregister(list,OS_INT);
  448. hregisterhi:=cg.getintregister(list,OS_INT);
  449. const_location := false;
  450. end;
  451. hreg64.reglo:=hregister;
  452. hreg64.reghi:=hregisterhi;
  453. { load value in new register }
  454. cg64.a_load64_loc_reg(list,l,hreg64);
  455. if not const_location then
  456. location_reset(l,LOC_REGISTER,dst_size)
  457. else
  458. location_reset(l,LOC_CREGISTER,dst_size);
  459. l.register64.reglo:=hregister;
  460. l.register64.reghi:=hregisterhi;
  461. end;
  462. end
  463. else
  464. begin
  465. {Do not bother to recycle the existing register. The register
  466. allocator eliminates unnecessary moves, so it's not needed
  467. and trying to recycle registers can cause problems because
  468. the registers changes size and may need aditional constraints.
  469. Not if it's about LOC_CREGISTER's (JM)
  470. }
  471. const_location :=
  472. (maybeconst) and
  473. (l.loc = LOC_CREGISTER) and
  474. (TCGSize2Size[l.size] = TCGSize2Size[dst_size]) and
  475. ((l.size = dst_size) or
  476. (TCGSize2Size[l.size] = TCGSize2Size[OS_INT]));
  477. if not const_location then
  478. hregister:=cg.getintregister(list,dst_size)
  479. else
  480. hregister := l.register;
  481. { load value in new register }
  482. case l.loc of
  483. LOC_FLAGS :
  484. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  485. LOC_JUMP :
  486. begin
  487. cg.a_label(list,current_procinfo.CurrTrueLabel);
  488. cg.a_load_const_reg(list,dst_size,1,hregister);
  489. current_asmdata.getjumplabel(hl);
  490. cg.a_jmp_always(list,hl);
  491. cg.a_label(list,current_procinfo.CurrFalseLabel);
  492. cg.a_load_const_reg(list,dst_size,0,hregister);
  493. cg.a_label(list,hl);
  494. end;
  495. else
  496. begin
  497. { load_loc_reg can only handle size >= l.size, when the
  498. new size is smaller then we need to adjust the size
  499. of the orignal and maybe recalculate l.register for i386 }
  500. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  501. begin
  502. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  503. l.register:=cg.makeregsize(list,l.register,dst_size);
  504. { for big endian systems, the reference's offset must }
  505. { be increased in this case, since they have the }
  506. { MSB first in memory and e.g. byte(word_var) should }
  507. { return the second byte in this case (JM) }
  508. if (target_info.endian = ENDIAN_BIG) and
  509. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  510. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  511. {$ifdef x86}
  512. l.size:=dst_size;
  513. {$endif x86}
  514. end;
  515. cg.a_load_loc_reg(list,dst_size,l,hregister);
  516. {$ifndef x86}
  517. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  518. l.size:=dst_size;
  519. {$endif not x86}
  520. end;
  521. end;
  522. if not const_location then
  523. location_reset(l,LOC_REGISTER,dst_size)
  524. else
  525. location_reset(l,LOC_CREGISTER,dst_size);
  526. l.register:=hregister;
  527. end;
  528. { Release temp when it was a reference }
  529. if oldloc.loc=LOC_REFERENCE then
  530. location_freetemp(list,oldloc);
  531. end;
  532. {$else cpu64bit}
  533. { 64-bit version }
  534. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  535. var
  536. hregister : tregister;
  537. hl : tasmlabel;
  538. oldloc : tlocation;
  539. begin
  540. oldloc:=l;
  541. hregister:=cg.getintregister(list,dst_size);
  542. { load value in new register }
  543. case l.loc of
  544. LOC_FLAGS :
  545. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  546. LOC_JUMP :
  547. begin
  548. cg.a_label(list,current_procinfo.CurrTrueLabel);
  549. cg.a_load_const_reg(list,dst_size,1,hregister);
  550. current_asmdata.getjumplabel(hl);
  551. cg.a_jmp_always(list,hl);
  552. cg.a_label(list,current_procinfo.CurrFalseLabel);
  553. cg.a_load_const_reg(list,dst_size,0,hregister);
  554. cg.a_label(list,hl);
  555. end;
  556. else
  557. begin
  558. { load_loc_reg can only handle size >= l.size, when the
  559. new size is smaller then we need to adjust the size
  560. of the orignal and maybe recalculate l.register for i386 }
  561. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  562. begin
  563. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  564. l.register:=cg.makeregsize(list,l.register,dst_size);
  565. { for big endian systems, the reference's offset must }
  566. { be increased in this case, since they have the }
  567. { MSB first in memory and e.g. byte(word_var) should }
  568. { return the second byte in this case (JM) }
  569. if (target_info.endian = ENDIAN_BIG) and
  570. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  571. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  572. {$ifdef x86}
  573. l.size:=dst_size;
  574. {$endif x86}
  575. end;
  576. cg.a_load_loc_reg(list,dst_size,l,hregister);
  577. {$ifndef x86}
  578. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  579. l.size:=dst_size;
  580. {$endif not x86}
  581. end;
  582. end;
  583. if (l.loc <> LOC_CREGISTER) or
  584. not maybeconst then
  585. location_reset(l,LOC_REGISTER,dst_size)
  586. else
  587. location_reset(l,LOC_CREGISTER,dst_size);
  588. l.register:=hregister;
  589. { Release temp when it was a reference }
  590. if oldloc.loc=LOC_REFERENCE then
  591. location_freetemp(list,oldloc);
  592. end;
  593. {$endif cpu64bit}
  594. procedure location_force_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  595. var
  596. reg : tregister;
  597. href : treference;
  598. begin
  599. if (l.loc<>LOC_FPUREGISTER) and
  600. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  601. begin
  602. { if it's in an mm register, store to memory first }
  603. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  604. begin
  605. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  606. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,href,mms_movescalar);
  607. location_reset(l,LOC_REFERENCE,l.size);
  608. l.reference:=href;
  609. end;
  610. reg:=cg.getfpuregister(list,l.size);
  611. cg.a_loadfpu_loc_reg(list,l,reg);
  612. location_freetemp(list,l);
  613. location_reset(l,LOC_FPUREGISTER,l.size);
  614. l.register:=reg;
  615. end;
  616. end;
  617. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  618. var
  619. reg : tregister;
  620. href : treference;
  621. begin
  622. if (l.loc<>LOC_MMREGISTER) and
  623. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  624. begin
  625. { if it's in an fpu register, store to memory first }
  626. if (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  627. begin
  628. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  629. cg.a_loadfpu_reg_ref(list,l.size,l.register,href);
  630. location_reset(l,LOC_REFERENCE,l.size);
  631. l.reference:=href;
  632. end;
  633. reg:=cg.getmmregister(list,l.size);
  634. cg.a_loadmm_loc_reg(list,l.size,l,reg,mms_movescalar);
  635. location_freetemp(list,l);
  636. location_reset(l,LOC_MMREGISTER,l.size);
  637. l.register:=reg;
  638. end;
  639. end;
  640. procedure location_force_mem(list:TAsmList;var l:tlocation);
  641. var
  642. r : treference;
  643. begin
  644. case l.loc of
  645. LOC_FPUREGISTER,
  646. LOC_CFPUREGISTER :
  647. begin
  648. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  649. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  650. location_reset(l,LOC_REFERENCE,l.size);
  651. l.reference:=r;
  652. end;
  653. LOC_MMREGISTER,
  654. LOC_CMMREGISTER:
  655. begin
  656. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  657. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  658. location_reset(l,LOC_REFERENCE,l.size);
  659. l.reference:=r;
  660. end;
  661. LOC_CONSTANT,
  662. LOC_REGISTER,
  663. LOC_CREGISTER :
  664. begin
  665. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  666. {$ifndef cpu64bit}
  667. if l.size in [OS_64,OS_S64] then
  668. cg64.a_load64_loc_ref(list,l,r)
  669. else
  670. {$endif cpu64bit}
  671. cg.a_load_loc_ref(list,l.size,l,r);
  672. location_reset(l,LOC_REFERENCE,l.size);
  673. l.reference:=r;
  674. end;
  675. LOC_CREFERENCE,
  676. LOC_REFERENCE : ;
  677. else
  678. internalerror(200203219);
  679. end;
  680. end;
  681. procedure location_get_data_ref(list:TAsmList;const l:tlocation;var ref:treference;loadref:boolean);
  682. begin
  683. case l.loc of
  684. LOC_REGISTER,
  685. LOC_CREGISTER :
  686. begin
  687. if not loadref then
  688. internalerror(200410231);
  689. reference_reset_base(ref,l.register,0);
  690. end;
  691. LOC_REFERENCE,
  692. LOC_CREFERENCE :
  693. begin
  694. if loadref then
  695. begin
  696. reference_reset_base(ref,cg.getaddressregister(list),0);
  697. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,l.reference,ref.base);
  698. end
  699. else
  700. ref:=l.reference;
  701. end;
  702. else
  703. internalerror(200309181);
  704. end;
  705. end;
  706. {*****************************************************************************
  707. Maybe_Save
  708. *****************************************************************************}
  709. function maybe_pushfpu(list:TAsmList;needed : byte;var l:tlocation) : boolean;
  710. begin
  711. {$ifdef i386}
  712. if (needed>=maxfpuregs) and
  713. (l.loc = LOC_FPUREGISTER) then
  714. begin
  715. location_force_mem(list,l);
  716. maybe_pushfpu:=true;
  717. end
  718. else
  719. maybe_pushfpu:=false;
  720. {$else i386}
  721. maybe_pushfpu:=false;
  722. {$endif i386}
  723. end;
  724. {****************************************************************************
  725. Init/Finalize Code
  726. ****************************************************************************}
  727. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  728. var
  729. href : treference;
  730. hreg : tregister;
  731. list : TAsmList;
  732. hsym : tparavarsym;
  733. l : longint;
  734. localcopyloc : tlocation;
  735. begin
  736. list:=TAsmList(arg);
  737. if (tsym(p).typ=paravarsym) and
  738. (tparavarsym(p).varspez=vs_value) and
  739. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vartype.def,current_procinfo.procdef.proccalloption)) then
  740. begin
  741. location_get_data_ref(list,tparavarsym(p).localloc,href,true);
  742. if is_open_array(tparavarsym(p).vartype.def) or
  743. is_array_of_const(tparavarsym(p).vartype.def) then
  744. begin
  745. { cdecl functions don't have a high pointer so it is not possible to generate
  746. a local copy }
  747. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  748. begin
  749. hsym:=tparavarsym(tsym(p).owner.search('high'+p.name));
  750. if not assigned(hsym) then
  751. internalerror(200306061);
  752. hreg:=cg.getaddressregister(list);
  753. cg.g_copyvaluepara_openarray(list,href,hsym.localloc,tarraydef(tparavarsym(p).vartype.def).elesize,hreg);
  754. cg.a_load_reg_loc(list,OS_ADDR,hreg,tparavarsym(p).localloc);
  755. end;
  756. end
  757. else
  758. begin
  759. { Allocate space for the local copy }
  760. l:=tparavarsym(p).getsize;
  761. localcopyloc.loc:=LOC_REFERENCE;
  762. localcopyloc.size:=int_cgsize(l);
  763. tg.GetLocal(list,l,tparavarsym(p).vartype.def,localcopyloc.reference);
  764. { Copy data }
  765. if is_shortstring(tparavarsym(p).vartype.def) then
  766. begin
  767. { this code is only executed before the code for the body and the entry/exit code is generated
  768. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  769. }
  770. include(current_procinfo.flags,pi_do_call);
  771. cg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vartype.def).len)
  772. end
  773. else
  774. begin
  775. { pass proper alignment info }
  776. localcopyloc.reference.alignment:=tparavarsym(p).vartype.def.alignment;
  777. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vartype.def.size);
  778. end;
  779. { update localloc of varsym }
  780. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  781. tparavarsym(p).localloc:=localcopyloc;
  782. end;
  783. end;
  784. end;
  785. { initializes the regvars from staticsymtable with 0 }
  786. procedure initialize_regvars(p : tnamedindexitem;arg:pointer);
  787. begin
  788. if (tsym(p).typ=globalvarsym) then
  789. begin
  790. case tglobalvarsym(p).localloc.loc of
  791. LOC_CREGISTER :
  792. cg.a_load_const_reg(TAsmList(arg),reg_cgsize(tglobalvarsym(p).localloc.register),0,
  793. tglobalvarsym(p).localloc.register);
  794. LOC_REFERENCE : ;
  795. LOC_CMMREGISTER :
  796. { clear the whole register }
  797. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tglobalvarsym(p).localloc.register),
  798. tglobalvarsym(p).localloc.register,
  799. tglobalvarsym(p).localloc.register,
  800. nil);
  801. LOC_CFPUREGISTER :
  802. ;
  803. else
  804. internalerror(200410124);
  805. end;
  806. end;
  807. end;
  808. { generates the code for initialisation of local data }
  809. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  810. var
  811. OldAsmList : TAsmList;
  812. hp : tnode;
  813. begin
  814. if (tsym(p).typ in [globalvarsym,localvarsym]) and
  815. (tabstractvarsym(p).refs>0) and
  816. not(vo_is_external in tabstractvarsym(p).varoptions) and
  817. not(is_class(tabstractvarsym(p).vartype.def)) and
  818. tabstractvarsym(p).vartype.def.needs_inittable then
  819. begin
  820. OldAsmList:=current_asmdata.CurrAsmList;
  821. current_asmdata.CurrAsmList:=TAsmList(arg);
  822. hp:=initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner));
  823. firstpass(hp);
  824. secondpass(hp);
  825. hp.free;
  826. current_asmdata.CurrAsmList:=OldAsmList;
  827. end;
  828. end;
  829. procedure finalize_sym(asmlist:TAsmList;sym:tsym);
  830. var
  831. hp : tnode;
  832. OldAsmList : TAsmList;
  833. begin
  834. include(current_procinfo.flags,pi_needs_implicit_finally);
  835. OldAsmList:=current_asmdata.CurrAsmList;
  836. current_asmdata.CurrAsmList:=asmlist;
  837. hp:=finalize_data_node(cloadnode.create(sym,sym.owner));
  838. firstpass(hp);
  839. secondpass(hp);
  840. hp.free;
  841. current_asmdata.CurrAsmList:=OldAsmList;
  842. end;
  843. { generates the code for finalisation of local variables }
  844. procedure finalize_local_vars(p : tnamedindexitem;arg:pointer);
  845. begin
  846. if (tsym(p).typ=localvarsym) and
  847. (tlocalvarsym(p).refs>0) and
  848. not(vo_is_external in tlocalvarsym(p).varoptions) and
  849. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  850. not(is_class(tlocalvarsym(p).vartype.def)) and
  851. tlocalvarsym(p).vartype.def.needs_inittable then
  852. finalize_sym(TAsmList(arg),tsym(p));
  853. end;
  854. { generates the code for finalisation of local typedconsts }
  855. procedure finalize_local_typedconst(p : tnamedindexitem;arg:pointer);
  856. var
  857. i : longint;
  858. pd : tprocdef;
  859. begin
  860. case tsym(p).typ of
  861. typedconstsym :
  862. begin
  863. if ttypedconstsym(p).is_writable and
  864. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  865. finalize_sym(TAsmList(arg),tsym(p));
  866. end;
  867. procsym :
  868. begin
  869. for i:=1 to tprocsym(p).procdef_count do
  870. begin
  871. pd:=tprocsym(p).procdef[i];
  872. if assigned(pd.localst) and
  873. (pd.procsym=tprocsym(p)) and
  874. (pd.localst.symtabletype<>staticsymtable) then
  875. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  876. end;
  877. end;
  878. end;
  879. end;
  880. { generates the code for finalization of static symtable and
  881. all local (static) typedconsts }
  882. procedure finalize_static_data(p : tnamedindexitem;arg:pointer);
  883. var
  884. i : longint;
  885. pd : tprocdef;
  886. begin
  887. case tsym(p).typ of
  888. globalvarsym :
  889. begin
  890. if (tglobalvarsym(p).refs>0) and
  891. not(vo_is_funcret in tglobalvarsym(p).varoptions) and
  892. not(vo_is_external in tglobalvarsym(p).varoptions) and
  893. not(is_class(tglobalvarsym(p).vartype.def)) and
  894. tglobalvarsym(p).vartype.def.needs_inittable then
  895. finalize_sym(TAsmList(arg),tsym(p));
  896. end;
  897. typedconstsym :
  898. begin
  899. if ttypedconstsym(p).is_writable and
  900. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  901. finalize_sym(TAsmList(arg),tsym(p));
  902. end;
  903. procsym :
  904. begin
  905. for i:=1 to tprocsym(p).procdef_count do
  906. begin
  907. pd:=tprocsym(p).procdef[i];
  908. if assigned(pd.localst) and
  909. (pd.procsym=tprocsym(p)) and
  910. (pd.localst.symtabletype<>staticsymtable) then
  911. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  912. end;
  913. end;
  914. end;
  915. end;
  916. { generates the code for incrementing the reference count of parameters and
  917. initialize out parameters }
  918. procedure init_paras(p : tnamedindexitem;arg:pointer);
  919. var
  920. href : treference;
  921. tmpreg : tregister;
  922. list : TAsmList;
  923. begin
  924. list:=TAsmList(arg);
  925. if (tsym(p).typ=paravarsym) and
  926. not is_class_or_interface(tparavarsym(p).vartype.def) and
  927. tparavarsym(p).vartype.def.needs_inittable then
  928. begin
  929. case tparavarsym(p).varspez of
  930. vs_value :
  931. begin
  932. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  933. cg.g_incrrefcount(list,tparavarsym(p).vartype.def,href);
  934. end;
  935. vs_out :
  936. begin
  937. tmpreg:=cg.getaddressregister(list);
  938. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).localloc,tmpreg);
  939. reference_reset_base(href,tmpreg,0);
  940. cg.g_initialize(list,tparavarsym(p).vartype.def,href);
  941. end;
  942. end;
  943. end;
  944. end;
  945. { generates the code for decrementing the reference count of parameters }
  946. procedure final_paras(p : tnamedindexitem;arg:pointer);
  947. var
  948. list : TAsmList;
  949. href : treference;
  950. begin
  951. if not(tsym(p).typ=paravarsym) then
  952. exit;
  953. list:=TAsmList(arg);
  954. if not is_class_or_interface(tparavarsym(p).vartype.def) and
  955. tparavarsym(p).vartype.def.needs_inittable then
  956. begin
  957. if (tparavarsym(p).varspez=vs_value) then
  958. begin
  959. include(current_procinfo.flags,pi_needs_implicit_finally);
  960. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  961. cg.g_decrrefcount(list,tparavarsym(p).vartype.def,href);
  962. end;
  963. end
  964. else
  965. if (tparavarsym(p).varspez=vs_value) and
  966. (is_open_array(tparavarsym(p).vartype.def) or
  967. is_array_of_const(tparavarsym(p).vartype.def)) then
  968. begin
  969. { cdecl functions don't have a high pointer so it is not possible to generate
  970. a local copy }
  971. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  972. cg.g_releasevaluepara_openarray(list,tparavarsym(p).localloc);
  973. end;
  974. end;
  975. { Initialize temp ansi/widestrings,interfaces }
  976. procedure inittempvariables(list:TAsmList);
  977. var
  978. hp : ptemprecord;
  979. href : treference;
  980. begin
  981. hp:=tg.templist;
  982. while assigned(hp) do
  983. begin
  984. if assigned(hp^.def) and
  985. hp^.def.needs_inittable then
  986. begin
  987. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  988. cg.g_initialize(list,hp^.def,href);
  989. end;
  990. hp:=hp^.next;
  991. end;
  992. end;
  993. procedure finalizetempvariables(list:TAsmList);
  994. var
  995. hp : ptemprecord;
  996. href : treference;
  997. begin
  998. hp:=tg.templist;
  999. while assigned(hp) do
  1000. begin
  1001. if assigned(hp^.def) and
  1002. hp^.def.needs_inittable then
  1003. begin
  1004. include(current_procinfo.flags,pi_needs_implicit_finally);
  1005. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  1006. cg.g_finalize(list,hp^.def,href);
  1007. end;
  1008. hp:=hp^.next;
  1009. end;
  1010. end;
  1011. procedure gen_load_return_value(list:TAsmList);
  1012. var
  1013. {$ifndef cpu64bit}
  1014. href : treference;
  1015. {$endif cpu64bit}
  1016. ressym : tabstractnormalvarsym;
  1017. resloc,
  1018. restmploc : tlocation;
  1019. hreg : tregister;
  1020. funcretloc : tlocation;
  1021. begin
  1022. { Is the loading needed? }
  1023. if (current_procinfo.procdef.funcretloc[calleeside].loc=LOC_VOID) or
  1024. (
  1025. (po_assembler in current_procinfo.procdef.procoptions) and
  1026. (not(assigned(current_procinfo.procdef.funcretsym)) or
  1027. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  1028. ) then
  1029. exit;
  1030. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  1031. { constructors return self }
  1032. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1033. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.search('self'))
  1034. else
  1035. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  1036. if (ressym.refs>0) then
  1037. begin
  1038. {$ifdef OLDREGVARS}
  1039. case ressym.localloc.loc of
  1040. LOC_CFPUREGISTER,
  1041. LOC_FPUREGISTER:
  1042. begin
  1043. location_reset(restmploc,LOC_CFPUREGISTER,funcretloc^.size);
  1044. restmploc.register:=ressym.localloc.register;
  1045. end;
  1046. LOC_CREGISTER,
  1047. LOC_REGISTER:
  1048. begin
  1049. location_reset(restmploc,LOC_CREGISTER,funcretloc^.size);
  1050. restmploc.register:=ressym.localloc.register;
  1051. end;
  1052. LOC_MMREGISTER:
  1053. begin
  1054. location_reset(restmploc,LOC_CMMREGISTER,funcretloc^.size);
  1055. restmploc.register:=ressym.localloc.register;
  1056. end;
  1057. LOC_REFERENCE:
  1058. begin
  1059. location_reset(restmploc,LOC_REFERENCE,funcretloc^.size);
  1060. restmploc.reference:=ressym.localloc.reference;
  1061. end;
  1062. else
  1063. internalerror(200309184);
  1064. end;
  1065. {$else}
  1066. restmploc:=ressym.localloc;
  1067. {$endif}
  1068. { Here, we return the function result. In most architectures, the value is
  1069. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1070. function returns in a register and the caller receives it in an other one }
  1071. case funcretloc.loc of
  1072. LOC_REGISTER:
  1073. begin
  1074. {$ifndef cpu64bit}
  1075. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64] then
  1076. begin
  1077. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1078. if resloc.loc<>LOC_REGISTER then
  1079. internalerror(200409141);
  1080. { Load low and high register separate to generate better register
  1081. allocation info }
  1082. if getsupreg(resloc.register64.reglo)<first_int_imreg then
  1083. begin
  1084. cg.getcpuregister(list,resloc.register64.reglo);
  1085. end;
  1086. case restmploc.loc of
  1087. LOC_REFERENCE :
  1088. begin
  1089. href:=restmploc.reference;
  1090. if target_info.endian=ENDIAN_BIG then
  1091. inc(href.offset,4);
  1092. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reglo);
  1093. end;
  1094. LOC_CREGISTER :
  1095. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reglo,resloc.register64.reglo);
  1096. else
  1097. internalerror(200409203);
  1098. end;
  1099. if getsupreg(resloc.register64.reghi)<first_int_imreg then
  1100. begin
  1101. cg.getcpuregister(list,resloc.register64.reghi);
  1102. end;
  1103. case restmploc.loc of
  1104. LOC_REFERENCE :
  1105. begin
  1106. href:=restmploc.reference;
  1107. if target_info.endian=ENDIAN_LITTLE then
  1108. inc(href.offset,4);
  1109. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reghi);
  1110. end;
  1111. LOC_CREGISTER :
  1112. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reghi,resloc.register64.reghi);
  1113. else
  1114. internalerror(200409204);
  1115. end;
  1116. end
  1117. else
  1118. {$endif cpu64bit}
  1119. begin
  1120. hreg:=cg.makeregsize(list,funcretloc.register,funcretloc.size);
  1121. if getsupreg(funcretloc.register)<first_int_imreg then
  1122. begin
  1123. cg.getcpuregister(list,funcretloc.register);
  1124. end;
  1125. { it could be that a structure is passed in memory but the function is expected to
  1126. return a pointer to this memory }
  1127. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1128. cg.a_load_loc_reg(list,OS_ADDR,restmploc,hreg)
  1129. else
  1130. cg.a_load_loc_reg(list,restmploc.size,restmploc,hreg);
  1131. end;
  1132. end;
  1133. LOC_FPUREGISTER:
  1134. begin
  1135. if getsupreg(funcretloc.register)<first_fpu_imreg then
  1136. begin
  1137. cg.getcpuregister(list,funcretloc.register);
  1138. end;
  1139. { we can't do direct moves between fpu and mm registers }
  1140. if restmploc.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  1141. location_force_fpureg(list,restmploc,false);
  1142. cg.a_loadfpu_loc_reg(list,restmploc,funcretloc.register);
  1143. end;
  1144. LOC_MMREGISTER:
  1145. begin
  1146. if getsupreg(funcretloc.register)<first_mm_imreg then
  1147. begin
  1148. cg.getcpuregister(list,funcretloc.register);
  1149. end;
  1150. cg.a_loadmm_loc_reg(list,restmploc.size,restmploc,funcretloc.register,mms_movescalar);
  1151. end;
  1152. LOC_INVALID,
  1153. LOC_REFERENCE:
  1154. ;
  1155. else
  1156. internalerror(200405025);
  1157. end;
  1158. end
  1159. {$ifdef x86}
  1160. else
  1161. begin
  1162. { the caller will pop a value off the cpu stack }
  1163. if (funcretloc.loc = LOC_FPUREGISTER) then
  1164. list.concat(taicpu.op_none(A_FLDZ));
  1165. end;
  1166. {$endif x86}
  1167. end;
  1168. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym);
  1169. begin
  1170. case sym.localloc.loc of
  1171. LOC_CREGISTER:
  1172. begin
  1173. {$ifndef cpu64bit}
  1174. if sym.localloc.size in [OS_64,OS_S64] then
  1175. begin
  1176. sym.localloc.register64.reglo:=cg.getintregister(list,OS_32);
  1177. sym.localloc.register64.reghi:=cg.getintregister(list,OS_32);
  1178. end
  1179. else
  1180. {$endif cpu64bit}
  1181. sym.localloc.register:=cg.getintregister(list,sym.localloc.size);
  1182. end;
  1183. LOC_CFPUREGISTER:
  1184. begin
  1185. sym.localloc.register:=cg.getfpuregister(list,sym.localloc.size);
  1186. end;
  1187. LOC_CMMREGISTER:
  1188. begin
  1189. sym. localloc.register:=cg.getmmregister(list,sym.localloc.size);
  1190. end;
  1191. end;
  1192. if (pi_has_goto in current_procinfo.flags) then
  1193. begin
  1194. { Allocate register already, to prevent first allocation to be
  1195. inside a loop }
  1196. {$ifndef cpu64bit}
  1197. if sym.localloc.size in [OS_64,OS_S64] then
  1198. begin
  1199. cg.a_reg_sync(list,sym.localloc.register64.reglo);
  1200. cg.a_reg_sync(list,sym.localloc.register64.reghi);
  1201. end
  1202. else
  1203. {$endif cpu64bit}
  1204. cg.a_reg_sync(list,sym.localloc.register);
  1205. end;
  1206. end;
  1207. procedure gen_load_para_value(list:TAsmList);
  1208. procedure get_para(const paraloc:TCGParaLocation);
  1209. begin
  1210. case paraloc.loc of
  1211. LOC_REGISTER :
  1212. begin
  1213. if getsupreg(paraloc.register)<first_int_imreg then
  1214. cg.getcpuregister(list,paraloc.register);
  1215. end;
  1216. LOC_MMREGISTER :
  1217. begin
  1218. if getsupreg(paraloc.register)<first_mm_imreg then
  1219. cg.getcpuregister(list,paraloc.register);
  1220. end;
  1221. LOC_FPUREGISTER :
  1222. begin
  1223. if getsupreg(paraloc.register)<first_fpu_imreg then
  1224. cg.getcpuregister(list,paraloc.register);
  1225. end;
  1226. end;
  1227. end;
  1228. procedure unget_para(const paraloc:TCGParaLocation);
  1229. begin
  1230. case paraloc.loc of
  1231. LOC_REGISTER :
  1232. begin
  1233. if getsupreg(paraloc.register)<first_int_imreg then
  1234. cg.ungetcpuregister(list,paraloc.register);
  1235. end;
  1236. LOC_MMREGISTER :
  1237. begin
  1238. if getsupreg(paraloc.register)<first_mm_imreg then
  1239. cg.ungetcpuregister(list,paraloc.register);
  1240. end;
  1241. LOC_FPUREGISTER :
  1242. begin
  1243. if getsupreg(paraloc.register)<first_fpu_imreg then
  1244. cg.ungetcpuregister(list,paraloc.register);
  1245. end;
  1246. end;
  1247. end;
  1248. procedure gen_load_ref(const paraloc:TCGParaLocation;const ref:treference;sizeleft:aint);
  1249. var
  1250. href : treference;
  1251. begin
  1252. case paraloc.loc of
  1253. LOC_REGISTER :
  1254. begin
  1255. {$IFDEF POWERPC64}
  1256. if (paraloc.shiftval <> 0) then
  1257. cg.a_op_const_reg_reg(list, OP_SHL, OS_INT, paraloc.shiftval, paraloc.register, paraloc.register);
  1258. {$ENDIF POWERPC64}
  1259. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1260. end;
  1261. LOC_MMREGISTER :
  1262. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1263. LOC_FPUREGISTER :
  1264. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.register,ref);
  1265. LOC_REFERENCE :
  1266. begin
  1267. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1268. { use concatcopy, because it can also be a float which fails when
  1269. load_ref_ref is used. Don't copy data when the references are equal }
  1270. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1271. cg.g_concatcopy(list,href,ref,sizeleft);
  1272. end;
  1273. else
  1274. internalerror(2002081302);
  1275. end;
  1276. end;
  1277. procedure gen_load_reg(const paraloc:TCGParaLocation;reg:tregister);
  1278. var
  1279. href : treference;
  1280. begin
  1281. case paraloc.loc of
  1282. LOC_REGISTER :
  1283. cg.a_load_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1284. LOC_MMREGISTER :
  1285. cg.a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,mms_movescalar);
  1286. LOC_FPUREGISTER :
  1287. cg.a_loadfpu_reg_reg(list,paraloc.size,paraloc.register,reg);
  1288. LOC_REFERENCE :
  1289. begin
  1290. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1291. case getregtype(reg) of
  1292. R_INTREGISTER :
  1293. cg.a_load_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1294. R_FPUREGISTER :
  1295. cg.a_loadfpu_ref_reg(list,paraloc.size,href,reg);
  1296. R_MMREGISTER :
  1297. cg.a_loadmm_ref_reg(list,paraloc.size,paraloc.size,href,reg,mms_movescalar);
  1298. else
  1299. internalerror(2004101012);
  1300. end;
  1301. end;
  1302. else
  1303. internalerror(2002081302);
  1304. end;
  1305. end;
  1306. var
  1307. i : longint;
  1308. currpara : tparavarsym;
  1309. paraloc : pcgparalocation;
  1310. href : treference;
  1311. sizeleft : aint;
  1312. {$ifdef sparc}
  1313. tempref : treference;
  1314. {$endif sparc}
  1315. begin
  1316. if (po_assembler in current_procinfo.procdef.procoptions) then
  1317. exit;
  1318. { Allocate registers used by parameters }
  1319. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1320. begin
  1321. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1322. paraloc:=currpara.paraloc[calleeside].location;
  1323. while assigned(paraloc) do
  1324. begin
  1325. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1326. get_para(paraloc^);
  1327. paraloc:=paraloc^.next;
  1328. end;
  1329. end;
  1330. { Copy parameters to local references/registers }
  1331. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1332. begin
  1333. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1334. paraloc:=currpara.paraloc[calleeside].location;
  1335. { skip e.g. empty records }
  1336. if not assigned(paraloc) then
  1337. internalerror(200408203);
  1338. if (paraloc^.loc = LOC_VOID) then
  1339. continue;
  1340. case currpara.localloc.loc of
  1341. LOC_REFERENCE :
  1342. begin
  1343. { If the parameter location is reused we don't need to copy
  1344. anything }
  1345. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1346. begin
  1347. href:=currpara.localloc.reference;
  1348. sizeleft:=currpara.paraloc[calleeside].intsize;
  1349. while assigned(paraloc) do
  1350. begin
  1351. unget_para(paraloc^);
  1352. if (paraloc^.size=OS_NO) then
  1353. begin
  1354. { Can only be a reference that contains the rest
  1355. of the parameter }
  1356. if (paraloc^.loc<>LOC_REFERENCE) or
  1357. assigned(paraloc^.next) then
  1358. internalerror(2005013010);
  1359. gen_load_ref(paraloc^,href,sizeleft);
  1360. inc(href.offset,sizeleft);
  1361. sizeleft:=0;
  1362. end
  1363. else
  1364. begin
  1365. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size]);
  1366. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1367. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1368. end;
  1369. paraloc:=paraloc^.next;
  1370. end;
  1371. end;
  1372. end;
  1373. LOC_CREGISTER :
  1374. begin
  1375. {$ifndef cpu64bit}
  1376. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1377. is_64bit(currpara.vartype.def) then
  1378. begin
  1379. case paraloc^.loc of
  1380. LOC_REGISTER:
  1381. begin
  1382. if not assigned(paraloc^.next) then
  1383. internalerror(200410104);
  1384. if (target_info.endian=ENDIAN_BIG) then
  1385. begin
  1386. { paraloc^ -> high
  1387. paraloc^.next -> low }
  1388. unget_para(paraloc^);
  1389. gen_alloc_regvar(list,currpara);
  1390. gen_load_reg(paraloc^,currpara.localloc.register64.reghi);
  1391. unget_para(paraloc^.next^);
  1392. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reglo);
  1393. end
  1394. else
  1395. begin
  1396. { paraloc^ -> low
  1397. paraloc^.next -> high }
  1398. unget_para(paraloc^);
  1399. gen_alloc_regvar(list,currpara);
  1400. gen_load_reg(paraloc^,currpara.localloc.register64.reglo);
  1401. unget_para(paraloc^.next^);
  1402. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reghi);
  1403. end;
  1404. end;
  1405. LOC_REFERENCE:
  1406. begin
  1407. gen_alloc_regvar(list,currpara);
  1408. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset);
  1409. cg64.a_load64_ref_reg(list,href,currpara.localloc.register64);
  1410. unget_para(paraloc^);
  1411. end;
  1412. else
  1413. internalerror(2005101501);
  1414. end
  1415. end
  1416. else
  1417. {$endif cpu64bit}
  1418. begin
  1419. if assigned(paraloc^.next) then
  1420. internalerror(200410105);
  1421. unget_para(paraloc^);
  1422. gen_alloc_regvar(list,currpara);
  1423. gen_load_reg(paraloc^,currpara.localloc.register);
  1424. end;
  1425. end;
  1426. LOC_CFPUREGISTER :
  1427. begin
  1428. {$ifdef sparc}
  1429. { Sparc passes floats in int registers, when loading to fpu register
  1430. we need a temp }
  1431. sizeleft := TCGSize2Size[currpara.localloc.size];
  1432. tg.GetTemp(list,sizeleft,tt_normal,tempref);
  1433. href:=tempref;
  1434. while assigned(paraloc) do
  1435. begin
  1436. unget_para(paraloc^);
  1437. gen_load_ref(paraloc^,href,sizeleft);
  1438. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1439. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1440. paraloc:=paraloc^.next;
  1441. end;
  1442. gen_alloc_regvar(list,currpara);
  1443. cg.a_loadfpu_ref_reg(list,currpara.localloc.size,tempref,currpara.localloc.register);
  1444. tg.UnGetTemp(list,tempref);
  1445. {$else sparc}
  1446. unget_para(paraloc^);
  1447. gen_alloc_regvar(list,currpara);
  1448. gen_load_reg(paraloc^,currpara.localloc.register);
  1449. if assigned(paraloc^.next) then
  1450. internalerror(200410109);
  1451. {$endif sparc}
  1452. end;
  1453. LOC_CMMREGISTER :
  1454. begin
  1455. unget_para(paraloc^);
  1456. gen_alloc_regvar(list,currpara);
  1457. gen_load_reg(paraloc^,currpara.localloc.register);
  1458. { data could come in two memory locations, for now
  1459. we simply ignore the sanity check (FK)
  1460. if assigned(paraloc^.next) then
  1461. internalerror(200410108);
  1462. }
  1463. end;
  1464. end;
  1465. end;
  1466. { generate copies of call by value parameters, must be done before
  1467. the initialization and body is parsed because the refcounts are
  1468. incremented using the local copies }
  1469. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1470. {$ifdef powerpc}
  1471. { unget the register that contains the stack pointer before the procedure entry, }
  1472. { which is used to access the parameters in their original callee-side location }
  1473. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1474. cg.a_reg_dealloc(list,NR_R12);
  1475. {$endif powerpc}
  1476. {$ifdef powerpc64}
  1477. { unget the register that contains the stack pointer before the procedure entry, }
  1478. { which is used to access the parameters in their original callee-side location }
  1479. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1480. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1481. {$endif powerpc64}
  1482. end;
  1483. procedure gen_initialize_code(list:TAsmList);
  1484. begin
  1485. { initialize local data like ansistrings }
  1486. case current_procinfo.procdef.proctypeoption of
  1487. potype_unitinit:
  1488. begin
  1489. { this is also used for initialization of variables in a
  1490. program which does not have a globalsymtable }
  1491. if assigned(current_module.globalsymtable) then
  1492. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1493. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1494. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1495. end;
  1496. { units have seperate code for initilization and finalization }
  1497. potype_unitfinalize: ;
  1498. { program init/final is generated in separate procedure }
  1499. potype_proginit:
  1500. begin
  1501. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1502. end;
  1503. else
  1504. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1505. end;
  1506. { initialisizes temp. ansi/wide string data }
  1507. inittempvariables(list);
  1508. { initialize ansi/widesstring para's }
  1509. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1510. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1511. {$ifdef OLDREGVARS}
  1512. load_regvars(list,nil);
  1513. {$endif OLDREGVARS}
  1514. end;
  1515. procedure gen_finalize_code(list:TAsmList);
  1516. begin
  1517. {$ifdef OLDREGVARS}
  1518. cleanup_regvars(list);
  1519. {$endif OLDREGVARS}
  1520. { finalize temporary data }
  1521. finalizetempvariables(list);
  1522. { finalize local data like ansistrings}
  1523. case current_procinfo.procdef.proctypeoption of
  1524. potype_unitfinalize:
  1525. begin
  1526. { this is also used for initialization of variables in a
  1527. program which does not have a globalsymtable }
  1528. if assigned(current_module.globalsymtable) then
  1529. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1530. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1531. end;
  1532. { units/progs have separate code for initialization and finalization }
  1533. potype_unitinit: ;
  1534. { program init/final is generated in separate procedure }
  1535. potype_proginit: ;
  1536. else
  1537. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_local_vars,list);
  1538. end;
  1539. { finalize paras data }
  1540. if assigned(current_procinfo.procdef.parast) and
  1541. not(po_assembler in current_procinfo.procdef.procoptions) then
  1542. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1543. end;
  1544. procedure gen_entry_code(list:TAsmList);
  1545. var
  1546. paraloc1,
  1547. paraloc2 : tcgpara;
  1548. begin
  1549. paraloc1.init;
  1550. paraloc2.init;
  1551. { the actual profile code can clobber some registers,
  1552. therefore if the context must be saved, do it before
  1553. the actual call to the profile code
  1554. }
  1555. if (cs_profile in aktmoduleswitches) and
  1556. not(po_assembler in current_procinfo.procdef.procoptions) then
  1557. begin
  1558. { non-win32 can call mcout even in main }
  1559. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1560. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1561. begin
  1562. cg.g_profilecode(list);
  1563. end;
  1564. end;
  1565. { call startup helpers from main program }
  1566. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1567. begin
  1568. { initialize units }
  1569. cg.allocallcpuregisters(list);
  1570. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1571. cg.deallocallcpuregisters(list);
  1572. end;
  1573. list.concat(Tai_force_line.Create);
  1574. {$ifdef OLDREGVARS}
  1575. load_regvars(list,nil);
  1576. {$endif OLDREGVARS}
  1577. paraloc1.done;
  1578. paraloc2.done;
  1579. end;
  1580. procedure gen_exit_code(list:TAsmList);
  1581. begin
  1582. { call __EXIT for main program }
  1583. if (not DLLsource) and
  1584. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1585. cg.a_call_name(list,'FPC_DO_EXIT');
  1586. end;
  1587. {****************************************************************************
  1588. Entry/Exit
  1589. ****************************************************************************}
  1590. procedure alloc_proc_symbol(pd: tprocdef);
  1591. var
  1592. item: tstringlistitem;
  1593. begin
  1594. item := tstringlistitem(pd.aliasnames.first);
  1595. while assigned(item) do
  1596. begin
  1597. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1598. item := tstringlistitem(item.next);
  1599. end;
  1600. end;
  1601. procedure gen_proc_symbol(list:TAsmList);
  1602. var
  1603. hs : string;
  1604. begin
  1605. repeat
  1606. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1607. if hs='' then
  1608. break;
  1609. if (cs_profile in aktmoduleswitches) or
  1610. (po_global in current_procinfo.procdef.procoptions) then
  1611. list.concat(Tai_symbol.createname_global(hs,AT_FUNCTION,0))
  1612. else
  1613. list.concat(Tai_symbol.createname(hs,AT_FUNCTION,0));
  1614. if tf_use_function_relative_addresses in target_info.flags then
  1615. list.concat(Tai_function_name.create(hs));
  1616. until false;
  1617. current_procinfo.procdef.procstarttai:=tai(list.last);
  1618. end;
  1619. procedure gen_proc_symbol_end(list:TAsmList);
  1620. begin
  1621. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1622. current_procinfo.procdef.procendtai:=tai(list.last);
  1623. { finalisation marker for Mac OS X }
  1624. if (target_info.system in [system_powerpc_darwin,system_i386_darwin]) and
  1625. (current_module.islibrary) and
  1626. (((current_module.flags and uf_finalize)<>0) or
  1627. (current_procinfo.procdef.proctypeoption = potype_proginit)) then
  1628. begin
  1629. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  1630. list.concat(tai_directive.create(asd_mod_init_func,''))
  1631. else
  1632. list.concat(tai_directive.create(asd_mod_term_func,''));
  1633. list.concat(tai_align.create(4));
  1634. list.concat(Tai_const.Createname(current_procinfo.procdef.mangledname,0));
  1635. end;
  1636. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1637. begin
  1638. if (target_info.system in [system_powerpc_darwin,system_i386_darwin,system_powerpc_macos]) and
  1639. not(current_module.islibrary) then
  1640. begin
  1641. list.concat(tai_section.create(sec_code,'',4));
  1642. list.concat(tai_symbol.createname_global(
  1643. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  1644. { keep argc, argv and envp properly on the stack }
  1645. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1646. end;
  1647. end;
  1648. end;
  1649. procedure gen_proc_entry_code(list:TAsmList);
  1650. var
  1651. hitemp,
  1652. lotemp : longint;
  1653. begin
  1654. { generate call frame marker for dwarf call frame info }
  1655. current_asmdata.asmcfi.start_frame(list);
  1656. { All temps are know, write offsets used for information }
  1657. if (cs_asm_source in aktglobalswitches) then
  1658. begin
  1659. if tg.direction>0 then
  1660. begin
  1661. lotemp:=current_procinfo.tempstart;
  1662. hitemp:=tg.lasttemp;
  1663. end
  1664. else
  1665. begin
  1666. lotemp:=tg.lasttemp;
  1667. hitemp:=current_procinfo.tempstart;
  1668. end;
  1669. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1670. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1671. end;
  1672. { generate target specific proc entry code }
  1673. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1674. end;
  1675. procedure gen_proc_exit_code(list:TAsmList);
  1676. var
  1677. parasize : longint;
  1678. begin
  1679. { c style clearstack does not need to remove parameters from the stack, only the
  1680. return value when it was pushed by arguments }
  1681. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1682. begin
  1683. parasize:=0;
  1684. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1685. inc(parasize,sizeof(aint));
  1686. end
  1687. else
  1688. parasize:=current_procinfo.para_stack_size;
  1689. { generate target specific proc exit code }
  1690. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1691. { release return registers, needed for optimizer }
  1692. if not is_void(current_procinfo.procdef.rettype.def) then
  1693. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  1694. { end of frame marker for call frame info }
  1695. current_asmdata.asmcfi.end_frame(list);
  1696. end;
  1697. procedure gen_stack_check_size_para(list:TAsmList);
  1698. var
  1699. paraloc1 : tcgpara;
  1700. begin
  1701. paraloc1.init;
  1702. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1703. paramanager.allocparaloc(list,paraloc1);
  1704. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1705. paramanager.freeparaloc(list,paraloc1);
  1706. paraloc1.done;
  1707. end;
  1708. procedure gen_stack_check_call(list:TAsmList);
  1709. var
  1710. paraloc1 : tcgpara;
  1711. begin
  1712. paraloc1.init;
  1713. { Also alloc the register needed for the parameter }
  1714. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1715. paramanager.allocparaloc(list,paraloc1);
  1716. paramanager.freeparaloc(list,paraloc1);
  1717. { Call the helper }
  1718. cg.allocallcpuregisters(list);
  1719. cg.a_call_name(list,'FPC_STACKCHECK');
  1720. cg.deallocallcpuregisters(list);
  1721. paraloc1.done;
  1722. end;
  1723. procedure gen_save_used_regs(list:TAsmList);
  1724. begin
  1725. { Pure assembler routines need to save the registers themselves }
  1726. if (po_assembler in current_procinfo.procdef.procoptions) then
  1727. exit;
  1728. { oldfpccall expects all registers to be destroyed }
  1729. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1730. cg.g_save_standard_registers(list);
  1731. end;
  1732. procedure gen_restore_used_regs(list:TAsmList);
  1733. begin
  1734. { Pure assembler routines need to save the registers themselves }
  1735. if (po_assembler in current_procinfo.procdef.procoptions) then
  1736. exit;
  1737. { oldfpccall expects all registers to be destroyed }
  1738. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1739. cg.g_restore_standard_registers(list);
  1740. end;
  1741. procedure gen_got_load(list : TAsmList);
  1742. begin
  1743. { if loading got is necessary for more cpus, it can be moved
  1744. to the cg }
  1745. {$ifdef i386}
  1746. { allocate PIC register }
  1747. if (cs_create_pic in aktmoduleswitches) and
  1748. (tf_pic_uses_got in target_info.flags) and
  1749. (pi_needs_got in current_procinfo.flags) then
  1750. begin
  1751. current_module.requires_ebx_pic_helper:=true;
  1752. cg.a_call_name_static(list,'fpc_geteipasebx');
  1753. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_L,current_asmdata.RefAsmSymbol('_GLOBAL_OFFSET_TABLE_'),0,NR_PIC_OFFSET_REG));
  1754. list.concat(tai_regalloc.alloc(NR_PIC_OFFSET_REG,nil));
  1755. { ecx could be used in leave procedures }
  1756. current_procinfo.got:=NR_EBX;
  1757. end;
  1758. {$endif i386}
  1759. end;
  1760. {****************************************************************************
  1761. External handling
  1762. ****************************************************************************}
  1763. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  1764. begin
  1765. { add the procedure to the al_procedures }
  1766. maybe_new_object_file(list);
  1767. new_section(list,sec_code,lower(pd.mangledname),aktalignment.procalign);
  1768. list.concat(Tai_align.create(aktalignment.procalign));
  1769. if (po_global in pd.procoptions) then
  1770. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1771. else
  1772. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1773. cg.a_jmp_name(list,externalname);
  1774. end;
  1775. {****************************************************************************
  1776. Const Data
  1777. ****************************************************************************}
  1778. procedure insertbssdata(sym : tglobalvarsym);
  1779. var
  1780. l : aint;
  1781. varalign : shortint;
  1782. storefilepos : tfileposinfo;
  1783. list : TAsmList;
  1784. sectype : TAsmSectiontype;
  1785. begin
  1786. storefilepos:=aktfilepos;
  1787. aktfilepos:=sym.fileinfo;
  1788. l:=sym.getsize;
  1789. if tf_section_threadvars in target_info.flags then
  1790. begin
  1791. if (vo_is_thread_var in sym.varoptions) then
  1792. begin
  1793. list:=current_asmdata.asmlists[al_threadvars];
  1794. sectype:=sec_threadvar;
  1795. end
  1796. else
  1797. begin
  1798. list:=current_asmdata.asmlists[al_globals];
  1799. sectype:=sec_bss;
  1800. end;
  1801. end
  1802. else
  1803. begin
  1804. if (vo_is_thread_var in sym.varoptions) then
  1805. inc(l,sizeof(aint));
  1806. list:=current_asmdata.asmlists[al_globals];
  1807. sectype:=sec_bss;
  1808. end;
  1809. varalign:=var_align(size_2_align(l));
  1810. maybe_new_object_file(list);
  1811. new_section(list,sectype,lower(sym.mangledname),varalign);
  1812. if (sym.owner.symtabletype=globalsymtable) or
  1813. maybe_smartlink_symbol or
  1814. DLLSource or
  1815. (assigned(current_procinfo) and
  1816. (po_inline in current_procinfo.procdef.procoptions)) or
  1817. (vo_is_exported in sym.varoptions) or
  1818. (vo_is_C_var in sym.varoptions) then
  1819. list.concat(Tai_datablock.create_global(sym.mangledname,l))
  1820. else
  1821. list.concat(Tai_datablock.create(sym.mangledname,l));
  1822. aktfilepos:=storefilepos;
  1823. end;
  1824. procedure init_regvar_loc(sym:tabstractnormalvarsym;cgsize:tcgsize);
  1825. begin
  1826. with sym do
  1827. begin
  1828. localloc.size:=cgsize;
  1829. case varregable of
  1830. vr_intreg :
  1831. begin
  1832. localloc.loc:=LOC_CREGISTER;
  1833. end;
  1834. vr_fpureg :
  1835. begin
  1836. localloc.loc:=LOC_CFPUREGISTER;
  1837. end;
  1838. vr_mmreg :
  1839. begin
  1840. localloc.loc:=LOC_CMMREGISTER;
  1841. end;
  1842. else
  1843. internalerror(2004101010);
  1844. end;
  1845. end;
  1846. end;
  1847. procedure gen_alloc_symtable(list:TAsmList;st:tsymtable);
  1848. var
  1849. sym : tsym;
  1850. isaddr : boolean;
  1851. cgsize : tcgsize;
  1852. begin
  1853. sym:=tsym(st.symindex.first);
  1854. while assigned(sym) do
  1855. begin
  1856. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1857. begin
  1858. with tabstractnormalvarsym(sym) do
  1859. begin
  1860. { Parameters passed to assembler procedures need to be kept
  1861. in the original location }
  1862. if (sym.typ=paravarsym) and
  1863. (po_assembler in current_procinfo.procdef.procoptions) then
  1864. begin
  1865. tparavarsym(sym).paraloc[calleeside].get_location(localloc);
  1866. end
  1867. else
  1868. begin
  1869. isaddr:=(st.symtabletype=parasymtable) and
  1870. paramanager.push_addr_param(varspez,vartype.def,current_procinfo.procdef.proccalloption);
  1871. if isaddr then
  1872. cgsize:=OS_ADDR
  1873. else
  1874. cgsize:=def_cgsize(vartype.def);
  1875. {$ifndef OLDREGVARS}
  1876. { When there is assembler code we can't use regvars }
  1877. if is_regvar then
  1878. begin
  1879. init_regvar_loc(tabstractnormalvarsym(sym),cgsize);
  1880. if (st.symtabletype <> parasymtable) then
  1881. gen_alloc_regvar(list,tabstractnormalvarsym(sym));
  1882. end
  1883. else
  1884. {$endif NOT OLDREGVARS}
  1885. begin
  1886. localloc.loc:=LOC_REFERENCE;
  1887. localloc.size:=cgsize;
  1888. case st.symtabletype of
  1889. parasymtable :
  1890. begin
  1891. { Reuse the parameter location for values to are at a single location on the stack }
  1892. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1893. begin
  1894. reference_reset_base(localloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1895. tparavarsym(sym).paraloc[calleeside].location^.reference.offset);
  1896. end
  1897. else
  1898. begin
  1899. if isaddr then
  1900. tg.GetLocal(list,sizeof(aint),voidpointertype.def,localloc.reference)
  1901. else
  1902. tg.GetLocal(list,getsize,tparavarsym(sym).paraloc[calleeside].alignment,vartype.def,localloc.reference);
  1903. end;
  1904. end;
  1905. localsymtable,
  1906. stt_exceptsymtable :
  1907. begin
  1908. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1909. end;
  1910. staticsymtable :
  1911. begin
  1912. { PIC, DLL and Threadvar need extra code and are handled in ncgld }
  1913. if not(vo_is_dll_var in varoptions) and ((tf_section_threadvars in target_info.flags) or
  1914. not(vo_is_thread_var in varoptions)) then
  1915. reference_reset_symbol(localloc.reference,current_asmdata.RefAsmSymbol(mangledname),0);
  1916. end;
  1917. else
  1918. internalerror(200410103);
  1919. end;
  1920. end;
  1921. end;
  1922. if cs_asm_source in aktglobalswitches then
  1923. begin
  1924. case localloc.loc of
  1925. LOC_REFERENCE :
  1926. begin
  1927. if not assigned(localloc.reference.symbol) then
  1928. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located at '+
  1929. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  1930. end;
  1931. end;
  1932. end;
  1933. end;
  1934. end;
  1935. sym:=tsym(sym.indexnext);
  1936. end;
  1937. end;
  1938. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  1939. begin
  1940. case location.loc of
  1941. LOC_CREGISTER:
  1942. {$ifndef cpu64bit}
  1943. if location.size in [OS_64,OS_S64] then
  1944. begin
  1945. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  1946. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  1947. end
  1948. else
  1949. {$endif cpu64bit}
  1950. rv.intregvars.addnodup(getsupreg(location.register));
  1951. LOC_CFPUREGISTER:
  1952. rv.fpuregvars.addnodup(getsupreg(location.register));
  1953. LOC_CMMREGISTER:
  1954. rv.mmregvars.addnodup(getsupreg(location.register));
  1955. end;
  1956. end;
  1957. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  1958. var
  1959. rv: pusedregvars absolute arg;
  1960. begin
  1961. case (n.nodetype) of
  1962. temprefn:
  1963. { We only have to synchronise a tempnode before a loop if it is }
  1964. { not created inside the loop, and only synchronise after the }
  1965. { loop if it's not destroyed inside the loop. If it's created }
  1966. { before the loop and not yet destroyed, then before the loop }
  1967. { is secondpassed tempinfo^.valid will be true, and we get the }
  1968. { correct registers. If it's not destroyed inside the loop, }
  1969. { then after the loop has been secondpassed tempinfo^.valid }
  1970. { be true and we also get the right registers. In other cases, }
  1971. { tempinfo^.valid will be false and so we do not add }
  1972. { unnecessary registers. This way, we don't have to look at }
  1973. { tempcreate and tempdestroy nodes to get this info (JM) }
  1974. if (ttemprefnode(n).tempinfo^.valid) then
  1975. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  1976. loadn:
  1977. if (tloadnode(n).symtableentry.typ in [globalvarsym,localvarsym,paravarsym]) then
  1978. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  1979. end;
  1980. result := fen_true;
  1981. end;
  1982. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  1983. begin
  1984. foreachnodestatic(n,@do_get_used_regvars,@rv);
  1985. end;
  1986. (*
  1987. See comments at declaration of pusedregvarscommon
  1988. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  1989. var
  1990. rv: pusedregvarscommon absolute arg;
  1991. begin
  1992. if (n.nodetype = loadn) and
  1993. (tloadnode(n).symtableentry.typ in [globalvarsym,localvarsym,paravarsym]) then
  1994. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  1995. case loc of
  1996. LOC_CREGISTER:
  1997. { if not yet encountered in this node tree }
  1998. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1999. { but nevertheless already encountered somewhere }
  2000. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2001. { then it's a regvar used in two or more node trees }
  2002. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2003. LOC_CFPUREGISTER:
  2004. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2005. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2006. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2007. LOC_CMMREGISTER:
  2008. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2009. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2010. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2011. end;
  2012. result := fen_true;
  2013. end;
  2014. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  2015. begin
  2016. rv.myregvars.intregvars.clear;
  2017. rv.myregvars.fpuregvars.clear;
  2018. rv.myregvars.mmregvars.clear;
  2019. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  2020. end;
  2021. *)
  2022. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  2023. var
  2024. count: longint;
  2025. begin
  2026. for count := 1 to rv.intregvars.length do
  2027. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  2028. for count := 1 to rv.fpuregvars.length do
  2029. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  2030. for count := 1 to rv.mmregvars.length do
  2031. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  2032. end;
  2033. procedure gen_free_symtable(list:TAsmList;st:tsymtable);
  2034. var
  2035. sym : tsym;
  2036. begin
  2037. sym:=tsym(st.symindex.first);
  2038. while assigned(sym) do
  2039. begin
  2040. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  2041. begin
  2042. with tabstractnormalvarsym(sym) do
  2043. begin
  2044. { Note: We need to keep the data available in memory
  2045. for the sub procedures that can access local data
  2046. in the parent procedures }
  2047. case localloc.loc of
  2048. LOC_CREGISTER :
  2049. {$ifndef cpu64bit}
  2050. if (pi_has_goto in current_procinfo.flags) then
  2051. if def_cgsize(vartype.def) in [OS_64,OS_S64] then
  2052. begin
  2053. cg.a_reg_sync(list,localloc.register64.reglo);
  2054. cg.a_reg_sync(list,localloc.register64.reghi);
  2055. end
  2056. else
  2057. {$endif cpu64bit}
  2058. cg.a_reg_sync(list,localloc.register);
  2059. LOC_CFPUREGISTER,
  2060. LOC_CMMREGISTER:
  2061. if (pi_has_goto in current_procinfo.flags) then
  2062. cg.a_reg_sync(list,localloc.register);
  2063. LOC_REFERENCE :
  2064. begin
  2065. case st.symtabletype of
  2066. localsymtable,
  2067. parasymtable,
  2068. stt_exceptsymtable :
  2069. tg.Ungetlocal(list,localloc.reference);
  2070. end;
  2071. end;
  2072. end;
  2073. end;
  2074. end;
  2075. sym:=tsym(sym.indexnext);
  2076. end;
  2077. end;
  2078. { persistent rtti generation }
  2079. procedure generate_rtti(p:Ttypesym);
  2080. var
  2081. rsym : trttisym;
  2082. def : tstoreddef;
  2083. begin
  2084. { rtti can only be generated for classes that are always typesyms }
  2085. def:=tstoreddef(ttypesym(p).restype.def);
  2086. { there is an error, skip rtti info }
  2087. if (def.deftype=errordef) or (Errorcount>0) then
  2088. exit;
  2089. { only create rtti once for each definition }
  2090. if not(df_has_rttitable in def.defoptions) then
  2091. begin
  2092. { definition should be in the same symtable as the symbol }
  2093. if p.owner<>def.owner then
  2094. internalerror(200108262);
  2095. { create rttisym }
  2096. rsym:=trttisym.create(p.name,fullrtti);
  2097. p.owner.insert(rsym);
  2098. { register rttisym in definition }
  2099. include(def.defoptions,df_has_rttitable);
  2100. def.rttitablesym:=rsym;
  2101. { write rtti data }
  2102. def.write_child_rtti_data(fullrtti);
  2103. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  2104. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2105. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  2106. def.write_rtti_data(fullrtti);
  2107. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  2108. end;
  2109. end;
  2110. { persistent init table generation }
  2111. procedure generate_inittable(p:tsym);
  2112. var
  2113. rsym : trttisym;
  2114. def : tstoreddef;
  2115. begin
  2116. { anonymous types are also allowed for records that can be varsym }
  2117. case p.typ of
  2118. typesym :
  2119. def:=tstoreddef(ttypesym(p).restype.def);
  2120. globalvarsym,
  2121. localvarsym,
  2122. paravarsym :
  2123. def:=tstoreddef(tabstractvarsym(p).vartype.def);
  2124. else
  2125. internalerror(200108263);
  2126. end;
  2127. { only create inittable once for each definition }
  2128. if not(df_has_inittable in def.defoptions) then
  2129. begin
  2130. { definition should be in the same symtable as the symbol }
  2131. if p.owner<>def.owner then
  2132. internalerror(200108264);
  2133. { create rttisym }
  2134. rsym:=trttisym.create(p.name,initrtti);
  2135. p.owner.insert(rsym);
  2136. { register rttisym in definition }
  2137. include(def.defoptions,df_has_inittable);
  2138. def.inittablesym:=rsym;
  2139. { write inittable data }
  2140. def.write_child_rtti_data(initrtti);
  2141. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  2142. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2143. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  2144. def.write_rtti_data(initrtti);
  2145. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  2146. end;
  2147. end;
  2148. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  2149. var
  2150. i,j,
  2151. proccount : longint;
  2152. tmps : string;
  2153. begin
  2154. for i:=1 to _class.implementedinterfaces.count do
  2155. begin
  2156. { only if implemented by this class }
  2157. if _class.implementedinterfaces.implindex(i)=i then
  2158. begin
  2159. proccount:=_class.implementedinterfaces.implproccount(i);
  2160. for j:=1 to proccount do
  2161. begin
  2162. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  2163. _class.implementedinterfaces.interfaces(i).objname^+'_$_'+
  2164. tostr(j)+'_$_'+_class.implementedinterfaces.implprocs(i,j).mangledname);
  2165. { create wrapper code }
  2166. new_section(list,sec_code,lower(tmps),0);
  2167. cg.init_register_allocators;
  2168. cg.g_intf_wrapper(list,_class.implementedinterfaces.implprocs(i,j),tmps,_class.implementedinterfaces.ioffsets(i));
  2169. cg.done_register_allocators;
  2170. end;
  2171. end;
  2172. end;
  2173. end;
  2174. procedure gen_intf_wrappers(list:TAsmList;st:tsymtable);
  2175. var
  2176. def : tstoreddef;
  2177. begin
  2178. def:=tstoreddef(st.defindex.first);
  2179. while assigned(def) do
  2180. begin
  2181. if is_class(def) then
  2182. gen_intf_wrapper(list,tobjectdef(def));
  2183. def:=tstoreddef(def.indexnext);
  2184. end;
  2185. end;
  2186. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  2187. var
  2188. href : treference;
  2189. begin
  2190. if is_object(objdef) then
  2191. begin
  2192. case selfloc.loc of
  2193. LOC_CREFERENCE,
  2194. LOC_REFERENCE:
  2195. begin
  2196. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2197. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  2198. end;
  2199. else
  2200. internalerror(200305056);
  2201. end;
  2202. end
  2203. else
  2204. begin
  2205. case selfloc.loc of
  2206. LOC_REGISTER:
  2207. begin
  2208. {$ifdef cpu_uses_separate_address_registers}
  2209. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  2210. begin
  2211. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2212. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2213. end
  2214. else
  2215. {$endif cpu_uses_separate_address_registers}
  2216. reference_reset_base(href,selfloc.register,objdef.vmt_offset);
  2217. end;
  2218. LOC_CREGISTER,
  2219. LOC_CREFERENCE,
  2220. LOC_REFERENCE:
  2221. begin
  2222. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2223. cg.a_load_loc_reg(list,OS_ADDR,selfloc,href.base);
  2224. end;
  2225. else
  2226. internalerror(200305057);
  2227. end;
  2228. end;
  2229. vmtreg:=cg.getaddressregister(list);
  2230. cg.g_maybe_testself(list,href.base);
  2231. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2232. { test validity of VMT }
  2233. if not(is_interface(objdef)) and
  2234. not(is_cppclass(objdef)) then
  2235. cg.g_maybe_testvmt(list,vmtreg,objdef);
  2236. end;
  2237. function getprocalign : shortint;
  2238. begin
  2239. { gprof uses 16 byte granularity }
  2240. if (cs_profile in aktmoduleswitches) then
  2241. result:=16
  2242. else
  2243. result:=aktalignment.procalign;
  2244. end;
  2245. procedure gen_pic_helpers(list : TAsmList);
  2246. var
  2247. href : treference;
  2248. begin
  2249. { if other cpus require such helpers as well, it can be solved more cleaner }
  2250. {$ifdef i386}
  2251. if current_module.requires_ebx_pic_helper then
  2252. begin
  2253. new_section(list,sec_code,'fpc_geteipasebx',0);
  2254. list.concat(tai_symbol.Createname('fpc_geteipasebx',AT_FUNCTION,getprocalign));
  2255. reference_reset(href);
  2256. href.base:=NR_ESP;
  2257. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_EBX));
  2258. list.concat(taicpu.op_none(A_RET,S_NO));
  2259. end;
  2260. if current_module.requires_ecx_pic_helper then
  2261. begin
  2262. new_section(list,sec_code,'fpc_geteipasecx',0);
  2263. list.concat(tai_symbol.Createname('fpc_geteipasecx',AT_FUNCTION,getprocalign));
  2264. reference_reset(href);
  2265. href.base:=NR_ESP;
  2266. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_ECX));
  2267. list.concat(taicpu.op_none(A_RET,S_NO));
  2268. end;
  2269. {$endif i386}
  2270. end;
  2271. end.