ncgutil.pas 95 KB

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