ncgutil.pas 104 KB

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