ncgutil.pas 98 KB

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