ncgutil.pas 101 KB

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