ncgutil.pas 97 KB

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