ncgutil.pas 94 KB

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