ncgutil.pas 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534
  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,
  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. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  40. procedure gen_proc_symbol(list:Taasmoutput);
  41. procedure gen_proc_symbol_end(list:Taasmoutput);
  42. procedure gen_stackalloc_code(list:Taasmoutput);
  43. procedure gen_stackfree_code(list:Taasmoutput;usesacc,usesacchi:boolean);
  44. procedure gen_save_used_regs(list:TAAsmoutput);
  45. procedure gen_restore_used_regs(list:TAAsmoutput;usesacc,usesacchi,usesfpu:boolean);
  46. procedure gen_initialize_code(list:TAAsmoutput;inlined:boolean);
  47. procedure gen_finalize_code(list:TAAsmoutput;inlined:boolean);
  48. procedure gen_entry_code(list:TAAsmoutput);
  49. procedure gen_exit_code(list:TAAsmoutput);
  50. procedure gen_load_para_value(list:TAAsmoutput);
  51. procedure gen_load_return_value(list:TAAsmoutput; var uses_acc,uses_acchi,uses_fpu : boolean);
  52. (*
  53. procedure geninlineentrycode(list:TAAsmoutput;stackframe:longint);
  54. procedure geninlineexitcode(list:TAAsmoutput;inlined:boolean);
  55. *)
  56. {#
  57. Allocate the buffers for exception management and setjmp environment.
  58. Return a pointer to these buffers, send them to the utility routine
  59. so they are registered, and then call setjmp.
  60. Then compare the result of setjmp with 0, and if not equal
  61. to zero, then jump to exceptlabel.
  62. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  63. It is to note that this routine may be called *after* the stackframe of a
  64. routine has been called, therefore on machines where the stack cannot
  65. be modified, all temps should be allocated on the heap instead of the
  66. stack.
  67. }
  68. procedure new_exception(list:TAAsmoutput;const jmpbuf,envbuf, href : treference;
  69. a : aword; exceptlabel : tasmlabel);
  70. procedure free_exception(list:TAAsmoutput;const jmpbuf, envbuf, href : treference;
  71. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  72. procedure insertconstdata(sym : ttypedconstsym);
  73. procedure insertbssdata(sym : tvarsym);
  74. procedure gen_alloc_localst(list:TAAsmoutput;st:tlocalsymtable);
  75. procedure gen_free_localst(list:TAAsmoutput;st:tlocalsymtable);
  76. procedure gen_alloc_parast(list:TAAsmoutput;st:tparasymtable);
  77. procedure gen_alloc_inline_parast(list:TAAsmoutput;st:tparasymtable);
  78. procedure gen_free_parast(list:TAAsmoutput;st:tparasymtable);
  79. implementation
  80. uses
  81. {$ifdef Delphi}
  82. Sysutils,
  83. {$else}
  84. strings,
  85. {$endif}
  86. cutils,cclasses,
  87. globals,systems,verbose,
  88. defutil,
  89. procinfo,paramgr,fmodule,
  90. regvars,
  91. {$ifdef GDB}
  92. gdb,
  93. {$endif GDB}
  94. ncon,
  95. tgobj,cgobj;
  96. const
  97. { Please leave this here, this module should NOT use
  98. exprasmlist, the lists are always passed as arguments.
  99. Declaring it as string here results in an error when compiling (PFV) }
  100. exprasmlist = 'error';
  101. {*****************************************************************************
  102. Misc Helpers
  103. *****************************************************************************}
  104. { DO NOT RELY on the fact that the tnode is not yet swaped
  105. because of inlining code PM }
  106. procedure firstcomplex(p : tbinarynode);
  107. var
  108. hp : tnode;
  109. begin
  110. { always calculate boolean AND and OR from left to right }
  111. if (p.nodetype in [orn,andn]) and
  112. is_boolean(p.left.resulttype.def) then
  113. begin
  114. if nf_swaped in p.flags then
  115. internalerror(234234);
  116. end
  117. else
  118. if (
  119. (p.location.loc=LOC_FPUREGISTER) and
  120. (p.right.registersfpu > p.left.registersfpu)
  121. ) or
  122. (
  123. (
  124. (
  125. ((p.left.registersfpu = 0) and (p.right.registersfpu = 0)) or
  126. (p.location.loc<>LOC_FPUREGISTER)
  127. ) and
  128. (p.left.registers32<p.right.registers32)
  129. )
  130. ) then
  131. begin
  132. hp:=p.left;
  133. p.left:=p.right;
  134. p.right:=hp;
  135. if nf_swaped in p.flags then
  136. exclude(p.flags,nf_swaped)
  137. else
  138. include(p.flags,nf_swaped);
  139. end;
  140. end;
  141. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  142. {
  143. produces jumps to true respectively false labels using boolean expressions
  144. depending on whether the loading of regvars is currently being
  145. synchronized manually (such as in an if-node) or automatically (most of
  146. the other cases where this procedure is called), loadregvars can be
  147. "lr_load_regvars" or "lr_dont_load_regvars"
  148. }
  149. var
  150. opsize : tcgsize;
  151. storepos : tfileposinfo;
  152. begin
  153. if nf_error in p.flags then
  154. exit;
  155. storepos:=aktfilepos;
  156. aktfilepos:=p.fileinfo;
  157. if is_boolean(p.resulttype.def) then
  158. begin
  159. {$ifdef OLDREGVARS}
  160. if loadregvars = lr_load_regvars then
  161. load_all_regvars(list);
  162. {$endif OLDREGVARS}
  163. if is_constboolnode(p) then
  164. begin
  165. if tordconstnode(p).value<>0 then
  166. cg.a_jmp_always(list,truelabel)
  167. else
  168. cg.a_jmp_always(list,falselabel)
  169. end
  170. else
  171. begin
  172. opsize:=def_cgsize(p.resulttype.def);
  173. case p.location.loc of
  174. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  175. begin
  176. {$ifdef OLDREGVARS}
  177. if (p.location.loc = LOC_CREGISTER) then
  178. load_regvar_reg(list,p.location.register);
  179. {$endif OLDREGVARS}
  180. cg.a_cmp_const_loc_label(list,opsize,OC_NE,
  181. 0,p.location,truelabel);
  182. { !!! should happen right after cmp (JM) }
  183. location_release(list,p.location);
  184. cg.a_jmp_always(list,falselabel);
  185. end;
  186. LOC_JUMP:
  187. ;
  188. {$ifdef cpuflags}
  189. LOC_FLAGS :
  190. begin
  191. cg.a_jmp_flags(list,p.location.resflags,
  192. truelabel);
  193. cg.a_jmp_always(list,falselabel);
  194. end;
  195. {$endif cpuflags}
  196. else
  197. internalerror(200308241);
  198. end;
  199. end;
  200. end
  201. else
  202. internalerror(200112305);
  203. aktfilepos:=storepos;
  204. end;
  205. (*
  206. This code needs fixing. It is not safe to use rgint; on the m68000 it
  207. would be rgaddr.
  208. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  209. begin
  210. case t.loc of
  211. LOC_REGISTER:
  212. begin
  213. { can't be a regvar, since it would be LOC_CREGISTER then }
  214. exclude(regs,getsupreg(t.register));
  215. if t.registerhigh<>NR_NO then
  216. exclude(regs,getsupreg(t.registerhigh));
  217. end;
  218. LOC_CREFERENCE,LOC_REFERENCE:
  219. begin
  220. if not(cs_regvars in aktglobalswitches) or
  221. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  222. exclude(regs,getsupreg(t.reference.base));
  223. if not(cs_regvars in aktglobalswitches) or
  224. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  225. exclude(regs,getsupreg(t.reference.index));
  226. end;
  227. end;
  228. end;
  229. *)
  230. {*****************************************************************************
  231. EXCEPTION MANAGEMENT
  232. *****************************************************************************}
  233. procedure new_exception(list:TAAsmoutput;const jmpbuf,envbuf, href : treference;
  234. a : aword; exceptlabel : tasmlabel);
  235. var
  236. paraloc1,paraloc2,paraloc3 : tparalocation;
  237. begin
  238. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  239. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  240. paraloc3:=paramanager.getintparaloc(pocall_default,3);
  241. paramanager.allocparaloc(list,paraloc3);
  242. cg.a_paramaddr_ref(list,envbuf,paraloc3);
  243. paramanager.allocparaloc(list,paraloc2);
  244. cg.a_paramaddr_ref(list,jmpbuf,paraloc2);
  245. { push type of exceptionframe }
  246. paramanager.allocparaloc(list,paraloc1);
  247. cg.a_param_const(list,OS_S32,1,paraloc1);
  248. paramanager.freeparaloc(list,paraloc3);
  249. paramanager.freeparaloc(list,paraloc2);
  250. paramanager.freeparaloc(list,paraloc1);
  251. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  252. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR');
  253. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  254. {$warning stdcall forced for SETJMP}
  255. paraloc1:=paramanager.getintparaloc(pocall_stdcall,1);
  256. paramanager.allocparaloc(list,paraloc1);
  257. cg.a_param_reg(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  258. paramanager.freeparaloc(list,paraloc1);
  259. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  260. cg.a_call_name(list,'FPC_SETJMP');
  261. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  262. cg.g_exception_reason_save(list, href);
  263. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,NR_FUNCTION_RESULT_REG,exceptlabel);
  264. end;
  265. procedure free_exception(list:TAAsmoutput;const jmpbuf, envbuf, href : treference;
  266. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  267. begin
  268. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  269. cg.a_call_name(list,'FPC_POPADDRSTACK');
  270. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  271. if not onlyfree then
  272. begin
  273. cg.g_exception_reason_load(list, href);
  274. cg.a_cmp_const_reg_label(list,OS_S32,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  275. end;
  276. end;
  277. {*****************************************************************************
  278. TLocation
  279. *****************************************************************************}
  280. {$ifndef cpu64bit}
  281. { 32-bit version }
  282. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  283. var
  284. hregister,
  285. hregisterhi : tregister;
  286. hreg64 : tregister64;
  287. hl : tasmlabel;
  288. oldloc : tlocation;
  289. begin
  290. oldloc:=l;
  291. if dst_size=OS_NO then
  292. internalerror(200309144);
  293. { handle transformations to 64bit separate }
  294. if dst_size in [OS_64,OS_S64] then
  295. begin
  296. if not (l.size in [OS_64,OS_S64]) then
  297. begin
  298. { load a smaller size to OS_64 }
  299. if l.loc=LOC_REGISTER then
  300. begin
  301. hregister:=cg.makeregsize(l.registerlow,OS_32);
  302. cg.a_load_reg_reg(list,l.size,OS_32,l.registerlow,hregister);
  303. end
  304. else
  305. begin
  306. location_release(list,l);
  307. hregister:=cg.getintregister(list,OS_INT);
  308. end;
  309. { load value in low register }
  310. case l.loc of
  311. LOC_FLAGS :
  312. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  313. LOC_JUMP :
  314. begin
  315. cg.a_label(list,truelabel);
  316. cg.a_load_const_reg(list,OS_INT,1,hregister);
  317. objectlibrary.getlabel(hl);
  318. cg.a_jmp_always(list,hl);
  319. cg.a_label(list,falselabel);
  320. cg.a_load_const_reg(list,OS_INT,0,hregister);
  321. cg.a_label(list,hl);
  322. end;
  323. else
  324. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  325. end;
  326. { reset hi part, take care of the signed bit of the current value }
  327. hregisterhi:=cg.getintregister(list,OS_INT);
  328. if (dst_size=OS_S64) and
  329. (l.size in [OS_S8,OS_S16,OS_S32]) then
  330. begin
  331. if l.loc=LOC_CONSTANT then
  332. begin
  333. if (longint(l.value)<0) then
  334. cg.a_load_const_reg(list,OS_32,$ffffffff,hregisterhi)
  335. else
  336. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  337. end
  338. else
  339. begin
  340. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  341. hregisterhi);
  342. end;
  343. end
  344. else
  345. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  346. location_reset(l,LOC_REGISTER,dst_size);
  347. l.registerlow:=hregister;
  348. l.registerhigh:=hregisterhi;
  349. end
  350. else
  351. begin
  352. { 64bit to 64bit }
  353. if (l.loc=LOC_REGISTER) or
  354. ((l.loc=LOC_CREGISTER) and maybeconst) then
  355. begin
  356. hregister:=l.registerlow;
  357. hregisterhi:=l.registerhigh;
  358. end
  359. else
  360. begin
  361. hregister:=cg.getintregister(list,OS_INT);
  362. hregisterhi:=cg.getintregister(list,OS_INT);
  363. location_release(list,l);
  364. end;
  365. hreg64.reglo:=hregister;
  366. hreg64.reghi:=hregisterhi;
  367. { load value in new register }
  368. cg64.a_load64_loc_reg(list,l,hreg64,false);
  369. location_reset(l,LOC_REGISTER,dst_size);
  370. l.registerlow:=hregister;
  371. l.registerhigh:=hregisterhi;
  372. end;
  373. end
  374. else
  375. begin
  376. {Do not bother to recycle the existing register. The register
  377. allocator eliminates unnecessary moves, so it's not needed
  378. and trying to recycle registers can cause problems because
  379. the registers changes size and may need aditional constraints.}
  380. location_release(list,l);
  381. hregister:=cg.getintregister(list,dst_size);
  382. { load value in new register }
  383. case l.loc of
  384. LOC_FLAGS :
  385. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  386. LOC_JUMP :
  387. begin
  388. cg.a_label(list,truelabel);
  389. cg.a_load_const_reg(list,dst_size,1,hregister);
  390. objectlibrary.getlabel(hl);
  391. cg.a_jmp_always(list,hl);
  392. cg.a_label(list,falselabel);
  393. cg.a_load_const_reg(list,dst_size,0,hregister);
  394. cg.a_label(list,hl);
  395. end;
  396. else
  397. begin
  398. { load_loc_reg can only handle size >= l.size, when the
  399. new size is smaller then we need to adjust the size
  400. of the orignal and maybe recalculate l.register for i386 }
  401. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  402. begin
  403. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  404. l.register:=cg.makeregsize(l.register,dst_size);
  405. { for big endian systems, the reference's offset must }
  406. { be increased in this case, since they have the }
  407. { MSB first in memory and e.g. byte(word_var) should }
  408. { return the second byte in this case (JM) }
  409. if (target_info.endian = ENDIAN_BIG) and
  410. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  411. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  412. {$ifdef i386}
  413. l.size:=dst_size;
  414. {$endif i386}
  415. end;
  416. cg.a_load_loc_reg(list,dst_size,l,hregister);
  417. {$ifndef i386}
  418. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  419. l.size:=dst_size;
  420. {$endif not i386}
  421. end;
  422. end;
  423. if (l.loc <> LOC_CREGISTER) or
  424. not maybeconst then
  425. location_reset(l,LOC_REGISTER,dst_size)
  426. else
  427. location_reset(l,LOC_CREGISTER,dst_size);
  428. l.register:=hregister;
  429. end;
  430. { Release temp when it was a reference }
  431. if oldloc.loc=LOC_REFERENCE then
  432. location_freetemp(list,oldloc);
  433. end;
  434. {$else cpu64bit}
  435. { 64-bit version }
  436. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  437. var
  438. hregister : tregister;
  439. hl : tasmlabel;
  440. oldloc : tlocation;
  441. begin
  442. oldloc:=l;
  443. if dst_size=OS_NO then
  444. internalerror(200309144);
  445. { handle transformations to 64bit separate }
  446. if dst_size in [OS_64,OS_S64] then
  447. begin
  448. { load a smaller size to OS_64 }
  449. if l.loc=LOC_REGISTER then
  450. hregister:=cg.makeregsize(l.register,OS_INT)
  451. else
  452. begin
  453. location_release(list,l);
  454. hregister:=cg.getregisterint(list,OS_INT);
  455. end;
  456. { load value in low register }
  457. case l.loc of
  458. {$ifdef cpuflags}
  459. LOC_FLAGS :
  460. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  461. {$endif cpuflags}
  462. LOC_JUMP :
  463. begin
  464. cg.a_label(list,truelabel);
  465. cg.a_load_const_reg(list,OS_INT,1,hregister);
  466. objectlibrary.getlabel(hl);
  467. cg.a_jmp_always(list,hl);
  468. cg.a_label(list,falselabel);
  469. cg.a_load_const_reg(list,OS_INT,0,hregister);
  470. cg.a_label(list,hl);
  471. end;
  472. else
  473. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  474. end;
  475. location_reset(l,LOC_REGISTER,dst_size);
  476. l.register:=hregister;
  477. end
  478. else
  479. begin
  480. { transformations to 32bit or smaller }
  481. if l.loc=LOC_REGISTER then
  482. begin
  483. hregister:=l.register;
  484. end
  485. else
  486. begin
  487. { get new register }
  488. if (l.loc=LOC_CREGISTER) and
  489. maybeconst and
  490. (TCGSize2Size[dst_size]=TCGSize2Size[l.size]) then
  491. hregister:=l.register
  492. else
  493. begin
  494. location_release(list,l);
  495. hregister:=cg.getregisterint(list,OS_INT);
  496. end;
  497. end;
  498. hregister:=cg.makeregsize(hregister,dst_size);
  499. { load value in new register }
  500. case l.loc of
  501. {$ifdef cpuflags}
  502. LOC_FLAGS :
  503. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  504. {$endif cpuflags}
  505. LOC_JUMP :
  506. begin
  507. cg.a_label(list,truelabel);
  508. cg.a_load_const_reg(list,dst_size,1,hregister);
  509. objectlibrary.getlabel(hl);
  510. cg.a_jmp_always(list,hl);
  511. cg.a_label(list,falselabel);
  512. cg.a_load_const_reg(list,dst_size,0,hregister);
  513. cg.a_label(list,hl);
  514. end;
  515. else
  516. begin
  517. { load_loc_reg can only handle size >= l.size, when the
  518. new size is smaller then we need to adjust the size
  519. of the orignal and maybe recalculate l.register for i386 }
  520. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  521. begin
  522. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  523. l.register:=cg.makeregsize(l.register,dst_size);
  524. { for big endian systems, the reference's offset must }
  525. { be increased in this case, since they have the }
  526. { MSB first in memory and e.g. byte(word_var) should }
  527. { return the second byte in this case (JM) }
  528. if (target_info.endian = ENDIAN_BIG) and
  529. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  530. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  531. {$ifdef i386}
  532. l.size:=dst_size;
  533. {$endif i386}
  534. end;
  535. cg.a_load_loc_reg(list,dst_size,l,hregister);
  536. {$ifndef i386}
  537. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  538. l.size:=dst_size;
  539. {$endif not i386}
  540. end;
  541. end;
  542. location_reset(l,LOC_REGISTER,dst_size);
  543. l.register:=hregister;
  544. end;
  545. { Release temp when it was a reference }
  546. if oldloc.loc=LOC_REFERENCE then
  547. location_freetemp(list,oldloc);
  548. end;
  549. {$endif cpu64bit}
  550. procedure location_force_fpureg(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  551. var
  552. reg : tregister;
  553. begin
  554. if (l.loc<>LOC_FPUREGISTER) and
  555. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  556. begin
  557. reg:=cg.getfpuregister(list,l.size);
  558. cg.a_loadfpu_loc_reg(list,l,reg);
  559. location_freetemp(list,l);
  560. location_release(list,l);
  561. location_reset(l,LOC_FPUREGISTER,l.size);
  562. l.register:=reg;
  563. end;
  564. end;
  565. procedure location_force_mem(list:TAAsmoutput;var l:tlocation);
  566. var
  567. r : treference;
  568. begin
  569. case l.loc of
  570. LOC_FPUREGISTER,
  571. LOC_CFPUREGISTER :
  572. begin
  573. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  574. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  575. location_release(list,l);
  576. location_reset(l,LOC_REFERENCE,l.size);
  577. l.reference:=r;
  578. end;
  579. LOC_CONSTANT,
  580. LOC_REGISTER,
  581. LOC_CREGISTER :
  582. begin
  583. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  584. if l.size in [OS_64,OS_S64] then
  585. begin
  586. cg64.a_load64_loc_ref(list,l,r);
  587. location_release(list,l);
  588. end
  589. else
  590. begin
  591. location_release(list,l);
  592. cg.a_load_loc_ref(list,l.size,l,r);
  593. end;
  594. location_reset(l,LOC_REFERENCE,l.size);
  595. l.reference:=r;
  596. end;
  597. LOC_CREFERENCE,
  598. LOC_REFERENCE : ;
  599. else
  600. internalerror(200203219);
  601. end;
  602. end;
  603. {*****************************************************************************
  604. Maybe_Save
  605. *****************************************************************************}
  606. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  607. begin
  608. {$ifdef i386}
  609. if (needed>=maxfpuregs) and
  610. (l.loc = LOC_FPUREGISTER) then
  611. begin
  612. location_force_mem(list,l);
  613. maybe_pushfpu:=true;
  614. end
  615. else
  616. maybe_pushfpu:=false;
  617. {$endif i386}
  618. end;
  619. {****************************************************************************
  620. Init/Finalize Code
  621. ****************************************************************************}
  622. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  623. var
  624. href1,href2 : treference;
  625. list:TAAsmoutput;
  626. hsym : tvarsym;
  627. l : longint;
  628. loadref : boolean;
  629. localcopyloc : tparalocation;
  630. begin
  631. list:=taasmoutput(arg);
  632. if (tsym(p).typ=varsym) and
  633. (tvarsym(p).varspez=vs_value) and
  634. (paramanager.push_addr_param(tvarsym(p).varspez,tvarsym(p).vartype.def,current_procinfo.procdef.proccalloption)) then
  635. begin
  636. loadref:=true;
  637. case tvarsym(p).localloc.loc of
  638. LOC_REGISTER :
  639. begin
  640. reference_reset_base(href1,tvarsym(p).localloc.register,0);
  641. loadref:=false;
  642. end;
  643. LOC_REFERENCE :
  644. reference_reset_base(href1,tvarsym(p).localloc.reference.index,
  645. tvarsym(p).localloc.reference.offset);
  646. else
  647. internalerror(200309181);
  648. end;
  649. if is_open_array(tvarsym(p).vartype.def) or
  650. is_array_of_const(tvarsym(p).vartype.def) then
  651. begin
  652. { cdecl functions don't have a high pointer so it is not possible to generate
  653. a local copy }
  654. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  655. begin
  656. hsym:=tvarsym(tsym(p).owner.search('high'+p.name));
  657. if not assigned(hsym) then
  658. internalerror(200306061);
  659. case hsym.localloc.loc of
  660. LOC_REFERENCE :
  661. begin
  662. reference_reset_base(href2,hsym.localloc.reference.index,hsym.localloc.reference.offset);
  663. cg.g_copyvaluepara_openarray(list,href1,href2,tarraydef(tvarsym(p).vartype.def).elesize)
  664. end
  665. else
  666. internalerror(200309182);
  667. end;
  668. end;
  669. end
  670. else
  671. begin
  672. if tvarsym(p).localloc.loc<>LOC_REFERENCE then
  673. internalerror(200309183);
  674. { Allocate space for the local copy }
  675. l:=tvarsym(p).getvaluesize;
  676. localcopyloc.loc:=LOC_REFERENCE;
  677. localcopyloc.size:=int_cgsize(l);
  678. tg.GetLocal(list,l,localcopyloc.reference);
  679. { Copy data }
  680. reference_reset_base(href2,localcopyloc.reference.index,localcopyloc.reference.offset);
  681. if is_shortstring(tvarsym(p).vartype.def) then
  682. begin
  683. { this code is only executed before the code for the body and the entry/exit code is generated
  684. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  685. }
  686. include(current_procinfo.flags,pi_do_call);
  687. cg.g_copyshortstring(list,href1,href2,tstringdef(tvarsym(p).vartype.def).len,false,loadref)
  688. end
  689. else
  690. cg.g_concatcopy(list,href1,href2,tvarsym(p).vartype.def.size,true,loadref);
  691. { update localloc of varsym }
  692. tg.Ungetlocal(list,tvarsym(p).localloc.reference);
  693. tvarsym(p).localloc:=localcopyloc;
  694. end;
  695. end;
  696. end;
  697. { generates the code for initialisation of local data }
  698. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  699. var
  700. href : treference;
  701. list:TAAsmoutput;
  702. begin
  703. list:=taasmoutput(arg);
  704. if (tsym(p).typ=varsym) and
  705. (tvarsym(p).refs>0) and
  706. assigned(tvarsym(p).vartype.def) and
  707. not(is_class(tvarsym(p).vartype.def)) and
  708. tvarsym(p).vartype.def.needs_inittable then
  709. begin
  710. if (cs_implicit_exceptions in aktmoduleswitches) then
  711. include(current_procinfo.flags,pi_needs_implicit_finally);
  712. if tvarsym(p).owner.symtabletype=localsymtable then
  713. begin
  714. case tvarsym(p).localloc.loc of
  715. LOC_REFERENCE :
  716. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  717. else
  718. internalerror(2003091810);
  719. end;
  720. end
  721. else
  722. reference_reset_symbol(href,objectlibrary.newasmsymboldata(tvarsym(p).mangledname),0);
  723. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  724. end;
  725. end;
  726. { generates the code for finalisation of local data }
  727. procedure finalize_data(p : tnamedindexitem;arg:pointer);
  728. var
  729. href : treference;
  730. list:TAAsmoutput;
  731. begin
  732. list:=taasmoutput(arg);
  733. case tsym(p).typ of
  734. varsym :
  735. begin
  736. if (tvarsym(p).refs>0) and
  737. not(vo_is_funcret in tvarsym(p).varoptions) and
  738. assigned(tvarsym(p).vartype.def) and
  739. not(is_class(tvarsym(p).vartype.def)) and
  740. tvarsym(p).vartype.def.needs_inittable then
  741. begin
  742. if tvarsym(p).owner.symtabletype=localsymtable then
  743. begin
  744. case tvarsym(p).localloc.loc of
  745. LOC_REFERENCE :
  746. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  747. else
  748. internalerror(2003091811);
  749. end;
  750. end
  751. else
  752. reference_reset_symbol(href,objectlibrary.newasmsymboldata(tvarsym(p).mangledname),0);
  753. cg.g_finalize(list,tvarsym(p).vartype.def,href,false);
  754. end;
  755. end;
  756. typedconstsym :
  757. begin
  758. if ttypedconstsym(p).is_writable and
  759. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  760. begin
  761. reference_reset_symbol(href,objectlibrary.newasmsymboldata(ttypedconstsym(p).mangledname),0);
  762. cg.g_finalize(list,ttypedconstsym(p).typedconsttype.def,href,false);
  763. end;
  764. end;
  765. end;
  766. end;
  767. { generates the code for incrementing the reference count of parameters and
  768. initialize out parameters }
  769. procedure init_paras(p : tnamedindexitem;arg:pointer);
  770. var
  771. href : treference;
  772. tmpreg : tregister;
  773. list:TAAsmoutput;
  774. begin
  775. list:=taasmoutput(arg);
  776. if (tsym(p).typ=varsym) and
  777. not is_class(tvarsym(p).vartype.def) and
  778. tvarsym(p).vartype.def.needs_inittable then
  779. begin
  780. case tvarsym(p).varspez of
  781. vs_value :
  782. begin
  783. if (cs_implicit_exceptions in aktmoduleswitches) then
  784. include(current_procinfo.flags,pi_needs_implicit_finally);
  785. if tvarsym(p).localloc.loc<>LOC_REFERENCE then
  786. internalerror(200309187);
  787. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  788. cg.g_incrrefcount(list,tvarsym(p).vartype.def,href,is_open_array(tvarsym(p).vartype.def));
  789. end;
  790. vs_out :
  791. begin
  792. case tvarsym(p).localloc.loc of
  793. LOC_REFERENCE :
  794. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  795. else
  796. internalerror(2003091810);
  797. end;
  798. tmpreg:=cg.getaddressregister(list);
  799. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,tmpreg);
  800. reference_reset_base(href,tmpreg,0);
  801. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  802. cg.ungetregister(list,tmpreg);
  803. end;
  804. end;
  805. end;
  806. end;
  807. { generates the code for decrementing the reference count of parameters }
  808. procedure final_paras(p : tnamedindexitem;arg:pointer);
  809. var
  810. href : treference;
  811. list:TAAsmoutput;
  812. begin
  813. list:=taasmoutput(arg);
  814. if (tsym(p).typ=varsym) and
  815. not is_class(tvarsym(p).vartype.def) and
  816. tvarsym(p).vartype.def.needs_inittable then
  817. begin
  818. if (tvarsym(p).varspez=vs_value) then
  819. begin
  820. if tvarsym(p).localloc.loc<>LOC_REFERENCE then
  821. internalerror(200309188);
  822. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  823. cg.g_decrrefcount(list,tvarsym(p).vartype.def,href,is_open_array(tvarsym(p).vartype.def));
  824. end;
  825. end;
  826. end;
  827. { Initialize temp ansi/widestrings,interfaces }
  828. procedure inittempvariables(list:taasmoutput);
  829. var
  830. hp : ptemprecord;
  831. href : treference;
  832. begin
  833. hp:=tg.templist;
  834. while assigned(hp) do
  835. begin
  836. if assigned(hp^.def) then
  837. begin
  838. if (cs_implicit_exceptions in aktmoduleswitches) then
  839. include(current_procinfo.flags,pi_needs_implicit_finally);
  840. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  841. cg.g_initialize(list,hp^.def,href,false);
  842. end;
  843. hp:=hp^.next;
  844. end;
  845. end;
  846. procedure finalizetempvariables(list:taasmoutput);
  847. var
  848. hp : ptemprecord;
  849. href : treference;
  850. paraloc1 : tparalocation;
  851. begin
  852. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  853. hp:=tg.templist;
  854. while assigned(hp) do
  855. begin
  856. if assigned(hp^.def) then
  857. begin
  858. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  859. cg.g_finalize(list,hp^.def,href,false);
  860. end;
  861. hp:=hp^.next;
  862. end;
  863. end;
  864. procedure gen_load_return_value(list:TAAsmoutput; var uses_acc,uses_acchi,uses_fpu : boolean);
  865. var
  866. ressym : tvarsym;
  867. resloc : tlocation;
  868. href : treference;
  869. hreg : tregister;
  870. begin
  871. { Is the loading needed? }
  872. if is_void(current_procinfo.procdef.rettype.def) or
  873. (
  874. (po_assembler in current_procinfo.procdef.procoptions) and
  875. (not(assigned(current_procinfo.procdef.funcretsym)) or
  876. (tvarsym(current_procinfo.procdef.funcretsym).refs=0))
  877. ) then
  878. exit;
  879. { Constructors need to return self }
  880. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  881. begin
  882. cg.getexplicitregister(list,NR_FUNCTION_RETURN_REG);
  883. { return the self pointer }
  884. ressym:=tvarsym(current_procinfo.procdef.parast.search('self'));
  885. if not assigned(ressym) then
  886. internalerror(200305058);
  887. case ressym.localloc.loc of
  888. LOC_REFERENCE :
  889. reference_reset_base(href,ressym.localloc.reference.index,ressym.localloc.reference.offset);
  890. else
  891. internalerror(2003091810);
  892. end;
  893. cg.ungetregister(list,NR_FUNCTION_RETURN_REG);
  894. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_FUNCTION_RETURN_REG);
  895. uses_acc:=true;
  896. exit;
  897. end;
  898. ressym := tvarsym(current_procinfo.procdef.funcretsym);
  899. if (ressym.refs>0) then
  900. begin
  901. case ressym.localloc.loc of
  902. LOC_FPUREGISTER,
  903. LOC_REGISTER :
  904. begin
  905. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  906. location_reset(resloc,LOC_CREGISTER,OS_ADDR)
  907. else
  908. if ressym.vartype.def.deftype = floatdef then
  909. location_reset(resloc,LOC_CFPUREGISTER,def_cgsize(current_procinfo.procdef.rettype.def))
  910. else
  911. location_reset(resloc,LOC_CREGISTER,def_cgsize(current_procinfo.procdef.rettype.def));
  912. resloc.register:=ressym.localloc.register;
  913. end;
  914. LOC_REFERENCE :
  915. begin
  916. location_reset(resloc,LOC_REFERENCE,def_cgsize(current_procinfo.procdef.rettype.def));
  917. reference_reset_base(resloc.reference,ressym.localloc.reference.index,ressym.localloc.reference.offset);
  918. end;
  919. else
  920. internalerror(200309184);
  921. end;
  922. { Here, we return the function result. In most architectures, the value is
  923. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  924. function returns in a register and the caller receives it in an other one }
  925. case current_procinfo.procdef.rettype.def.deftype of
  926. orddef,
  927. enumdef :
  928. begin
  929. uses_acc:=true;
  930. {$ifndef cpu64bit}
  931. if resloc.size in [OS_64,OS_S64] then
  932. begin
  933. uses_acchi:=true;
  934. cg.getexplicitregister(list,NR_FUNCTION_RETURN64_LOW_REG);
  935. cg.getexplicitregister(list,NR_FUNCTION_RETURN64_HIGH_REG);
  936. cg.ungetregister(list,NR_FUNCTION_RETURN64_LOW_REG);
  937. cg.ungetregister(list,NR_FUNCTION_RETURN64_HIGH_REG);
  938. cg64.a_load64_loc_reg(list,resloc,joinreg64(NR_FUNCTION_RETURN64_LOW_REG,
  939. NR_FUNCTION_RETURN64_HIGH_REG),false);
  940. end
  941. else
  942. {$endif cpu64bit}
  943. begin
  944. cg.getexplicitregister(list,NR_FUNCTION_RETURN_REG);
  945. hreg:=cg.makeregsize(NR_FUNCTION_RETURN_REG,resloc.size);
  946. cg.ungetregister(list,hreg);
  947. cg.a_load_loc_reg(list,resloc.size,resloc,hreg);
  948. end;
  949. end;
  950. floatdef :
  951. begin
  952. uses_fpu := true;
  953. {$ifdef cpufpemu}
  954. if cs_fp_emulation in aktmoduleswitches then
  955. cg.a_loadfpu_loc_reg(list,resloc,NR_FUNCTION_RETURN_REG);
  956. else
  957. {$endif cpufpemu}
  958. cg.a_loadfpu_loc_reg(list,resloc,NR_FPU_RESULT_REG);
  959. end;
  960. else
  961. begin
  962. if not paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  963. begin
  964. uses_acc:=true;
  965. {$ifndef cpu64bit}
  966. { Win32 can return records in EAX:EDX }
  967. if resloc.size in [OS_64,OS_S64] then
  968. begin
  969. uses_acchi:=true;
  970. cg.getexplicitregister(list,NR_FUNCTION_RETURN64_LOW_REG);
  971. cg.getexplicitregister(list,NR_FUNCTION_RETURN64_HIGH_REG);
  972. cg.ungetregister(list,NR_FUNCTION_RETURN64_LOW_REG);
  973. cg.ungetregister(list,NR_FUNCTION_RETURN64_HIGH_REG);
  974. cg64.a_load64_loc_reg(list,resloc,joinreg64(NR_FUNCTION_RETURN64_LOW_REG,
  975. NR_FUNCTION_RETURN64_HIGH_REG),false);
  976. end
  977. else
  978. {$endif cpu64bit}
  979. begin
  980. cg.getexplicitregister(list,NR_FUNCTION_RETURN_REG);
  981. hreg:=cg.makeregsize(NR_FUNCTION_RETURN_REG,resloc.size);
  982. cg.ungetregister(list,hreg);
  983. cg.a_load_loc_reg(list,resloc.size,resloc,hreg);
  984. end;
  985. end
  986. end;
  987. end;
  988. end;
  989. end;
  990. procedure gen_load_para_value(list:TAAsmoutput);
  991. var
  992. hp : tparaitem;
  993. href : treference;
  994. gotregvarparas : boolean;
  995. begin
  996. { Store register parameters in reference or in register variable }
  997. if assigned(current_procinfo.procdef.parast) and
  998. not (po_assembler in current_procinfo.procdef.procoptions) then
  999. begin
  1000. { move register parameters which aren't regable into memory }
  1001. { we do this before init_paras because that one calls routines which may overwrite these }
  1002. { registers and it also expects the values to be in memory }
  1003. hp:=tparaitem(current_procinfo.procdef.para.first);
  1004. gotregvarparas := false;
  1005. while assigned(hp) do
  1006. begin
  1007. case tvarsym(hp.parasym).localloc.loc of
  1008. LOC_REGISTER :
  1009. begin
  1010. gotregvarparas := true;
  1011. { cg.a_load_param_reg will first allocate and then deallocate paraloc }
  1012. { register (if the parameter resides in a register) and then allocate }
  1013. { the regvar (which is currently not allocated) }
  1014. cg.a_loadany_param_reg(list,hp.paraloc[calleeside],tvarsym(hp.parasym).localloc.register,nil);
  1015. end;
  1016. LOC_FPUREGISTER:
  1017. begin
  1018. gotregvarparas := true;
  1019. { cg.a_load_param_reg will first allocate and then deallocate paraloc }
  1020. { register (if the parameter resides in a register) and then allocate }
  1021. { the regvar (which is currently not allocated) }
  1022. cg.a_loadany_param_reg(list,hp.paraloc[calleeside],tvarsym(hp.parasym).localloc.register,nil);
  1023. end;
  1024. LOC_REFERENCE :
  1025. begin
  1026. if hp.paraloc[calleeside].loc<>LOC_REFERENCE then
  1027. begin
  1028. if getsupreg(hp.paraloc[calleeside].register)<first_int_imreg then
  1029. begin
  1030. {$ifndef cpu64bit}
  1031. if (hp.paraloc[calleeside].size in [OS_S64,OS_64]) then
  1032. begin
  1033. cg.getexplicitregister(list,hp.paraloc[calleeside].registerlow);
  1034. cg.getexplicitregister(list,hp.paraloc[calleeside].registerhigh);
  1035. end
  1036. else
  1037. {$endif cpu64bit}
  1038. cg.getexplicitregister(list,hp.paraloc[calleeside].register);
  1039. end;
  1040. { Release parameter register }
  1041. {$ifndef cpu64bit}
  1042. if (hp.paraloc[calleeside].size in [OS_S64,OS_64]) then
  1043. begin
  1044. cg.ungetregister(list,hp.paraloc[calleeside].registerlow);
  1045. cg.ungetregister(list,hp.paraloc[calleeside].registerhigh);
  1046. end
  1047. else
  1048. {$endif cpu64bit}
  1049. cg.ungetregister(list,hp.paraloc[calleeside].register);
  1050. reference_reset_base(href,tvarsym(hp.parasym).localloc.reference.index,tvarsym(hp.parasym).localloc.reference.offset);
  1051. cg.a_loadany_param_ref(list,hp.paraloc[calleeside],href,nil);
  1052. end;
  1053. end;
  1054. else
  1055. internalerror(200309185);
  1056. end;
  1057. hp:=tparaitem(hp.next);
  1058. end;
  1059. if gotregvarparas then
  1060. begin
  1061. { deallocate all register variables again }
  1062. hp:=tparaitem(current_procinfo.procdef.para.first);
  1063. while assigned(hp) do
  1064. begin
  1065. if (tvarsym(hp.parasym).localloc.loc=LOC_REGISTER) then
  1066. cg.ungetregister(list,tvarsym(hp.parasym).localloc.register);
  1067. hp:=tparaitem(hp.next);
  1068. end;
  1069. end;
  1070. end;
  1071. { generate copies of call by value parameters, must be done before
  1072. the initialization and body is parsed because the refcounts are
  1073. incremented using the local copies }
  1074. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1075. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1076. end;
  1077. procedure gen_initialize_code(list:TAAsmoutput;inlined:boolean);
  1078. begin
  1079. { initialize local data like ansistrings }
  1080. case current_procinfo.procdef.proctypeoption of
  1081. potype_unitinit:
  1082. begin
  1083. { this is also used for initialization of variables in a
  1084. program which does not have a globalsymtable }
  1085. if assigned(current_module.globalsymtable) then
  1086. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1087. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1088. end;
  1089. { units have seperate code for initilization and finalization }
  1090. potype_unitfinalize: ;
  1091. { program init/final is generated in separate procedure }
  1092. potype_proginit: ;
  1093. else
  1094. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1095. end;
  1096. { initialisizes temp. ansi/wide string data }
  1097. inittempvariables(list);
  1098. { initialize ansi/widesstring para's }
  1099. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1100. {$ifdef OLDREGVARS}
  1101. load_regvars(list,nil);
  1102. {$endif OLDREGVARS}
  1103. end;
  1104. procedure gen_finalize_code(list:TAAsmoutput;inlined:boolean);
  1105. begin
  1106. {$ifdef OLDREGVARS}
  1107. cleanup_regvars(list);
  1108. {$endif OLDREGVARS}
  1109. { finalize temporary data }
  1110. finalizetempvariables(list);
  1111. { finalize local data like ansistrings}
  1112. case current_procinfo.procdef.proctypeoption of
  1113. potype_unitfinalize:
  1114. begin
  1115. { this is also used for initialization of variables in a
  1116. program which does not have a globalsymtable }
  1117. if assigned(current_module.globalsymtable) then
  1118. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1119. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1120. end;
  1121. { units/progs have separate code for initialization and finalization }
  1122. potype_unitinit: ;
  1123. { program init/final is generated in separate procedure }
  1124. potype_proginit: ;
  1125. else
  1126. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1127. end;
  1128. { finalize paras data }
  1129. if assigned(current_procinfo.procdef.parast) then
  1130. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1131. end;
  1132. procedure gen_entry_code(list:TAAsmoutput);
  1133. var
  1134. href : treference;
  1135. paraloc1,
  1136. paraloc2 : tparalocation;
  1137. begin
  1138. { the actual profile code can clobber some registers,
  1139. therefore if the context must be saved, do it before
  1140. the actual call to the profile code
  1141. }
  1142. if (cs_profile in aktmoduleswitches) and
  1143. not(po_assembler in current_procinfo.procdef.procoptions) then
  1144. begin
  1145. { non-win32 can call mcout even in main }
  1146. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1147. cg.g_profilecode(list)
  1148. else
  1149. { wdosx, and win32 should not call mcount before monstartup has been called }
  1150. if not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1151. cg.g_profilecode(list);
  1152. end;
  1153. { call startup helpers from main program }
  1154. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1155. begin
  1156. { initialize profiling for win32 }
  1157. if (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1158. (cs_profile in aktmoduleswitches) then
  1159. begin
  1160. reference_reset_symbol(href,objectlibrary.newasmsymboldata('etext'),0);
  1161. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1162. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  1163. paramanager.allocparaloc(list,paraloc2);
  1164. cg.a_paramaddr_ref(list,href,paraloc2);
  1165. reference_reset_symbol(href,objectlibrary.newasmsymboldata('__image_base__'),0);
  1166. paramanager.allocparaloc(list,paraloc1);
  1167. cg.a_paramaddr_ref(list,href,paraloc1);
  1168. paramanager.freeparaloc(list,paraloc2);
  1169. paramanager.freeparaloc(list,paraloc1);
  1170. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_cdecl));
  1171. cg.a_call_name(list,'_monstartup');
  1172. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_cdecl));
  1173. end;
  1174. { initialize units }
  1175. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1176. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1177. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1178. end;
  1179. {$ifdef GDB}
  1180. if (cs_debuginfo in aktmoduleswitches) then
  1181. list.concat(Tai_force_line.Create);
  1182. {$endif GDB}
  1183. {$ifdef OLDREGVARS}
  1184. load_regvars(list,nil);
  1185. {$endif OLDREGVARS}
  1186. end;
  1187. procedure gen_exit_code(list:TAAsmoutput);
  1188. begin
  1189. { call __EXIT for main program }
  1190. if (not DLLsource) and
  1191. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1192. cg.a_call_name(list,'FPC_DO_EXIT');
  1193. end;
  1194. {****************************************************************************
  1195. Entry/Exit
  1196. ****************************************************************************}
  1197. procedure gen_proc_symbol(list:Taasmoutput);
  1198. var
  1199. hs : string;
  1200. begin
  1201. { add symbol entry point as well as debug information }
  1202. { will be inserted in front of the rest of this list. }
  1203. { Insert alignment and assembler names }
  1204. { Align, gprof uses 16 byte granularity }
  1205. if (cs_profile in aktmoduleswitches) then
  1206. list.concat(Tai_align.create(16))
  1207. else
  1208. list.concat(Tai_align.create(aktalignment.procalign));
  1209. {$ifdef GDB}
  1210. if (cs_debuginfo in aktmoduleswitches) then
  1211. begin
  1212. if (po_public in current_procinfo.procdef.procoptions) then
  1213. Tprocsym(current_procinfo.procdef.procsym).is_global:=true;
  1214. current_procinfo.procdef.concatstabto(list);
  1215. Tprocsym(current_procinfo.procdef.procsym).isstabwritten:=true;
  1216. end;
  1217. {$endif GDB}
  1218. repeat
  1219. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1220. if hs='' then
  1221. break;
  1222. {$ifdef GDB}
  1223. if (cs_debuginfo in aktmoduleswitches) and
  1224. target_info.use_function_relative_addresses then
  1225. list.concat(Tai_stab_function_name.create(strpnew(hs)));
  1226. {$endif GDB}
  1227. if (cs_profile in aktmoduleswitches) or
  1228. (po_public in current_procinfo.procdef.procoptions) then
  1229. list.concat(Tai_symbol.createname_global(hs,0))
  1230. else
  1231. list.concat(Tai_symbol.createname(hs,0));
  1232. until false;
  1233. end;
  1234. procedure gen_proc_symbol_end(list:Taasmoutput);
  1235. {$ifdef GDB}
  1236. var
  1237. stabsendlabel : tasmlabel;
  1238. mangled_length : longint;
  1239. p : pchar;
  1240. {$endif GDB}
  1241. begin
  1242. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1243. {$ifdef GDB}
  1244. if (cs_debuginfo in aktmoduleswitches) then
  1245. begin
  1246. objectlibrary.getlabel(stabsendlabel);
  1247. cg.a_label(list,stabsendlabel);
  1248. { define calling EBP as pseudo local var PM }
  1249. { this enables test if the function is a local one !! }
  1250. {if assigned(current_procinfo.parent) and
  1251. (current_procinfo.procdef.parast.symtablelevel>normal_function_level) then
  1252. list.concat(Tai_stabs.Create(strpnew(
  1253. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  1254. tostr(N_LSYM)+',0,0,'+tostr(current_procinfo.parent_framepointer_offset)))); }
  1255. if (not is_void(current_procinfo.procdef.rettype.def)) and
  1256. (tvarsym(current_procinfo.procdef.funcretsym).refs>0) then
  1257. begin
  1258. if tvarsym(current_procinfo.procdef.funcretsym).localloc.loc<>LOC_REFERENCE then
  1259. internalerror(2003091812);
  1260. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1261. begin
  1262. list.concat(Tai_stabs.Create(strpnew(
  1263. '"'+current_procinfo.procdef.procsym.name+':X*'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1264. tostr(N_tsym)+',0,0,'+tostr(tvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1265. if (m_result in aktmodeswitches) then
  1266. list.concat(Tai_stabs.Create(strpnew(
  1267. '"RESULT:X*'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1268. tostr(N_tsym)+',0,0,'+tostr(tvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))))
  1269. end
  1270. else
  1271. begin
  1272. list.concat(Tai_stabs.Create(strpnew(
  1273. '"'+current_procinfo.procdef.procsym.name+':X'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1274. tostr(N_tsym)+',0,0,'+tostr(tvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1275. if (m_result in aktmodeswitches) then
  1276. list.concat(Tai_stabs.Create(strpnew(
  1277. '"RESULT:X'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1278. tostr(N_tsym)+',0,0,'+tostr(tvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1279. end;
  1280. end;
  1281. mangled_length:=length(current_procinfo.procdef.mangledname);
  1282. getmem(p,2*mangled_length+50);
  1283. strpcopy(p,'192,0,0,');
  1284. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1285. if (target_info.use_function_relative_addresses) then
  1286. begin
  1287. strpcopy(strend(p),'-');
  1288. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1289. end;
  1290. list.concat(Tai_stabn.Create(strnew(p)));
  1291. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  1292. +current_procinfo.procdef.mangledname))));
  1293. p[0]:='2';p[1]:='2';p[2]:='4';
  1294. strpcopy(strend(p),'_end');}
  1295. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  1296. if (target_info.use_function_relative_addresses) then
  1297. begin
  1298. strpcopy(strend(p),'-');
  1299. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1300. end;
  1301. list.concatlist(withdebuglist);
  1302. list.concat(Tai_stabn.Create(strnew(p)));
  1303. { strpnew('224,0,0,'
  1304. +current_procinfo.procdef.mangledname+'_end'))));}
  1305. freemem(p,2*mangled_length+50);
  1306. end;
  1307. {$endif GDB}
  1308. end;
  1309. procedure gen_stackalloc_code(list:Taasmoutput);
  1310. var
  1311. stackframe : longint;
  1312. begin
  1313. { Calculate size of stackframe }
  1314. stackframe:=current_procinfo.calc_stackframe_size;
  1315. {$ifndef powerpc}
  1316. { at least for the ppc this applies always, so this code isn't usable (FK) }
  1317. { omit stack frame ? }
  1318. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1319. begin
  1320. CGmessage(cg_d_stackframe_omited);
  1321. if stackframe<>0 then
  1322. cg.g_stackpointer_alloc(list,stackframe);
  1323. end
  1324. else
  1325. {$endif powerpc}
  1326. begin
  1327. if (po_interrupt in current_procinfo.procdef.procoptions) then
  1328. cg.g_interrupt_stackframe_entry(list);
  1329. cg.g_stackframe_entry(list,stackframe);
  1330. {Never call stack checking before the standard system unit
  1331. has been initialized.}
  1332. if (cs_check_stack in aktlocalswitches) and (current_procinfo.procdef.proctypeoption<>potype_proginit) then
  1333. cg.g_stackcheck(list,stackframe);
  1334. end;
  1335. end;
  1336. procedure gen_stackfree_code(list:Taasmoutput;usesacc,usesacchi:boolean);
  1337. var
  1338. stacksize,
  1339. retsize : longint;
  1340. begin
  1341. {$ifndef powerpc}
  1342. { remove stackframe }
  1343. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1344. begin
  1345. stacksize:=current_procinfo.calc_stackframe_size;
  1346. if (stacksize<>0) then
  1347. cg.a_op_const_reg(list,OP_ADD,OS_32,stacksize,current_procinfo.framepointer);
  1348. end
  1349. else
  1350. cg.g_restore_frame_pointer(list);
  1351. {$endif}
  1352. { at last, the return is generated }
  1353. if (po_interrupt in current_procinfo.procdef.procoptions) then
  1354. cg.g_interrupt_stackframe_exit(list,usesacc,usesacchi)
  1355. else
  1356. begin
  1357. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1358. begin
  1359. retsize:=0;
  1360. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1361. inc(retsize,POINTER_SIZE);
  1362. end
  1363. else
  1364. retsize:=current_procinfo.para_stack_size;
  1365. cg.g_return_from_proc(list,retsize);
  1366. end;
  1367. end;
  1368. procedure gen_save_used_regs(list:TAAsmoutput);
  1369. begin
  1370. { Pure assembler routines need to save the registers themselves }
  1371. if (po_assembler in current_procinfo.procdef.procoptions) then
  1372. exit;
  1373. { for the save all registers we can simply use a pusha,popa which
  1374. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1375. if (po_saveregisters in current_procinfo.procdef.procoptions) then
  1376. cg.g_save_all_registers(list)
  1377. else
  1378. if current_procinfo.procdef.proccalloption in savestdregs_pocalls then
  1379. cg.g_save_standard_registers(list);
  1380. end;
  1381. procedure gen_restore_used_regs(list:TAAsmoutput;usesacc,usesacchi,usesfpu:boolean);
  1382. begin
  1383. { Pure assembler routines need to save the registers themselves }
  1384. if (po_assembler in current_procinfo.procdef.procoptions) then
  1385. exit;
  1386. { for the save all registers we can simply use a pusha,popa which
  1387. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1388. if (po_saveregisters in current_procinfo.procdef.procoptions) then
  1389. cg.g_restore_all_registers(list,usesacc,usesacchi)
  1390. else
  1391. if current_procinfo.procdef.proccalloption in savestdregs_pocalls then
  1392. cg.g_restore_standard_registers(list);
  1393. end;
  1394. {****************************************************************************
  1395. Inlining
  1396. ****************************************************************************}
  1397. (*
  1398. procedure load_inlined_return_value(list:TAAsmoutput);
  1399. var
  1400. ressym: tvarsym;
  1401. resloc: tlocation;
  1402. r,r2 : tregister;
  1403. begin
  1404. if not is_void(current_procinfo.procdef.rettype.def) then
  1405. begin
  1406. ressym := tvarsym(current_procinfo.procdef.funcretsym);
  1407. if ressym.reg.enum <> R_NO then
  1408. begin
  1409. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1410. location_reset(resloc,LOC_CREGISTER,OS_ADDR)
  1411. else
  1412. if ressym.vartype.def.deftype = floatdef then
  1413. location_reset(resloc,LOC_CFPUREGISTER,def_cgsize(current_procinfo.procdef.rettype.def))
  1414. else
  1415. location_reset(resloc,LOC_CREGISTER,def_cgsize(current_procinfo.procdef.rettype.def));
  1416. resloc.register := ressym.reg;
  1417. end
  1418. else
  1419. begin
  1420. location_reset(resloc,LOC_CREGISTER,def_cgsize(current_procinfo.procdef.rettype.def));
  1421. reference_reset_base(resloc.reference,current_procinfo.framepointer,tvarsym(current_procinfo.procdef.funcretsym).adjusted_address);
  1422. end;
  1423. { Here, we return the function result. In most architectures, the value is
  1424. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1425. function returns in a register and the caller receives it in an other one }
  1426. case current_procinfo.procdef.rettype.def.deftype of
  1427. orddef,
  1428. enumdef :
  1429. begin
  1430. {$ifndef cpu64bit}
  1431. if resloc.size in [OS_64,OS_S64] then
  1432. begin
  1433. r:=cg.getregisterint(list,OS_INT);
  1434. r2:=cg.getregisterint(list,OS_INT);
  1435. cg64.a_load64_loc_reg(list,resloc,joinreg64(r,r2),false);
  1436. end
  1437. else
  1438. {$endif cpu64bit}
  1439. begin
  1440. r:=cg.getregisterint(list,resloc.size);
  1441. cg.a_load_loc_reg(list,resloc.size,resloc,r);
  1442. end;
  1443. end;
  1444. floatdef :
  1445. begin
  1446. {$ifdef cpufpemu}
  1447. if cs_fp_emulation in aktmoduleswitches then
  1448. r.enum := FUNCTION_RETURN_REG
  1449. else
  1450. {$endif cpufpemu}
  1451. r.enum:=FPU_RESULT_REG;
  1452. cg.a_loadfpu_loc_reg(list,resloc,r);
  1453. end;
  1454. else
  1455. begin
  1456. if not paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1457. begin
  1458. {$ifndef cpu64bit}
  1459. { Win32 can return records in EAX:EDX }
  1460. if resloc.size in [OS_64,OS_S64] then
  1461. begin
  1462. r:=cg.getregisterint(list,OS_INT);
  1463. r2:=cg.getregisterint(list,OS_INT);
  1464. cg64.a_load64_loc_reg(list,resloc,joinreg64(r,r2),false);
  1465. end
  1466. else
  1467. {$endif cpu64bit}
  1468. begin
  1469. r:=cg.getregisterint(list,resloc.size);
  1470. cg.a_load_loc_reg(list,resloc.size,resloc,r);
  1471. end;
  1472. end
  1473. end;
  1474. end;
  1475. end;
  1476. end;
  1477. procedure geninlineentrycode(list:TAAsmoutput;stackframe:longint);
  1478. begin
  1479. { initialize return value }
  1480. initretvalue(list);
  1481. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1482. { initialisizes temp. ansi/wide string data }
  1483. inittempvariables(list);
  1484. { initialize ansi/widesstring para's }
  1485. if assigned(current_procinfo.procdef.parast) then
  1486. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1487. { generate copies of call by value parameters }
  1488. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1489. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1490. load_regvars(list,nil);
  1491. end;
  1492. procedure geninlineexitcode(list:TAAsmoutput;inlined:boolean);
  1493. var
  1494. usesacc,
  1495. usesacchi,
  1496. usesfpu : boolean;
  1497. begin
  1498. if aktexitlabel.is_used then
  1499. cg.a_label(list,aktexitlabel);
  1500. cleanup_regvars(list);
  1501. { finalize temporary data }
  1502. finalizetempvariables(list);
  1503. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1504. { finalize paras data }
  1505. if assigned(current_procinfo.procdef.parast) then
  1506. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1507. { handle return value, this is not done for assembler routines when
  1508. they didn't reference the result variable }
  1509. if not(po_assembler in current_procinfo.procdef.procoptions) or
  1510. (assigned(current_procinfo.procdef.funcretsym) and
  1511. (tvarsym(current_procinfo.procdef.funcretsym).refcount>1)) then
  1512. begin
  1513. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1514. internalerror(200305263);
  1515. // load_inlined_return_value(list);
  1516. load_return_value(list,usesacc,usesacchi,usesfpu)
  1517. end;
  1518. cleanup_regvars(list);
  1519. end;
  1520. *)
  1521. {****************************************************************************
  1522. Const Data
  1523. ****************************************************************************}
  1524. procedure insertconstdata(sym : ttypedconstsym);
  1525. { this does not affect the local stack space, since all
  1526. typed constansts and initialized variables are always
  1527. put in the .data / .rodata section
  1528. }
  1529. var
  1530. storefilepos : tfileposinfo;
  1531. curconstsegment : taasmoutput;
  1532. l : longint;
  1533. begin
  1534. storefilepos:=aktfilepos;
  1535. aktfilepos:=sym.fileinfo;
  1536. if sym.is_writable then
  1537. curconstsegment:=datasegment
  1538. else
  1539. curconstsegment:=consts;
  1540. l:=sym.getsize;
  1541. { insert cut for smartlinking or alignment }
  1542. if (cs_create_smart in aktmoduleswitches) then
  1543. curconstSegment.concat(Tai_cut.Create);
  1544. curconstSegment.concat(Tai_align.create(const_align(l)));
  1545. {$ifdef GDB}
  1546. if cs_debuginfo in aktmoduleswitches then
  1547. sym.concatstabto(curconstsegment);
  1548. {$endif GDB}
  1549. if (sym.owner.symtabletype=globalsymtable) or
  1550. (cs_create_smart in aktmoduleswitches) or
  1551. DLLSource then
  1552. curconstSegment.concat(Tai_symbol.Createdataname_global(sym.mangledname,l))
  1553. else
  1554. curconstSegment.concat(Tai_symbol.Createdataname(sym.mangledname,l));
  1555. aktfilepos:=storefilepos;
  1556. end;
  1557. procedure insertbssdata(sym : tvarsym);
  1558. var
  1559. l,varalign : longint;
  1560. storefilepos : tfileposinfo;
  1561. begin
  1562. storefilepos:=aktfilepos;
  1563. aktfilepos:=sym.fileinfo;
  1564. l:=sym.getvaluesize;
  1565. if (vo_is_thread_var in sym.varoptions) then
  1566. inc(l,pointer_size);
  1567. varalign:=var_align(l);
  1568. {
  1569. sym.address:=align(datasize,varalign);
  1570. datasize:=tvarsym(sym).address+l;
  1571. }
  1572. { insert cut for smartlinking or alignment }
  1573. if (cs_create_smart in aktmoduleswitches) then
  1574. bssSegment.concat(Tai_cut.Create);
  1575. bssSegment.concat(Tai_align.create(varalign));
  1576. {$ifdef GDB}
  1577. if cs_debuginfo in aktmoduleswitches then
  1578. sym.concatstabto(bsssegment);
  1579. {$endif GDB}
  1580. if (sym.owner.symtabletype=globalsymtable) or
  1581. (cs_create_smart in aktmoduleswitches) or
  1582. DLLSource or
  1583. (vo_is_exported in sym.varoptions) or
  1584. (vo_is_C_var in sym.varoptions) then
  1585. bssSegment.concat(Tai_datablock.Create_global(sym.mangledname,l))
  1586. else
  1587. bssSegment.concat(Tai_datablock.Create(sym.mangledname,l));
  1588. aktfilepos:=storefilepos;
  1589. end;
  1590. procedure gen_alloc_localst(list:TAAsmoutput;st:tlocalsymtable);
  1591. var
  1592. sym : tsym;
  1593. begin
  1594. sym:=tsym(st.symindex.first);
  1595. while assigned(sym) do
  1596. begin
  1597. { Only allocate space for referenced locals }
  1598. if (sym.typ=varsym) and
  1599. (tvarsym(sym).refs>0) then
  1600. begin
  1601. with tvarsym(sym) do
  1602. begin
  1603. {$warning TODO Add support for register variables}
  1604. localloc.loc:=LOC_REFERENCE;
  1605. tg.GetLocal(list,getvaluesize,localloc.reference);
  1606. if cs_asm_source in aktglobalswitches then
  1607. list.concat(Tai_comment.Create(strpnew('Local '+realname+' located at '+
  1608. std_regname(localloc.reference.index)+tostr_with_plus(localloc.reference.offset))));
  1609. end;
  1610. end;
  1611. sym:=tsym(sym.indexnext);
  1612. end;
  1613. end;
  1614. procedure gen_free_localst(list:TAAsmoutput;st:tlocalsymtable);
  1615. var
  1616. sym : tsym;
  1617. begin
  1618. sym:=tsym(st.symindex.first);
  1619. while assigned(sym) do
  1620. begin
  1621. if (sym.typ=varsym) and
  1622. (tvarsym(sym).refs>0) then
  1623. begin
  1624. with tvarsym(sym) do
  1625. begin
  1626. { Note: We need to keep the data available in memory
  1627. for the sub procedures that can access local data
  1628. in the parent procedures }
  1629. case localloc.loc of
  1630. LOC_REFERENCE :
  1631. tg.Ungetlocal(list,localloc.reference);
  1632. LOC_REGISTER :
  1633. begin
  1634. {$ifndef cpu64bit}
  1635. if localloc.size in [OS_64,OS_S64] then
  1636. begin
  1637. cg.ungetregister(list,localloc.registerlow);
  1638. cg.ungetregister(list,localloc.registerhigh);
  1639. end
  1640. else
  1641. {$endif cpu64bit}
  1642. cg.ungetregister(list,localloc.register);
  1643. end;
  1644. end;
  1645. end;
  1646. end;
  1647. sym:=tsym(sym.indexnext);
  1648. end;
  1649. end;
  1650. procedure gen_alloc_parast(list:TAAsmoutput;st:tparasymtable);
  1651. var
  1652. sym : tsym;
  1653. begin
  1654. sym:=tsym(st.symindex.first);
  1655. while assigned(sym) do
  1656. begin
  1657. if sym.typ=varsym then
  1658. begin
  1659. with tvarsym(sym) do
  1660. begin
  1661. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1662. begin
  1663. case paraitem.paraloc[calleeside].loc of
  1664. LOC_FPUREGISTER,
  1665. LOC_REGISTER:
  1666. begin
  1667. (*
  1668. if paraitem.paraloc[calleeside].register=NR_NO then
  1669. begin
  1670. paraitem.paraloc[calleeside].loc:=LOC_REGISTER;
  1671. paraitem.paraloc[calleeside].size:=paraitem.paraloc[calleeside].size;
  1672. {$ifndef cpu64bit}
  1673. if paraitem.paraloc[calleeside].size in [OS_64,OS_S64] then
  1674. begin
  1675. paraitem.paraloc[calleeside].registerlow:=cg.getregisterint(list,OS_32);
  1676. paraitem.paraloc[calleeside].registerhigh:=cg.getregisterint(list,OS_32);
  1677. end
  1678. else
  1679. {$endif cpu64bit}
  1680. paraitem.paraloc[calleeside].register:=cg.getregisterint(list,localloc.size);
  1681. end;
  1682. *)
  1683. (*
  1684. {$warning TODO Allocate register paras}
  1685. localloc.loc:=LOC_REGISTER;
  1686. localloc.size:=paraitem.paraloc[calleeside].size;
  1687. {$ifndef cpu64bit}
  1688. if localloc.size in [OS_64,OS_S64] then
  1689. begin
  1690. localloc.registerlow:=cg.getregisterint(list,OS_32);
  1691. localloc.registerhigh:=cg.getregisterint(list,OS_32);
  1692. end
  1693. else
  1694. {$endif cpu64bit}
  1695. localloc.register:=cg.getregisterint(list,localloc.size);
  1696. *)
  1697. localloc.loc:=LOC_REFERENCE;
  1698. localloc.size:=paraitem.paraloc[calleeside].size;
  1699. tg.GetLocal(list,tcgsize2size[localloc.size],localloc.reference);
  1700. end
  1701. else
  1702. localloc:=paraitem.paraloc[calleeside];
  1703. end;
  1704. end
  1705. else
  1706. localloc:=paraitem.paraloc[calleeside];
  1707. if cs_asm_source in aktglobalswitches then
  1708. begin
  1709. case localloc.loc of
  1710. LOC_REFERENCE :
  1711. list.concat(Tai_comment.Create(strpnew('Para '+realname+' located at '+
  1712. std_regname(localloc.reference.index)+tostr_with_plus(localloc.reference.offset))));
  1713. end;
  1714. end;
  1715. end;
  1716. end;
  1717. sym:=tsym(sym.indexnext);
  1718. end;
  1719. end;
  1720. procedure gen_alloc_inline_parast(list:TAAsmoutput;st:tparasymtable);
  1721. var
  1722. sym : tsym;
  1723. begin
  1724. sym:=tsym(st.symindex.first);
  1725. while assigned(sym) do
  1726. begin
  1727. if sym.typ=varsym then
  1728. begin
  1729. with tvarsym(sym) do
  1730. begin
  1731. {$warning TODO Allocate register paras}
  1732. localloc.loc:=LOC_REFERENCE;
  1733. localloc.size:=int_cgsize(paramanager.push_size(vs_value,vartype.def,pocall_inline));
  1734. tg.GetLocal(list,tcgsize2size[localloc.size],localloc.reference);
  1735. if cs_asm_source in aktglobalswitches then
  1736. begin
  1737. case localloc.loc of
  1738. LOC_REFERENCE :
  1739. list.concat(Tai_comment.Create(strpnew('Para '+realname+' allocated at '+
  1740. std_regname(localloc.reference.index)+tostr_with_plus(localloc.reference.offset))));
  1741. end;
  1742. end;
  1743. paraitem.paraloc[callerside]:=localloc;
  1744. paraitem.paraloc[calleeside]:=localloc;
  1745. end;
  1746. end;
  1747. sym:=tsym(sym.indexnext);
  1748. end;
  1749. end;
  1750. procedure gen_free_parast(list:TAAsmoutput;st:tparasymtable);
  1751. var
  1752. sym : tsym;
  1753. begin
  1754. sym:=tsym(st.symindex.first);
  1755. while assigned(sym) do
  1756. begin
  1757. if sym.typ=varsym then
  1758. begin
  1759. with tvarsym(sym) do
  1760. begin
  1761. { Note: We need to keep the data available in memory
  1762. for the sub procedures that can access local data
  1763. in the parent procedures }
  1764. case localloc.loc of
  1765. LOC_REFERENCE :
  1766. tg.UngetLocal(list,localloc.reference);
  1767. LOC_REGISTER :
  1768. begin
  1769. if localloc.register<>paraitem.paraloc[calleeside].register then
  1770. begin
  1771. {$ifndef cpu64bit}
  1772. if localloc.size in [OS_64,OS_S64] then
  1773. begin
  1774. cg.ungetregister(list,localloc.registerlow);
  1775. cg.ungetregister(list,localloc.registerhigh);
  1776. end
  1777. else
  1778. {$endif cpu64bit}
  1779. cg.ungetregister(list,localloc.register);
  1780. end;
  1781. end;
  1782. end;
  1783. end;
  1784. end;
  1785. sym:=tsym(sym.indexnext);
  1786. end;
  1787. end;
  1788. end.
  1789. {
  1790. $Log$
  1791. Revision 1.163 2003-11-04 15:35:13 peter
  1792. * fix for referencecounted temps
  1793. Revision 1.162 2003/10/25 11:34:02 florian
  1794. * fixed compilation of ppc system unit
  1795. Revision 1.161 2003/10/19 01:34:30 florian
  1796. * some ppc stuff fixed
  1797. * memory leak fixed
  1798. Revision 1.160 2003/10/17 15:08:34 peter
  1799. * commented out more obsolete constants
  1800. Revision 1.159 2003/10/17 14:38:32 peter
  1801. * 64k registers supported
  1802. * fixed some memory leaks
  1803. Revision 1.158 2003/10/10 17:48:13 peter
  1804. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1805. * tregisteralloctor renamed to trgobj
  1806. * removed rgobj from a lot of units
  1807. * moved location_* and reference_* to cgobj
  1808. * first things for mmx register allocation
  1809. Revision 1.157 2003/10/09 21:31:37 daniel
  1810. * Register allocator splitted, ans abstract now
  1811. Revision 1.156 2003/10/07 18:18:16 peter
  1812. * fix register calling for assembler procedures
  1813. * fix result loading for assembler procedures
  1814. Revision 1.155 2003/10/07 15:17:07 peter
  1815. * inline supported again, LOC_REFERENCEs are used to pass the
  1816. parameters
  1817. * inlineparasymtable,inlinelocalsymtable removed
  1818. * exitlabel inserting fixed
  1819. Revision 1.154 2003/10/01 20:34:48 peter
  1820. * procinfo unit contains tprocinfo
  1821. * cginfo renamed to cgbase
  1822. * moved cgmessage to verbose
  1823. * fixed ppc and sparc compiles
  1824. Revision 1.153 2003/09/30 21:02:37 peter
  1825. * updates for inlining
  1826. Revision 1.152 2003/09/29 20:58:56 peter
  1827. * optimized releasing of registers
  1828. Revision 1.151 2003/09/28 21:47:18 peter
  1829. * register paras and local copies updates
  1830. Revision 1.150 2003/09/28 17:55:03 peter
  1831. * parent framepointer changed to hidden parameter
  1832. * tloadparentfpnode added
  1833. Revision 1.149 2003/09/28 13:39:38 peter
  1834. * optimized releasing of registers
  1835. Revision 1.148 2003/09/25 21:28:00 peter
  1836. * parameter fixes
  1837. Revision 1.147 2003/09/23 21:03:59 peter
  1838. * check for refs>0 in init/final local data
  1839. Revision 1.146 2003/09/23 17:56:05 peter
  1840. * locals and paras are allocated in the code generation
  1841. * tvarsym.localloc contains the location of para/local when
  1842. generating code for the current procedure
  1843. Revision 1.145 2003/09/16 16:17:01 peter
  1844. * varspez in calls to push_addr_param
  1845. Revision 1.144 2003/09/14 21:33:37 peter
  1846. * location_force_reg gives IE when size=OS_NO
  1847. Revision 1.143 2003/09/14 19:18:10 peter
  1848. * remove obsolete code already in comments
  1849. Revision 1.142 2003/09/11 11:54:59 florian
  1850. * improved arm code generation
  1851. * move some protected and private field around
  1852. * the temp. register for register parameters/arguments are now released
  1853. before the move to the parameter register is done. This improves
  1854. the code in a lot of cases.
  1855. Revision 1.141 2003/09/10 08:31:47 marco
  1856. * Patch from Peter for paraloc
  1857. Revision 1.140 2003/09/07 22:09:35 peter
  1858. * preparations for different default calling conventions
  1859. * various RA fixes
  1860. Revision 1.139 2003/09/03 15:55:01 peter
  1861. * NEWRA branch merged
  1862. Revision 1.138 2003/09/03 11:18:37 florian
  1863. * fixed arm concatcopy
  1864. + arm support in the common compiler sources added
  1865. * moved some generic cg code around
  1866. + tfputype added
  1867. * ...
  1868. Revision 1.137.2.4 2003/08/31 15:46:26 peter
  1869. * more updates for tregister
  1870. Revision 1.137.2.3 2003/08/29 17:28:59 peter
  1871. * next batch of updates
  1872. Revision 1.137.2.2 2003/08/28 18:35:08 peter
  1873. * tregister changed to cardinal
  1874. Revision 1.137.2.1 2003/08/27 19:55:54 peter
  1875. * first tregister patch
  1876. Revision 1.137 2003/08/20 20:29:06 daniel
  1877. * Some more R_NO changes
  1878. * Preventive code to loadref added
  1879. Revision 1.136 2003/08/20 17:48:49 peter
  1880. * fixed stackalloc to not allocate localst.datasize twice
  1881. * order of stackalloc code fixed for implicit init/final
  1882. Revision 1.135 2003/08/17 16:59:20 jonas
  1883. * fixed regvars so they work with newra (at least for ppc)
  1884. * fixed some volatile register bugs
  1885. + -dnotranslation option for -dnewra, which causes the registers not to
  1886. be translated from virtual to normal registers. Requires support in
  1887. the assembler writer as well, which is only implemented in aggas/
  1888. agppcgas currently
  1889. Revision 1.134 2003/08/11 21:18:20 peter
  1890. * start of sparc support for newra
  1891. Revision 1.133 2003/08/09 18:56:54 daniel
  1892. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1893. allocator
  1894. * Some preventive changes to i386 spillinh code
  1895. Revision 1.132 2003/08/03 14:09:50 daniel
  1896. * Fixed a register allocator bug
  1897. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  1898. statements: changes in location_force. These moves are now no longer
  1899. constrained so they are optimized away.
  1900. Revision 1.131 2003/07/23 11:04:15 jonas
  1901. * split en_exit_code into a part that may allocate a register and a part
  1902. that doesn't, so the former can be done before the register colouring
  1903. has been performed
  1904. Revision 1.130 2003/07/06 17:58:22 peter
  1905. * framepointer fixes for sparc
  1906. * parent framepointer code more generic
  1907. Revision 1.129 2003/07/06 15:31:20 daniel
  1908. * Fixed register allocator. *Lots* of fixes.
  1909. Revision 1.128 2003/07/02 22:18:04 peter
  1910. * paraloc splitted in callerparaloc,calleeparaloc
  1911. * sparc calling convention updates
  1912. Revision 1.127 2003/06/17 18:13:51 jonas
  1913. * fixed -dnewra compilation problems
  1914. Revision 1.126 2003/06/17 16:32:44 peter
  1915. * alloc register for function result
  1916. Revision 1.125 2003/06/13 21:19:30 peter
  1917. * current_procdef removed, use current_procinfo.procdef instead
  1918. Revision 1.124 2003/06/09 12:23:30 peter
  1919. * init/final of procedure data splitted from genentrycode
  1920. * use asmnode getposition to insert final at the correct position
  1921. als for the implicit try...finally
  1922. Revision 1.123 2003/06/07 18:57:04 jonas
  1923. + added freeintparaloc
  1924. * ppc get/freeintparaloc now check whether the parameter regs are
  1925. properly allocated/deallocated (and get an extra list para)
  1926. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  1927. * fixed lot of missing pi_do_call's
  1928. Revision 1.122 2003/06/06 14:43:02 peter
  1929. * g_copyopenarrayvalue gets length reference
  1930. * don't copy open arrays for cdecl
  1931. Revision 1.121 2003/06/03 21:11:09 peter
  1932. * cg.a_load_* get a from and to size specifier
  1933. * makeregsize only accepts newregister
  1934. * i386 uses generic tcgnotnode,tcgunaryminus
  1935. Revision 1.120 2003/06/03 15:49:49 jonas
  1936. * fixed ref/loc problems
  1937. Revision 1.119 2003/06/03 15:06:37 daniel
  1938. * fixed conflict marks
  1939. Revision 1.118 2003/06/03 13:01:59 daniel
  1940. * Register allocator finished
  1941. Revision 1.117 2003/06/02 21:42:05 jonas
  1942. * function results can now also be regvars
  1943. - removed tprocinfo.return_offset, never use it again since it's invalid
  1944. if the result is a regvar
  1945. Revision 1.116 2003/06/01 21:38:06 peter
  1946. * getregisterfpu size parameter added
  1947. * op_const_reg size parameter added
  1948. * sparc updates
  1949. Revision 1.115 2003/05/31 20:28:17 jonas
  1950. * changed copyvalueparas so it also supports register parameters
  1951. (except for copy_value_openarray, but that one is seriously broken
  1952. anyway, since it expects that the high parameter will always be in
  1953. memory right after the pointer to the array, while it could just as
  1954. will be in a register)
  1955. Revision 1.114 2003/05/31 15:05:28 peter
  1956. * FUNCTION_RESULT64_LOW/HIGH_REG added for int64 results
  1957. Revision 1.113 2003/05/31 00:48:15 jonas
  1958. * fixed my previous commit
  1959. Revision 1.112 2003/05/30 23:57:08 peter
  1960. * more sparc cleanup
  1961. * accumulator removed, splitted in function_return_reg (called) and
  1962. function_result_reg (caller)
  1963. Revision 1.111 2003/05/30 23:49:18 jonas
  1964. * a_load_loc_reg now has an extra size parameter for the destination
  1965. register (properly fixes what I worked around in revision 1.106 of
  1966. ncgutil.pas)
  1967. Revision 1.110 2003/05/30 18:52:10 jonas
  1968. * fixed bug with intregvars
  1969. * locapara.loc can also be LOC_CFPUREGISTER -> also fixed
  1970. rcgppc.a_param_ref, which previously got bogus size values
  1971. Revision 1.109 2003/05/27 21:19:08 jonas
  1972. * fixed ppc cycle
  1973. Revision 1.108 2003/05/27 14:28:14 jonas
  1974. * patch from Peter for nested procedures
  1975. Revision 1.107 2003/05/26 21:17:17 peter
  1976. * procinlinenode removed
  1977. * aktexit2label removed, fast exit removed
  1978. + tcallnode.inlined_pass_2 added
  1979. Revision 1.106 2003/05/24 11:59:42 jonas
  1980. * fixed integer typeconversion problems
  1981. Revision 1.105 2003/05/23 14:27:35 peter
  1982. * remove some unit dependencies
  1983. * current_procinfo changes to store more info
  1984. Revision 1.104 2003/05/15 18:58:53 peter
  1985. * removed selfpointer_offset, vmtpointer_offset
  1986. * tvarsym.adjusted_address
  1987. * address in localsymtable is now in the real direction
  1988. * removed some obsolete globals
  1989. Revision 1.103 2003/05/14 19:37:25 jonas
  1990. * patch from Peter for int64 function results
  1991. Revision 1.102 2003/05/13 19:14:41 peter
  1992. * failn removed
  1993. * inherited result code check moven to pexpr
  1994. Revision 1.101 2003/05/13 15:16:13 peter
  1995. * removed ret_in_acc, it's the reverse of ret_in_param
  1996. * fixed ret_in_param for win32 cdecl array
  1997. Revision 1.100 2003/05/12 08:08:27 jonas
  1998. * fixed several initialization and finalization related bugs (missing
  1999. tg.direction's, wrong paralocation for decreasing refcount of
  2000. everything but ansistrings)
  2001. Revision 1.99 2003/05/11 21:37:03 peter
  2002. * moved implicit exception frame from ncgutil to psub
  2003. * constructor/destructor helpers moved from cobj/ncgutil to psub
  2004. Revision 1.98 2003/05/11 14:45:12 peter
  2005. * tloadnode does not support objectsymtable,withsymtable anymore
  2006. * withnode cleanup
  2007. * direct with rewritten to use temprefnode
  2008. Revision 1.97 2003/05/10 13:20:23 jonas
  2009. * moved storing of register parameters to memory even earlier in the
  2010. entry code to fix problems with constructors
  2011. Revision 1.96 2003/05/09 17:47:02 peter
  2012. * self moved to hidden parameter
  2013. * removed hdisposen,hnewn,selfn
  2014. Revision 1.95 2003/04/29 07:28:52 michael
  2015. + Patch from peter to fix wrong pushing of ansistring function results in open array
  2016. Revision 1.94 2003/04/28 21:17:38 peter
  2017. * do not finalize function results
  2018. Revision 1.93 2003/04/27 16:30:34 jonas
  2019. * store register para's to memory before copying the valuepara's, because
  2020. that one requires them to be there already (and it calls subroutines ->
  2021. could overwrite those registers)
  2022. Revision 1.92 2003/04/27 11:21:33 peter
  2023. * aktprocdef renamed to current_procinfo.procdef
  2024. * procinfo renamed to current_procinfo
  2025. * procinfo will now be stored in current_module so it can be
  2026. cleaned up properly
  2027. * gen_main_procsym changed to create_main_proc and release_main_proc
  2028. to also generate a tprocinfo structure
  2029. * fixed unit implicit initfinal
  2030. Revision 1.91 2003/04/27 07:29:50 peter
  2031. * current_procinfo.procdef cleanup, current_procinfo.procdef is now always nil when parsing
  2032. a new procdef declaration
  2033. * aktprocsym removed
  2034. * lexlevel removed, use symtable.symtablelevel instead
  2035. * implicit init/final code uses the normal genentry/genexit
  2036. * funcret state checking updated for new funcret handling
  2037. Revision 1.90 2003/04/26 17:21:08 florian
  2038. * fixed passing of fpu values by fpu register
  2039. Revision 1.89 2003/04/25 20:59:33 peter
  2040. * removed funcretn,funcretsym, function result is now in varsym
  2041. and aliases for result and function name are added using absolutesym
  2042. * vs_hidden parameter for funcret passed in parameter
  2043. * vs_hidden fixes
  2044. * writenode changed to printnode and released from extdebug
  2045. * -vp option added to generate a tree.log with the nodetree
  2046. * nicer printnode for statements, callnode
  2047. Revision 1.88 2003/04/23 12:35:34 florian
  2048. * fixed several issues with powerpc
  2049. + applied a patch from Jonas for nested function calls (PowerPC only)
  2050. * ...
  2051. Revision 1.87 2003/04/22 14:33:38 peter
  2052. * removed some notes/hints
  2053. Revision 1.86 2003/04/22 13:47:08 peter
  2054. * fixed C style array of const
  2055. * fixed C array passing
  2056. * fixed left to right with high parameters
  2057. Revision 1.85 2003/04/22 10:09:35 daniel
  2058. + Implemented the actual register allocator
  2059. + Scratch registers unavailable when new register allocator used
  2060. + maybe_save/maybe_restore unavailable when new register allocator used
  2061. Revision 1.84 2003/04/16 09:26:55 jonas
  2062. * assembler procedures now again get a stackframe if they have local
  2063. variables. No space is reserved for a function result however.
  2064. Also, the register parameters aren't automatically saved on the stack
  2065. anymore in assembler procedures.
  2066. Revision 1.83 2003/04/06 21:11:23 olle
  2067. * changed newasmsymbol to newasmsymboldata for data symbols
  2068. Revision 1.82 2003/03/30 20:59:07 peter
  2069. * fix classmethod from classmethod call
  2070. * move BeforeDestruction/AfterConstruction calls to
  2071. genentrycode/genexitcode instead of generating them on the fly
  2072. after a call to a constructor
  2073. Revision 1.81 2003/03/28 19:16:56 peter
  2074. * generic constructor working for i386
  2075. * remove fixed self register
  2076. * esi added as address register for i386
  2077. Revision 1.80 2003/03/17 15:52:20 peter
  2078. * fix range error
  2079. Revision 1.79 2003/03/11 21:46:24 jonas
  2080. * lots of new regallocator fixes, both in generic and ppc-specific code
  2081. (ppc compiler still can't compile the linux system unit though)
  2082. Revision 1.78 2003/02/26 21:15:43 daniel
  2083. * Fixed the optimizer
  2084. Revision 1.77 2003/02/19 22:00:14 daniel
  2085. * Code generator converted to new register notation
  2086. - Horribily outdated todo.txt removed
  2087. Revision 1.76 2003/02/15 22:17:38 carl
  2088. * bugfix of FPU emulation code
  2089. Revision 1.75 2003/01/09 22:00:53 florian
  2090. * fixed some PowerPC issues
  2091. Revision 1.74 2003/01/09 20:41:10 florian
  2092. * fixed broken PowerPC compiler
  2093. Revision 1.73 2003/01/08 18:43:56 daniel
  2094. * Tregister changed into a record
  2095. Revision 1.72 2002/12/29 23:51:43 florian
  2096. * web bug 2214 fixed: ie 10 in const array constructors
  2097. Revision 1.71 2002/12/24 15:56:50 peter
  2098. * stackpointer_alloc added for adjusting ESP. Win32 needs
  2099. this for the pageprotection
  2100. Revision 1.70 2002/12/05 14:39:21 florian
  2101. * added missing then, Carl did you really a make fullcycle :) ?
  2102. Revision 1.69 2002/12/03 22:13:39 carl
  2103. * bugfix of problems with profile code which clobbers some registers
  2104. Revision 1.68 2002/12/01 22:06:59 carl
  2105. * warning of portabilitiy problems with parasize / localsize
  2106. Revision 1.67 2002/11/30 18:44:57 carl
  2107. + profiling support for Win32
  2108. Revision 1.66 2002/11/30 14:39:15 carl
  2109. * try to fix profiling for win32
  2110. Revision 1.65 2002/11/28 23:28:14 florian
  2111. * push_value_para didn't release floatdef locations, fixes tw2045
  2112. Revision 1.64 2002/11/27 02:33:19 peter
  2113. * copy_value_on_stack method added for cdecl record passing
  2114. Revision 1.63 2002/11/25 17:43:18 peter
  2115. * splitted defbase in defutil,symutil,defcmp
  2116. * merged isconvertable and is_equal into compare_defs(_ext)
  2117. * made operator search faster by walking the list only once
  2118. Revision 1.62 2002/11/18 17:31:55 peter
  2119. * pass proccalloption to ret_in_xxx and push_xxx functions
  2120. Revision 1.61 2002/11/17 17:49:08 mazen
  2121. + return_result_reg and FUNCTION_RESULT_REG are now used, in all plateforms, to pass functions result between called function and its caller. See the explanation of each one
  2122. Revision 1.60 2002/11/17 16:31:56 carl
  2123. * memory optimization (3-4%) : cleanup of tai fields,
  2124. cleanup of tdef and tsym fields.
  2125. * make it work for m68k
  2126. Revision 1.59 2002/11/15 01:58:51 peter
  2127. * merged changes from 1.0.7 up to 04-11
  2128. - -V option for generating bug report tracing
  2129. - more tracing for option parsing
  2130. - errors for cdecl and high()
  2131. - win32 import stabs
  2132. - win32 records<=8 are returned in eax:edx (turned off by default)
  2133. - heaptrc update
  2134. - more info for temp management in .s file with EXTDEBUG
  2135. Revision 1.58 2002/11/10 19:07:45 mazen
  2136. * SPARC calling mechanism almost OK (as in GCC./mppcsparc )
  2137. Revision 1.57 2002/11/03 20:22:40 mazen
  2138. * parameter handling updated
  2139. Revision 1.56 2002/10/16 19:01:43 peter
  2140. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  2141. implicit exception frames for procedures with initialized variables
  2142. and for constructors. The default is on for compatibility
  2143. Revision 1.55 2002/10/14 19:42:33 peter
  2144. * only use init tables for threadvars
  2145. Revision 1.54 2002/10/06 19:41:30 peter
  2146. * Add finalization of typed consts
  2147. * Finalization of globals in the main program
  2148. Revision 1.53 2002/10/05 15:18:42 carl
  2149. * fix heap leaks
  2150. Revision 1.52 2002/09/30 07:00:46 florian
  2151. * fixes to common code to get the alpha compiler compiled applied
  2152. Revision 1.51 2002/09/22 14:02:35 carl
  2153. * stack checking cannot be called before system unit is initialized
  2154. * MC68020 define
  2155. Revision 1.50 2002/09/17 18:54:03 jonas
  2156. * a_load_reg_reg() now has two size parameters: source and dest. This
  2157. allows some optimizations on architectures that don't encode the
  2158. register size in the register name.
  2159. Revision 1.49 2002/09/10 21:48:30 florian
  2160. * improved handling of procedures with register calling conventions
  2161. Revision 1.48 2002/09/07 15:25:03 peter
  2162. * old logs removed and tabs fixed
  2163. Revision 1.47 2002/09/02 18:44:48 peter
  2164. * fixed (not) pushing of empty parameters
  2165. * fixed implicit initialization/finalization generation
  2166. * fixed/optimized local copy of value arguments init/final
  2167. Revision 1.46 2002/09/01 19:27:34 peter
  2168. * use index register when available for generating a reference with
  2169. only a signle register. Using the base register could possibly
  2170. destroy the framepointer
  2171. Revision 1.45 2002/09/01 18:50:20 peter
  2172. * fixed maybe_save that did not support a reference with only
  2173. a index register. It now also updates the location with the new
  2174. base register only
  2175. Revision 1.44 2002/09/01 14:42:41 peter
  2176. * removevaluepara added to fix the stackpointer so restoring of
  2177. saved registers works
  2178. Revision 1.43 2002/08/25 19:25:18 peter
  2179. * sym.insert_in_data removed
  2180. * symtable.insertvardata/insertconstdata added
  2181. * removed insert_in_data call from symtable.insert, it needs to be
  2182. called separatly. This allows to deref the address calculation
  2183. * procedures now calculate the parast addresses after the procedure
  2184. directives are parsed. This fixes the cdecl parast problem
  2185. * push_addr_param has an extra argument that specifies if cdecl is used
  2186. or not
  2187. Revision 1.42 2002/08/24 18:38:26 peter
  2188. * really use tt_noreuse for exception frame buffers
  2189. Revision 1.41 2002/08/23 16:14:49 peter
  2190. * tempgen cleanup
  2191. * tt_noreuse temp type added that will be used in genentrycode
  2192. Revision 1.40 2002/08/18 10:42:37 florian
  2193. * remaining assembler writer bugs fixed, the errors in the
  2194. system unit are inline assembler problems
  2195. Revision 1.39 2002/08/17 09:23:36 florian
  2196. * first part of procinfo rewrite
  2197. Revision 1.38 2002/08/16 14:24:57 carl
  2198. * issameref() to test if two references are the same (then emit no opcodes)
  2199. + ret_in_reg to replace ret_in_acc
  2200. (fix some register allocation bugs at the same time)
  2201. + save_std_register now has an extra parameter which is the
  2202. usedinproc registers
  2203. Revision 1.37 2002/08/15 15:15:55 carl
  2204. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  2205. * more generic nodes for maths
  2206. * several fixes for better m68k support
  2207. Revision 1.36 2002/08/14 19:25:09 carl
  2208. * fix Florian's last commit for m68k compilation
  2209. Revision 1.35 2002/08/13 21:40:56 florian
  2210. * more fixes for ppc calling conventions
  2211. Revision 1.34 2002/08/12 15:08:39 carl
  2212. + stab register indexes for powerpc (moved from gdb to cpubase)
  2213. + tprocessor enumeration moved to cpuinfo
  2214. + linker in target_info is now a class
  2215. * many many updates for m68k (will soon start to compile)
  2216. - removed some ifdef or correct them for correct cpu
  2217. Revision 1.33 2002/08/11 14:32:27 peter
  2218. * renamed current_library to objectlibrary
  2219. Revision 1.32 2002/08/11 13:24:12 peter
  2220. * saving of asmsymbols in ppu supported
  2221. * asmsymbollist global is removed and moved into a new class
  2222. tasmlibrarydata that will hold the info of a .a file which
  2223. corresponds with a single module. Added librarydata to tmodule
  2224. to keep the library info stored for the module. In the future the
  2225. objectfiles will also be stored to the tasmlibrarydata class
  2226. * all getlabel/newasmsymbol and friends are moved to the new class
  2227. Revision 1.31 2002/08/09 19:16:57 carl
  2228. * stack allocation is now done separately (at the end) of genentrycode
  2229. so temps. can be allocated before.
  2230. * fix generic exception handling
  2231. Revision 1.30 2002/08/06 20:55:21 florian
  2232. * first part of ppc calling conventions fix
  2233. Revision 1.29 2002/08/04 19:09:22 carl
  2234. + added generic exception support (still does not work!)
  2235. + more documentation
  2236. Revision 1.28 2002/07/29 21:23:42 florian
  2237. * more fixes for the ppc
  2238. + wrappers for the tcnvnode.first_* stuff introduced
  2239. }