ncgutil.pas 94 KB

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