ncgutil.pas 94 KB

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