ncgutil.pas 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Helper routines for all code generators
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncgutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,cpuinfo,
  22. globtype,
  23. cpubase,cgbase,parabase,cgutils,
  24. aasmbase,aasmtai,aasmdata,aasmcpu,
  25. symconst,symbase,symdef,symsym,symtype,symtable
  26. {$ifndef cpu64bitalu}
  27. ,cg64f32
  28. {$endif not cpu64bitalu}
  29. ;
  30. type
  31. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  32. pusedregvars = ^tusedregvars;
  33. tusedregvars = record
  34. intregvars, fpuregvars, mmregvars: Tsuperregisterworklist;
  35. end;
  36. {
  37. Not used currently, implemented because I thought we had to
  38. synchronise around if/then/else as well, but not needed. May
  39. still be useful for SSA once we get around to implementing
  40. that (JM)
  41. pusedregvarscommon = ^tusedregvarscommon;
  42. tusedregvarscommon = record
  43. allregvars, commonregvars, myregvars: tusedregvars;
  44. end;
  45. }
  46. procedure firstcomplex(p : tbinarynode);
  47. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  48. // procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  49. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  50. procedure location_force_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  51. procedure location_force_mem(list:TAsmList;var l:tlocation);
  52. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  53. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  54. procedure location_allocate_register(list:TAsmList;out l: tlocation;def: tdef;constant: boolean);
  55. { load a tlocation into a cgpara }
  56. procedure gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);
  57. { loads a cgpara into a tlocation; assumes that loc.loc is already
  58. initialised }
  59. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  60. { allocate registers for a tlocation; assumes that loc.loc is already
  61. set to LOC_CREGISTER/LOC_CFPUREGISTER/... }
  62. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation);
  63. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  64. function has_alias_name(pd:tprocdef;const s:string):boolean;
  65. procedure alloc_proc_symbol(pd: tprocdef);
  66. procedure gen_proc_symbol(list:TAsmList);
  67. procedure gen_proc_symbol_end(list:TAsmList);
  68. procedure gen_proc_entry_code(list:TAsmList);
  69. procedure gen_proc_exit_code(list:TAsmList);
  70. procedure gen_stack_check_size_para(list:TAsmList);
  71. procedure gen_stack_check_call(list:TAsmList);
  72. procedure gen_save_used_regs(list:TAsmList);
  73. procedure gen_restore_used_regs(list:TAsmList);
  74. procedure gen_entry_code(list:TAsmList);
  75. procedure gen_exit_code(list:TAsmList);
  76. procedure gen_load_para_value(list:TAsmList);
  77. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  78. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable;nested:boolean);
  79. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  80. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  81. { adds the regvars used in n and its children to rv.allregvars,
  82. those which were already in rv.allregvars to rv.commonregvars and
  83. uses rv.myregvars as scratch (so that two uses of the same regvar
  84. in a single tree to make it appear in commonregvars). Useful to
  85. find out which regvars are used in two different node trees
  86. (e.g. in the "else" and "then" path, or in various case blocks }
  87. // procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  88. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  89. { if the result of n is a LOC_C(..)REGISTER, try to find the corresponding }
  90. { loadn and change its location to a new register (= SSA). In case reload }
  91. { is true, transfer the old to the new register }
  92. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  93. {#
  94. Allocate the buffers for exception management and setjmp environment.
  95. Return a pointer to these buffers, send them to the utility routine
  96. so they are registered, and then call setjmp.
  97. Then compare the result of setjmp with 0, and if not equal
  98. to zero, then jump to exceptlabel.
  99. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  100. It is to note that this routine may be called *after* the stackframe of a
  101. routine has been called, therefore on machines where the stack cannot
  102. be modified, all temps should be allocated on the heap instead of the
  103. stack.
  104. }
  105. const
  106. EXCEPT_BUF_SIZE = 3*sizeof(pint);
  107. type
  108. texceptiontemps=record
  109. jmpbuf,
  110. envbuf,
  111. reasonbuf : treference;
  112. end;
  113. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  114. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  115. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  116. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  117. procedure gen_alloc_symtable(list:TAsmList;st:TSymtable);
  118. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  119. procedure location_free(list: TAsmList; const location : TLocation);
  120. function getprocalign : shortint;
  121. procedure gen_fpc_dummy(list : TAsmList);
  122. procedure InsertInterruptTable;
  123. implementation
  124. uses
  125. version,
  126. cutils,cclasses,
  127. globals,systems,verbose,export,
  128. ppu,defutil,
  129. procinfo,paramgr,fmodule,
  130. regvars,dbgbase,
  131. pass_1,pass_2,
  132. nbas,ncon,nld,nmem,nutils,ngenutil,
  133. tgobj,cgobj,cgcpu,hlcgobj,hlcgcpu
  134. {$ifdef powerpc}
  135. , cpupi
  136. {$endif}
  137. {$ifdef powerpc64}
  138. , cpupi
  139. {$endif}
  140. {$ifdef SUPPORT_MMX}
  141. , cgx86
  142. {$endif SUPPORT_MMX}
  143. ;
  144. {*****************************************************************************
  145. Misc Helpers
  146. *****************************************************************************}
  147. procedure location_free(list: TAsmList; const location : TLocation);
  148. begin
  149. case location.loc of
  150. LOC_VOID:
  151. ;
  152. LOC_REGISTER,
  153. LOC_CREGISTER:
  154. begin
  155. {$ifdef cpu64bitalu}
  156. { x86-64 system v abi:
  157. structs with up to 16 bytes are returned in registers }
  158. if location.size in [OS_128,OS_S128] then
  159. begin
  160. if getsupreg(location.register)<first_int_imreg then
  161. cg.ungetcpuregister(list,location.register);
  162. if getsupreg(location.registerhi)<first_int_imreg then
  163. cg.ungetcpuregister(list,location.registerhi);
  164. end
  165. {$else cpu64bitalu}
  166. if location.size in [OS_64,OS_S64] then
  167. begin
  168. if getsupreg(location.register64.reglo)<first_int_imreg then
  169. cg.ungetcpuregister(list,location.register64.reglo);
  170. if getsupreg(location.register64.reghi)<first_int_imreg then
  171. cg.ungetcpuregister(list,location.register64.reghi);
  172. end
  173. {$endif}
  174. else
  175. if getsupreg(location.register)<first_int_imreg then
  176. cg.ungetcpuregister(list,location.register);
  177. end;
  178. LOC_FPUREGISTER,
  179. LOC_CFPUREGISTER:
  180. begin
  181. if getsupreg(location.register)<first_fpu_imreg then
  182. cg.ungetcpuregister(list,location.register);
  183. end;
  184. LOC_MMREGISTER,
  185. LOC_CMMREGISTER :
  186. begin
  187. if getsupreg(location.register)<first_mm_imreg then
  188. cg.ungetcpuregister(list,location.register);
  189. end;
  190. LOC_REFERENCE,
  191. LOC_CREFERENCE :
  192. begin
  193. if paramanager.use_fixed_stack then
  194. location_freetemp(list,location);
  195. end;
  196. else
  197. internalerror(2004110211);
  198. end;
  199. end;
  200. procedure firstcomplex(p : tbinarynode);
  201. var
  202. fcl, fcr: longint;
  203. ncl, ncr: longint;
  204. begin
  205. { always calculate boolean AND and OR from left to right }
  206. if (p.nodetype in [orn,andn]) and
  207. is_boolean(p.left.resultdef) then
  208. begin
  209. if nf_swapped in p.flags then
  210. internalerror(200709253);
  211. end
  212. else
  213. begin
  214. fcl:=node_resources_fpu(p.left);
  215. fcr:=node_resources_fpu(p.right);
  216. ncl:=node_complexity(p.left);
  217. ncr:=node_complexity(p.right);
  218. { We swap left and right if
  219. a) right needs more floating point registers than left, and
  220. left needs more than 0 floating point registers (if it
  221. doesn't need any, swapping won't change the floating
  222. point register pressure)
  223. b) both left and right need an equal amount of floating
  224. point registers or right needs no floating point registers,
  225. and in addition right has a higher complexity than left
  226. (+- needs more integer registers, but not necessarily)
  227. }
  228. if ((fcr>fcl) and
  229. (fcl>0)) or
  230. (((fcr=fcl) or
  231. (fcr=0)) and
  232. (ncr>ncl)) then
  233. p.swapleftright
  234. end;
  235. end;
  236. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  237. {
  238. produces jumps to true respectively false labels using boolean expressions
  239. depending on whether the loading of regvars is currently being
  240. synchronized manually (such as in an if-node) or automatically (most of
  241. the other cases where this procedure is called), loadregvars can be
  242. "lr_load_regvars" or "lr_dont_load_regvars"
  243. }
  244. var
  245. opsize : tcgsize;
  246. storepos : tfileposinfo;
  247. tmpreg : tregister;
  248. begin
  249. if nf_error in p.flags then
  250. exit;
  251. storepos:=current_filepos;
  252. current_filepos:=p.fileinfo;
  253. if is_boolean(p.resultdef) then
  254. begin
  255. {$ifdef OLDREGVARS}
  256. if loadregvars = lr_load_regvars then
  257. load_all_regvars(list);
  258. {$endif OLDREGVARS}
  259. if is_constboolnode(p) then
  260. begin
  261. if Tordconstnode(p).value.uvalue<>0 then
  262. cg.a_jmp_always(list,current_procinfo.CurrTrueLabel)
  263. else
  264. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel)
  265. end
  266. else
  267. begin
  268. opsize:=def_cgsize(p.resultdef);
  269. case p.location.loc of
  270. LOC_SUBSETREG,LOC_CSUBSETREG,
  271. LOC_SUBSETREF,LOC_CSUBSETREF:
  272. begin
  273. tmpreg := cg.getintregister(list,OS_INT);
  274. cg.a_load_loc_reg(list,OS_INT,p.location,tmpreg);
  275. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,tmpreg,current_procinfo.CurrTrueLabel);
  276. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  277. end;
  278. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  279. begin
  280. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  281. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  282. end;
  283. LOC_JUMP:
  284. ;
  285. {$ifdef cpuflags}
  286. LOC_FLAGS :
  287. begin
  288. cg.a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  289. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  290. end;
  291. {$endif cpuflags}
  292. else
  293. begin
  294. printnode(output,p);
  295. internalerror(200308241);
  296. end;
  297. end;
  298. end;
  299. end
  300. else
  301. internalerror(200112305);
  302. current_filepos:=storepos;
  303. end;
  304. (*
  305. This code needs fixing. It is not safe to use rgint; on the m68000 it
  306. would be rgaddr.
  307. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  308. begin
  309. case t.loc of
  310. LOC_REGISTER:
  311. begin
  312. { can't be a regvar, since it would be LOC_CREGISTER then }
  313. exclude(regs,getsupreg(t.register));
  314. if t.register64.reghi<>NR_NO then
  315. exclude(regs,getsupreg(t.register64.reghi));
  316. end;
  317. LOC_CREFERENCE,LOC_REFERENCE:
  318. begin
  319. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  320. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  321. exclude(regs,getsupreg(t.reference.base));
  322. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  323. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  324. exclude(regs,getsupreg(t.reference.index));
  325. end;
  326. end;
  327. end;
  328. *)
  329. {*****************************************************************************
  330. EXCEPTION MANAGEMENT
  331. *****************************************************************************}
  332. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  333. var
  334. srsym : ttypesym;
  335. begin
  336. if jmp_buf_size=-1 then
  337. begin
  338. srsym:=search_system_type('JMP_BUF');
  339. jmp_buf_size:=srsym.typedef.size;
  340. jmp_buf_align:=srsym.typedef.alignment;
  341. end;
  342. tg.GetTemp(list,EXCEPT_BUF_SIZE,sizeof(pint),tt_persistent,t.envbuf);
  343. tg.GetTemp(list,jmp_buf_size,jmp_buf_align,tt_persistent,t.jmpbuf);
  344. tg.GetTemp(list,sizeof(pint),sizeof(pint),tt_persistent,t.reasonbuf);
  345. end;
  346. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  347. begin
  348. tg.Ungettemp(list,t.jmpbuf);
  349. tg.ungettemp(list,t.envbuf);
  350. tg.ungettemp(list,t.reasonbuf);
  351. end;
  352. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  353. var
  354. paraloc1,paraloc2,paraloc3 : tcgpara;
  355. begin
  356. paraloc1.init;
  357. paraloc2.init;
  358. paraloc3.init;
  359. paramanager.getintparaloc(pocall_default,1,paraloc1);
  360. paramanager.getintparaloc(pocall_default,2,paraloc2);
  361. paramanager.getintparaloc(pocall_default,3,paraloc3);
  362. cg.a_loadaddr_ref_cgpara(list,t.envbuf,paraloc3);
  363. cg.a_loadaddr_ref_cgpara(list,t.jmpbuf,paraloc2);
  364. { push type of exceptionframe }
  365. cg.a_load_const_cgpara(list,OS_S32,1,paraloc1);
  366. paramanager.freecgpara(list,paraloc3);
  367. paramanager.freecgpara(list,paraloc2);
  368. paramanager.freecgpara(list,paraloc1);
  369. cg.allocallcpuregisters(list);
  370. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR',false);
  371. cg.deallocallcpuregisters(list);
  372. paramanager.getintparaloc(pocall_default,1,paraloc1);
  373. cg.a_load_reg_cgpara(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  374. paramanager.freecgpara(list,paraloc1);
  375. cg.allocallcpuregisters(list);
  376. cg.a_call_name(list,'FPC_SETJMP',false);
  377. cg.deallocallcpuregisters(list);
  378. cg.alloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  379. cg.g_exception_reason_save(list, t.reasonbuf);
  380. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,cg.makeregsize(list,NR_FUNCTION_RESULT_REG,OS_S32),exceptlabel);
  381. cg.dealloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  382. paraloc1.done;
  383. paraloc2.done;
  384. paraloc3.done;
  385. end;
  386. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  387. begin
  388. cg.allocallcpuregisters(list);
  389. cg.a_call_name(list,'FPC_POPADDRSTACK',false);
  390. cg.deallocallcpuregisters(list);
  391. if not onlyfree then
  392. begin
  393. { g_exception_reason_load already allocates NR_FUNCTION_RESULT_REG }
  394. cg.g_exception_reason_load(list, t.reasonbuf);
  395. cg.a_cmp_const_reg_label(list,OS_INT,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  396. cg.a_reg_dealloc(list,NR_FUNCTION_RESULT_REG);
  397. end;
  398. end;
  399. {*****************************************************************************
  400. TLocation
  401. *****************************************************************************}
  402. {$ifndef cpu64bitalu}
  403. { 32-bit version }
  404. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  405. var
  406. hregister,
  407. hregisterhi : tregister;
  408. hreg64 : tregister64;
  409. hl : tasmlabel;
  410. oldloc : tlocation;
  411. const_location: boolean;
  412. begin
  413. oldloc:=l;
  414. if dst_size=OS_NO then
  415. internalerror(200309144);
  416. { handle transformations to 64bit separate }
  417. if dst_size in [OS_64,OS_S64] then
  418. begin
  419. if not (l.size in [OS_64,OS_S64]) then
  420. begin
  421. { load a smaller size to OS_64 }
  422. if l.loc=LOC_REGISTER then
  423. begin
  424. {$ifdef AVR}
  425. { on avr, we cannot change the size of a register
  426. due to the nature how register with size > OS8 are handled
  427. }
  428. hregister:=cg.getintregister(list,OS_32);
  429. {$else AVR}
  430. hregister:=cg.makeregsize(list,l.register64.reglo,OS_32);
  431. {$endif AVR}
  432. cg.a_load_reg_reg(list,l.size,OS_32,l.register64.reglo,hregister);
  433. end
  434. else
  435. hregister:=cg.getintregister(list,OS_32);
  436. { load value in low register }
  437. case l.loc of
  438. {$ifdef cpuflags}
  439. LOC_FLAGS :
  440. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  441. {$endif cpuflags}
  442. LOC_JUMP :
  443. begin
  444. cg.a_label(list,current_procinfo.CurrTrueLabel);
  445. cg.a_load_const_reg(list,OS_INT,1,hregister);
  446. current_asmdata.getjumplabel(hl);
  447. cg.a_jmp_always(list,hl);
  448. cg.a_label(list,current_procinfo.CurrFalseLabel);
  449. cg.a_load_const_reg(list,OS_INT,0,hregister);
  450. cg.a_label(list,hl);
  451. end;
  452. else
  453. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  454. end;
  455. { reset hi part, take care of the signed bit of the current value }
  456. hregisterhi:=cg.getintregister(list,OS_32);
  457. if (l.size in [OS_S8,OS_S16,OS_S32]) then
  458. begin
  459. if l.loc=LOC_CONSTANT then
  460. begin
  461. if (longint(l.value)<0) then
  462. cg.a_load_const_reg(list,OS_32,aint($ffffffff),hregisterhi)
  463. else
  464. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  465. end
  466. else
  467. begin
  468. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  469. hregisterhi);
  470. end;
  471. end
  472. else
  473. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  474. location_reset(l,LOC_REGISTER,dst_size);
  475. l.register64.reglo:=hregister;
  476. l.register64.reghi:=hregisterhi;
  477. end
  478. else
  479. begin
  480. { 64bit to 64bit }
  481. if ((l.loc=LOC_CREGISTER) and maybeconst) then
  482. begin
  483. hregister:=l.register64.reglo;
  484. hregisterhi:=l.register64.reghi;
  485. const_location := true;
  486. end
  487. else
  488. begin
  489. hregister:=cg.getintregister(list,OS_32);
  490. hregisterhi:=cg.getintregister(list,OS_32);
  491. const_location := false;
  492. end;
  493. hreg64.reglo:=hregister;
  494. hreg64.reghi:=hregisterhi;
  495. { load value in new register }
  496. cg64.a_load64_loc_reg(list,l,hreg64);
  497. if not const_location then
  498. location_reset(l,LOC_REGISTER,dst_size)
  499. else
  500. location_reset(l,LOC_CREGISTER,dst_size);
  501. l.register64.reglo:=hregister;
  502. l.register64.reghi:=hregisterhi;
  503. end;
  504. end
  505. else
  506. begin
  507. {Do not bother to recycle the existing register. The register
  508. allocator eliminates unnecessary moves, so it's not needed
  509. and trying to recycle registers can cause problems because
  510. the registers changes size and may need aditional constraints.
  511. Not if it's about LOC_CREGISTER's (JM)
  512. }
  513. const_location :=
  514. (maybeconst) and
  515. (l.loc = LOC_CREGISTER) and
  516. (TCGSize2Size[l.size] = TCGSize2Size[dst_size]) and
  517. ((l.size = dst_size) or
  518. (TCGSize2Size[l.size] = sizeof(aint)));
  519. if not const_location then
  520. hregister:=cg.getintregister(list,dst_size)
  521. else
  522. hregister := l.register;
  523. { load value in new register }
  524. case l.loc of
  525. {$ifdef cpuflags}
  526. LOC_FLAGS :
  527. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  528. {$endif cpuflags}
  529. LOC_JUMP :
  530. begin
  531. cg.a_label(list,current_procinfo.CurrTrueLabel);
  532. cg.a_load_const_reg(list,dst_size,1,hregister);
  533. current_asmdata.getjumplabel(hl);
  534. cg.a_jmp_always(list,hl);
  535. cg.a_label(list,current_procinfo.CurrFalseLabel);
  536. cg.a_load_const_reg(list,dst_size,0,hregister);
  537. cg.a_label(list,hl);
  538. end;
  539. else
  540. begin
  541. { load_loc_reg can only handle size >= l.size, when the
  542. new size is smaller then we need to adjust the size
  543. of the orignal and maybe recalculate l.register for i386 }
  544. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  545. begin
  546. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  547. l.register:=cg.makeregsize(list,l.register,dst_size);
  548. { for big endian systems, the reference's offset must }
  549. { be increased in this case, since they have the }
  550. { MSB first in memory and e.g. byte(word_var) should }
  551. { return the second byte in this case (JM) }
  552. if (target_info.endian = ENDIAN_BIG) and
  553. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  554. begin
  555. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  556. l.reference.alignment:=newalignment(l.reference.alignment,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  557. end;
  558. {$ifdef x86}
  559. if not (l.loc in [LOC_SUBSETREG,LOC_CSUBSETREG]) then
  560. l.size:=dst_size;
  561. {$endif x86}
  562. end;
  563. cg.a_load_loc_reg(list,dst_size,l,hregister);
  564. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size])
  565. {$ifdef x86}
  566. and (l.loc in [LOC_SUBSETREG,LOC_CSUBSETREG])
  567. {$endif x86}
  568. then
  569. l.size:=dst_size;
  570. end;
  571. end;
  572. if not const_location then
  573. location_reset(l,LOC_REGISTER,dst_size)
  574. else
  575. location_reset(l,LOC_CREGISTER,dst_size);
  576. l.register:=hregister;
  577. end;
  578. { Release temp when it was a reference }
  579. if oldloc.loc=LOC_REFERENCE then
  580. location_freetemp(list,oldloc);
  581. end;
  582. {$else not cpu64bitalu}
  583. { 64-bit version }
  584. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  585. var
  586. hregister : tregister;
  587. hl : tasmlabel;
  588. oldloc : tlocation;
  589. begin
  590. oldloc:=l;
  591. hregister:=cg.getintregister(list,dst_size);
  592. { load value in new register }
  593. case l.loc of
  594. {$ifdef cpuflags}
  595. LOC_FLAGS :
  596. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  597. {$endif cpuflags}
  598. LOC_JUMP :
  599. begin
  600. cg.a_label(list,current_procinfo.CurrTrueLabel);
  601. cg.a_load_const_reg(list,dst_size,1,hregister);
  602. current_asmdata.getjumplabel(hl);
  603. cg.a_jmp_always(list,hl);
  604. cg.a_label(list,current_procinfo.CurrFalseLabel);
  605. cg.a_load_const_reg(list,dst_size,0,hregister);
  606. cg.a_label(list,hl);
  607. end;
  608. else
  609. begin
  610. { load_loc_reg can only handle size >= l.size, when the
  611. new size is smaller then we need to adjust the size
  612. of the orignal and maybe recalculate l.register for i386 }
  613. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  614. begin
  615. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  616. l.register:=cg.makeregsize(list,l.register,dst_size);
  617. { for big endian systems, the reference's offset must }
  618. { be increased in this case, since they have the }
  619. { MSB first in memory and e.g. byte(word_var) should }
  620. { return the second byte in this case (JM) }
  621. if (target_info.endian = ENDIAN_BIG) and
  622. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  623. begin
  624. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  625. l.reference.alignment:=newalignment(l.reference.alignment,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  626. end;
  627. {$ifdef x86}
  628. l.size:=dst_size;
  629. {$endif x86}
  630. end;
  631. cg.a_load_loc_reg(list,dst_size,l,hregister);
  632. {$ifndef x86}
  633. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  634. l.size:=dst_size;
  635. {$endif not x86}
  636. end;
  637. end;
  638. if (l.loc <> LOC_CREGISTER) or
  639. not maybeconst then
  640. location_reset(l,LOC_REGISTER,dst_size)
  641. else
  642. location_reset(l,LOC_CREGISTER,dst_size);
  643. l.register:=hregister;
  644. { Release temp when it was a reference }
  645. if oldloc.loc=LOC_REFERENCE then
  646. location_freetemp(list,oldloc);
  647. end;
  648. {$endif not cpu64bitalu}
  649. procedure location_force_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  650. var
  651. reg : tregister;
  652. href : treference;
  653. begin
  654. if (l.loc<>LOC_FPUREGISTER) and
  655. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  656. begin
  657. { if it's in an mm register, store to memory first }
  658. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  659. begin
  660. tg.GetTemp(list,tcgsize2size[l.size],tcgsize2size[l.size],tt_normal,href);
  661. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,href,mms_movescalar);
  662. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  663. l.reference:=href;
  664. end;
  665. reg:=cg.getfpuregister(list,l.size);
  666. cg.a_loadfpu_loc_reg(list,l.size,l,reg);
  667. location_freetemp(list,l);
  668. location_reset(l,LOC_FPUREGISTER,l.size);
  669. l.register:=reg;
  670. end;
  671. end;
  672. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  673. var
  674. reg : tregister;
  675. href : treference;
  676. newsize : tcgsize;
  677. begin
  678. if (l.loc<>LOC_MMREGISTER) and
  679. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  680. begin
  681. { if it's in an fpu register, store to memory first }
  682. if (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  683. begin
  684. tg.GetTemp(list,tcgsize2size[l.size],tcgsize2size[l.size],tt_normal,href);
  685. cg.a_loadfpu_reg_ref(list,l.size,l.size,l.register,href);
  686. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  687. l.reference:=href;
  688. end;
  689. {$ifndef cpu64bitalu}
  690. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  691. (l.size in [OS_64,OS_S64]) then
  692. begin
  693. reg:=cg.getmmregister(list,OS_F64);
  694. cg64.a_loadmm_intreg64_reg(list,OS_F64,l.register64,reg);
  695. l.size:=OS_F64
  696. end
  697. else
  698. {$endif not cpu64bitalu}
  699. begin
  700. { on ARM, CFP values may be located in integer registers,
  701. and its second_int_to_real() also uses this routine to
  702. force integer (memory) values in an mmregister }
  703. if (l.size in [OS_32,OS_S32]) then
  704. newsize:=OS_F32
  705. else if (l.size in [OS_64,OS_S64]) then
  706. newsize:=OS_F64
  707. else
  708. newsize:=l.size;
  709. reg:=cg.getmmregister(list,newsize);
  710. cg.a_loadmm_loc_reg(list,newsize,l,reg,mms_movescalar);
  711. l.size:=newsize;
  712. end;
  713. location_freetemp(list,l);
  714. location_reset(l,LOC_MMREGISTER,l.size);
  715. l.register:=reg;
  716. end;
  717. end;
  718. procedure gen_loadfpu_loc_cgpara(list: TAsmList; const l: tlocation;const cgpara: tcgpara;locintsize: longint);
  719. var
  720. {$ifdef i386}
  721. href : treference;
  722. size : longint;
  723. {$endif i386}
  724. locsize : tcgsize;
  725. tmploc : tlocation;
  726. begin
  727. if not(l.size in [OS_32,OS_S32,OS_64,OS_S64,OS_128,OS_S128]) then
  728. locsize:=l.size
  729. else
  730. locsize:=int_float_cgsize(tcgsize2size[l.size]);
  731. {$ifdef i386}
  732. case l.loc of
  733. LOC_FPUREGISTER,
  734. LOC_CFPUREGISTER:
  735. begin
  736. case cgpara.location^.loc of
  737. LOC_REFERENCE:
  738. begin
  739. size:=align(locintsize,cgpara.alignment);
  740. if (not paramanager.use_fixed_stack) and
  741. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  742. begin
  743. cg.g_stackpointer_alloc(list,size);
  744. reference_reset_base(href,NR_STACK_POINTER_REG,0,sizeof(pint));
  745. end
  746. else
  747. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  748. cg.a_loadfpu_reg_ref(list,locsize,locsize,l.register,href);
  749. end;
  750. LOC_FPUREGISTER:
  751. begin
  752. cg.a_loadfpu_reg_reg(list,locsize,cgpara.location^.size,l.register,cgpara.location^.register);
  753. end;
  754. { can happen if a record with only 1 "single field" is
  755. returned in a floating point register and then is directly
  756. passed to a regcall parameter }
  757. LOC_REGISTER:
  758. begin
  759. tmploc:=l;
  760. location_force_mem(list,tmploc);
  761. case locsize of
  762. OS_F32:
  763. tmploc.size:=OS_32;
  764. OS_F64:
  765. tmploc.size:=OS_64;
  766. else
  767. internalerror(2010053116);
  768. end;
  769. cg.a_load_loc_cgpara(list,tmploc,cgpara);
  770. location_freetemp(list,tmploc);
  771. end
  772. else
  773. internalerror(2010053003);
  774. end;
  775. end;
  776. LOC_MMREGISTER,
  777. LOC_CMMREGISTER:
  778. begin
  779. case cgpara.location^.loc of
  780. LOC_REFERENCE:
  781. begin
  782. { can't use TCGSize2Size[l.size], because the size of an
  783. 80 bit extended parameter can be either 10 or 12 bytes }
  784. size:=align(locintsize,cgpara.alignment);
  785. if (not paramanager.use_fixed_stack) and
  786. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  787. begin
  788. cg.g_stackpointer_alloc(list,size);
  789. reference_reset_base(href,NR_STACK_POINTER_REG,0,sizeof(pint));
  790. end
  791. else
  792. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  793. cg.a_loadmm_reg_ref(list,locsize,locsize,l.register,href,mms_movescalar);
  794. end;
  795. LOC_FPUREGISTER:
  796. begin
  797. tmploc:=l;
  798. location_force_mem(list,tmploc);
  799. cg.a_loadfpu_ref_cgpara(list,tmploc.size,tmploc.reference,cgpara);
  800. location_freetemp(list,tmploc);
  801. end;
  802. else
  803. internalerror(2010053004);
  804. end;
  805. end;
  806. LOC_REFERENCE,
  807. LOC_CREFERENCE :
  808. begin
  809. case cgpara.location^.loc of
  810. LOC_REFERENCE:
  811. begin
  812. size:=align(locintsize,cgpara.alignment);
  813. if (not paramanager.use_fixed_stack) and
  814. (cgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  815. cg.a_load_ref_cgpara(list,locsize,l.reference,cgpara)
  816. else
  817. begin
  818. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  819. cg.g_concatcopy(list,l.reference,href,size);
  820. end;
  821. end;
  822. LOC_FPUREGISTER:
  823. begin
  824. cg.a_loadfpu_ref_cgpara(list,locsize,l.reference,cgpara);
  825. end;
  826. else
  827. internalerror(2010053005);
  828. end;
  829. end;
  830. else
  831. internalerror(2002042430);
  832. end;
  833. {$else i386}
  834. case l.loc of
  835. LOC_MMREGISTER,
  836. LOC_CMMREGISTER:
  837. case cgpara.location^.loc of
  838. LOC_REFERENCE,
  839. LOC_CREFERENCE,
  840. LOC_MMREGISTER,
  841. LOC_CMMREGISTER,
  842. LOC_REGISTER,
  843. LOC_CREGISTER :
  844. cg.a_loadmm_reg_cgpara(list,locsize,l.register,cgpara,mms_movescalar);
  845. LOC_FPUREGISTER,
  846. LOC_CFPUREGISTER:
  847. begin
  848. tmploc:=l;
  849. location_force_fpureg(list,tmploc,false);
  850. cg.a_loadfpu_reg_cgpara(list,tmploc.size,tmploc.register,cgpara);
  851. end;
  852. else
  853. internalerror(200204249);
  854. end;
  855. LOC_FPUREGISTER,
  856. LOC_CFPUREGISTER:
  857. case cgpara.location^.loc of
  858. LOC_MMREGISTER,
  859. LOC_CMMREGISTER:
  860. begin
  861. tmploc:=l;
  862. location_force_mmregscalar(list,tmploc,false);
  863. cg.a_loadmm_reg_cgpara(list,tmploc.size,tmploc.register,cgpara,mms_movescalar);
  864. end;
  865. { Some targets pass floats in normal registers }
  866. LOC_REGISTER,
  867. LOC_CREGISTER,
  868. LOC_REFERENCE,
  869. LOC_CREFERENCE,
  870. LOC_FPUREGISTER,
  871. LOC_CFPUREGISTER:
  872. cg.a_loadfpu_reg_cgpara(list,locsize,l.register,cgpara);
  873. else
  874. internalerror(2002042433);
  875. end;
  876. LOC_REFERENCE,
  877. LOC_CREFERENCE:
  878. case cgpara.location^.loc of
  879. LOC_MMREGISTER,
  880. LOC_CMMREGISTER:
  881. cg.a_loadmm_ref_cgpara(list,locsize,l.reference,cgpara,mms_movescalar);
  882. { Some targets pass floats in normal registers }
  883. LOC_REGISTER,
  884. LOC_CREGISTER,
  885. LOC_REFERENCE,
  886. LOC_CREFERENCE,
  887. LOC_FPUREGISTER,
  888. LOC_CFPUREGISTER:
  889. cg.a_loadfpu_ref_cgpara(list,locsize,l.reference,cgpara);
  890. else
  891. internalerror(2002042431);
  892. end;
  893. LOC_REGISTER,
  894. LOC_CREGISTER :
  895. begin
  896. {$ifndef cpu64bitalu}
  897. { Only a_load_ref_cgpara supports multiple locations, when the
  898. value is still a const or in a register then write it
  899. to a reference first. This situation can be triggered
  900. by typecasting an int64 constant to a record of 8 bytes }
  901. if locsize = OS_F64 then
  902. begin
  903. tmploc:=l;
  904. location_force_mem(list,tmploc);
  905. cg.a_load_loc_cgpara(list,tmploc,cgpara);
  906. location_freetemp(list,tmploc);
  907. end
  908. else
  909. {$endif not cpu64bitalu}
  910. cg.a_load_loc_cgpara(list,l,cgpara);
  911. end;
  912. else
  913. internalerror(2002042432);
  914. end;
  915. {$endif i386}
  916. end;
  917. procedure gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);
  918. {$ifndef cpu64bitalu}
  919. var
  920. tmploc: tlocation;
  921. {$endif not cpu64bitalu}
  922. begin
  923. { Handle Floating point types differently
  924. This doesn't depend on emulator settings, emulator settings should
  925. be handled by cpupara }
  926. if (vardef.typ=floatdef) or
  927. { some ABIs return certain records in an fpu register }
  928. (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) or
  929. (assigned(cgpara.location) and
  930. (cgpara.Location^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) then
  931. begin
  932. gen_loadfpu_loc_cgpara(list,l,cgpara,vardef.size);
  933. exit;
  934. end;
  935. case l.loc of
  936. LOC_CONSTANT,
  937. LOC_REGISTER,
  938. LOC_CREGISTER,
  939. LOC_REFERENCE,
  940. LOC_CREFERENCE :
  941. begin
  942. {$ifndef cpu64bitalu}
  943. { use cg64 only for int64, not for 8 byte records }
  944. if is_64bit(vardef) then
  945. cg64.a_load64_loc_cgpara(list,l,cgpara)
  946. else
  947. {$endif not cpu64bitalu}
  948. begin
  949. {$ifndef cpu64bitalu}
  950. { Only a_load_ref_cgpara supports multiple locations, when the
  951. value is still a const or in a register then write it
  952. to a reference first. This situation can be triggered
  953. by typecasting an int64 constant to a record of 8 bytes }
  954. if l.size in [OS_64,OS_S64] then
  955. begin
  956. tmploc:=l;
  957. location_force_mem(list,tmploc);
  958. cg.a_load_loc_cgpara(list,tmploc,cgpara);
  959. { do not free the tmploc in case the original value was
  960. already in memory, because the caller (ncgcal) will then
  961. free it again later }
  962. if not(l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  963. location_freetemp(list,tmploc);
  964. end
  965. else
  966. {$endif not cpu64bitalu}
  967. cg.a_load_loc_cgpara(list,l,cgpara);
  968. end;
  969. end;
  970. LOC_MMREGISTER,
  971. LOC_CMMREGISTER:
  972. begin
  973. case l.size of
  974. OS_F32,
  975. OS_F64:
  976. cg.a_loadmm_loc_cgpara(list,l,cgpara,mms_movescalar);
  977. else
  978. cg.a_loadmm_loc_cgpara(list,l,cgpara,nil);
  979. end;
  980. end;
  981. {$ifdef SUPPORT_MMX}
  982. LOC_MMXREGISTER,
  983. LOC_CMMXREGISTER:
  984. cg.a_loadmm_reg_cgpara(list,OS_M64,l.register,cgpara,nil);
  985. {$endif SUPPORT_MMX}
  986. else
  987. internalerror(200204241);
  988. end;
  989. end;
  990. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  991. var
  992. tmpreg: tregister;
  993. begin
  994. if (setbase<>0) then
  995. begin
  996. if not(l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  997. internalerror(2007091502);
  998. { subtract the setbase }
  999. case l.loc of
  1000. LOC_CREGISTER:
  1001. begin
  1002. tmpreg := cg.getintregister(list,l.size);
  1003. cg.a_op_const_reg_reg(list,OP_SUB,l.size,setbase,l.register,tmpreg);
  1004. l.loc:=LOC_REGISTER;
  1005. l.register:=tmpreg;
  1006. end;
  1007. LOC_REGISTER:
  1008. begin
  1009. cg.a_op_const_reg(list,OP_SUB,l.size,setbase,l.register);
  1010. end;
  1011. end;
  1012. end;
  1013. end;
  1014. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  1015. var
  1016. reg : tregister;
  1017. begin
  1018. if (l.loc<>LOC_MMREGISTER) and
  1019. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  1020. begin
  1021. reg:=cg.getmmregister(list,OS_VECTOR);
  1022. cg.a_loadmm_loc_reg(list,OS_VECTOR,l,reg,nil);
  1023. location_freetemp(list,l);
  1024. location_reset(l,LOC_MMREGISTER,OS_VECTOR);
  1025. l.register:=reg;
  1026. end;
  1027. end;
  1028. procedure location_allocate_register(list: TAsmList;out l: tlocation;def: tdef;constant: boolean);
  1029. begin
  1030. l.size:=def_cgsize(def);
  1031. if (def.typ=floatdef) and
  1032. not(cs_fp_emulation in current_settings.moduleswitches) then
  1033. begin
  1034. if use_vectorfpu(def) then
  1035. begin
  1036. if constant then
  1037. location_reset(l,LOC_CMMREGISTER,l.size)
  1038. else
  1039. location_reset(l,LOC_MMREGISTER,l.size);
  1040. l.register:=cg.getmmregister(list,l.size);
  1041. end
  1042. else
  1043. begin
  1044. if constant then
  1045. location_reset(l,LOC_CFPUREGISTER,l.size)
  1046. else
  1047. location_reset(l,LOC_FPUREGISTER,l.size);
  1048. l.register:=cg.getfpuregister(list,l.size);
  1049. end;
  1050. end
  1051. else
  1052. begin
  1053. if constant then
  1054. location_reset(l,LOC_CREGISTER,l.size)
  1055. else
  1056. location_reset(l,LOC_REGISTER,l.size);
  1057. {$ifndef cpu64bitalu}
  1058. if l.size in [OS_64,OS_S64,OS_F64] then
  1059. begin
  1060. l.register64.reglo:=cg.getintregister(list,OS_32);
  1061. l.register64.reghi:=cg.getintregister(list,OS_32);
  1062. end
  1063. else
  1064. {$endif not cpu64bitalu}
  1065. l.register:=cg.getintregister(list,l.size);
  1066. end;
  1067. end;
  1068. procedure location_force_mem(list:TAsmList;var l:tlocation);
  1069. var
  1070. r : treference;
  1071. begin
  1072. case l.loc of
  1073. LOC_FPUREGISTER,
  1074. LOC_CFPUREGISTER :
  1075. begin
  1076. tg.GetTemp(list,TCGSize2Size[l.size],TCGSize2Size[l.size],tt_normal,r);
  1077. cg.a_loadfpu_reg_ref(list,l.size,l.size,l.register,r);
  1078. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1079. l.reference:=r;
  1080. end;
  1081. LOC_MMREGISTER,
  1082. LOC_CMMREGISTER:
  1083. begin
  1084. tg.GetTemp(list,TCGSize2Size[l.size],TCGSize2Size[l.size],tt_normal,r);
  1085. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  1086. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1087. l.reference:=r;
  1088. end;
  1089. LOC_CONSTANT,
  1090. LOC_REGISTER,
  1091. LOC_CREGISTER :
  1092. begin
  1093. tg.GetTemp(list,TCGSize2Size[l.size],TCGSize2Size[l.size],tt_normal,r);
  1094. {$ifndef cpu64bitalu}
  1095. if l.size in [OS_64,OS_S64] then
  1096. cg64.a_load64_loc_ref(list,l,r)
  1097. else
  1098. {$endif not cpu64bitalu}
  1099. cg.a_load_loc_ref(list,l.size,l,r);
  1100. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1101. l.reference:=r;
  1102. end;
  1103. LOC_SUBSETREG,
  1104. LOC_CSUBSETREG,
  1105. LOC_SUBSETREF,
  1106. LOC_CSUBSETREF:
  1107. begin
  1108. tg.GetTemp(list,TCGSize2Size[l.size],TCGSize2Size[l.size],tt_normal,r);
  1109. cg.a_load_loc_ref(list,l.size,l,r);
  1110. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1111. l.reference:=r;
  1112. end;
  1113. LOC_CREFERENCE,
  1114. LOC_REFERENCE : ;
  1115. else
  1116. internalerror(200203219);
  1117. end;
  1118. end;
  1119. {****************************************************************************
  1120. Init/Finalize Code
  1121. ****************************************************************************}
  1122. procedure copyvalueparas(p:TObject;arg:pointer);
  1123. var
  1124. href : treference;
  1125. hreg : tregister;
  1126. list : TAsmList;
  1127. hsym : tparavarsym;
  1128. l : longint;
  1129. localcopyloc : tlocation;
  1130. begin
  1131. list:=TAsmList(arg);
  1132. if (tsym(p).typ=paravarsym) and
  1133. (tparavarsym(p).varspez=vs_value) and
  1134. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  1135. begin
  1136. { we have no idea about the alignment at the caller side }
  1137. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  1138. if is_open_array(tparavarsym(p).vardef) or
  1139. is_array_of_const(tparavarsym(p).vardef) then
  1140. begin
  1141. { cdecl functions don't have a high pointer so it is not possible to generate
  1142. a local copy }
  1143. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  1144. begin
  1145. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  1146. if not assigned(hsym) then
  1147. internalerror(200306061);
  1148. hreg:=cg.getaddressregister(list);
  1149. if not is_packed_array(tparavarsym(p).vardef) then
  1150. cg.g_copyvaluepara_openarray(list,href,hsym.initialloc,tarraydef(tparavarsym(p).vardef).elesize,hreg)
  1151. else
  1152. internalerror(2006080401);
  1153. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  1154. cg.a_load_reg_loc(list,OS_ADDR,hreg,tparavarsym(p).initialloc);
  1155. end;
  1156. end
  1157. else
  1158. begin
  1159. { Allocate space for the local copy }
  1160. l:=tparavarsym(p).getsize;
  1161. localcopyloc.loc:=LOC_REFERENCE;
  1162. localcopyloc.size:=int_cgsize(l);
  1163. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  1164. { Copy data }
  1165. if is_shortstring(tparavarsym(p).vardef) then
  1166. begin
  1167. { this code is only executed before the code for the body and the entry/exit code is generated
  1168. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  1169. }
  1170. include(current_procinfo.flags,pi_do_call);
  1171. cg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef).len)
  1172. end
  1173. else if tparavarsym(p).vardef.typ = variantdef then
  1174. begin
  1175. { this code is only executed before the code for the body and the entry/exit code is generated
  1176. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  1177. }
  1178. include(current_procinfo.flags,pi_do_call);
  1179. cg.g_copyvariant(list,href,localcopyloc.reference)
  1180. end
  1181. else
  1182. begin
  1183. { pass proper alignment info }
  1184. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  1185. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vardef.size);
  1186. end;
  1187. { update localloc of varsym }
  1188. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  1189. tparavarsym(p).localloc:=localcopyloc;
  1190. tparavarsym(p).initialloc:=localcopyloc;
  1191. end;
  1192. end;
  1193. end;
  1194. const
  1195. {$ifdef cpu64bitalu}
  1196. trashintvalues: array[0..nroftrashvalues-1] of aint = ($5555555555555555,aint($AAAAAAAAAAAAAAAA),aint($EFEFEFEFEFEFEFEF),0);
  1197. {$endif cpu64bitalu}
  1198. {$ifdef cpu32bitalu}
  1199. trashintvalues: array[0..nroftrashvalues-1] of aint = ($55555555,aint($AAAAAAAA),aint($EFEFEFEF),0);
  1200. {$endif cpu32bitalu}
  1201. {$ifdef cpu8bitalu}
  1202. trashintvalues: array[0..nroftrashvalues-1] of aint = ($55,aint($AA),aint($EF),0);
  1203. {$endif cpu8bitalu}
  1204. procedure trash_reference(list: TAsmList; const ref: treference; size: aint);
  1205. var
  1206. countreg, valuereg: tregister;
  1207. hl: tasmlabel;
  1208. trashintval: aint;
  1209. tmpref: treference;
  1210. begin
  1211. trashintval := trashintvalues[localvartrashing];
  1212. case size of
  1213. 0: ; { empty record }
  1214. 1: cg.a_load_const_ref(list,OS_8,byte(trashintval),ref);
  1215. 2: cg.a_load_const_ref(list,OS_16,word(trashintval),ref);
  1216. 4: cg.a_load_const_ref(list,OS_32,longint(trashintval),ref);
  1217. {$ifdef cpu64bitalu}
  1218. 8: cg.a_load_const_ref(list,OS_64,int64(trashintval),ref);
  1219. {$endif cpu64bitalu}
  1220. else
  1221. begin
  1222. countreg := cg.getintregister(list,OS_ADDR);
  1223. valuereg := cg.getintregister(list,OS_8);
  1224. cg.a_load_const_reg(list,OS_INT,size,countreg);
  1225. cg.a_load_const_reg(list,OS_8,byte(trashintval),valuereg);
  1226. current_asmdata.getjumplabel(hl);
  1227. tmpref := ref;
  1228. if (tmpref.index <> NR_NO) then
  1229. internalerror(200607201);
  1230. tmpref.index := countreg;
  1231. dec(tmpref.offset);
  1232. cg.a_label(list,hl);
  1233. cg.a_load_reg_ref(list,OS_8,OS_8,valuereg,tmpref);
  1234. cg.a_op_const_reg(list,OP_SUB,OS_INT,1,countreg);
  1235. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,countreg,hl);
  1236. cg.a_reg_sync(list,tmpref.base);
  1237. cg.a_reg_sync(list,valuereg);
  1238. end;
  1239. end;
  1240. end;
  1241. { trash contents of local variables or parameters (function result) }
  1242. procedure trash_variable(p:TObject;arg:pointer);
  1243. var
  1244. trashintval: aint;
  1245. list: TAsmList absolute arg;
  1246. begin
  1247. if ((tsym(p).typ=localvarsym) or
  1248. ((tsym(p).typ=paravarsym) and
  1249. (vo_is_funcret in tparavarsym(p).varoptions))) and
  1250. not(is_managed_type(tabstractnormalvarsym(p).vardef)) and
  1251. not(assigned(tabstractnormalvarsym(p).defaultconstsym)) then
  1252. begin
  1253. trashintval := trashintvalues[localvartrashing];
  1254. case tabstractnormalvarsym(p).initialloc.loc of
  1255. LOC_CREGISTER :
  1256. {$ifopt q+}
  1257. {$define overflowon}
  1258. {$q-}
  1259. {$endif}
  1260. begin
  1261. { avoid problems with broken x86 shifts }
  1262. case tcgsize2size[tabstractnormalvarsym(p).initialloc.size] of
  1263. 1: cg.a_load_const_reg(list,OS_8,byte(trashintval),tabstractnormalvarsym(p).initialloc.register);
  1264. 2: cg.a_load_const_reg(list,OS_16,word(trashintval),tabstractnormalvarsym(p).initialloc.register);
  1265. 4: cg.a_load_const_reg(list,OS_32,longint(trashintval),tabstractnormalvarsym(p).initialloc.register);
  1266. 8:
  1267. begin
  1268. {$ifdef cpu64bitalu}
  1269. cg.a_load_const_reg(list,OS_64,aint(trashintval),tabstractnormalvarsym(p).initialloc.register);
  1270. {$else}
  1271. cg64.a_load64_const_reg(list,int64(trashintval) shl 32 or int64(trashintval),tabstractnormalvarsym(p).initialloc.register64);
  1272. {$endif}
  1273. end;
  1274. else
  1275. internalerror(2010060801);
  1276. end;
  1277. end;
  1278. {$ifdef overflowon}
  1279. {$undef overflowon}
  1280. {$q+}
  1281. {$endif}
  1282. LOC_REFERENCE :
  1283. begin
  1284. if ((tsym(p).typ=localvarsym) and
  1285. not(vo_is_funcret in tabstractvarsym(p).varoptions)) or
  1286. not is_shortstring(tabstractnormalvarsym(p).vardef) then
  1287. trash_reference(list,tabstractnormalvarsym(p).initialloc.reference,
  1288. tlocalvarsym(p).getsize)
  1289. else
  1290. { may be an open string, even if is_open_string() returns }
  1291. { false (for some helpers in the system unit) }
  1292. { an open string has at least size 2 }
  1293. trash_reference(list,tabstractnormalvarsym(p).initialloc.reference,
  1294. 2);
  1295. end;
  1296. LOC_CMMREGISTER :
  1297. ;
  1298. LOC_CFPUREGISTER :
  1299. ;
  1300. else
  1301. internalerror(200410124);
  1302. end;
  1303. end;
  1304. end;
  1305. { generates the code for incrementing the reference count of parameters and
  1306. initialize out parameters }
  1307. procedure init_paras(p:TObject;arg:pointer);
  1308. var
  1309. href : treference;
  1310. hsym : tparavarsym;
  1311. eldef : tdef;
  1312. list : TAsmList;
  1313. needs_inittable,
  1314. do_trashing : boolean;
  1315. begin
  1316. list:=TAsmList(arg);
  1317. if (tsym(p).typ=paravarsym) then
  1318. begin
  1319. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  1320. do_trashing:=
  1321. (localvartrashing <> -1) and
  1322. (not assigned(tparavarsym(p).defaultconstsym)) and
  1323. not needs_inittable;
  1324. case tparavarsym(p).varspez of
  1325. vs_value :
  1326. if needs_inittable then
  1327. begin
  1328. { variants are already handled by the call to fpc_variant_copy_overwrite if
  1329. they are passed by reference }
  1330. if not((tparavarsym(p).vardef.typ=variantdef) and
  1331. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  1332. begin
  1333. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  1334. if is_open_array(tparavarsym(p).vardef) then
  1335. begin
  1336. { open arrays do not contain correct element count in their rtti,
  1337. the actual count must be passed separately. }
  1338. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  1339. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  1340. if not assigned(hsym) then
  1341. internalerror(201003031);
  1342. cg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'FPC_ADDREF_ARRAY');
  1343. end
  1344. else
  1345. cg.g_incrrefcount(list,tparavarsym(p).vardef,href);
  1346. end;
  1347. end;
  1348. vs_out :
  1349. begin
  1350. if needs_inittable or
  1351. do_trashing then
  1352. begin
  1353. { we have no idea about the alignment at the callee side,
  1354. and the user also cannot specify "unaligned" here, so
  1355. assume worst case }
  1356. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  1357. if do_trashing and
  1358. { needs separate implementation to trash open arrays }
  1359. { since their size is only known at run time }
  1360. not is_special_array(tparavarsym(p).vardef) then
  1361. { may be an open string, even if is_open_string() returns }
  1362. { false (for some helpers in the system unit) }
  1363. if not is_shortstring(tparavarsym(p).vardef) then
  1364. trash_reference(list,href,tparavarsym(p).vardef.size)
  1365. else
  1366. trash_reference(list,href,2);
  1367. if needs_inittable then
  1368. begin
  1369. if is_open_array(tparavarsym(p).vardef) then
  1370. begin
  1371. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  1372. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  1373. if not assigned(hsym) then
  1374. internalerror(201103033);
  1375. cg.g_array_rtti_helper(list,eldef,href,hsym.initialloc,'FPC_INITIALIZE_ARRAY');
  1376. end
  1377. else
  1378. cg.g_initialize(list,tparavarsym(p).vardef,href);
  1379. end;
  1380. end;
  1381. end;
  1382. else if do_trashing and
  1383. ([vo_is_funcret,vo_is_hidden_para] * tparavarsym(p).varoptions = [vo_is_funcret,vo_is_hidden_para]) then
  1384. begin
  1385. { should always have standard alignment. If a function is assigned
  1386. to a non-aligned variable, the optimisation to pass this variable
  1387. directly as hidden function result must/cannot be performed
  1388. (see tcallnode.funcret_can_be_reused)
  1389. }
  1390. hlcg.location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,
  1391. used_align(tparavarsym(p).vardef.alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax));
  1392. { may be an open string, even if is_open_string() returns }
  1393. { false (for some helpers in the system unit) }
  1394. if not is_shortstring(tparavarsym(p).vardef) then
  1395. trash_reference(list,href,tparavarsym(p).vardef.size)
  1396. else
  1397. { an open string has at least size 2 }
  1398. trash_reference(list,href,2);
  1399. end
  1400. end;
  1401. end;
  1402. end;
  1403. procedure gen_alloc_regloc(list:TAsmList;var loc: tlocation);
  1404. begin
  1405. case loc.loc of
  1406. LOC_CREGISTER:
  1407. begin
  1408. {$ifndef cpu64bitalu}
  1409. if loc.size in [OS_64,OS_S64] then
  1410. begin
  1411. loc.register64.reglo:=cg.getintregister(list,OS_32);
  1412. loc.register64.reghi:=cg.getintregister(list,OS_32);
  1413. end
  1414. else
  1415. {$endif cpu64bitalu}
  1416. loc.register:=cg.getintregister(list,loc.size);
  1417. end;
  1418. LOC_CFPUREGISTER:
  1419. begin
  1420. loc.register:=cg.getfpuregister(list,loc.size);
  1421. end;
  1422. LOC_CMMREGISTER:
  1423. begin
  1424. loc.register:=cg.getmmregister(list,loc.size);
  1425. end;
  1426. end;
  1427. end;
  1428. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym; allocreg: boolean);
  1429. begin
  1430. if allocreg then
  1431. gen_alloc_regloc(list,sym.initialloc);
  1432. if (pi_has_label in current_procinfo.flags) then
  1433. begin
  1434. { Allocate register already, to prevent first allocation to be
  1435. inside a loop }
  1436. {$ifndef cpu64bitalu}
  1437. if sym.initialloc.size in [OS_64,OS_S64] then
  1438. begin
  1439. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  1440. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  1441. end
  1442. else
  1443. {$endif not cpu64bitalu}
  1444. cg.a_reg_sync(list,sym.initialloc.register);
  1445. end;
  1446. sym.localloc:=sym.initialloc;
  1447. end;
  1448. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  1449. procedure unget_para(const paraloc:TCGParaLocation);
  1450. begin
  1451. case paraloc.loc of
  1452. LOC_REGISTER :
  1453. begin
  1454. if getsupreg(paraloc.register)<first_int_imreg then
  1455. cg.ungetcpuregister(list,paraloc.register);
  1456. end;
  1457. LOC_MMREGISTER :
  1458. begin
  1459. if getsupreg(paraloc.register)<first_mm_imreg then
  1460. cg.ungetcpuregister(list,paraloc.register);
  1461. end;
  1462. LOC_FPUREGISTER :
  1463. begin
  1464. if getsupreg(paraloc.register)<first_fpu_imreg then
  1465. cg.ungetcpuregister(list,paraloc.register);
  1466. end;
  1467. end;
  1468. end;
  1469. var
  1470. paraloc : pcgparalocation;
  1471. href : treference;
  1472. sizeleft : aint;
  1473. {$if defined(sparc) or defined(arm)}
  1474. tempref : treference;
  1475. {$endif sparc}
  1476. {$ifndef cpu64bitalu}
  1477. reg64: tregister64;
  1478. {$endif not cpu64bitalu}
  1479. begin
  1480. paraloc:=para.location;
  1481. if not assigned(paraloc) then
  1482. internalerror(200408203);
  1483. { skip e.g. empty records }
  1484. if (paraloc^.loc = LOC_VOID) then
  1485. exit;
  1486. case destloc.loc of
  1487. LOC_REFERENCE :
  1488. begin
  1489. { If the parameter location is reused we don't need to copy
  1490. anything }
  1491. if not reusepara then
  1492. begin
  1493. href:=destloc.reference;
  1494. sizeleft:=para.intsize;
  1495. while assigned(paraloc) do
  1496. begin
  1497. if (paraloc^.size=OS_NO) then
  1498. begin
  1499. { Can only be a reference that contains the rest
  1500. of the parameter }
  1501. if (paraloc^.loc<>LOC_REFERENCE) or
  1502. assigned(paraloc^.next) then
  1503. internalerror(2005013010);
  1504. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  1505. inc(href.offset,sizeleft);
  1506. sizeleft:=0;
  1507. end
  1508. else
  1509. begin
  1510. cg.a_load_cgparaloc_ref(list,paraloc^,href,tcgsize2size[paraloc^.size],destloc.reference.alignment);
  1511. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1512. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1513. end;
  1514. unget_para(paraloc^);
  1515. paraloc:=paraloc^.next;
  1516. end;
  1517. end;
  1518. end;
  1519. LOC_REGISTER,
  1520. LOC_CREGISTER :
  1521. begin
  1522. {$ifndef cpu64bitalu}
  1523. if (para.size in [OS_64,OS_S64,OS_F64]) and
  1524. (is_64bit(vardef) or
  1525. { in case of fpu emulation, or abi's that pass fpu values
  1526. via integer registers }
  1527. (vardef.typ=floatdef)) then
  1528. begin
  1529. case paraloc^.loc of
  1530. LOC_REGISTER:
  1531. begin
  1532. if not assigned(paraloc^.next) then
  1533. internalerror(200410104);
  1534. if (target_info.endian=ENDIAN_BIG) then
  1535. begin
  1536. { paraloc^ -> high
  1537. paraloc^.next -> low }
  1538. unget_para(paraloc^);
  1539. gen_alloc_regloc(list,destloc);
  1540. { reg->reg, alignment is irrelevant }
  1541. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reghi,4);
  1542. unget_para(paraloc^.next^);
  1543. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reglo,4);
  1544. end
  1545. else
  1546. begin
  1547. { paraloc^ -> low
  1548. paraloc^.next -> high }
  1549. unget_para(paraloc^);
  1550. gen_alloc_regloc(list,destloc);
  1551. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^,destloc.register64.reglo,4);
  1552. unget_para(paraloc^.next^);
  1553. cg.a_load_cgparaloc_anyreg(list,OS_32,paraloc^.next^,destloc.register64.reghi,4);
  1554. end;
  1555. end;
  1556. LOC_REFERENCE:
  1557. begin
  1558. gen_alloc_regloc(list,destloc);
  1559. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,para.alignment);
  1560. cg64.a_load64_ref_reg(list,href,destloc.register64);
  1561. unget_para(paraloc^);
  1562. end;
  1563. else
  1564. internalerror(2005101501);
  1565. end
  1566. end
  1567. else
  1568. {$endif not cpu64bitalu}
  1569. begin
  1570. if assigned(paraloc^.next) then
  1571. internalerror(200410105);
  1572. unget_para(paraloc^);
  1573. gen_alloc_regloc(list,destloc);
  1574. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,sizeof(aint));
  1575. end;
  1576. end;
  1577. LOC_FPUREGISTER,
  1578. LOC_CFPUREGISTER :
  1579. begin
  1580. {$if defined(sparc) or defined(arm)}
  1581. { Arm and Sparc passes floats in int registers, when loading to fpu register
  1582. we need a temp }
  1583. sizeleft := TCGSize2Size[destloc.size];
  1584. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  1585. href:=tempref;
  1586. while assigned(paraloc) do
  1587. begin
  1588. unget_para(paraloc^);
  1589. cg.a_load_cgparaloc_ref(list,paraloc^,href,sizeleft,destloc.reference.alignment);
  1590. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1591. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1592. paraloc:=paraloc^.next;
  1593. end;
  1594. gen_alloc_regloc(list,destloc);
  1595. cg.a_loadfpu_ref_reg(list,destloc.size,destloc.size,tempref,destloc.register);
  1596. tg.UnGetTemp(list,tempref);
  1597. {$else sparc}
  1598. unget_para(paraloc^);
  1599. gen_alloc_regloc(list,destloc);
  1600. { from register to register -> alignment is irrelevant }
  1601. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1602. if assigned(paraloc^.next) then
  1603. internalerror(200410109);
  1604. {$endif sparc}
  1605. end;
  1606. LOC_MMREGISTER,
  1607. LOC_CMMREGISTER :
  1608. begin
  1609. {$ifndef cpu64bitalu}
  1610. { ARM vfp floats are passed in integer registers }
  1611. if (para.size=OS_F64) and
  1612. (paraloc^.size in [OS_32,OS_S32]) and
  1613. use_vectorfpu(vardef) then
  1614. begin
  1615. { we need 2x32bit reg }
  1616. if not assigned(paraloc^.next) or
  1617. assigned(paraloc^.next^.next) then
  1618. internalerror(2009112421);
  1619. unget_para(paraloc^);
  1620. unget_para(paraloc^.next^);
  1621. gen_alloc_regloc(list,destloc);
  1622. if (target_info.endian=endian_big) then
  1623. { paraloc^ -> high
  1624. paraloc^.next -> low }
  1625. reg64:=joinreg64(paraloc^.next^.register,paraloc^.register)
  1626. else
  1627. reg64:=joinreg64(paraloc^.register,paraloc^.next^.register);
  1628. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,destloc.register);
  1629. end
  1630. else
  1631. {$endif not cpu64bitalu}
  1632. begin
  1633. unget_para(paraloc^);
  1634. gen_alloc_regloc(list,destloc);
  1635. { from register to register -> alignment is irrelevant }
  1636. cg.a_load_cgparaloc_anyreg(list,destloc.size,paraloc^,destloc.register,0);
  1637. { data could come in two memory locations, for now
  1638. we simply ignore the sanity check (FK)
  1639. if assigned(paraloc^.next) then
  1640. internalerror(200410108);
  1641. }
  1642. end;
  1643. end;
  1644. else
  1645. internalerror(2010052903);
  1646. end;
  1647. end;
  1648. procedure gen_load_para_value(list:TAsmList);
  1649. procedure get_para(const paraloc:TCGParaLocation);
  1650. begin
  1651. case paraloc.loc of
  1652. LOC_REGISTER :
  1653. begin
  1654. if getsupreg(paraloc.register)<first_int_imreg then
  1655. cg.getcpuregister(list,paraloc.register);
  1656. end;
  1657. LOC_MMREGISTER :
  1658. begin
  1659. if getsupreg(paraloc.register)<first_mm_imreg then
  1660. cg.getcpuregister(list,paraloc.register);
  1661. end;
  1662. LOC_FPUREGISTER :
  1663. begin
  1664. if getsupreg(paraloc.register)<first_fpu_imreg then
  1665. cg.getcpuregister(list,paraloc.register);
  1666. end;
  1667. end;
  1668. end;
  1669. var
  1670. i : longint;
  1671. currpara : tparavarsym;
  1672. paraloc : pcgparalocation;
  1673. begin
  1674. if (po_assembler in current_procinfo.procdef.procoptions) then
  1675. exit;
  1676. { Allocate registers used by parameters }
  1677. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1678. begin
  1679. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1680. paraloc:=currpara.paraloc[calleeside].location;
  1681. while assigned(paraloc) do
  1682. begin
  1683. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1684. get_para(paraloc^);
  1685. paraloc:=paraloc^.next;
  1686. end;
  1687. end;
  1688. { Copy parameters to local references/registers }
  1689. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1690. begin
  1691. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1692. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  1693. { gen_load_cgpara_loc() already allocated the initialloc
  1694. -> don't allocate again }
  1695. if currpara.initialloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMREGISTER] then
  1696. gen_alloc_regvar(list,currpara,false);
  1697. end;
  1698. { generate copies of call by value parameters, must be done before
  1699. the initialization and body is parsed because the refcounts are
  1700. incremented using the local copies }
  1701. current_procinfo.procdef.parast.SymList.ForEachCall(@copyvalueparas,list);
  1702. {$ifdef powerpc}
  1703. { unget the register that contains the stack pointer before the procedure entry, }
  1704. { which is used to access the parameters in their original callee-side location }
  1705. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1706. cg.a_reg_dealloc(list,NR_R12);
  1707. {$endif powerpc}
  1708. {$ifdef powerpc64}
  1709. { unget the register that contains the stack pointer before the procedure entry, }
  1710. { which is used to access the parameters in their original callee-side location }
  1711. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1712. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1713. {$endif powerpc64}
  1714. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1715. begin
  1716. { has to be done here rather than in gen_initialize_code, because
  1717. the initialisation code is generated a) later and b) with
  1718. rad_backwards, so the register allocator would generate
  1719. information as if this code comes before loading the parameters
  1720. from their original registers to their local location }
  1721. if (localvartrashing <> -1) then
  1722. current_procinfo.procdef.localst.SymList.ForEachCall(@trash_variable,list);
  1723. { initialize refcounted paras, and trash others. Needed here
  1724. instead of in gen_initialize_code, because when a reference is
  1725. intialised or trashed while the pointer to that reference is kept
  1726. in a regvar, we add a register move and that one again has to
  1727. come after the parameter loading code as far as the register
  1728. allocator is concerned }
  1729. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  1730. end;
  1731. end;
  1732. procedure gen_entry_code(list:TAsmList);
  1733. begin
  1734. { the actual profile code can clobber some registers,
  1735. therefore if the context must be saved, do it before
  1736. the actual call to the profile code
  1737. }
  1738. if (cs_profile in current_settings.moduleswitches) and
  1739. not(po_assembler in current_procinfo.procdef.procoptions) then
  1740. begin
  1741. { non-win32 can call mcout even in main }
  1742. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1743. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1744. begin
  1745. cg.g_profilecode(list);
  1746. end;
  1747. end;
  1748. { call startup helpers from main program }
  1749. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1750. begin
  1751. { initialize units }
  1752. cg.allocallcpuregisters(list);
  1753. if not(current_module.islibrary) then
  1754. cg.a_call_name(list,'FPC_INITIALIZEUNITS',false)
  1755. else
  1756. cg.a_call_name(list,'FPC_LIBINITIALIZEUNITS',false);
  1757. cg.deallocallcpuregisters(list);
  1758. end;
  1759. list.concat(Tai_force_line.Create);
  1760. {$ifdef OLDREGVARS}
  1761. load_regvars(list,nil);
  1762. {$endif OLDREGVARS}
  1763. end;
  1764. procedure gen_exit_code(list:TAsmList);
  1765. begin
  1766. { call __EXIT for main program }
  1767. if (not DLLsource) and
  1768. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1769. cg.a_call_name(list,'FPC_DO_EXIT',false);
  1770. end;
  1771. {****************************************************************************
  1772. Entry/Exit
  1773. ****************************************************************************}
  1774. function has_alias_name(pd:tprocdef;const s:string):boolean;
  1775. var
  1776. item : TCmdStrListItem;
  1777. begin
  1778. result:=true;
  1779. if pd.mangledname=s then
  1780. exit;
  1781. item := TCmdStrListItem(pd.aliasnames.first);
  1782. while assigned(item) do
  1783. begin
  1784. if item.str=s then
  1785. exit;
  1786. item := TCmdStrListItem(item.next);
  1787. end;
  1788. result:=false;
  1789. end;
  1790. procedure alloc_proc_symbol(pd: tprocdef);
  1791. var
  1792. item : TCmdStrListItem;
  1793. begin
  1794. item := TCmdStrListItem(pd.aliasnames.first);
  1795. while assigned(item) do
  1796. begin
  1797. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1798. item := TCmdStrListItem(item.next);
  1799. end;
  1800. end;
  1801. procedure gen_proc_symbol(list:TAsmList);
  1802. var
  1803. item,
  1804. previtem : TCmdStrListItem;
  1805. begin
  1806. previtem:=nil;
  1807. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  1808. while assigned(item) do
  1809. begin
  1810. {$ifdef arm}
  1811. if current_settings.cputype in cpu_thumb2 then
  1812. list.concat(tai_thumb_func.create);
  1813. {$endif arm}
  1814. { "double link" all procedure entry symbols via .reference }
  1815. { directives on darwin, because otherwise the linker }
  1816. { sometimes strips the procedure if only on of the symbols }
  1817. { is referenced }
  1818. if assigned(previtem) and
  1819. (target_info.system in systems_darwin) then
  1820. list.concat(tai_directive.create(asd_reference,item.str));
  1821. if (cs_profile in current_settings.moduleswitches) or
  1822. (po_global in current_procinfo.procdef.procoptions) then
  1823. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  1824. else
  1825. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  1826. if assigned(previtem) and
  1827. (target_info.system in systems_darwin) then
  1828. list.concat(tai_directive.create(asd_reference,previtem.str));
  1829. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1830. list.concat(Tai_function_name.create(item.str));
  1831. previtem:=item;
  1832. item := TCmdStrListItem(item.next);
  1833. end;
  1834. current_procinfo.procdef.procstarttai:=tai(list.last);
  1835. end;
  1836. procedure gen_proc_symbol_end(list:TAsmList);
  1837. begin
  1838. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1839. current_procinfo.procdef.procendtai:=tai(list.last);
  1840. if (current_module.islibrary) then
  1841. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  1842. { setinitname may generate a new section -> don't add to the
  1843. current list, because we assume this remains a text section }
  1844. exportlib.setinitname(current_asmdata.AsmLists[al_exports],current_procinfo.procdef.mangledname);
  1845. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1846. begin
  1847. if (target_info.system in (systems_darwin+[system_powerpc_macos])) and
  1848. not(current_module.islibrary) then
  1849. begin
  1850. new_section(list,sec_code,'',4);
  1851. list.concat(tai_symbol.createname_global(
  1852. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  1853. { keep argc, argv and envp properly on the stack }
  1854. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1855. end;
  1856. end;
  1857. end;
  1858. procedure gen_proc_entry_code(list:TAsmList);
  1859. var
  1860. hitemp,
  1861. lotemp : longint;
  1862. begin
  1863. { generate call frame marker for dwarf call frame info }
  1864. current_asmdata.asmcfi.start_frame(list);
  1865. { All temps are know, write offsets used for information }
  1866. if (cs_asm_source in current_settings.globalswitches) then
  1867. begin
  1868. if tg.direction>0 then
  1869. begin
  1870. lotemp:=current_procinfo.tempstart;
  1871. hitemp:=tg.lasttemp;
  1872. end
  1873. else
  1874. begin
  1875. lotemp:=tg.lasttemp;
  1876. hitemp:=current_procinfo.tempstart;
  1877. end;
  1878. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1879. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1880. end;
  1881. { generate target specific proc entry code }
  1882. hlcg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1883. end;
  1884. procedure gen_proc_exit_code(list:TAsmList);
  1885. var
  1886. parasize : longint;
  1887. begin
  1888. { c style clearstack does not need to remove parameters from the stack, only the
  1889. return value when it was pushed by arguments }
  1890. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1891. begin
  1892. parasize:=0;
  1893. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  1894. inc(parasize,sizeof(pint));
  1895. end
  1896. else
  1897. begin
  1898. parasize:=current_procinfo.para_stack_size;
  1899. { the parent frame pointer para has to be removed by the caller in
  1900. case of Delphi-style parent frame pointer passing }
  1901. if not paramanager.use_fixed_stack and
  1902. (po_delphi_nested_cc in current_procinfo.procdef.procoptions) then
  1903. dec(parasize,sizeof(pint));
  1904. end;
  1905. { generate target specific proc exit code }
  1906. hlcg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1907. { release return registers, needed for optimizer }
  1908. if not is_void(current_procinfo.procdef.returndef) then
  1909. paramanager.freecgpara(list,current_procinfo.procdef.funcretloc[calleeside]);
  1910. { end of frame marker for call frame info }
  1911. current_asmdata.asmcfi.end_frame(list);
  1912. end;
  1913. procedure gen_stack_check_size_para(list:TAsmList);
  1914. var
  1915. paraloc1 : tcgpara;
  1916. begin
  1917. paraloc1.init;
  1918. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1919. cg.a_load_const_cgpara(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1920. paramanager.freecgpara(list,paraloc1);
  1921. paraloc1.done;
  1922. end;
  1923. procedure gen_stack_check_call(list:TAsmList);
  1924. var
  1925. paraloc1 : tcgpara;
  1926. begin
  1927. paraloc1.init;
  1928. { Also alloc the register needed for the parameter }
  1929. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1930. paramanager.freecgpara(list,paraloc1);
  1931. { Call the helper }
  1932. cg.allocallcpuregisters(list);
  1933. cg.a_call_name(list,'FPC_STACKCHECK',false);
  1934. cg.deallocallcpuregisters(list);
  1935. paraloc1.done;
  1936. end;
  1937. procedure gen_save_used_regs(list:TAsmList);
  1938. begin
  1939. { Pure assembler routines need to save the registers themselves }
  1940. if (po_assembler in current_procinfo.procdef.procoptions) then
  1941. exit;
  1942. { oldfpccall expects all registers to be destroyed }
  1943. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1944. cg.g_save_registers(list);
  1945. end;
  1946. procedure gen_restore_used_regs(list:TAsmList);
  1947. begin
  1948. { Pure assembler routines need to save the registers themselves }
  1949. if (po_assembler in current_procinfo.procdef.procoptions) then
  1950. exit;
  1951. { oldfpccall expects all registers to be destroyed }
  1952. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1953. cg.g_restore_registers(list);
  1954. end;
  1955. {****************************************************************************
  1956. External handling
  1957. ****************************************************************************}
  1958. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  1959. begin
  1960. create_hlcodegen;
  1961. { add the procedure to the al_procedures }
  1962. maybe_new_object_file(list);
  1963. new_section(list,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  1964. list.concat(Tai_align.create(current_settings.alignment.procalign));
  1965. if (po_global in pd.procoptions) then
  1966. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1967. else
  1968. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1969. cg.g_external_wrapper(list,pd,externalname);
  1970. destroy_hlcodegen;
  1971. end;
  1972. {****************************************************************************
  1973. Const Data
  1974. ****************************************************************************}
  1975. procedure gen_alloc_symtable(list:TAsmList;st:TSymtable);
  1976. procedure setlocalloc(vs:tabstractnormalvarsym);
  1977. begin
  1978. if cs_asm_source in current_settings.globalswitches then
  1979. begin
  1980. case vs.initialloc.loc of
  1981. LOC_REFERENCE :
  1982. begin
  1983. if not assigned(vs.initialloc.reference.symbol) then
  1984. list.concat(Tai_comment.Create(strpnew('Var '+vs.realname+' located at '+
  1985. std_regname(vs.initialloc.reference.base)+tostr_with_plus(vs.initialloc.reference.offset))));
  1986. end;
  1987. end;
  1988. end;
  1989. vs.localloc:=vs.initialloc;
  1990. end;
  1991. var
  1992. i : longint;
  1993. sym : tsym;
  1994. vs : tabstractnormalvarsym;
  1995. isaddr : boolean;
  1996. begin
  1997. for i:=0 to st.SymList.Count-1 do
  1998. begin
  1999. sym:=tsym(st.SymList[i]);
  2000. case sym.typ of
  2001. staticvarsym :
  2002. begin
  2003. vs:=tabstractnormalvarsym(sym);
  2004. { The code in loadnode.pass_generatecode will create the
  2005. LOC_REFERENCE instead for all none register variables. This is
  2006. required because we can't store an asmsymbol in the localloc because
  2007. the asmsymbol is invalid after an unit is compiled. This gives
  2008. problems when this procedure is inlined in another unit (PFV) }
  2009. if vs.is_regvar(false) then
  2010. begin
  2011. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  2012. vs.initialloc.size:=def_cgsize(vs.vardef);
  2013. gen_alloc_regvar(list,vs,true);
  2014. setlocalloc(vs);
  2015. end;
  2016. end;
  2017. paravarsym :
  2018. begin
  2019. vs:=tabstractnormalvarsym(sym);
  2020. { Parameters passed to assembler procedures need to be kept
  2021. in the original location }
  2022. if (po_assembler in current_procinfo.procdef.procoptions) then
  2023. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  2024. else
  2025. begin
  2026. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,current_procinfo.procdef.proccalloption);
  2027. if isaddr then
  2028. vs.initialloc.size:=OS_ADDR
  2029. else
  2030. vs.initialloc.size:=def_cgsize(vs.vardef);
  2031. if vs.is_regvar(isaddr) then
  2032. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  2033. else
  2034. begin
  2035. vs.initialloc.loc:=LOC_REFERENCE;
  2036. { Reuse the parameter location for values to are at a single location on the stack }
  2037. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  2038. begin
  2039. reference_reset_base(vs.initialloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  2040. tparavarsym(sym).paraloc[calleeside].location^.reference.offset,tparavarsym(sym).paraloc[calleeside].alignment);
  2041. end
  2042. else
  2043. begin
  2044. if isaddr then
  2045. tg.GetLocal(list,sizeof(pint),voidpointertype,vs.initialloc.reference)
  2046. else
  2047. tg.GetLocal(list,vs.getsize,tparavarsym(sym).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  2048. end;
  2049. end;
  2050. end;
  2051. setlocalloc(vs);
  2052. end;
  2053. localvarsym :
  2054. begin
  2055. vs:=tabstractnormalvarsym(sym);
  2056. vs.initialloc.size:=def_cgsize(vs.vardef);
  2057. if (m_delphi in current_settings.modeswitches) and
  2058. (po_assembler in current_procinfo.procdef.procoptions) and
  2059. (vo_is_funcret in vs.varoptions) and
  2060. (vs.refs=0) then
  2061. begin
  2062. { not referenced, so don't allocate. Use dummy to }
  2063. { avoid ie's later on because of LOC_INVALID }
  2064. vs.initialloc.loc:=LOC_REGISTER;
  2065. vs.initialloc.size:=OS_INT;
  2066. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  2067. end
  2068. else if vs.is_regvar(false) then
  2069. begin
  2070. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  2071. gen_alloc_regvar(list,vs,true);
  2072. end
  2073. else
  2074. begin
  2075. vs.initialloc.loc:=LOC_REFERENCE;
  2076. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  2077. end;
  2078. setlocalloc(vs);
  2079. end;
  2080. end;
  2081. end;
  2082. end;
  2083. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  2084. begin
  2085. case location.loc of
  2086. LOC_CREGISTER:
  2087. {$ifndef cpu64bitalu}
  2088. if location.size in [OS_64,OS_S64] then
  2089. begin
  2090. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  2091. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  2092. end
  2093. else
  2094. {$endif not cpu64bitalu}
  2095. rv.intregvars.addnodup(getsupreg(location.register));
  2096. LOC_CFPUREGISTER:
  2097. rv.fpuregvars.addnodup(getsupreg(location.register));
  2098. LOC_CMMREGISTER:
  2099. rv.mmregvars.addnodup(getsupreg(location.register));
  2100. end;
  2101. end;
  2102. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  2103. var
  2104. rv: pusedregvars absolute arg;
  2105. begin
  2106. case (n.nodetype) of
  2107. temprefn:
  2108. { We only have to synchronise a tempnode before a loop if it is }
  2109. { not created inside the loop, and only synchronise after the }
  2110. { loop if it's not destroyed inside the loop. If it's created }
  2111. { before the loop and not yet destroyed, then before the loop }
  2112. { is secondpassed tempinfo^.valid will be true, and we get the }
  2113. { correct registers. If it's not destroyed inside the loop, }
  2114. { then after the loop has been secondpassed tempinfo^.valid }
  2115. { be true and we also get the right registers. In other cases, }
  2116. { tempinfo^.valid will be false and so we do not add }
  2117. { unnecessary registers. This way, we don't have to look at }
  2118. { tempcreate and tempdestroy nodes to get this info (JM) }
  2119. if (ti_valid in ttemprefnode(n).tempinfo^.flags) then
  2120. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  2121. loadn:
  2122. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  2123. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  2124. vecn:
  2125. { range checks sometimes need the high parameter }
  2126. if (cs_check_range in current_settings.localswitches) and
  2127. (is_open_array(tvecnode(n).left.resultdef) or
  2128. is_array_of_const(tvecnode(n).left.resultdef)) and
  2129. not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  2130. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  2131. end;
  2132. result := fen_true;
  2133. end;
  2134. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  2135. begin
  2136. foreachnodestatic(n,@do_get_used_regvars,@rv);
  2137. end;
  2138. (*
  2139. See comments at declaration of pusedregvarscommon
  2140. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  2141. var
  2142. rv: pusedregvarscommon absolute arg;
  2143. begin
  2144. if (n.nodetype = loadn) and
  2145. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  2146. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  2147. case loc of
  2148. LOC_CREGISTER:
  2149. { if not yet encountered in this node tree }
  2150. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2151. { but nevertheless already encountered somewhere }
  2152. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2153. { then it's a regvar used in two or more node trees }
  2154. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2155. LOC_CFPUREGISTER:
  2156. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2157. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2158. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2159. LOC_CMMREGISTER:
  2160. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2161. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2162. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2163. end;
  2164. result := fen_true;
  2165. end;
  2166. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  2167. begin
  2168. rv.myregvars.intregvars.clear;
  2169. rv.myregvars.fpuregvars.clear;
  2170. rv.myregvars.mmregvars.clear;
  2171. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  2172. end;
  2173. *)
  2174. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  2175. var
  2176. count: longint;
  2177. begin
  2178. for count := 1 to rv.intregvars.length do
  2179. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  2180. for count := 1 to rv.fpuregvars.length do
  2181. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  2182. for count := 1 to rv.mmregvars.length do
  2183. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  2184. end;
  2185. {*****************************************************************************
  2186. SSA support
  2187. *****************************************************************************}
  2188. type
  2189. preplaceregrec = ^treplaceregrec;
  2190. treplaceregrec = record
  2191. old, new: tregister;
  2192. {$ifndef cpu64bitalu}
  2193. oldhi, newhi: tregister;
  2194. {$endif not cpu64bitalu}
  2195. ressym: tsym;
  2196. end;
  2197. function doreplace(var n: tnode; para: pointer): foreachnoderesult;
  2198. var
  2199. rr: preplaceregrec absolute para;
  2200. begin
  2201. result := fen_false;
  2202. if (nf_is_funcret in n.flags) and (fc_exit in flowcontrol) then
  2203. exit;
  2204. case n.nodetype of
  2205. loadn:
  2206. begin
  2207. if (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  2208. not assigned(tloadnode(n).left) and
  2209. ((tloadnode(n).symtableentry <> rr^.ressym) or
  2210. not(fc_exit in flowcontrol)
  2211. ) and
  2212. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  2213. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register = rr^.old) then
  2214. begin
  2215. {$ifndef cpu64bitalu}
  2216. { it's possible a 64 bit location was shifted and/xor typecasted }
  2217. { in a 32 bit value, so only 1 register was left in the location }
  2218. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_64,OS_S64]) then
  2219. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi = rr^.oldhi) then
  2220. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi := rr^.newhi
  2221. else
  2222. exit;
  2223. {$endif not cpu64bitalu}
  2224. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register := rr^.new;
  2225. result := fen_norecurse_true;
  2226. end;
  2227. end;
  2228. temprefn:
  2229. begin
  2230. if (ti_valid in ttemprefnode(n).tempinfo^.flags) and
  2231. (ttemprefnode(n).tempinfo^.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  2232. (ttemprefnode(n).tempinfo^.location.register = rr^.old) then
  2233. begin
  2234. {$ifndef cpu64bitalu}
  2235. { it's possible a 64 bit location was shifted and/xor typecasted }
  2236. { in a 32 bit value, so only 1 register was left in the location }
  2237. if (ttemprefnode(n).tempinfo^.location.size in [OS_64,OS_S64]) then
  2238. if (ttemprefnode(n).tempinfo^.location.register64.reghi = rr^.oldhi) then
  2239. ttemprefnode(n).tempinfo^.location.register64.reghi := rr^.newhi
  2240. else
  2241. exit;
  2242. {$endif not cpu64bitalu}
  2243. ttemprefnode(n).tempinfo^.location.register := rr^.new;
  2244. result := fen_norecurse_true;
  2245. end;
  2246. end;
  2247. { optimize the searching a bit }
  2248. derefn,addrn,
  2249. calln,inlinen,casen,
  2250. addn,subn,muln,
  2251. andn,orn,xorn,
  2252. ltn,lten,gtn,gten,equaln,unequaln,
  2253. slashn,divn,shrn,shln,notn,
  2254. inn,
  2255. asn,isn:
  2256. result := fen_norecurse_false;
  2257. end;
  2258. end;
  2259. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  2260. var
  2261. rr: treplaceregrec;
  2262. begin
  2263. {$ifdef jvm}
  2264. exit;
  2265. {$endif}
  2266. if not (n.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) or
  2267. ([fc_inflowcontrol,fc_gotolabel,fc_lefthandled] * flowcontrol <> []) then
  2268. exit;
  2269. rr.old := n.location.register;
  2270. rr.ressym := nil;
  2271. {$ifndef cpu64bitalu}
  2272. rr.oldhi := NR_NO;
  2273. {$endif not cpu64bitalu}
  2274. case n.location.loc of
  2275. LOC_CREGISTER:
  2276. begin
  2277. {$ifndef cpu64bitalu}
  2278. if (n.location.size in [OS_64,OS_S64]) then
  2279. begin
  2280. rr.oldhi := n.location.register64.reghi;
  2281. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  2282. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  2283. end
  2284. else
  2285. {$endif not cpu64bitalu}
  2286. rr.new := cg.getintregister(current_asmdata.CurrAsmList,n.location.size);
  2287. end;
  2288. LOC_CFPUREGISTER:
  2289. rr.new := cg.getfpuregister(current_asmdata.CurrAsmList,n.location.size);
  2290. {$ifdef SUPPORT_MMX}
  2291. LOC_CMMXREGISTER:
  2292. rr.new := tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  2293. {$endif SUPPORT_MMX}
  2294. LOC_CMMREGISTER:
  2295. rr.new := cg.getmmregister(current_asmdata.CurrAsmList,n.location.size);
  2296. else
  2297. exit;
  2298. end;
  2299. if not is_void(current_procinfo.procdef.returndef) and
  2300. assigned(current_procinfo.procdef.funcretsym) and
  2301. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  2302. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  2303. rr.ressym:=tsym(current_procinfo.procdef.parast.Find('self'))
  2304. else
  2305. rr.ressym:=current_procinfo.procdef.funcretsym;
  2306. if not foreachnodestatic(n,@doreplace,@rr) then
  2307. exit;
  2308. if reload then
  2309. case n.location.loc of
  2310. LOC_CREGISTER:
  2311. begin
  2312. {$ifndef cpu64bitalu}
  2313. if (n.location.size in [OS_64,OS_S64]) then
  2314. cg64.a_load64_reg_reg(list,n.location.register64,joinreg64(rr.new,rr.newhi))
  2315. else
  2316. {$endif not cpu64bitalu}
  2317. cg.a_load_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  2318. end;
  2319. LOC_CFPUREGISTER:
  2320. cg.a_loadfpu_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  2321. {$ifdef SUPPORT_MMX}
  2322. LOC_CMMXREGISTER:
  2323. cg.a_loadmm_reg_reg(list,OS_M64,OS_M64,n.location.register,rr.new,nil);
  2324. {$endif SUPPORT_MMX}
  2325. LOC_CMMREGISTER:
  2326. cg.a_loadmm_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new,nil);
  2327. else
  2328. internalerror(2006090920);
  2329. end;
  2330. { now that we've change the loadn/temp, also change the node result location }
  2331. {$ifndef cpu64bitalu}
  2332. if (n.location.size in [OS_64,OS_S64]) then
  2333. begin
  2334. n.location.register64.reglo := rr.new;
  2335. n.location.register64.reghi := rr.newhi;
  2336. end
  2337. else
  2338. {$endif not cpu64bitalu}
  2339. n.location.register := rr.new;
  2340. end;
  2341. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  2342. var
  2343. i : longint;
  2344. sym : tsym;
  2345. begin
  2346. for i:=0 to st.SymList.Count-1 do
  2347. begin
  2348. sym:=tsym(st.SymList[i]);
  2349. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  2350. begin
  2351. with tabstractnormalvarsym(sym) do
  2352. begin
  2353. { Note: We need to keep the data available in memory
  2354. for the sub procedures that can access local data
  2355. in the parent procedures }
  2356. case localloc.loc of
  2357. LOC_CREGISTER :
  2358. if (pi_has_label in current_procinfo.flags) then
  2359. {$ifndef cpu64bitalu}
  2360. if def_cgsize(vardef) in [OS_64,OS_S64] then
  2361. begin
  2362. cg.a_reg_sync(list,localloc.register64.reglo);
  2363. cg.a_reg_sync(list,localloc.register64.reghi);
  2364. end
  2365. else
  2366. {$endif not cpu64bitalu}
  2367. cg.a_reg_sync(list,localloc.register);
  2368. LOC_CFPUREGISTER,
  2369. LOC_CMMREGISTER:
  2370. if (pi_has_label in current_procinfo.flags) then
  2371. cg.a_reg_sync(list,localloc.register);
  2372. LOC_REFERENCE :
  2373. begin
  2374. if typ in [localvarsym,paravarsym] then
  2375. tg.Ungetlocal(list,localloc.reference);
  2376. end;
  2377. end;
  2378. end;
  2379. end;
  2380. end;
  2381. end;
  2382. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  2383. var
  2384. i,j : longint;
  2385. tmps : string;
  2386. pd : TProcdef;
  2387. ImplIntf : TImplementedInterface;
  2388. begin
  2389. for i:=0 to _class.ImplementedInterfaces.count-1 do
  2390. begin
  2391. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  2392. if (ImplIntf=ImplIntf.VtblImplIntf) and
  2393. assigned(ImplIntf.ProcDefs) then
  2394. begin
  2395. maybe_new_object_file(list);
  2396. for j:=0 to ImplIntf.ProcDefs.Count-1 do
  2397. begin
  2398. pd:=TProcdef(ImplIntf.ProcDefs[j]);
  2399. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  2400. ImplIntf.IntfDef.objname^+'_$_'+tostr(j)+'_$_'+pd.mangledname);
  2401. { create wrapper code }
  2402. new_section(list,sec_code,tmps,0);
  2403. hlcg.init_register_allocators;
  2404. cg.g_intf_wrapper(list,pd,tmps,ImplIntf.ioffset);
  2405. hlcg.done_register_allocators;
  2406. end;
  2407. end;
  2408. end;
  2409. end;
  2410. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable;nested:boolean);
  2411. var
  2412. i : longint;
  2413. def : tdef;
  2414. begin
  2415. if not nested then
  2416. create_hlcodegen;
  2417. for i:=0 to st.DefList.Count-1 do
  2418. begin
  2419. def:=tdef(st.DefList[i]);
  2420. { if def can contain nested types then handle it symtable }
  2421. if def.typ in [objectdef,recorddef] then
  2422. gen_intf_wrappers(list,tabstractrecorddef(def).symtable,true);
  2423. if is_class(def) then
  2424. gen_intf_wrapper(list,tobjectdef(def));
  2425. end;
  2426. if not nested then
  2427. destroy_hlcodegen;
  2428. end;
  2429. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  2430. var
  2431. href : treference;
  2432. begin
  2433. if is_object(objdef) then
  2434. begin
  2435. case selfloc.loc of
  2436. LOC_CREFERENCE,
  2437. LOC_REFERENCE:
  2438. begin
  2439. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  2440. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  2441. end;
  2442. else
  2443. internalerror(200305056);
  2444. end;
  2445. end
  2446. else
  2447. { This is also valid for Objective-C classes: vmt_offset is 0 there,
  2448. and the first "field" of an Objective-C class instance is a pointer
  2449. to its "meta-class". }
  2450. begin
  2451. case selfloc.loc of
  2452. LOC_REGISTER:
  2453. begin
  2454. {$ifdef cpu_uses_separate_address_registers}
  2455. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  2456. begin
  2457. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  2458. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2459. end
  2460. else
  2461. {$endif cpu_uses_separate_address_registers}
  2462. reference_reset_base(href,selfloc.register,objdef.vmt_offset,sizeof(pint));
  2463. end;
  2464. LOC_CREGISTER,
  2465. LOC_CREFERENCE,
  2466. LOC_REFERENCE:
  2467. begin
  2468. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  2469. cg.a_load_loc_reg(list,OS_ADDR,selfloc,href.base);
  2470. end;
  2471. else
  2472. internalerror(200305057);
  2473. end;
  2474. end;
  2475. vmtreg:=cg.getaddressregister(list);
  2476. cg.g_maybe_testself(list,href.base);
  2477. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2478. { test validity of VMT }
  2479. if not(is_interface(objdef)) and
  2480. not(is_cppclass(objdef)) and
  2481. not(is_objc_class_or_protocol(objdef)) then
  2482. cg.g_maybe_testvmt(list,vmtreg,objdef);
  2483. end;
  2484. function getprocalign : shortint;
  2485. begin
  2486. { gprof uses 16 byte granularity }
  2487. if (cs_profile in current_settings.moduleswitches) then
  2488. result:=16
  2489. else
  2490. result:=current_settings.alignment.procalign;
  2491. end;
  2492. procedure gen_fpc_dummy(list : TAsmList);
  2493. begin
  2494. {$ifdef i386}
  2495. { fix me! }
  2496. list.concat(Taicpu.Op_const_reg(A_MOV,S_L,1,NR_EAX));
  2497. list.concat(Taicpu.Op_const(A_RET,S_W,12));
  2498. {$endif i386}
  2499. end;
  2500. procedure InsertInterruptTable;
  2501. procedure WriteVector(const name: string);
  2502. {$IFDEF arm}
  2503. var
  2504. ai: taicpu;
  2505. {$ENDIF arm}
  2506. begin
  2507. {$IFDEF arm}
  2508. if current_settings.cputype in [cpu_armv7m, cpu_cortexm3] then
  2509. current_asmdata.asmlists[al_globals].concat(tai_const.Createname(name,0))
  2510. else
  2511. begin
  2512. ai:=taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(name));
  2513. ai.is_jmp:=true;
  2514. current_asmdata.asmlists[al_globals].concat(ai);
  2515. end;
  2516. {$ENDIF arm}
  2517. end;
  2518. function GetInterruptTableLength: longint;
  2519. begin
  2520. {$if defined(ARM)}
  2521. result:=interruptvectors[current_settings.controllertype];
  2522. {$else}
  2523. result:=0;
  2524. {$endif}
  2525. end;
  2526. var
  2527. hp: tused_unit;
  2528. sym: tsym;
  2529. i, i2: longint;
  2530. interruptTable: array of tprocdef;
  2531. pd: tprocdef;
  2532. begin
  2533. SetLength(interruptTable, GetInterruptTableLength);
  2534. FillChar(interruptTable[0], length(interruptTable)*sizeof(pointer), 0);
  2535. hp:=tused_unit(usedunits.first);
  2536. while assigned(hp) do
  2537. begin
  2538. for i := 0 to hp.u.symlist.Count-1 do
  2539. begin
  2540. sym:=tsym(hp.u.symlist[i]);
  2541. if not assigned(sym) then
  2542. continue;
  2543. if sym.typ = procsym then
  2544. begin
  2545. for i2 := 0 to tprocsym(sym).ProcdefList.Count-1 do
  2546. begin
  2547. pd:=tprocdef(tprocsym(sym).ProcdefList[i2]);
  2548. if pd.interruptvector >= 0 then
  2549. begin
  2550. if pd.interruptvector > high(interruptTable) then
  2551. Internalerror(2011030602);
  2552. if interruptTable[pd.interruptvector] <> nil then
  2553. internalerror(2011030601);
  2554. interruptTable[pd.interruptvector]:=pd;
  2555. break;
  2556. end;
  2557. end;
  2558. end;
  2559. end;
  2560. hp:=tused_unit(hp.next);
  2561. end;
  2562. new_section(current_asmdata.asmlists[al_globals],sec_init,'VECTORS',sizeof(pint));
  2563. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('VECTORS',AT_DATA,0));
  2564. {$IFDEF arm}
  2565. if current_settings.cputype in [cpu_armv7m, cpu_cortexm3] then
  2566. current_asmdata.asmlists[al_globals].concat(tai_const.Createname('_stack_top',0)); { ARMv7-M processors have the initial stack value at address 0 }
  2567. {$ENDIF arm}
  2568. for i:=0 to high(interruptTable) do
  2569. begin
  2570. if interruptTable[i]<>nil then
  2571. writeVector(interruptTable[i].mangledname)
  2572. else
  2573. writeVector('DefaultHandler'); { Default handler name }
  2574. end;
  2575. end;
  2576. end.