ncgutil.pas 100 KB

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