ncgutil.pas 98 KB

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