ncgutil.pas 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  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,cpupara,
  25. aasmbase,aasmtai,aasmcpu,
  26. cginfo,symbase,symdef,symtype,
  27. {$ifndef cpu64bit}
  28. cg64f32,
  29. {$endif cpu64bit}
  30. rgobj;
  31. type
  32. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  33. tmaybesave = record
  34. saved : boolean;
  35. ref : treference;
  36. end;
  37. procedure firstcomplex(p : tbinarynode);
  38. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  39. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: tregisterset);
  40. procedure location_force_reg(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  41. procedure location_force_mem(list: TAAsmoutput;var l:tlocation);
  42. procedure maybe_save(list:taasmoutput;needed:integer;var l:tlocation;var s:tmaybesave);
  43. procedure maybe_restore(list:taasmoutput;var l:tlocation;const s:tmaybesave);
  44. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  45. procedure push_value_para(p:tnode;calloption:tproccalloption;
  46. para_offset:longint;alignment : longint;
  47. const locpara : tparalocation);
  48. procedure genentrycode(list : TAAsmoutput;
  49. make_global:boolean;
  50. stackframe:longint;
  51. var parasize:longint;var nostackframe:boolean;
  52. inlined : boolean);
  53. procedure genexitcode(list : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  54. procedure genimplicitunitinit(list : TAAsmoutput);
  55. procedure genimplicitunitfinal(list : TAAsmoutput);
  56. {#
  57. Allocate the buffers for exception management and setjmp environment.
  58. Return a pointer to these buffers, send them to the utility routine
  59. so they are registered, and then call setjmp.
  60. Then compare the result of setjmp with 0, and if not equal
  61. to zero, then jump to exceptlabel.
  62. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  63. It is to note that this routine may be called *after* the stackframe of a
  64. routine has been called, therefore on machines where the stack cannot
  65. be modified, all temps should be allocated on the heap instead of the
  66. stack.
  67. }
  68. procedure new_exception(list : taasmoutput;const jmpbuf,envbuf, href : treference;
  69. a : aword; exceptlabel : tasmlabel);
  70. procedure free_exception(list : taasmoutput;const jmpbuf, envbuf, href : treference;
  71. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  72. implementation
  73. uses
  74. {$ifdef Delphi}
  75. Sysutils,
  76. {$else}
  77. strings,
  78. {$endif}
  79. cutils,cclasses,
  80. globals,systems,verbose,
  81. symconst,symsym,symtable,defutil,
  82. paramgr,fmodule,
  83. cgbase,regvars,
  84. {$ifdef GDB}
  85. gdb,
  86. {$endif GDB}
  87. ncon,
  88. tgobj,cgobj,cgcpu;
  89. {*****************************************************************************
  90. Misc Helpers
  91. *****************************************************************************}
  92. { DO NOT RELY on the fact that the tnode is not yet swaped
  93. because of inlining code PM }
  94. procedure firstcomplex(p : tbinarynode);
  95. var
  96. hp : tnode;
  97. begin
  98. { always calculate boolean AND and OR from left to right }
  99. if (p.nodetype in [orn,andn]) and
  100. is_boolean(p.left.resulttype.def) then
  101. begin
  102. if nf_swaped in p.flags then
  103. internalerror(234234);
  104. end
  105. else
  106. if (
  107. (p.location.loc=LOC_FPUREGISTER) and
  108. (p.right.registersfpu > p.left.registersfpu)
  109. ) or
  110. (
  111. (
  112. (
  113. ((p.left.registersfpu = 0) and (p.right.registersfpu = 0)) or
  114. (p.location.loc<>LOC_FPUREGISTER)
  115. ) and
  116. (p.left.registers32<p.right.registers32)
  117. ) and
  118. { the following check is appropriate, because all }
  119. { 4 registers are rarely used and it is thereby }
  120. { achieved that the extra code is being dropped }
  121. { by exchanging not commutative operators }
  122. (p.right.registers32<=c_countusableregsint)
  123. ) then
  124. begin
  125. hp:=p.left;
  126. p.left:=p.right;
  127. p.right:=hp;
  128. if nf_swaped in p.flags then
  129. exclude(p.flags,nf_swaped)
  130. else
  131. include(p.flags,nf_swaped);
  132. end;
  133. end;
  134. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  135. {
  136. produces jumps to true respectively false labels using boolean expressions
  137. depending on whether the loading of regvars is currently being
  138. synchronized manually (such as in an if-node) or automatically (most of
  139. the other cases where this procedure is called), loadregvars can be
  140. "lr_load_regvars" or "lr_dont_load_regvars"
  141. }
  142. var
  143. opsize : tcgsize;
  144. storepos : tfileposinfo;
  145. begin
  146. if nf_error in p.flags then
  147. exit;
  148. storepos:=aktfilepos;
  149. aktfilepos:=p.fileinfo;
  150. if is_boolean(p.resulttype.def) then
  151. begin
  152. if loadregvars = lr_load_regvars then
  153. load_all_regvars(list);
  154. if is_constboolnode(p) then
  155. begin
  156. if tordconstnode(p).value<>0 then
  157. cg.a_jmp_always(list,truelabel)
  158. else
  159. cg.a_jmp_always(list,falselabel)
  160. end
  161. else
  162. begin
  163. opsize:=def_cgsize(p.resulttype.def);
  164. case p.location.loc of
  165. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  166. begin
  167. if (p.location.loc = LOC_CREGISTER) then
  168. load_regvar_reg(list,p.location.register);
  169. cg.a_cmp_const_loc_label(list,opsize,OC_NE,
  170. 0,p.location,truelabel);
  171. { !!! should happen right after cmp (JM) }
  172. location_release(list,p.location);
  173. cg.a_jmp_always(list,falselabel);
  174. end;
  175. {$ifdef cpuflags}
  176. LOC_FLAGS :
  177. begin
  178. cg.a_jmp_flags(list,p.location.resflags,
  179. truelabel);
  180. cg.a_jmp_always(list,falselabel);
  181. end;
  182. {$endif cpuflags}
  183. end;
  184. end;
  185. end
  186. else
  187. internalerror(200112305);
  188. aktfilepos:=storepos;
  189. end;
  190. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: tregisterset);
  191. begin
  192. case t.loc of
  193. LOC_REGISTER:
  194. begin
  195. { can't be a regvar, since it would be LOC_CREGISTER then }
  196. if t.register.enum>lastreg then
  197. internalerror(200201081);
  198. if t.registerhigh.enum>lastreg then
  199. internalerror(200201081);
  200. exclude(regs,t.register.enum);
  201. if t.registerhigh.enum <> R_NO then
  202. exclude(regs,t.registerhigh.enum);
  203. end;
  204. LOC_CREFERENCE,LOC_REFERENCE:
  205. begin
  206. if t.reference.base.enum>lastreg then
  207. internalerror(200201081);
  208. if t.reference.index.enum>lastreg then
  209. internalerror(200201081);
  210. if not(cs_regalloc in aktglobalswitches) or
  211. (t.reference.base.enum in rg.usableregsint) then
  212. exclude(regs,t.reference.base.enum);
  213. if not(cs_regalloc in aktglobalswitches) or
  214. (t.reference.index.enum in rg.usableregsint) then
  215. exclude(regs,t.reference.index.enum);
  216. end;
  217. end;
  218. end;
  219. {*****************************************************************************
  220. EXCEPTION MANAGEMENT
  221. *****************************************************************************}
  222. procedure new_exception(list : taasmoutput;const jmpbuf,envbuf, href : treference;
  223. a : aword; exceptlabel : tasmlabel);
  224. var r:Tregister;
  225. begin
  226. cg.a_paramaddr_ref(list,envbuf,paramanager.getintparaloc(3));
  227. cg.a_paramaddr_ref(list,jmpbuf,paramanager.getintparaloc(2));
  228. { push type of exceptionframe }
  229. cg.a_param_const(list,OS_S32,1,paramanager.getintparaloc(1));
  230. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR');
  231. r.enum:=accumulator;
  232. cg.a_param_reg(list,OS_ADDR,r,paramanager.getintparaloc(1));
  233. cg.a_call_name(list,'FPC_SETJMP');
  234. cg.g_exception_reason_save(list, href);
  235. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,r,exceptlabel);
  236. end;
  237. procedure free_exception(list : taasmoutput;const jmpbuf, envbuf, href : treference;
  238. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  239. var r:Tregister;
  240. begin
  241. cg.a_call_name(list,'FPC_POPADDRSTACK');
  242. if not onlyfree then
  243. begin
  244. cg.g_exception_reason_load(list, href);
  245. r.enum:=accumulator;
  246. cg.a_cmp_const_reg_label(list,OS_S32,OC_EQ,a,r,endexceptlabel);
  247. end;
  248. end;
  249. {*****************************************************************************
  250. TLocation
  251. *****************************************************************************}
  252. {$ifndef cpu64bit}
  253. { 32-bit version }
  254. procedure location_force(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  255. var
  256. hregister,
  257. hregisterhi : tregister;
  258. hreg64 : tregister64;
  259. hl : tasmlabel;
  260. begin
  261. { handle transformations to 64bit separate }
  262. if dst_size in [OS_64,OS_S64] then
  263. begin
  264. if not (l.size in [OS_64,OS_S64]) then
  265. begin
  266. { load a smaller size to OS_64 }
  267. if l.loc=LOC_REGISTER then
  268. begin
  269. hregister:=rg.makeregsize(l.registerlow,OS_INT);
  270. cg.a_load_reg_reg(list,l.size,OS_32,l.registerlow,hregister);
  271. end
  272. else
  273. hregister:=rg.getregisterint(list);
  274. { load value in low register }
  275. case l.loc of
  276. LOC_FLAGS :
  277. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  278. LOC_JUMP :
  279. begin
  280. cg.a_label(list,truelabel);
  281. cg.a_load_const_reg(list,OS_INT,1,hregister);
  282. objectlibrary.getlabel(hl);
  283. cg.a_jmp_always(list,hl);
  284. cg.a_label(list,falselabel);
  285. cg.a_load_const_reg(list,OS_INT,0,hregister);
  286. cg.a_label(list,hl);
  287. end;
  288. else
  289. cg.a_load_loc_reg(list,l,hregister);
  290. end;
  291. { reset hi part, take care of the signed bit of the current value }
  292. hregisterhi:=rg.getregisterint(list);
  293. if (dst_size=OS_S64) and
  294. (l.size in [OS_S8,OS_S16,OS_S32]) then
  295. begin
  296. if l.loc=LOC_CONSTANT then
  297. begin
  298. if (longint(l.value)<0) then
  299. cg.a_load_const_reg(list,OS_32,$ffffffff,hregisterhi)
  300. else
  301. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  302. end
  303. else
  304. begin
  305. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  306. hregisterhi);
  307. end;
  308. end
  309. else
  310. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  311. location_reset(l,LOC_REGISTER,dst_size);
  312. l.registerlow:=hregister;
  313. l.registerhigh:=hregisterhi;
  314. end
  315. else
  316. begin
  317. { 64bit to 64bit }
  318. if (l.loc=LOC_REGISTER) or
  319. ((l.loc=LOC_CREGISTER) and maybeconst) then
  320. begin
  321. hregister:=l.registerlow;
  322. hregisterhi:=l.registerhigh;
  323. end
  324. else
  325. begin
  326. hregister:=rg.getregisterint(list);
  327. hregisterhi:=rg.getregisterint(list);
  328. end;
  329. hreg64.reglo:=hregister;
  330. hreg64.reghi:=hregisterhi;
  331. { load value in new register }
  332. cg64.a_load64_loc_reg(list,l,hreg64);
  333. location_reset(l,LOC_REGISTER,dst_size);
  334. l.registerlow:=hregister;
  335. l.registerhigh:=hregisterhi;
  336. end;
  337. end
  338. else
  339. begin
  340. { transformations to 32bit or smaller }
  341. if l.loc=LOC_REGISTER then
  342. begin
  343. { if the previous was 64bit release the high register }
  344. if l.size in [OS_64,OS_S64] then
  345. begin
  346. rg.ungetregisterint(list,l.registerhigh);
  347. l.registerhigh.enum:=R_NO;
  348. end;
  349. hregister:=l.register;
  350. end
  351. else
  352. begin
  353. { get new register }
  354. if (l.loc=LOC_CREGISTER) and
  355. maybeconst and
  356. (TCGSize2Size[dst_size]=TCGSize2Size[l.size]) then
  357. hregister:=l.register
  358. else
  359. hregister:=rg.getregisterint(list);
  360. end;
  361. hregister:=rg.makeregsize(hregister,dst_size);
  362. { load value in new register }
  363. case l.loc of
  364. LOC_FLAGS :
  365. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  366. LOC_JUMP :
  367. begin
  368. cg.a_label(list,truelabel);
  369. cg.a_load_const_reg(list,dst_size,1,hregister);
  370. objectlibrary.getlabel(hl);
  371. cg.a_jmp_always(list,hl);
  372. cg.a_label(list,falselabel);
  373. cg.a_load_const_reg(list,dst_size,0,hregister);
  374. cg.a_label(list,hl);
  375. end;
  376. else
  377. begin
  378. { load_loc_reg can only handle size >= l.size, when the
  379. new size is smaller then we need to adjust the size
  380. of the orignal and maybe recalculate l.register for i386 }
  381. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  382. begin
  383. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  384. l.register:=rg.makeregsize(l.register,dst_size);
  385. { for big endian systems, the reference's offset must }
  386. { be increased in this case, since they have the }
  387. { MSB first in memory and e.g. byte(word_var) should }
  388. { return the second byte in this case (JM) }
  389. if (target_info.endian = ENDIAN_BIG) and
  390. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  391. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  392. l.size:=dst_size;
  393. end;
  394. cg.a_load_loc_reg(list,l,hregister);
  395. end;
  396. end;
  397. location_reset(l,LOC_REGISTER,dst_size);
  398. l.register:=hregister;
  399. end;
  400. end;
  401. {$else cpu64bit}
  402. { 64-bit version }
  403. procedure location_force(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  404. var
  405. hregister : tregister;
  406. hl : tasmlabel;
  407. begin
  408. { handle transformations to 64bit separate }
  409. if dst_size in [OS_64,OS_S64] then
  410. begin
  411. { load a smaller size to OS_64 }
  412. if l.loc=LOC_REGISTER then
  413. hregister:=rg.makeregsize(l.register,OS_INT)
  414. else
  415. hregister:=rg.getregisterint(list);
  416. { load value in low register }
  417. case l.loc of
  418. {$ifdef cpuflags}
  419. LOC_FLAGS :
  420. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  421. {$endif cpuflags}
  422. LOC_JUMP :
  423. begin
  424. cg.a_label(list,truelabel);
  425. cg.a_load_const_reg(list,OS_INT,1,hregister);
  426. objectlibrary.getlabel(hl);
  427. cg.a_jmp_always(list,hl);
  428. cg.a_label(list,falselabel);
  429. cg.a_load_const_reg(list,OS_INT,0,hregister);
  430. cg.a_label(list,hl);
  431. end;
  432. else
  433. cg.a_load_loc_reg(list,l,hregister);
  434. end;
  435. location_reset(l,LOC_REGISTER,dst_size);
  436. l.register:=hregister;
  437. end
  438. else
  439. begin
  440. { transformations to 32bit or smaller }
  441. if l.loc=LOC_REGISTER then
  442. begin
  443. hregister:=l.register;
  444. end
  445. else
  446. begin
  447. { get new register }
  448. if (l.loc=LOC_CREGISTER) and
  449. maybeconst and
  450. (TCGSize2Size[dst_size]=TCGSize2Size[l.size]) then
  451. hregister:=l.register
  452. else
  453. hregister:=rg.getregisterint(list);
  454. end;
  455. hregister:=rg.makeregsize(hregister,dst_size);
  456. { load value in new register }
  457. case l.loc of
  458. {$ifdef cpuflags}
  459. LOC_FLAGS :
  460. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  461. {$endif cpuflags}
  462. LOC_JUMP :
  463. begin
  464. cg.a_label(list,truelabel);
  465. cg.a_load_const_reg(list,dst_size,1,hregister);
  466. objectlibrary.getlabel(hl);
  467. cg.a_jmp_always(list,hl);
  468. cg.a_label(list,falselabel);
  469. cg.a_load_const_reg(list,dst_size,0,hregister);
  470. cg.a_label(list,hl);
  471. end;
  472. else
  473. begin
  474. { load_loc_reg can only handle size >= l.size, when the
  475. new size is smaller then we need to adjust the size
  476. of the orignal and maybe recalculate l.register for i386 }
  477. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  478. begin
  479. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  480. l.register:=rg.makeregsize(l.register,dst_size);
  481. { for big endian systems, the reference's offset must }
  482. { be increased in this case, since they have the }
  483. { MSB first in memory and e.g. byte(word_var) should }
  484. { return the second byte in this case (JM) }
  485. if (target_info.endian = ENDIAN_BIG) and
  486. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  487. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  488. l.size:=dst_size;
  489. end;
  490. cg.a_load_loc_reg(list,l,hregister);
  491. end;
  492. end;
  493. location_reset(l,LOC_REGISTER,dst_size);
  494. l.register:=hregister;
  495. end;
  496. end;
  497. {$endif cpu64bit}
  498. procedure location_force_reg(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  499. begin
  500. { release previous location before demanding a new register }
  501. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  502. begin
  503. location_freetemp(list,l);
  504. location_release(list,l);
  505. end;
  506. location_force(list, l, dst_size, maybeconst)
  507. end;
  508. procedure location_force_mem(list: TAAsmoutput;var l:tlocation);
  509. var
  510. r : treference;
  511. begin
  512. case l.loc of
  513. LOC_FPUREGISTER,
  514. LOC_CFPUREGISTER :
  515. begin
  516. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  517. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  518. location_release(exprasmlist,l);
  519. location_reset(l,LOC_REFERENCE,l.size);
  520. l.reference:=r;
  521. end;
  522. LOC_CONSTANT,
  523. LOC_REGISTER,
  524. LOC_CREGISTER :
  525. begin
  526. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  527. if l.size in [OS_64,OS_S64] then
  528. cg64.a_load64_loc_ref(list,l,r)
  529. else
  530. cg.a_load_loc_ref(list,l,r);
  531. location_release(exprasmlist,l);
  532. location_reset(l,LOC_REFERENCE,l.size);
  533. l.reference:=r;
  534. end;
  535. LOC_CREFERENCE,
  536. LOC_REFERENCE : ;
  537. else
  538. internalerror(200203219);
  539. end;
  540. end;
  541. {*****************************************************************************
  542. Maybe_Save
  543. *****************************************************************************}
  544. procedure maybe_save(list:taasmoutput;needed:integer;var l:tlocation;var s:tmaybesave);
  545. begin
  546. s.saved:=false;
  547. if l.loc=LOC_CREGISTER then
  548. begin
  549. s.saved:=true;
  550. exit;
  551. end;
  552. if needed>rg.countunusedregsint then
  553. begin
  554. case l.loc of
  555. LOC_REGISTER :
  556. begin
  557. {$ifndef cpu64bit}
  558. if l.size in [OS_64,OS_S64] then
  559. begin
  560. tg.GetTemp(exprasmlist,8,tt_normal,s.ref);
  561. cg64.a_load64_reg_ref(exprasmlist,joinreg64(l.registerlow,l.registerhigh),s.ref);
  562. end
  563. else
  564. {$endif cpu64bit}
  565. begin
  566. tg.GetTemp(exprasmlist,TCGSize2Size[l.size],tt_normal,s.ref);
  567. cg.a_load_reg_ref(exprasmlist,l.size,l.register,s.ref);
  568. end;
  569. location_release(exprasmlist,l);
  570. s.saved:=true;
  571. end;
  572. LOC_REFERENCE,
  573. LOC_CREFERENCE :
  574. begin
  575. if l.reference.base.enum>lastreg then
  576. internalerror(200101081);
  577. if ((l.reference.base.enum<>R_NO) or
  578. (l.reference.index.enum<>R_NO)) then
  579. begin
  580. { load address into a single base register }
  581. if l.reference.index.enum<>R_NO then
  582. begin
  583. cg.a_loadaddr_ref_reg(list,l.reference,l.reference.index);
  584. rg.ungetregister(list,l.reference.base);
  585. reference_reset_base(l.reference,l.reference.index,0);
  586. end
  587. else
  588. begin
  589. cg.a_loadaddr_ref_reg(list,l.reference,l.reference.base);
  590. rg.ungetregister(list,l.reference.index);
  591. reference_reset_base(l.reference,l.reference.base,0);
  592. end;
  593. { save base register }
  594. tg.GetTemp(exprasmlist,TCGSize2Size[OS_ADDR],tt_normal,s.ref);
  595. cg.a_load_reg_ref(exprasmlist,OS_ADDR,l.reference.base,s.ref);
  596. { release }
  597. location_release(exprasmlist,l);
  598. s.saved:=true;
  599. end;
  600. end;
  601. end;
  602. end;
  603. end;
  604. procedure maybe_restore(list:taasmoutput;var l:tlocation;const s:tmaybesave);
  605. begin
  606. if not s.saved then
  607. exit;
  608. if l.loc=LOC_CREGISTER then
  609. begin
  610. load_regvar_reg(list,l.register);
  611. exit;
  612. end;
  613. case l.loc of
  614. LOC_REGISTER :
  615. begin
  616. {$ifndef cpu64bit}
  617. if l.size in [OS_64,OS_S64] then
  618. begin
  619. l.registerlow:=rg.getregisterint(exprasmlist);
  620. l.registerhigh:=rg.getregisterint(exprasmlist);
  621. cg64.a_load64_ref_reg(exprasmlist,s.ref,joinreg64(l.registerlow,l.registerhigh));
  622. end
  623. else
  624. {$endif cpu64bit}
  625. begin
  626. l.register:=rg.getregisterint(exprasmlist);
  627. cg.a_load_ref_reg(exprasmlist,OS_INT,s.ref,l.register);
  628. end;
  629. end;
  630. LOC_CREFERENCE,
  631. LOC_REFERENCE :
  632. begin
  633. reference_reset(l.reference);
  634. l.reference.base:=rg.getaddressregister(exprasmlist);
  635. cg.a_load_ref_reg(exprasmlist,OS_ADDR,s.ref,l.reference.base);
  636. end;
  637. end;
  638. tg.ungetiftemp(exprasmlist,s.ref);
  639. end;
  640. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  641. begin
  642. if (needed>=maxfpuregs) and
  643. (l.loc = LOC_FPUREGISTER) then
  644. begin
  645. location_force_mem(list,l);
  646. maybe_pushfpu:=true;
  647. end
  648. else
  649. maybe_pushfpu:=false;
  650. end;
  651. {*****************************************************************************
  652. Push Value Para
  653. *****************************************************************************}
  654. procedure push_value_para(p:tnode;calloption:tproccalloption;
  655. para_offset:longint;alignment : longint;
  656. const locpara : tparalocation);
  657. var
  658. tempreference : treference;
  659. href : treference;
  660. hreg : tregister;
  661. sizetopush,
  662. size : longint;
  663. cgsize : tcgsize;
  664. r:Tregister;
  665. begin
  666. { we've nothing to push when the size of the parameter is 0 }
  667. if p.resulttype.def.size=0 then
  668. exit;
  669. { Move flags and jump in register to make it less complex }
  670. if p.location.loc in [LOC_FLAGS,LOC_JUMP] then
  671. location_force_reg(exprasmlist,p.location,def_cgsize(p.resulttype.def),false);
  672. { Handle Floating point types differently }
  673. if p.resulttype.def.deftype=floatdef then
  674. begin
  675. case p.location.loc of
  676. LOC_FPUREGISTER,
  677. LOC_CFPUREGISTER:
  678. begin
  679. {$ifdef i386}
  680. size:=align(tfloatdef(p.resulttype.def).size,alignment);
  681. inc(pushedparasize,size);
  682. if calloption<>pocall_inline then
  683. cg.g_stackpointer_alloc(exprasmlist,size);
  684. {$ifdef GDB}
  685. if (cs_debuginfo in aktmoduleswitches) and
  686. (exprasmList.first=exprasmList.last) then
  687. exprasmList.concat(Tai_force_line.Create);
  688. {$endif GDB}
  689. { this is the easiest case for inlined !! }
  690. r.enum:=stack_pointer_reg;
  691. if calloption=pocall_inline then
  692. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize)
  693. else
  694. reference_reset_base(href,r,0);
  695. cg.a_loadfpu_reg_ref(exprasmlist,
  696. def_cgsize(p.resulttype.def),p.location.register,href);
  697. {$else i386}
  698. cg.a_paramfpu_reg(exprasmlist,def_cgsize(p.resulttype.def),p.location.register,locpara);
  699. {$endif i386}
  700. end;
  701. LOC_REFERENCE,
  702. LOC_CREFERENCE :
  703. begin
  704. sizetopush:=align(p.resulttype.def.size,alignment);
  705. tempreference:=p.location.reference;
  706. inc(tempreference.offset,sizetopush);
  707. while (sizetopush>0) do
  708. begin
  709. if sizetopush>=4 then
  710. begin
  711. cgsize:=OS_32;
  712. inc(pushedparasize,4);
  713. dec(tempreference.offset,4);
  714. dec(sizetopush,4);
  715. end
  716. else
  717. begin
  718. cgsize:=OS_16;
  719. inc(pushedparasize,2);
  720. dec(tempreference.offset,2);
  721. dec(sizetopush,2);
  722. end;
  723. if calloption=pocall_inline then
  724. begin
  725. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize);
  726. cg.a_load_ref_ref(exprasmlist,cgsize,tempreference,href);
  727. end
  728. else
  729. cg.a_param_ref(exprasmlist,cgsize,tempreference,locpara);
  730. end;
  731. end;
  732. else
  733. internalerror(200204243);
  734. end;
  735. location_release(exprasmlist,p.location);
  736. end
  737. else
  738. begin
  739. { copy the value on the stack or use normal parameter push? }
  740. if paramanager.copy_value_on_stack(p.resulttype.def,calloption) then
  741. begin
  742. {$ifdef i386}
  743. if not (p.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  744. internalerror(200204241);
  745. { push on stack }
  746. size:=align(p.resulttype.def.size,alignment);
  747. inc(pushedparasize,size);
  748. cg.g_stackpointer_alloc(exprasmlist,size);
  749. r.enum:=stack_pointer_reg;
  750. reference_reset_base(href,r,0);
  751. cg.g_concatcopy(exprasmlist,p.location.reference,href,size,false,false);
  752. {$else i386}
  753. cg.a_param_copy_ref(exprasmlist,p.resulttype.def.size,p.location.reference,locpara);
  754. {$endif i386}
  755. end
  756. else
  757. begin
  758. case p.location.loc of
  759. LOC_CONSTANT,
  760. LOC_REGISTER,
  761. LOC_CREGISTER,
  762. LOC_REFERENCE,
  763. LOC_CREFERENCE :
  764. begin
  765. cgsize:=def_cgsize(p.resulttype.def);
  766. if cgsize in [OS_64,OS_S64] then
  767. begin
  768. inc(pushedparasize,8);
  769. if calloption=pocall_inline then
  770. begin
  771. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize);
  772. if p.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  773. begin
  774. size:=align(p.resulttype.def.size,alignment);
  775. cg.g_concatcopy(exprasmlist,p.location.reference,href,size,false,false)
  776. end
  777. else
  778. cg64.a_load64_loc_ref(exprasmlist,p.location,href);
  779. end
  780. else
  781. cg64.a_param64_loc(exprasmlist,p.location,locpara);
  782. end
  783. else
  784. begin
  785. case cgsize of
  786. OS_8,OS_S8 :
  787. begin
  788. if alignment=4 then
  789. cgsize:=OS_32
  790. else
  791. cgsize:=OS_16;
  792. end;
  793. OS_16,OS_S16 :
  794. begin
  795. if alignment=4 then
  796. cgsize:=OS_32;
  797. end;
  798. end;
  799. { update register to use to match alignment }
  800. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  801. begin
  802. hreg:=p.location.register;
  803. p.location.register:=rg.makeregsize(p.location.register,cgsize);
  804. end;
  805. inc(pushedparasize,alignment);
  806. if calloption=pocall_inline then
  807. begin
  808. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize);
  809. if p.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  810. begin
  811. size:=align(p.resulttype.def.size,alignment);
  812. cg.g_concatcopy(exprasmlist,p.location.reference,href,size,false,false)
  813. end
  814. else
  815. cg.a_load_loc_ref(exprasmlist,p.location,href);
  816. end
  817. else
  818. cg.a_param_loc(exprasmlist,p.location,locpara);
  819. { restore old register }
  820. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  821. p.location.register:=hreg;
  822. end;
  823. location_release(exprasmlist,p.location);
  824. end;
  825. {$ifdef SUPPORT_MMX}
  826. LOC_MMXREGISTER,
  827. LOC_CMMXREGISTER:
  828. begin
  829. inc(pushedparasize,8);
  830. if calloption=pocall_inline then
  831. begin
  832. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize);
  833. cg.a_loadmm_reg_ref(exprasmlist,p.location.register,href);
  834. end
  835. else
  836. cg.a_parammm_reg(exprasmlist,p.location.register);
  837. end;
  838. {$endif SUPPORT_MMX}
  839. else
  840. internalerror(200204241);
  841. end;
  842. end;
  843. end;
  844. end;
  845. {****************************************************************************
  846. Entry/Exit Code
  847. ****************************************************************************}
  848. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  849. var
  850. href1,href2 : treference;
  851. list : taasmoutput;
  852. begin
  853. list:=taasmoutput(arg);
  854. if (tsym(p).typ=varsym) and
  855. (tvarsym(p).varspez=vs_value) and
  856. (paramanager.push_addr_param(tvarsym(p).vartype.def,procinfo.procdef.proccalloption)) then
  857. begin
  858. reference_reset_base(href1,procinfo.framepointer,tvarsym(p).address+procinfo.para_offset);
  859. if is_open_array(tvarsym(p).vartype.def) or
  860. is_array_of_const(tvarsym(p).vartype.def) then
  861. cg.g_copyvaluepara_openarray(list,href1,tarraydef(tvarsym(p).vartype.def).elesize)
  862. else
  863. begin
  864. reference_reset_base(href2,procinfo.framepointer,-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup);
  865. if is_shortstring(tvarsym(p).vartype.def) then
  866. cg.g_copyshortstring(list,href1,href2,tstringdef(tvarsym(p).vartype.def).len,false,true)
  867. else
  868. cg.g_concatcopy(list,href1,href2,tvarsym(p).vartype.def.size,true,true);
  869. end;
  870. end;
  871. end;
  872. procedure removevalueparas(p : tnamedindexitem;arg:pointer);
  873. var
  874. href1 : treference;
  875. list : taasmoutput;
  876. begin
  877. list:=taasmoutput(arg);
  878. if (tsym(p).typ=varsym) and
  879. (tvarsym(p).varspez=vs_value) and
  880. (is_open_array(tvarsym(p).vartype.def) or
  881. is_array_of_const(tvarsym(p).vartype.def)) and
  882. (paramanager.push_addr_param(tvarsym(p).vartype.def,procinfo.procdef.proccalloption)) then
  883. begin
  884. reference_reset_base(href1,procinfo.framepointer,tvarsym(p).address+procinfo.para_offset);
  885. cg.g_removevaluepara_openarray(list,href1,tarraydef(tvarsym(p).vartype.def).elesize);
  886. end;
  887. end;
  888. { generates the code for initialisation of local data }
  889. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  890. var
  891. href : treference;
  892. list : taasmoutput;
  893. begin
  894. list:=taasmoutput(arg);
  895. if (tsym(p).typ=varsym) and
  896. not(vo_is_local_copy in tvarsym(p).varoptions) and
  897. assigned(tvarsym(p).vartype.def) and
  898. not(is_class(tvarsym(p).vartype.def)) and
  899. tvarsym(p).vartype.def.needs_inittable then
  900. begin
  901. if assigned(procinfo) and
  902. (cs_implicit_exceptions in aktmoduleswitches) then
  903. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  904. if tsym(p).owner.symtabletype in [localsymtable,inlinelocalsymtable] then
  905. reference_reset_base(href,procinfo.framepointer,-tvarsym(p).address+tvarsym(p).owner.address_fixup)
  906. else
  907. reference_reset_symbol(href,objectlibrary.newasmsymbol(tvarsym(p).mangledname),0);
  908. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  909. end;
  910. end;
  911. { generates the code for finalisation of local data }
  912. procedure finalize_data(p : tnamedindexitem;arg:pointer);
  913. var
  914. href : treference;
  915. list : taasmoutput;
  916. begin
  917. list:=taasmoutput(arg);
  918. case tsym(p).typ of
  919. varsym :
  920. begin
  921. if not(vo_is_local_copy in tvarsym(p).varoptions) and
  922. assigned(tvarsym(p).vartype.def) and
  923. not(is_class(tvarsym(p).vartype.def)) and
  924. tvarsym(p).vartype.def.needs_inittable then
  925. begin
  926. if tsym(p).owner.symtabletype in [localsymtable,inlinelocalsymtable] then
  927. reference_reset_base(href,procinfo.framepointer,-tvarsym(p).address+tvarsym(p).owner.address_fixup)
  928. else
  929. reference_reset_symbol(href,objectlibrary.newasmsymbol(tvarsym(p).mangledname),0);
  930. cg.g_finalize(list,tvarsym(p).vartype.def,href,false);
  931. end;
  932. end;
  933. typedconstsym :
  934. begin
  935. if ttypedconstsym(p).is_writable and
  936. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  937. begin
  938. reference_reset_symbol(href,objectlibrary.newasmsymbol(ttypedconstsym(p).mangledname),0);
  939. cg.g_finalize(list,ttypedconstsym(p).typedconsttype.def,href,false);
  940. end;
  941. end;
  942. end;
  943. end;
  944. { generates the code for incrementing the reference count of parameters and
  945. initialize out parameters }
  946. procedure init_paras(p : tnamedindexitem;arg:pointer);
  947. var
  948. href : treference;
  949. tmpreg : tregister;
  950. list : taasmoutput;
  951. begin
  952. list:=taasmoutput(arg);
  953. if (tsym(p).typ=varsym) and
  954. not is_class(tvarsym(p).vartype.def) and
  955. tvarsym(p).vartype.def.needs_inittable then
  956. begin
  957. case tvarsym(p).varspez of
  958. vs_value :
  959. begin
  960. if (cs_implicit_exceptions in aktmoduleswitches) then
  961. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  962. if assigned(tvarsym(p).localvarsym) then
  963. reference_reset_base(href,procinfo.framepointer,
  964. -tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup)
  965. else
  966. reference_reset_base(href,procinfo.framepointer,tvarsym(p).address+procinfo.para_offset);
  967. cg.g_incrrefcount(list,tvarsym(p).vartype.def,href);
  968. end;
  969. vs_out :
  970. begin
  971. reference_reset_base(href,procinfo.framepointer,tvarsym(p).address+procinfo.para_offset);
  972. tmpreg:=cg.get_scratch_reg_address(list);
  973. cg.a_load_ref_reg(list,OS_ADDR,href,tmpreg);
  974. reference_reset_base(href,tmpreg,0);
  975. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  976. cg.free_scratch_reg(list,tmpreg);
  977. end;
  978. end;
  979. end;
  980. end;
  981. { generates the code for decrementing the reference count of parameters }
  982. procedure final_paras(p : tnamedindexitem;arg:pointer);
  983. var
  984. href : treference;
  985. list : taasmoutput;
  986. begin
  987. list:=taasmoutput(arg);
  988. if (tsym(p).typ=varsym) and
  989. not is_class(tvarsym(p).vartype.def) and
  990. tvarsym(p).vartype.def.needs_inittable then
  991. begin
  992. if (tvarsym(p).varspez=vs_value) then
  993. begin
  994. if assigned(tvarsym(p).localvarsym) then
  995. reference_reset_base(href,procinfo.framepointer,
  996. -tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup)
  997. else
  998. reference_reset_base(href,procinfo.framepointer,tvarsym(p).address+procinfo.para_offset);
  999. cg.g_decrrefcount(list,tvarsym(p).vartype.def,href);
  1000. end;
  1001. end;
  1002. end;
  1003. { Initialize temp ansi/widestrings,interfaces }
  1004. procedure inittempvariables(list:taasmoutput);
  1005. var
  1006. hp : ptemprecord;
  1007. href : treference;
  1008. begin
  1009. hp:=tg.templist;
  1010. while assigned(hp) do
  1011. begin
  1012. if hp^.temptype in [tt_ansistring,tt_freeansistring,
  1013. tt_widestring,tt_freewidestring,
  1014. tt_interfacecom,tt_freeinterfacecom] then
  1015. begin
  1016. if (cs_implicit_exceptions in aktmoduleswitches) then
  1017. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  1018. reference_reset_base(href,procinfo.framepointer,hp^.pos);
  1019. cg.a_load_const_ref(list,OS_ADDR,0,href);
  1020. end;
  1021. hp:=hp^.next;
  1022. end;
  1023. end;
  1024. procedure finalizetempvariables(list:taasmoutput);
  1025. var
  1026. hp : ptemprecord;
  1027. href : treference;
  1028. begin
  1029. hp:=tg.templist;
  1030. while assigned(hp) do
  1031. begin
  1032. case hp^.temptype of
  1033. tt_ansistring,
  1034. tt_freeansistring :
  1035. begin
  1036. reference_reset_base(href,procinfo.framepointer,hp^.pos);
  1037. cg.a_paramaddr_ref(list,href,paramanager.getintparaloc(1));
  1038. cg.a_call_name(list,'FPC_ANSISTR_DECR_REF');
  1039. end;
  1040. tt_widestring,
  1041. tt_freewidestring :
  1042. begin
  1043. reference_reset_base(href,procinfo.framepointer,hp^.pos);
  1044. cg.a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1045. cg.a_call_name(list,'FPC_WIDESTR_DECR_REF');
  1046. end;
  1047. tt_interfacecom :
  1048. begin
  1049. reference_reset_base(href,procinfo.framepointer,hp^.pos);
  1050. cg.a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1051. cg.a_call_name(list,'FPC_INTF_DECR_REF');
  1052. end;
  1053. end;
  1054. hp:=hp^.next;
  1055. end;
  1056. end;
  1057. procedure handle_return_value(list:TAAsmoutput; inlined : boolean;var uses_acc,uses_acchi,uses_fpu : boolean);
  1058. var
  1059. href : treference;
  1060. hreg,r,r2 : tregister;
  1061. cgsize : TCGSize;
  1062. begin
  1063. if not is_void(aktprocdef.rettype.def) then
  1064. begin
  1065. if (tfuncretsym(aktprocdef.funcretsym).funcretstate<>vs_assigned) and
  1066. (not inlined) then
  1067. CGMessage(sym_w_function_result_not_set);
  1068. reference_reset_base(href,procinfo.framepointer,procinfo.return_offset);
  1069. cgsize:=def_cgsize(aktprocdef.rettype.def);
  1070. case aktprocdef.rettype.def.deftype of
  1071. orddef,
  1072. enumdef :
  1073. begin
  1074. uses_acc:=true;
  1075. {$WARNING accumulator was replaced by return_result_reg}
  1076. {Here, we return the function result. In most architectures, the value is
  1077. passed into the accumulator, but in a windowed architecure like sparc a
  1078. function returns in a register and the caller receives it in an other one}
  1079. r.enum:=return_result_reg;
  1080. cg.a_reg_alloc(list,r);
  1081. {$ifndef cpu64bit}
  1082. if cgsize in [OS_64,OS_S64] then
  1083. begin
  1084. uses_acchi:=true;
  1085. r.enum:=accumulatorhigh;
  1086. cg.a_reg_alloc(list,r);
  1087. r.enum:=accumulator;
  1088. r2.enum:=accumulatorhigh;
  1089. cg64.a_load64_ref_reg(list,href,joinreg64(r,r2));
  1090. end
  1091. else
  1092. {$endif cpu64bit}
  1093. begin
  1094. {$WARNING accumulator was replaced by return_result_reg}
  1095. {Here, we return the function result. In most architectures, the value is
  1096. passed into the accumulator, but in a windowed architecure like sparc a
  1097. function returns in a register and the caller receives it in an other one}
  1098. r.enum:=return_result_reg;
  1099. hreg:=rg.makeregsize(r,cgsize);
  1100. cg.a_load_ref_reg(list,cgsize,href,hreg);
  1101. end;
  1102. end;
  1103. floatdef :
  1104. begin
  1105. uses_fpu := true;
  1106. r.enum:=fpu_result_reg;
  1107. cg.a_loadfpu_ref_reg(list,cgsize,href,r);
  1108. end;
  1109. else
  1110. begin
  1111. if paramanager.ret_in_acc(aktprocdef.rettype.def,aktprocdef.proccalloption) then
  1112. begin
  1113. uses_acc:=true;
  1114. r.enum:=accumulator;
  1115. cg.a_reg_alloc(list,r);
  1116. {$ifndef cpu64bit}
  1117. { Win32 can return records in EAX:EDX }
  1118. if cgsize in [OS_64,OS_S64] then
  1119. begin
  1120. uses_acchi:=true;
  1121. r.enum:=accumulatorhigh;
  1122. cg.a_reg_alloc(list,r);
  1123. r.enum:=accumulator;
  1124. r2.enum:=accumulatorhigh;
  1125. cg64.a_load64_ref_reg(list,href,joinreg64(r,r2));
  1126. end
  1127. else
  1128. {$endif cpu64bit}
  1129. r.enum:=accumulator;
  1130. cg.a_load_ref_reg(list,cgsize,href,r);
  1131. end
  1132. end;
  1133. end;
  1134. end;
  1135. end;
  1136. procedure handle_fast_exit_return_value(list:TAAsmoutput);
  1137. var
  1138. href : treference;
  1139. hreg : tregister;
  1140. cgsize : TCGSize;
  1141. r,r2 : Tregister;
  1142. begin
  1143. if not is_void(aktprocdef.rettype.def) then
  1144. begin
  1145. reference_reset_base(href,procinfo.framepointer,procinfo.return_offset);
  1146. cgsize:=def_cgsize(aktprocdef.rettype.def);
  1147. case aktprocdef.rettype.def.deftype of
  1148. orddef,
  1149. enumdef :
  1150. begin
  1151. {$ifndef cpu64bit}
  1152. r.enum:=accumulator;
  1153. r2.enum:=accumulatorhigh;
  1154. if cgsize in [OS_64,OS_S64] then
  1155. cg64.a_load64_reg_ref(list,joinreg64(r,r2),href)
  1156. else
  1157. {$endif cpu64bit}
  1158. begin
  1159. hreg:=rg.makeregsize(r,cgsize);
  1160. cg.a_load_reg_ref(list,cgsize,hreg,href);
  1161. end;
  1162. end;
  1163. floatdef :
  1164. begin
  1165. r.enum:=fpu_result_reg;
  1166. cg.a_loadfpu_reg_ref(list,cgsize,r,href);
  1167. end;
  1168. else
  1169. begin
  1170. r.enum:=accumulator;
  1171. if paramanager.ret_in_acc(aktprocdef.rettype.def,aktprocdef.proccalloption) then
  1172. cg.a_load_reg_ref(list,cgsize,r,href);
  1173. end;
  1174. end;
  1175. end;
  1176. end;
  1177. procedure genentrycode(list : TAAsmoutput;
  1178. make_global:boolean;
  1179. stackframe:longint;
  1180. var parasize:longint;var nostackframe:boolean;
  1181. inlined : boolean);
  1182. var
  1183. hs : string;
  1184. href : treference;
  1185. stackalloclist : taasmoutput;
  1186. hp : tparaitem;
  1187. paraloc : tparalocation;
  1188. r:Tregister;
  1189. begin
  1190. if not inlined then
  1191. stackalloclist:=taasmoutput.Create;
  1192. { the actual stack allocation code, symbol entry point and
  1193. gdb stabs information is generated AFTER the rest of this
  1194. code, since temp. allocation might occur before - carl
  1195. }
  1196. { for the save all registers we can simply use a pusha,popa which
  1197. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1198. if (po_saveregisters in aktprocdef.procoptions) then
  1199. cg.g_save_all_registers(list)
  1200. else
  1201. { should we save edi,esi,ebx like C ? }
  1202. if (po_savestdregs in aktprocdef.procoptions) then
  1203. cg.g_save_standard_registers(list,aktprocdef.usedregisters);
  1204. { the actual profile code can clobber some registers,
  1205. therefore if the context must be saved, do it before
  1206. the actual call to the profile code
  1207. }
  1208. if (cs_profile in aktmoduleswitches)
  1209. and not(po_assembler in aktprocdef.procoptions)
  1210. and not(inlined) then
  1211. begin
  1212. { non-win32 can call mcout even in main }
  1213. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1214. cg.g_profilecode(list)
  1215. else
  1216. { wdosx, and win32 should not call mcount before monstartup has been called }
  1217. if not (aktprocdef.proctypeoption=potype_proginit) then
  1218. cg.g_profilecode(list);
  1219. end;
  1220. { a constructor needs a help procedure }
  1221. if (aktprocdef.proctypeoption=potype_constructor) then
  1222. cg.g_call_constructor_helper(list);
  1223. { don't load ESI, does the caller }
  1224. { we must do it for local function }
  1225. { that can be called from a foreach_static }
  1226. { of another object than self !! PM }
  1227. if assigned(procinfo._class) and { !!!!! shouldn't we load ESI always? }
  1228. (lexlevel>normal_function_level) then
  1229. cg.g_maybe_loadself(list);
  1230. { When message method contains self as a parameter,
  1231. we must load it into ESI }
  1232. If (po_containsself in aktprocdef.procoptions) then
  1233. begin
  1234. r.enum:=self_pointer_reg;
  1235. list.concat(tai_regalloc.Alloc(r));
  1236. reference_reset_base(href,procinfo.framepointer,procinfo.selfpointer_offset);
  1237. cg.a_load_ref_reg(list,OS_ADDR,href,r);
  1238. end;
  1239. if not is_void(aktprocdef.rettype.def) then
  1240. begin
  1241. { for now the pointer to the result can't be a register }
  1242. if not(paramanager.ret_in_reg(aktprocdef.rettype.def,aktprocdef.proccalloption)) then
  1243. begin
  1244. paraloc:=paramanager.getfuncretparaloc(aktprocdef);
  1245. reference_reset_base(href,procinfo.framepointer,procinfo.return_offset);
  1246. case paraloc.loc of
  1247. LOC_CREGISTER,
  1248. LOC_REGISTER:
  1249. if not(paraloc.size in [OS_64,OS_S64]) then
  1250. cg.a_load_reg_ref(list,paraloc.size,paraloc.register,href)
  1251. else
  1252. cg64.a_load64_reg_ref(list,paraloc.register64,href);
  1253. LOC_CFPUREGISTER,
  1254. LOC_FPUREGISTER:
  1255. cg.a_load_reg_ref(list,paraloc.size,paraloc.register,href);
  1256. LOC_CMMREGISTER,
  1257. LOC_MMREGISTER:
  1258. cg.a_loadmm_reg_ref(list,paraloc.register,href);
  1259. end;
  1260. end;
  1261. { initialize return value }
  1262. if (aktprocdef.rettype.def.needs_inittable) then
  1263. begin
  1264. if (cs_implicit_exceptions in aktmoduleswitches) then
  1265. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  1266. reference_reset_base(href,procinfo.framepointer,procinfo.return_offset);
  1267. cg.g_initialize(list,aktprocdef.rettype.def,href,paramanager.ret_in_param(aktprocdef.rettype.def,aktprocdef.proccalloption));
  1268. end;
  1269. end;
  1270. { initialisize local data like ansistrings }
  1271. case aktprocdef.proctypeoption of
  1272. potype_unitinit:
  1273. begin
  1274. { using current_module.globalsymtable is hopefully }
  1275. { more robust than symtablestack and symtablestack.next }
  1276. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1277. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1278. end;
  1279. { units have seperate code for initilization and finalization }
  1280. potype_unitfinalize: ;
  1281. { program init/final is generated in separate procedure }
  1282. potype_proginit: ;
  1283. else
  1284. aktprocdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1285. end;
  1286. { initialisizes temp. ansi/wide string data }
  1287. inittempvariables(list);
  1288. { generate copies of call by value parameters }
  1289. if not(po_assembler in aktprocdef.procoptions) then
  1290. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1291. if assigned(aktprocdef.parast) then
  1292. begin
  1293. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1294. { move register parameters which aren't regable into memory }
  1295. { we do this after init_paras because it saves some code in init_paras if parameters are in register }
  1296. { instead in memory }
  1297. hp:=tparaitem(procinfo.procdef.para.first);
  1298. while assigned(hp) do
  1299. begin
  1300. if Tvarsym(hp.parasym).reg.enum>lastreg then
  1301. internalerror(200301081);
  1302. if (tvarsym(hp.parasym).reg.enum<>R_NO) then
  1303. case hp.paraloc.loc of
  1304. LOC_CREGISTER,
  1305. LOC_REGISTER:
  1306. // if not(hp.paraloc.size in [OS_S64,OS_64]) then
  1307. cg.a_load_reg_reg(list,hp.paraloc.size,OS_32,hp.paraloc.register,tvarsym(hp.parasym).reg);
  1308. // else
  1309. // cg64.a_load64_reg_reg(list,hp.paraloc.register64,tvarsym(hp.parasym).reg);
  1310. LOC_CFPUREGISTER,
  1311. LOC_FPUREGISTER:
  1312. cg.a_loadfpu_reg_reg(list,hp.paraloc.register,tvarsym(hp.parasym).reg);
  1313. end
  1314. else if (hp.paraloc.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER,
  1315. LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMREGISTER]) and
  1316. (tvarsym(hp.parasym).reg.enum=R_NO) then
  1317. begin
  1318. reference_reset_base(href,procinfo.framepointer,tvarsym(hp.parasym).address+
  1319. tvarsym(hp.parasym).owner.address_fixup);
  1320. case hp.paraloc.loc of
  1321. LOC_CREGISTER,
  1322. LOC_REGISTER:
  1323. if not(hp.paraloc.size in [OS_S64,OS_64]) then
  1324. cg.a_load_reg_ref(list,hp.paraloc.size,hp.paraloc.register,href)
  1325. else
  1326. cg64.a_load64_reg_ref(list,hp.paraloc.register64,href);
  1327. LOC_FPUREGISTER,
  1328. LOC_CFPUREGISTER:
  1329. cg.a_loadfpu_reg_ref(list,hp.paraloc.size,hp.paraloc.register,href);
  1330. else
  1331. internalerror(2002081302);
  1332. end;
  1333. end;
  1334. hp:=tparaitem(hp.next);
  1335. end;
  1336. end;
  1337. if (not inlined) then
  1338. begin
  1339. { call startup helpers from main program }
  1340. if (aktprocdef.proctypeoption=potype_proginit) then
  1341. begin
  1342. { initialize profiling for win32 }
  1343. if (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1344. (cs_profile in aktmoduleswitches) then
  1345. begin
  1346. reference_reset_symbol(href,objectlibrary.newasmsymbol('etext'),0);
  1347. cg.a_paramaddr_ref(list,href,paraloc);
  1348. reference_reset_symbol(href,objectlibrary.newasmsymbol('__image_base__'),0);
  1349. cg.a_paramaddr_ref(list,href,paraloc);
  1350. cg.a_call_name(list,'_monstartup');
  1351. end;
  1352. { initialize units }
  1353. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1354. end;
  1355. { do we need an exception frame because of ansi/widestrings/interfaces ? }
  1356. if ((procinfo.flags and pi_needs_implicit_finally)<>0) and
  1357. { but it's useless in init/final code of units }
  1358. not(aktprocdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  1359. begin
  1360. include(rg.usedinproc,accumulator);
  1361. tg.GetTemp(list,JMP_BUF_SIZE,tt_noreuse,procinfo.exception_jmp_ref);
  1362. tg.GetTemp(list,12,tt_noreuse,procinfo.exception_env_ref);
  1363. tg.GetTemp(list,sizeof(aword),tt_noreuse,procinfo.exception_result_ref);
  1364. new_exception(list,procinfo.exception_jmp_ref,
  1365. procinfo.exception_env_ref,
  1366. procinfo.exception_result_ref,1,aktexitlabel);
  1367. { probably we've to reload self here }
  1368. cg.g_maybe_loadself(list);
  1369. end;
  1370. {$ifdef GDB}
  1371. if (cs_debuginfo in aktmoduleswitches) then
  1372. list.concat(Tai_force_line.Create);
  1373. {$endif GDB}
  1374. end;
  1375. if inlined then
  1376. load_regvars(list,nil);
  1377. {************************* Stack allocation **************************}
  1378. { and symbol entry point as well as debug information }
  1379. { will be inserted in front of the rest of this list. }
  1380. { Insert alignment and assembler names }
  1381. if not inlined then
  1382. begin
  1383. { Align, gprof uses 16 byte granularity }
  1384. if (cs_profile in aktmoduleswitches) then
  1385. stackalloclist.concat(Tai_align.Create(16))
  1386. else
  1387. stackalloclist.concat(Tai_align.Create(aktalignment.procalign));
  1388. if (cs_profile in aktmoduleswitches) or
  1389. (aktprocdef.owner.symtabletype=globalsymtable) or
  1390. (assigned(procinfo._class) and (procinfo._class.owner.symtabletype=globalsymtable)) then
  1391. make_global:=true;
  1392. if make_global or ((procinfo.flags and pi_is_global) <> 0) then
  1393. aktprocsym.is_global := True;
  1394. {$ifdef GDB}
  1395. if (cs_debuginfo in aktmoduleswitches) then
  1396. begin
  1397. aktprocdef.concatstabto(stackalloclist);
  1398. aktprocsym.isstabwritten:=true;
  1399. end;
  1400. {$endif GDB}
  1401. repeat
  1402. hs:=aktprocdef.aliasnames.getfirst;
  1403. if hs='' then
  1404. break;
  1405. {$ifdef GDB}
  1406. if (cs_debuginfo in aktmoduleswitches) and
  1407. target_info.use_function_relative_addresses then
  1408. stackalloclist.concat(Tai_stab_function_name.Create(strpnew(hs)));
  1409. {$endif GDB}
  1410. if make_global then
  1411. stackalloclist.concat(Tai_symbol.Createname_global(hs,0))
  1412. else
  1413. stackalloclist.concat(Tai_symbol.Createname(hs,0));
  1414. until false;
  1415. stackframe:=stackframe+tg.gettempsize;
  1416. {$ifndef m68k}
  1417. { give a warning if the limit of local variables is reached }
  1418. if stackframe > maxlocalsize then
  1419. Message(cg_w_localsize_too_big);
  1420. {$endif}
  1421. {$ifndef powerpc}
  1422. { at least for the ppc this applies always, so this code isn't usable (FK) }
  1423. { omit stack frame ? }
  1424. if procinfo.framepointer.enum>lastreg then
  1425. internalerror(2003010801);
  1426. if (procinfo.framepointer.enum=STACK_POINTER_REG) then
  1427. begin
  1428. CGMessage(cg_d_stackframe_omited);
  1429. nostackframe:=true;
  1430. if (aktprocdef.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1431. parasize:=0
  1432. else
  1433. parasize:=aktprocdef.parast.datasize+procinfo.para_offset-4;
  1434. if stackframe<>0 then
  1435. cg.g_stackpointer_alloc(stackalloclist,stackframe);
  1436. end
  1437. else
  1438. {$endif powerpc}
  1439. begin
  1440. nostackframe:=false;
  1441. if (aktprocdef.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1442. parasize:=0
  1443. else
  1444. parasize:=aktprocdef.parast.datasize+procinfo.para_offset-target_info.first_parm_offset;
  1445. if (po_interrupt in aktprocdef.procoptions) then
  1446. cg.g_interrupt_stackframe_entry(stackalloclist);
  1447. cg.g_stackframe_entry(stackalloclist,stackframe);
  1448. { never call stack checking before the standard system unit
  1449. has not been initialized
  1450. }
  1451. if (cs_check_stack in aktlocalswitches) and (aktprocdef.proctypeoption<>potype_proginit) then
  1452. cg.g_stackcheck(stackalloclist,stackframe);
  1453. end;
  1454. list.insertlist(stackalloclist);
  1455. stackalloclist.free;
  1456. end;
  1457. {************************* End Stack allocation **************************}
  1458. end;
  1459. procedure genexitcode(list : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  1460. var
  1461. {$ifdef GDB}
  1462. stabsendlabel : tasmlabel;
  1463. mangled_length : longint;
  1464. p : pchar;
  1465. st : string[2];
  1466. {$endif GDB}
  1467. okexitlabel,
  1468. noreraiselabel,nodestroycall : tasmlabel;
  1469. tmpreg : tregister;
  1470. href : treference;
  1471. usesacc,
  1472. usesacchi,
  1473. usesself,usesfpu : boolean;
  1474. pd : tprocdef;
  1475. r,r2:Tregister;
  1476. begin
  1477. if aktexit2label.is_used and
  1478. ((procinfo.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0) then
  1479. begin
  1480. cg.a_jmp_always(list,aktexitlabel);
  1481. cg.a_label(list,aktexit2label);
  1482. handle_fast_exit_return_value(list);
  1483. end;
  1484. if aktexitlabel.is_used then
  1485. list.concat(Tai_label.Create(aktexitlabel));
  1486. cleanup_regvars(list);
  1487. { call the destructor help procedure }
  1488. if (aktprocdef.proctypeoption=potype_destructor) and
  1489. assigned(procinfo._class) then
  1490. cg.g_call_destructor_helper(list);
  1491. { finalize temporary data }
  1492. finalizetempvariables(list);
  1493. { finalize local data like ansistrings}
  1494. case aktprocdef.proctypeoption of
  1495. potype_unitfinalize:
  1496. begin
  1497. { using current_module.globalsymtable is hopefully }
  1498. { more robust than symtablestack and symtablestack.next }
  1499. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1500. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1501. end;
  1502. { units/progs have separate code for initialization and finalization }
  1503. potype_unitinit: ;
  1504. { program init/final is generated in separate procedure }
  1505. potype_proginit: ;
  1506. else
  1507. aktprocdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1508. end;
  1509. { finalize paras data }
  1510. if assigned(aktprocdef.parast) then
  1511. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1512. { do we need to handle exceptions because of ansi/widestrings ? }
  1513. if not inlined and
  1514. ((procinfo.flags and pi_needs_implicit_finally)<>0) and
  1515. { but it's useless in init/final code of units }
  1516. not(aktprocdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  1517. begin
  1518. { the exception helper routines modify all registers }
  1519. aktprocdef.usedregisters:=all_registers;
  1520. objectlibrary.getlabel(noreraiselabel);
  1521. free_exception(list,
  1522. procinfo.exception_jmp_ref,
  1523. procinfo.exception_env_ref,
  1524. procinfo.exception_result_ref,0,
  1525. noreraiselabel,false);
  1526. tg.Ungettemp(list,procinfo.exception_jmp_ref);
  1527. tg.Ungettemp(list,procinfo.exception_env_ref);
  1528. tg.Ungettemp(list,procinfo.exception_result_ref);
  1529. if (aktprocdef.proctypeoption=potype_constructor) then
  1530. begin
  1531. if assigned(procinfo._class) then
  1532. begin
  1533. pd:=procinfo._class.searchdestructor;
  1534. if assigned(pd) then
  1535. begin
  1536. objectlibrary.getlabel(nodestroycall);
  1537. reference_reset_base(href,procinfo.framepointer,procinfo.selfpointer_offset);
  1538. cg.a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nodestroycall);
  1539. r.enum:=self_pointer_reg;
  1540. if is_class(procinfo._class) then
  1541. begin
  1542. cg.a_param_const(list,OS_INT,1,paramanager.getintparaloc(2));
  1543. cg.a_param_reg(list,OS_ADDR,r,paramanager.getintparaloc(1));
  1544. end
  1545. else if is_object(procinfo._class) then
  1546. begin
  1547. cg.a_param_reg(list,OS_ADDR,r,paramanager.getintparaloc(2));
  1548. reference_reset_symbol(href,objectlibrary.newasmsymbol(procinfo._class.vmt_mangledname),0);
  1549. cg.a_paramaddr_ref(list,href,paramanager.getintparaloc(1));
  1550. end
  1551. else
  1552. Internalerror(200006164);
  1553. if (po_virtualmethod in pd.procoptions) then
  1554. begin
  1555. reference_reset_base(href,r,0);
  1556. tmpreg:=cg.get_scratch_reg_address(list);
  1557. cg.a_load_ref_reg(list,OS_ADDR,href,tmpreg);
  1558. reference_reset_base(href,tmpreg,procinfo._class.vmtmethodoffset(pd.extnumber));
  1559. cg.free_scratch_reg(list,tmpreg);
  1560. cg.a_call_ref(list,href);
  1561. end
  1562. else
  1563. cg.a_call_name(list,pd.mangledname);
  1564. { not necessary because the result is never assigned in the
  1565. case of an exception (FK) }
  1566. cg.a_label(list,nodestroycall);
  1567. end;
  1568. end
  1569. end
  1570. else
  1571. begin
  1572. { no constructor }
  1573. { must be the return value finalized before reraising the exception? }
  1574. if (not is_void(aktprocdef.rettype.def)) and
  1575. (aktprocdef.rettype.def.needs_inittable) and
  1576. ((aktprocdef.rettype.def.deftype<>objectdef) or
  1577. not is_class(aktprocdef.rettype.def)) then
  1578. begin
  1579. reference_reset_base(href,procinfo.framepointer,procinfo.return_offset);
  1580. cg.g_finalize(list,aktprocdef.rettype.def,href,paramanager.ret_in_param(aktprocdef.rettype.def,aktprocdef.proccalloption));
  1581. end;
  1582. end;
  1583. cg.a_call_name(list,'FPC_RERAISE');
  1584. cg.a_label(list,noreraiselabel);
  1585. end;
  1586. { call __EXIT for main program }
  1587. if (not DLLsource) and
  1588. (not inlined) and
  1589. (aktprocdef.proctypeoption=potype_proginit) then
  1590. begin
  1591. cg.a_call_name(list,'FPC_DO_EXIT');
  1592. end;
  1593. { handle return value, this is not done for assembler routines when
  1594. they didn't reference the result variable }
  1595. usesacc:=false;
  1596. usesacchi:=false;
  1597. usesself:=false;
  1598. if not(po_assembler in aktprocdef.procoptions) or
  1599. (assigned(aktprocdef.funcretsym) and
  1600. (tfuncretsym(aktprocdef.funcretsym).refcount>1)) then
  1601. begin
  1602. if (aktprocdef.proctypeoption<>potype_constructor) then
  1603. handle_return_value(list,inlined,usesacc,usesacchi,usesfpu)
  1604. else
  1605. begin
  1606. { successful constructor deletes the zero flag }
  1607. { and returns self in eax }
  1608. { eax must be set to zero if the allocation failed !!! }
  1609. objectlibrary.getlabel(okexitlabel);
  1610. cg.a_jmp_always(list,okexitlabel);
  1611. cg.a_label(list,faillabel);
  1612. cg.g_call_fail_helper(list);
  1613. cg.a_label(list,okexitlabel);
  1614. { for classes this is done after the call to }
  1615. { AfterConstruction }
  1616. if is_object(procinfo._class) then
  1617. begin
  1618. r.enum:=self_pointer_reg;
  1619. r2.enum:=accumulator;
  1620. cg.a_reg_alloc(list,r2);
  1621. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,r,r2);
  1622. usesacc:=true;
  1623. end;
  1624. {$ifdef i386}
  1625. r.enum:=R_ESI;
  1626. list.concat(taicpu.op_reg_reg(A_TEST,S_L,r,r));
  1627. {$else}
  1628. {$warning constructor returns in flags for i386}
  1629. {$endif i386}
  1630. usesself:=true;
  1631. end;
  1632. end;
  1633. if aktexit2label.is_used and not aktexit2label.is_set then
  1634. cg.a_label(list,aktexit2label);
  1635. {$ifdef GDB}
  1636. if ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  1637. begin
  1638. objectlibrary.getlabel(stabsendlabel);
  1639. cg.a_label(list,stabsendlabel);
  1640. end;
  1641. {$endif GDB}
  1642. { remove copies of call by value parameters when there are also
  1643. registers saved on the stack }
  1644. if ((po_saveregisters in aktprocdef.procoptions) or
  1645. (po_savestdregs in aktprocdef.procoptions)) and
  1646. not(po_assembler in aktprocdef.procoptions) then
  1647. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}removevalueparas,list);
  1648. { for the save all registers we can simply use a pusha,popa which
  1649. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1650. if (po_saveregisters in aktprocdef.procoptions) then
  1651. cg.g_restore_all_registers(list,usesself,usesacc,usesacchi)
  1652. else
  1653. { should we restore edi ? }
  1654. if (po_savestdregs in aktprocdef.procoptions) then
  1655. cg.g_restore_standard_registers(list,aktprocdef.usedregisters);
  1656. { remove stackframe }
  1657. if not inlined then
  1658. begin
  1659. if (not nostackframe) then
  1660. cg.g_restore_frame_pointer(list)
  1661. else
  1662. if (tg.gettempsize<>0) then
  1663. begin
  1664. r.enum:=stack_pointer_reg;
  1665. cg.a_op_const_reg(list,OP_ADD,tg.gettempsize,r);
  1666. end;
  1667. end;
  1668. { at last, the return is generated }
  1669. if not inlined then
  1670. begin
  1671. if (po_interrupt in aktprocdef.procoptions) then
  1672. cg.g_interrupt_stackframe_exit(list,usesself,usesacc,usesacchi)
  1673. else
  1674. begin
  1675. {$ifndef i386}
  1676. { give a warning if the limit of parameters allowed for
  1677. certain processors is reached.
  1678. }
  1679. if (parasize > maxparasize) then
  1680. Message(cg_w_parasize_too_big);
  1681. {$endif}
  1682. cg.g_return_from_proc(list,parasize);
  1683. end;
  1684. end;
  1685. if not inlined then
  1686. list.concat(Tai_symbol_end.Createname(aktprocdef.mangledname));
  1687. {$ifdef GDB}
  1688. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  1689. begin
  1690. if assigned(procinfo._class) then
  1691. if (not assigned(procinfo.parent) or
  1692. not assigned(procinfo.parent._class)) then
  1693. begin
  1694. if (po_classmethod in aktprocdef.procoptions) or
  1695. ((po_virtualmethod in aktprocdef.procoptions) and
  1696. (potype_constructor=aktprocdef.proctypeoption)) or
  1697. (po_staticmethod in aktprocdef.procoptions) then
  1698. begin
  1699. list.concat(Tai_stabs.Create(strpnew(
  1700. '"pvmt:p'+tstoreddef(pvmttype.def).numberstring+'",'+
  1701. tostr(N_tsym)+',0,0,'+tostr(procinfo.selfpointer_offset))));
  1702. end
  1703. else
  1704. begin
  1705. if not(is_class(procinfo._class)) then
  1706. st:='v'
  1707. else
  1708. st:='p';
  1709. list.concat(Tai_stabs.Create(strpnew(
  1710. '"$t:'+st+procinfo._class.numberstring+'",'+
  1711. tostr(N_tsym)+',0,0,'+tostr(procinfo.selfpointer_offset))));
  1712. end;
  1713. end
  1714. else
  1715. begin
  1716. if not is_class(procinfo._class) then
  1717. st:='*'
  1718. else
  1719. st:='';
  1720. list.concat(Tai_stabs.Create(strpnew(
  1721. '"$t:r'+st+procinfo._class.numberstring+'",'+
  1722. tostr(N_RSYM)+',0,0,'+tostr(stab_regindex[SELF_POINTER_REG]))));
  1723. end;
  1724. { define calling EBP as pseudo local var PM }
  1725. { this enables test if the function is a local one !! }
  1726. if assigned(procinfo.parent) and (lexlevel>normal_function_level) then
  1727. list.concat(Tai_stabs.Create(strpnew(
  1728. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  1729. tostr(N_LSYM)+',0,0,'+tostr(procinfo.framepointer_offset))));
  1730. if (not is_void(aktprocdef.rettype.def)) then
  1731. begin
  1732. if paramanager.ret_in_param(aktprocdef.rettype.def,aktprocdef.proccalloption) then
  1733. list.concat(Tai_stabs.Create(strpnew(
  1734. '"'+aktprocsym.name+':X*'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1735. tostr(N_tsym)+',0,0,'+tostr(procinfo.return_offset))))
  1736. else
  1737. list.concat(Tai_stabs.Create(strpnew(
  1738. '"'+aktprocsym.name+':X'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1739. tostr(N_tsym)+',0,0,'+tostr(procinfo.return_offset))));
  1740. if (m_result in aktmodeswitches) then
  1741. if paramanager.ret_in_param(aktprocdef.rettype.def,aktprocdef.proccalloption) then
  1742. list.concat(Tai_stabs.Create(strpnew(
  1743. '"RESULT:X*'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1744. tostr(N_tsym)+',0,0,'+tostr(procinfo.return_offset))))
  1745. else
  1746. list.concat(Tai_stabs.Create(strpnew(
  1747. '"RESULT:X'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1748. tostr(N_tsym)+',0,0,'+tostr(procinfo.return_offset))));
  1749. end;
  1750. mangled_length:=length(aktprocdef.mangledname);
  1751. getmem(p,2*mangled_length+50);
  1752. strpcopy(p,'192,0,0,');
  1753. strpcopy(strend(p),aktprocdef.mangledname);
  1754. if (target_info.use_function_relative_addresses) then
  1755. begin
  1756. strpcopy(strend(p),'-');
  1757. strpcopy(strend(p),aktprocdef.mangledname);
  1758. end;
  1759. list.concat(Tai_stabn.Create(strnew(p)));
  1760. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  1761. +aktprocdef.mangledname))));
  1762. p[0]:='2';p[1]:='2';p[2]:='4';
  1763. strpcopy(strend(p),'_end');}
  1764. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  1765. if (target_info.use_function_relative_addresses) then
  1766. begin
  1767. strpcopy(strend(p),'-');
  1768. strpcopy(strend(p),aktprocdef.mangledname);
  1769. end;
  1770. list.concatlist(withdebuglist);
  1771. list.concat(Tai_stabn.Create(strnew(p)));
  1772. { strpnew('224,0,0,'
  1773. +aktprocdef.mangledname+'_end'))));}
  1774. freemem(p,2*mangled_length+50);
  1775. end;
  1776. {$endif GDB}
  1777. if inlined then
  1778. cleanup_regvars(list);
  1779. end;
  1780. procedure genimplicitunitinit(list : TAAsmoutput);
  1781. begin
  1782. {$ifdef GDB}
  1783. if (cs_debuginfo in aktmoduleswitches) and
  1784. target_info.use_function_relative_addresses then
  1785. list.concat(Tai_stab_function_name.Create(strpnew('INIT$$'+current_module.modulename^)));
  1786. {$endif GDB}
  1787. list.concat(Tai_symbol.Createname_global('INIT$$'+current_module.modulename^,0));
  1788. list.concat(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_init',0));
  1789. { using current_module.globalsymtable is hopefully }
  1790. { more robust than symtablestack and symtablestack.next }
  1791. if assigned(current_module.globalsymtable) then
  1792. tsymtable(current_module.globalsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1793. tsymtable(current_module.localsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1794. cg.g_return_from_proc(list,0);
  1795. end;
  1796. procedure genimplicitunitfinal(list : TAAsmoutput);
  1797. begin
  1798. {$ifdef GDB}
  1799. if (cs_debuginfo in aktmoduleswitches) and
  1800. target_info.use_function_relative_addresses then
  1801. list.concat(Tai_stab_function_name.Create(strpnew('FINALIZE$$'+current_module.modulename^)));
  1802. {$endif GDB}
  1803. list.concat(Tai_symbol.Createname_global('FINALIZE$$'+current_module.modulename^,0));
  1804. list.concat(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_finalize',0));
  1805. { using current_module.globalsymtable is hopefully }
  1806. { more robust than symtablestack and symtablestack.next }
  1807. if assigned(current_module.globalsymtable) then
  1808. tsymtable(current_module.globalsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1809. tsymtable(current_module.localsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1810. cg.g_return_from_proc(list,0);
  1811. end;
  1812. end.
  1813. {
  1814. $Log$
  1815. Revision 1.75 2003-01-09 22:00:53 florian
  1816. * fixed some PowerPC issues
  1817. Revision 1.74 2003/01/09 20:41:10 florian
  1818. * fixed broken PowerPC compiler
  1819. Revision 1.73 2003/01/08 18:43:56 daniel
  1820. * Tregister changed into a record
  1821. Revision 1.72 2002/12/29 23:51:43 florian
  1822. * web bug 2214 fixed: ie 10 in const array constructors
  1823. Revision 1.71 2002/12/24 15:56:50 peter
  1824. * stackpointer_alloc added for adjusting ESP. Win32 needs
  1825. this for the pageprotection
  1826. Revision 1.70 2002/12/05 14:39:21 florian
  1827. * added missing then, Carl did you really a make fullcycle :) ?
  1828. Revision 1.69 2002/12/03 22:13:39 carl
  1829. * bugfix of problems with profile code which clobbers some registers
  1830. Revision 1.68 2002/12/01 22:06:59 carl
  1831. * warning of portabilitiy problems with parasize / localsize
  1832. Revision 1.67 2002/11/30 18:44:57 carl
  1833. + profiling support for Win32
  1834. Revision 1.66 2002/11/30 14:39:15 carl
  1835. * try to fix profiling for win32
  1836. Revision 1.65 2002/11/28 23:28:14 florian
  1837. * push_value_para didn't release floatdef locations, fixes tw2045
  1838. Revision 1.64 2002/11/27 02:33:19 peter
  1839. * copy_value_on_stack method added for cdecl record passing
  1840. Revision 1.63 2002/11/25 17:43:18 peter
  1841. * splitted defbase in defutil,symutil,defcmp
  1842. * merged isconvertable and is_equal into compare_defs(_ext)
  1843. * made operator search faster by walking the list only once
  1844. Revision 1.62 2002/11/18 17:31:55 peter
  1845. * pass proccalloption to ret_in_xxx and push_xxx functions
  1846. Revision 1.61 2002/11/17 17:49:08 mazen
  1847. + 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
  1848. Revision 1.60 2002/11/17 16:31:56 carl
  1849. * memory optimization (3-4%) : cleanup of tai fields,
  1850. cleanup of tdef and tsym fields.
  1851. * make it work for m68k
  1852. Revision 1.59 2002/11/15 01:58:51 peter
  1853. * merged changes from 1.0.7 up to 04-11
  1854. - -V option for generating bug report tracing
  1855. - more tracing for option parsing
  1856. - errors for cdecl and high()
  1857. - win32 import stabs
  1858. - win32 records<=8 are returned in eax:edx (turned off by default)
  1859. - heaptrc update
  1860. - more info for temp management in .s file with EXTDEBUG
  1861. Revision 1.58 2002/11/10 19:07:45 mazen
  1862. * SPARC calling mechanism almost OK (as in GCC./mppcsparc )
  1863. Revision 1.57 2002/11/03 20:22:40 mazen
  1864. * parameter handling updated
  1865. Revision 1.56 2002/10/16 19:01:43 peter
  1866. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1867. implicit exception frames for procedures with initialized variables
  1868. and for constructors. The default is on for compatibility
  1869. Revision 1.55 2002/10/14 19:42:33 peter
  1870. * only use init tables for threadvars
  1871. Revision 1.54 2002/10/06 19:41:30 peter
  1872. * Add finalization of typed consts
  1873. * Finalization of globals in the main program
  1874. Revision 1.53 2002/10/05 15:18:42 carl
  1875. * fix heap leaks
  1876. Revision 1.52 2002/09/30 07:00:46 florian
  1877. * fixes to common code to get the alpha compiler compiled applied
  1878. Revision 1.51 2002/09/22 14:02:35 carl
  1879. * stack checking cannot be called before system unit is initialized
  1880. * MC68020 define
  1881. Revision 1.50 2002/09/17 18:54:03 jonas
  1882. * a_load_reg_reg() now has two size parameters: source and dest. This
  1883. allows some optimizations on architectures that don't encode the
  1884. register size in the register name.
  1885. Revision 1.49 2002/09/10 21:48:30 florian
  1886. * improved handling of procedures with register calling conventions
  1887. Revision 1.48 2002/09/07 15:25:03 peter
  1888. * old logs removed and tabs fixed
  1889. Revision 1.47 2002/09/02 18:44:48 peter
  1890. * fixed (not) pushing of empty parameters
  1891. * fixed implicit initialization/finalization generation
  1892. * fixed/optimized local copy of value arguments init/final
  1893. Revision 1.46 2002/09/01 19:27:34 peter
  1894. * use index register when available for generating a reference with
  1895. only a signle register. Using the base register could possibly
  1896. destroy the framepointer
  1897. Revision 1.45 2002/09/01 18:50:20 peter
  1898. * fixed maybe_save that did not support a reference with only
  1899. a index register. It now also updates the location with the new
  1900. base register only
  1901. Revision 1.44 2002/09/01 14:42:41 peter
  1902. * removevaluepara added to fix the stackpointer so restoring of
  1903. saved registers works
  1904. Revision 1.43 2002/08/25 19:25:18 peter
  1905. * sym.insert_in_data removed
  1906. * symtable.insertvardata/insertconstdata added
  1907. * removed insert_in_data call from symtable.insert, it needs to be
  1908. called separatly. This allows to deref the address calculation
  1909. * procedures now calculate the parast addresses after the procedure
  1910. directives are parsed. This fixes the cdecl parast problem
  1911. * push_addr_param has an extra argument that specifies if cdecl is used
  1912. or not
  1913. Revision 1.42 2002/08/24 18:38:26 peter
  1914. * really use tt_noreuse for exception frame buffers
  1915. Revision 1.41 2002/08/23 16:14:49 peter
  1916. * tempgen cleanup
  1917. * tt_noreuse temp type added that will be used in genentrycode
  1918. Revision 1.40 2002/08/18 10:42:37 florian
  1919. * remaining assembler writer bugs fixed, the errors in the
  1920. system unit are inline assembler problems
  1921. Revision 1.39 2002/08/17 09:23:36 florian
  1922. * first part of procinfo rewrite
  1923. Revision 1.38 2002/08/16 14:24:57 carl
  1924. * issameref() to test if two references are the same (then emit no opcodes)
  1925. + ret_in_reg to replace ret_in_acc
  1926. (fix some register allocation bugs at the same time)
  1927. + save_std_register now has an extra parameter which is the
  1928. usedinproc registers
  1929. Revision 1.37 2002/08/15 15:15:55 carl
  1930. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  1931. * more generic nodes for maths
  1932. * several fixes for better m68k support
  1933. Revision 1.36 2002/08/14 19:25:09 carl
  1934. * fix Florian's last commit for m68k compilation
  1935. Revision 1.35 2002/08/13 21:40:56 florian
  1936. * more fixes for ppc calling conventions
  1937. Revision 1.34 2002/08/12 15:08:39 carl
  1938. + stab register indexes for powerpc (moved from gdb to cpubase)
  1939. + tprocessor enumeration moved to cpuinfo
  1940. + linker in target_info is now a class
  1941. * many many updates for m68k (will soon start to compile)
  1942. - removed some ifdef or correct them for correct cpu
  1943. Revision 1.33 2002/08/11 14:32:27 peter
  1944. * renamed current_library to objectlibrary
  1945. Revision 1.32 2002/08/11 13:24:12 peter
  1946. * saving of asmsymbols in ppu supported
  1947. * asmsymbollist global is removed and moved into a new class
  1948. tasmlibrarydata that will hold the info of a .a file which
  1949. corresponds with a single module. Added librarydata to tmodule
  1950. to keep the library info stored for the module. In the future the
  1951. objectfiles will also be stored to the tasmlibrarydata class
  1952. * all getlabel/newasmsymbol and friends are moved to the new class
  1953. Revision 1.31 2002/08/09 19:16:57 carl
  1954. * stack allocation is now done separately (at the end) of genentrycode
  1955. so temps. can be allocated before.
  1956. * fix generic exception handling
  1957. Revision 1.30 2002/08/06 20:55:21 florian
  1958. * first part of ppc calling conventions fix
  1959. Revision 1.29 2002/08/04 19:09:22 carl
  1960. + added generic exception support (still does not work!)
  1961. + more documentation
  1962. Revision 1.28 2002/07/29 21:23:42 florian
  1963. * more fixes for the ppc
  1964. + wrappers for the tcnvnode.first_* stuff introduced
  1965. }