cgai386.pas 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Helper routines for the i386 code generator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgai386;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cobjects,
  23. cpubase,cpuasm,
  24. symconst,symtype,symdef,aasm;
  25. {$define TESTGETTEMP to store const that
  26. are written into temps for later release PM }
  27. function def_opsize(p1:pdef):topsize;
  28. function def2def_opsize(p1,p2:pdef):topsize;
  29. function def_getreg(p1:pdef):tregister;
  30. function makereg8(r:tregister):tregister;
  31. function makereg16(r:tregister):tregister;
  32. function makereg32(r:tregister):tregister;
  33. procedure locflags2reg(var l:tlocation;opsize:topsize);
  34. procedure locjump2reg(var l:tlocation;opsize:topsize; otl, ofl: pasmlabel);
  35. procedure emitlab(var l : pasmlabel);
  36. procedure emitjmp(c : tasmcond;var l : pasmlabel);
  37. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  38. procedure emit_none(i : tasmop;s : topsize);
  39. procedure emit_const(i : tasmop;s : topsize;c : longint);
  40. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  41. procedure emit_ref(i : tasmop;s : topsize;ref : preference);
  42. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  43. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
  44. procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
  45. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
  46. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  47. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  48. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  49. procedure emit_sym(i : tasmop;s : topsize;op : pasmsymbol);
  50. procedure emit_sym_ofs(i : tasmop;s : topsize;op : pasmsymbol;ofs : longint);
  51. procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;reg : tregister);
  52. procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;ref : preference);
  53. procedure emitcall(const routine:string);
  54. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize;freetemp:boolean);
  55. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  56. procedure emit_mov_ref_reg64(r : treference;rl,rh : tregister);
  57. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
  58. procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
  59. procedure emit_push_loc(const t:tlocation);
  60. procedure emit_push_mem_size(const t: treference; size: longint);
  61. { pushes qword location to the stack }
  62. procedure emit_pushq_loc(const t : tlocation);
  63. procedure release_qword_loc(const t : tlocation);
  64. { remove non regvar registers in loc from regs (in the format }
  65. { pushusedregisters uses) }
  66. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: byte);
  67. { releases the registers of a location }
  68. procedure release_loc(const t : tlocation);
  69. procedure emit_pushw_loc(const t:tlocation);
  70. procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
  71. procedure emit_to_mem(var t:tlocation;def:pdef);
  72. procedure emit_to_reg16(var hr:tregister);
  73. procedure emit_to_reg32(var hr:tregister);
  74. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  75. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  76. procedure copyshortstring(const dref,sref : treference;len : byte;
  77. loadref, del_sref: boolean);
  78. procedure finalize(t : pdef;const ref : treference;is_already_ref : boolean);
  79. procedure incrstringref(t : pdef;const ref : treference);
  80. procedure decrstringref(t : pdef;const ref : treference);
  81. procedure push_int(l : longint);
  82. procedure emit_push_mem(const ref : treference);
  83. procedure emitpushreferenceaddr(const ref : treference);
  84. procedure incrcomintfref(t: pdef; const ref: treference);
  85. procedure decrcomintfref(t: pdef; const ref: treference);
  86. procedure floatload(t : tfloattype;const ref : treference);
  87. procedure floatstore(t : tfloattype;const ref : treference);
  88. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  89. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  90. procedure maybe_loadesi;
  91. procedure emitloadord2reg(const location:Tlocation;orddef:Porddef;destreg:Tregister;delloc:boolean);
  92. procedure concatcopy(source,dest : treference;size : longint;delsource : boolean;loadref:boolean);
  93. procedure genentrycode(alist : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
  94. stackframe:longint;
  95. var parasize:longint;var nostackframe:boolean;
  96. inlined : boolean);
  97. procedure genexitcode(alist : paasmoutput;parasize:longint;
  98. nostackframe,inlined:boolean);
  99. { if a unit doesn't have a explicit init/final code, }
  100. { we've to generate one, if the units has ansistrings }
  101. { in the interface or implementation }
  102. procedure genimplicitunitfinal(alist : paasmoutput);
  103. procedure genimplicitunitinit(alist : paasmoutput);
  104. {$ifdef test_dest_loc}
  105. const
  106. { used to avoid temporary assignments }
  107. dest_loc_known : boolean = false;
  108. in_dest_loc : boolean = false;
  109. dest_loc_tree : ptree = nil;
  110. var
  111. dest_loc : tlocation;
  112. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  113. {$endif test_dest_loc}
  114. implementation
  115. uses
  116. {$ifdef delphi}
  117. sysutils,
  118. {$else}
  119. strings,
  120. {$endif}
  121. cutils,
  122. globtype,systems,globals,verbose,
  123. fmodule,
  124. symbase,symsym,symtable,types,
  125. tgeni386,temp_gen,hcodegen,regvars
  126. {$ifdef GDB}
  127. ,gdb
  128. {$endif}
  129. {$ifndef NOTARGETWIN32}
  130. ,t_win32
  131. {$endif}
  132. ;
  133. {*****************************************************************************
  134. Helpers
  135. *****************************************************************************}
  136. function def_opsize(p1:pdef):topsize;
  137. begin
  138. case p1^.size of
  139. 1 : def_opsize:=S_B;
  140. 2 : def_opsize:=S_W;
  141. 4 : def_opsize:=S_L;
  142. else
  143. internalerror(130820001);
  144. end;
  145. end;
  146. function def2def_opsize(p1,p2:pdef):topsize;
  147. var
  148. o1 : topsize;
  149. begin
  150. case p1^.size of
  151. 1 : o1:=S_B;
  152. 2 : o1:=S_W;
  153. 4 : o1:=S_L;
  154. { I don't know if we need it (FK) }
  155. 8 : o1:=S_L;
  156. else
  157. internalerror(130820002);
  158. end;
  159. if assigned(p2) then
  160. begin
  161. case p2^.size of
  162. 1 : o1:=S_B;
  163. 2 : begin
  164. if o1=S_B then
  165. o1:=S_BW
  166. else
  167. o1:=S_W;
  168. end;
  169. 4,8:
  170. begin
  171. case o1 of
  172. S_B : o1:=S_BL;
  173. S_W : o1:=S_WL;
  174. end;
  175. end;
  176. end;
  177. end;
  178. def2def_opsize:=o1;
  179. end;
  180. function def_getreg(p1:pdef):tregister;
  181. begin
  182. case p1^.size of
  183. 1 : def_getreg:=reg32toreg8(getregister32);
  184. 2 : def_getreg:=reg32toreg16(getregister32);
  185. 4 : def_getreg:=getregister32;
  186. else
  187. internalerror(130820003);
  188. end;
  189. end;
  190. function makereg8(r:tregister):tregister;
  191. begin
  192. case r of
  193. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  194. makereg8:=reg32toreg8(r);
  195. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  196. makereg8:=reg16toreg8(r);
  197. R_AL,R_BL,R_CL,R_DL :
  198. makereg8:=r;
  199. end;
  200. end;
  201. function makereg16(r:tregister):tregister;
  202. begin
  203. case r of
  204. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  205. makereg16:=reg32toreg16(r);
  206. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  207. makereg16:=r;
  208. R_AL,R_BL,R_CL,R_DL :
  209. makereg16:=reg8toreg16(r);
  210. end;
  211. end;
  212. function makereg32(r:tregister):tregister;
  213. begin
  214. case r of
  215. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  216. makereg32:=r;
  217. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  218. makereg32:=reg16toreg32(r);
  219. R_AL,R_BL,R_CL,R_DL :
  220. makereg32:=reg8toreg32(r);
  221. end;
  222. end;
  223. procedure locflags2reg(var l:tlocation;opsize:topsize);
  224. var
  225. hregister : tregister;
  226. begin
  227. if (l.loc=LOC_FLAGS) then
  228. begin
  229. hregister:=getregister32;
  230. case opsize of
  231. S_W : hregister:=reg32toreg16(hregister);
  232. S_B : hregister:=reg32toreg8(hregister);
  233. end;
  234. emit_flag2reg(l.resflags,hregister);
  235. l.loc:=LOC_REGISTER;
  236. l.register:=hregister;
  237. end
  238. else internalerror(270720001);
  239. end;
  240. procedure locjump2reg(var l:tlocation;opsize:topsize; otl, ofl: pasmlabel);
  241. var
  242. hregister : tregister;
  243. hl : pasmlabel;
  244. begin
  245. if l.loc = LOC_JUMP then
  246. begin
  247. hregister:=getregister32;
  248. case opsize of
  249. S_W : hregister:=reg32toreg16(hregister);
  250. S_B : hregister:=reg32toreg8(hregister);
  251. end;
  252. l.loc:=LOC_REGISTER;
  253. l.register:=hregister;
  254. emitlab(truelabel);
  255. truelabel:=otl;
  256. emit_const_reg(A_MOV,opsize,1,hregister);
  257. getlabel(hl);
  258. emitjmp(C_None,hl);
  259. emitlab(falselabel);
  260. falselabel:=ofl;
  261. emit_reg_reg(A_XOR,S_L,makereg32(hregister),
  262. makereg32(hregister));
  263. emitlab(hl);
  264. end
  265. else internalerror(270720002);
  266. end;
  267. {*****************************************************************************
  268. Emit Assembler
  269. *****************************************************************************}
  270. procedure emitlab(var l : pasmlabel);
  271. begin
  272. if not l^.is_set then
  273. exprasmlist^.concat(new(pai_label,init(l)))
  274. else
  275. internalerror(7453984);
  276. end;
  277. procedure emitjmp(c : tasmcond;var l : pasmlabel);
  278. var
  279. ai : Paicpu;
  280. begin
  281. if c=C_None then
  282. ai := new(paicpu,op_sym(A_JMP,S_NO,l))
  283. else
  284. begin
  285. ai:=new(paicpu,op_sym(A_Jcc,S_NO,l));
  286. ai^.SetCondition(c);
  287. end;
  288. ai^.is_jmp:=true;
  289. exprasmlist^.concat(ai);
  290. end;
  291. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  292. var
  293. ai : paicpu;
  294. hreg : tregister;
  295. begin
  296. hreg:=makereg8(hregister);
  297. ai:=new(paicpu,op_reg(A_Setcc,S_B,hreg));
  298. ai^.SetCondition(flag_2_cond[flag]);
  299. exprasmlist^.concat(ai);
  300. if hreg<>hregister then
  301. begin
  302. if hregister in regset16bit then
  303. emit_to_reg16(hreg)
  304. else
  305. emit_to_reg32(hreg);
  306. end;
  307. end;
  308. procedure emit_none(i : tasmop;s : topsize);
  309. begin
  310. exprasmlist^.concat(new(paicpu,op_none(i,s)));
  311. end;
  312. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  313. begin
  314. exprasmlist^.concat(new(paicpu,op_reg(i,s,reg)));
  315. end;
  316. procedure emit_ref(i : tasmop;s : topsize;ref : preference);
  317. begin
  318. exprasmlist^.concat(new(paicpu,op_ref(i,s,ref)));
  319. end;
  320. procedure emit_const(i : tasmop;s : topsize;c : longint);
  321. begin
  322. exprasmlist^.concat(new(paicpu,op_const(i,s,c)));
  323. end;
  324. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  325. begin
  326. exprasmlist^.concat(new(paicpu,op_const_reg(i,s,c,reg)));
  327. end;
  328. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
  329. begin
  330. exprasmlist^.concat(new(paicpu,op_const_ref(i,s,c,ref)));
  331. end;
  332. procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
  333. begin
  334. exprasmlist^.concat(new(paicpu,op_ref_reg(i,s,ref,reg)));
  335. end;
  336. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
  337. begin
  338. exprasmlist^.concat(new(paicpu,op_reg_ref(i,s,reg,ref)));
  339. end;
  340. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  341. begin
  342. if (reg1<>reg2) or (i<>A_MOV) then
  343. exprasmlist^.concat(new(paicpu,op_reg_reg(i,s,reg1,reg2)));
  344. end;
  345. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  346. begin
  347. exprasmlist^.concat(new(paicpu,op_const_reg_reg(i,s,c,reg1,reg2)));
  348. end;
  349. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  350. begin
  351. exprasmlist^.concat(new(paicpu,op_reg_reg_reg(i,s,reg1,reg2,reg3)));
  352. end;
  353. procedure emit_sym(i : tasmop;s : topsize;op : pasmsymbol);
  354. begin
  355. exprasmlist^.concat(new(paicpu,op_sym(i,s,op)));
  356. end;
  357. procedure emit_sym_ofs(i : tasmop;s : topsize;op : pasmsymbol;ofs : longint);
  358. begin
  359. exprasmlist^.concat(new(paicpu,op_sym_ofs(i,s,op,ofs)));
  360. end;
  361. procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;reg : tregister);
  362. begin
  363. exprasmlist^.concat(new(paicpu,op_sym_ofs_reg(i,s,op,ofs,reg)));
  364. end;
  365. procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;ref : preference);
  366. begin
  367. exprasmlist^.concat(new(paicpu,op_sym_ofs_ref(i,s,op,ofs,ref)));
  368. end;
  369. procedure emitcall(const routine:string);
  370. begin
  371. exprasmlist^.concat(new(paicpu,op_sym(A_CALL,S_NO,newasmsymbol(routine))));
  372. end;
  373. { only usefull in startup code }
  374. procedure emitinsertcall(const routine:string);
  375. begin
  376. exprasmlist^.insert(new(paicpu,op_sym(A_CALL,S_NO,newasmsymbol(routine))));
  377. end;
  378. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize;freetemp:boolean);
  379. var
  380. hreg : tregister;
  381. pushedeax : boolean;
  382. begin
  383. pushedeax:=false;
  384. case t.loc of
  385. LOC_REGISTER,
  386. LOC_CREGISTER : begin
  387. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,siz,
  388. t.register,newreference(ref))));
  389. ungetregister32(t.register); { the register is not needed anymore }
  390. end;
  391. LOC_MEM,
  392. LOC_REFERENCE : begin
  393. if t.reference.is_immediate then
  394. emit_const_ref(A_MOV,siz,
  395. t.reference.offset,newreference(ref))
  396. else
  397. begin
  398. case siz of
  399. S_B : begin
  400. { we can't do a getregister in the code generator }
  401. { without problems!!! }
  402. if usablereg32>0 then
  403. hreg:=reg32toreg8(getregister32)
  404. else
  405. begin
  406. emit_reg(A_PUSH,S_L,R_EAX);
  407. pushedeax:=true;
  408. hreg:=R_AL;
  409. end;
  410. end;
  411. S_W : hreg:=R_DI;
  412. S_L : hreg:=R_EDI;
  413. end;
  414. if hreg in [R_DI,R_EDI] then
  415. getexplicitregister32(R_EDI);
  416. emit_ref_reg(A_MOV,siz,
  417. newreference(t.reference),hreg);
  418. del_reference(t.reference);
  419. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,siz,
  420. hreg,newreference(ref))));
  421. if siz=S_B then
  422. begin
  423. if pushedeax then
  424. emit_reg(A_POP,S_L,R_EAX)
  425. else
  426. ungetregister(hreg);
  427. end;
  428. if hreg in [R_DI,R_EDI] then
  429. ungetregister32(R_EDI);
  430. { we can release the registers }
  431. { but only AFTER the MOV! Important for the optimizer!
  432. (JM)}
  433. del_reference(ref);
  434. end;
  435. if freetemp then
  436. ungetiftemp(t.reference);
  437. end;
  438. else
  439. internalerror(330);
  440. end;
  441. end;
  442. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  443. begin
  444. case t.loc of
  445. LOC_REGISTER,
  446. LOC_CREGISTER : begin
  447. emit_reg_reg(A_MOV,S_L,t.register,reg);
  448. ungetregister32(t.register); { the register is not needed anymore }
  449. end;
  450. LOC_MEM,
  451. LOC_REFERENCE : begin
  452. if t.reference.is_immediate then
  453. emit_const_reg(A_MOV,S_L,
  454. t.reference.offset,reg)
  455. else
  456. begin
  457. emit_ref_reg(A_MOV,S_L,
  458. newreference(t.reference),reg);
  459. end;
  460. end;
  461. else
  462. internalerror(330);
  463. end;
  464. end;
  465. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  466. begin
  467. case t.loc of
  468. LOC_REGISTER,
  469. LOC_CREGISTER : begin
  470. emit_reg_reg(A_MOV,RegSize(Reg),
  471. reg,t.register);
  472. end;
  473. LOC_MEM,
  474. LOC_REFERENCE : begin
  475. if t.reference.is_immediate then
  476. internalerror(334)
  477. else
  478. begin
  479. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,RegSize(Reg),
  480. Reg,newreference(t.reference))));
  481. end;
  482. end;
  483. else
  484. internalerror(330);
  485. end;
  486. end;
  487. procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
  488. begin
  489. case t.loc of
  490. LOC_MEM,
  491. LOC_REFERENCE : begin
  492. if t.reference.is_immediate then
  493. internalerror(331)
  494. else
  495. begin
  496. emit_ref_reg(A_LEA,S_L,
  497. newreference(t.reference),reg);
  498. end;
  499. if freetemp then
  500. ungetiftemp(t.reference);
  501. end;
  502. else
  503. internalerror(332);
  504. end;
  505. end;
  506. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  507. begin
  508. case t.loc of
  509. LOC_REGISTER,
  510. LOC_CREGISTER : begin
  511. emit_reg_reg(A_MOV,S_L,
  512. reglow,t.registerlow);
  513. emit_reg_reg(A_MOV,S_L,
  514. reghigh,t.registerhigh);
  515. end;
  516. LOC_MEM,
  517. LOC_REFERENCE : begin
  518. if t.reference.is_immediate then
  519. internalerror(334)
  520. else
  521. begin
  522. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
  523. Reglow,newreference(t.reference))));
  524. inc(t.reference.offset,4);
  525. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
  526. Reghigh,newreference(t.reference))));
  527. end;
  528. end;
  529. else
  530. internalerror(330);
  531. end;
  532. end;
  533. procedure emit_pushq_loc(const t : tlocation);
  534. var
  535. hr : preference;
  536. begin
  537. case t.loc of
  538. LOC_REGISTER,
  539. LOC_CREGISTER:
  540. begin
  541. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,
  542. t.registerhigh)));
  543. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,
  544. t.registerlow)));
  545. end;
  546. LOC_MEM,
  547. LOC_REFERENCE:
  548. begin
  549. hr:=newreference(t.reference);
  550. inc(hr^.offset,4);
  551. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,
  552. hr)));
  553. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,
  554. newreference(t.reference))));
  555. ungetiftemp(t.reference);
  556. end;
  557. else internalerror(331);
  558. end;
  559. end;
  560. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: byte);
  561. begin
  562. case t.loc of
  563. LOC_REGISTER:
  564. { can't be a regvar, since it would be LOC_CREGISTER then }
  565. regs := regs and not($80 shr byte(t.register));
  566. LOC_MEM,LOC_REFERENCE:
  567. begin
  568. if not(cs_regalloc in aktglobalswitches) or
  569. (t.reference.base in usableregs) then
  570. regs := regs and
  571. not($80 shr byte(t.reference.base));
  572. if not(cs_regalloc in aktglobalswitches) or
  573. (t.reference.index in usableregs) then
  574. regs := regs and
  575. not($80 shr byte(t.reference.index));
  576. end;
  577. end;
  578. end;
  579. procedure release_loc(const t : tlocation);
  580. begin
  581. case t.loc of
  582. LOC_REGISTER,
  583. LOC_CREGISTER:
  584. begin
  585. ungetregister32(t.register);
  586. end;
  587. LOC_MEM,
  588. LOC_REFERENCE:
  589. del_reference(t.reference);
  590. else internalerror(332);
  591. end;
  592. end;
  593. procedure release_qword_loc(const t : tlocation);
  594. begin
  595. case t.loc of
  596. LOC_REGISTER,
  597. LOC_CREGISTER:
  598. begin
  599. ungetregister32(t.registerhigh);
  600. ungetregister32(t.registerlow);
  601. end;
  602. LOC_MEM,
  603. LOC_REFERENCE:
  604. del_reference(t.reference);
  605. else internalerror(331);
  606. end;
  607. end;
  608. procedure emit_push_loc(const t:tlocation);
  609. begin
  610. case t.loc of
  611. LOC_REGISTER,
  612. LOC_CREGISTER : begin
  613. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,makereg32(t.register))));
  614. ungetregister(t.register); { the register is not needed anymore }
  615. end;
  616. LOC_MEM,
  617. LOC_REFERENCE : begin
  618. if t.reference.is_immediate then
  619. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,t.reference.offset)))
  620. else
  621. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,newreference(t.reference))));
  622. del_reference(t.reference);
  623. ungetiftemp(t.reference);
  624. end;
  625. else
  626. internalerror(330);
  627. end;
  628. end;
  629. procedure emit_pushw_loc(const t:tlocation);
  630. var
  631. opsize : topsize;
  632. begin
  633. case t.loc of
  634. LOC_REGISTER,
  635. LOC_CREGISTER : begin
  636. if target_os.stackalignment=4 then
  637. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,makereg32(t.register))))
  638. else
  639. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_W,makereg16(t.register))));
  640. ungetregister(t.register); { the register is not needed anymore }
  641. end;
  642. LOC_MEM,
  643. LOC_REFERENCE : begin
  644. if target_os.stackalignment=4 then
  645. opsize:=S_L
  646. else
  647. opsize:=S_W;
  648. if t.reference.is_immediate then
  649. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,opsize,t.reference.offset)))
  650. else
  651. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,opsize,newreference(t.reference))));
  652. del_reference(t.reference);
  653. ungetiftemp(t.reference);
  654. end;
  655. else
  656. internalerror(330);
  657. end;
  658. end;
  659. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
  660. begin
  661. case t.loc of
  662. LOC_MEM,
  663. LOC_REFERENCE : begin
  664. if t.reference.is_immediate then
  665. internalerror(331)
  666. else
  667. begin
  668. getexplicitregister32(R_EDI);
  669. emit_ref_reg(A_LEA,S_L,
  670. newreference(t.reference),R_EDI);
  671. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
  672. R_EDI,newreference(ref))));
  673. ungetregister32(R_EDI);
  674. end;
  675. { release the registers }
  676. del_reference(t.reference);
  677. if freetemp then
  678. ungetiftemp(t.reference);
  679. end;
  680. else
  681. internalerror(332);
  682. end;
  683. end;
  684. procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
  685. begin
  686. case t.loc of
  687. LOC_MEM,
  688. LOC_REFERENCE : begin
  689. if t.reference.is_immediate then
  690. internalerror(331)
  691. else
  692. begin
  693. getexplicitregister32(R_EDI);
  694. emit_ref_reg(A_LEA,S_L,
  695. newreference(t.reference),R_EDI);
  696. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
  697. ungetregister32(R_EDI);
  698. end;
  699. if freetemp then
  700. ungetiftemp(t.reference);
  701. end;
  702. else
  703. internalerror(332);
  704. end;
  705. end;
  706. procedure emit_push_mem_size(const t: treference; size: longint);
  707. var
  708. s: topsize;
  709. begin
  710. if t.is_immediate then
  711. begin
  712. if (size=4) or
  713. (target_os.stackalignment=4) then
  714. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,t.offset)))
  715. else
  716. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_W,t.offset)));
  717. end
  718. else
  719. if size < 4 then
  720. begin
  721. getexplicitregister32(R_EDI);
  722. case size of
  723. 1: s := S_BL;
  724. 2: s := S_WL;
  725. else internalerror(200008071);
  726. end;
  727. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOVZX,s,
  728. newreference(t),R_EDI)));
  729. if target_os.stackalignment=4 then
  730. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)))
  731. else
  732. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_W,R_DI)));
  733. ungetregister32(R_EDI);
  734. end
  735. else
  736. if size = 4 then
  737. emit_push_mem(t)
  738. else
  739. internalerror(200008072);
  740. end;
  741. procedure emit_to_mem(var t:tlocation;def:pdef);
  742. var
  743. r : treference;
  744. begin
  745. case t.loc of
  746. LOC_FPU : begin
  747. reset_reference(t.reference);
  748. gettempofsizereference(10,t.reference);
  749. floatstore(pfloatdef(def)^.typ,t.reference);
  750. end;
  751. LOC_REGISTER:
  752. begin
  753. if is_64bitint(def) then
  754. begin
  755. gettempofsizereference(8,r);
  756. emit_reg_ref(A_MOV,S_L,t.registerlow,newreference(r));
  757. inc(r.offset,4);
  758. emit_reg_ref(A_MOV,S_L,t.registerhigh,newreference(r));
  759. dec(r.offset,4);
  760. t.reference:=r;
  761. end
  762. else
  763. internalerror(1405001);
  764. end;
  765. LOC_MEM,
  766. LOC_REFERENCE : ;
  767. LOC_CFPUREGISTER : begin
  768. emit_reg(A_FLD,S_NO,correct_fpuregister(t.register,fpuvaroffset));
  769. inc(fpuvaroffset);
  770. reset_reference(t.reference);
  771. gettempofsizereference(10,t.reference);
  772. floatstore(pfloatdef(def)^.typ,t.reference);
  773. end;
  774. else
  775. internalerror(333);
  776. end;
  777. t.loc:=LOC_MEM;
  778. end;
  779. procedure emit_to_reg16(var hr:tregister);
  780. begin
  781. { ranges are a little bit bug sensitive ! }
  782. case hr of
  783. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP,R_EBP:
  784. begin
  785. hr:=reg32toreg16(hr);
  786. end;
  787. R_AL,R_BL,R_CL,R_DL:
  788. begin
  789. hr:=reg8toreg16(hr);
  790. emit_const_reg(A_AND,S_W,$ff,hr);
  791. end;
  792. R_AH,R_BH,R_CH,R_DH:
  793. begin
  794. hr:=reg8toreg16(hr);
  795. emit_const_reg(A_AND,S_W,$ff00,hr);
  796. end;
  797. end;
  798. end;
  799. procedure emit_to_reg32(var hr:tregister);
  800. begin
  801. { ranges are a little bit bug sensitive ! }
  802. case hr of
  803. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP,R_BP:
  804. begin
  805. hr:=reg16toreg32(hr);
  806. emit_const_reg(A_AND,S_L,$ffff,hr);
  807. end;
  808. R_AL,R_BL,R_CL,R_DL:
  809. begin
  810. hr:=reg8toreg32(hr);
  811. emit_const_reg(A_AND,S_L,$ff,hr);
  812. end;
  813. R_AH,R_BH,R_CH,R_DH:
  814. begin
  815. hr:=reg8toreg32(hr);
  816. emit_const_reg(A_AND,S_L,$ff00,hr);
  817. end;
  818. end;
  819. end;
  820. procedure emit_mov_ref_reg64(r : treference;rl,rh : tregister);
  821. var
  822. hr : preference;
  823. begin
  824. { if we load a 64 bit reference, we must be careful because }
  825. { we could overwrite the registers of the reference by }
  826. { accident }
  827. getexplicitregister32(R_EDI);
  828. if r.base=rl then
  829. begin
  830. emit_reg_reg(A_MOV,S_L,r.base,
  831. R_EDI);
  832. r.base:=R_EDI;
  833. end
  834. else if r.index=rl then
  835. begin
  836. emit_reg_reg(A_MOV,S_L,r.index,
  837. R_EDI);
  838. r.index:=R_EDI;
  839. end;
  840. emit_ref_reg(A_MOV,S_L,
  841. newreference(r),rl);
  842. hr:=newreference(r);
  843. inc(hr^.offset,4);
  844. emit_ref_reg(A_MOV,S_L,
  845. hr,rh);
  846. ungetregister32(R_EDI);
  847. end;
  848. {*****************************************************************************
  849. Emit String Functions
  850. *****************************************************************************}
  851. procedure incrcomintfref(t: pdef; const ref: treference);
  852. var
  853. pushedregs : tpushed;
  854. begin
  855. pushusedregisters(pushedregs,$ff);
  856. emitpushreferenceaddr(ref);
  857. if is_interfacecom(t) then
  858. begin
  859. emitcall('FPC_INTF_INCR_REF');
  860. end
  861. else internalerror(1859);
  862. popusedregisters(pushedregs);
  863. end;
  864. procedure decrcomintfref(t: pdef; const ref: treference);
  865. var
  866. pushedregs : tpushed;
  867. begin
  868. pushusedregisters(pushedregs,$ff);
  869. emitpushreferenceaddr(ref);
  870. if is_interfacecom(t) then
  871. begin
  872. emitcall('FPC_INTF_DECR_REF');
  873. end
  874. else internalerror(1859);
  875. popusedregisters(pushedregs);
  876. end;
  877. procedure copyshortstring(const dref,sref : treference;len : byte;
  878. loadref, del_sref: boolean);
  879. begin
  880. emitpushreferenceaddr(dref);
  881. { if it's deleted right before it's used, the optimizer can move }
  882. { the reg deallocations to the right places (JM) }
  883. if del_sref then
  884. del_reference(sref);
  885. if loadref then
  886. emit_push_mem(sref)
  887. else
  888. emitpushreferenceaddr(sref);
  889. push_int(len);
  890. emitcall('FPC_SHORTSTR_COPY');
  891. maybe_loadesi;
  892. end;
  893. procedure copylongstring(const dref,sref : treference;len : longint;loadref:boolean);
  894. begin
  895. emitpushreferenceaddr(dref);
  896. if loadref then
  897. emit_push_mem(sref)
  898. else
  899. emitpushreferenceaddr(sref);
  900. push_int(len);
  901. emitcall('FPC_LONGSTR_COPY');
  902. maybe_loadesi;
  903. end;
  904. procedure incrstringref(t : pdef;const ref : treference);
  905. var
  906. pushedregs : tpushed;
  907. begin
  908. pushusedregisters(pushedregs,$ff);
  909. emitpushreferenceaddr(ref);
  910. if is_ansistring(t) then
  911. begin
  912. emitcall('FPC_ANSISTR_INCR_REF');
  913. end
  914. else if is_widestring(t) then
  915. begin
  916. emitcall('FPC_WIDESTR_INCR_REF');
  917. end
  918. else internalerror(1859);
  919. popusedregisters(pushedregs);
  920. end;
  921. procedure decrstringref(t : pdef;const ref : treference);
  922. var
  923. pushedregs : tpushed;
  924. begin
  925. pushusedregisters(pushedregs,$ff);
  926. emitpushreferenceaddr(ref);
  927. if is_ansistring(t) then
  928. begin
  929. emitcall('FPC_ANSISTR_DECR_REF');
  930. end
  931. else if is_widestring(t) then
  932. begin
  933. emitcall('FPC_WIDESTR_DECR_REF');
  934. end
  935. else internalerror(1859);
  936. popusedregisters(pushedregs);
  937. end;
  938. {*****************************************************************************
  939. Emit Push Functions
  940. *****************************************************************************}
  941. procedure push_int(l : longint);
  942. begin
  943. if (l = 0) and
  944. not(aktoptprocessor in [Class386, ClassP6]) and
  945. not(cs_littlesize in aktglobalswitches)
  946. Then
  947. begin
  948. getexplicitregister32(R_EDI);
  949. emit_reg_reg(A_XOR,S_L,R_EDI,R_EDI);
  950. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
  951. ungetregister32(R_EDI);
  952. end
  953. else
  954. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,l)));
  955. end;
  956. procedure emit_push_mem(const ref : treference);
  957. begin
  958. if ref.is_immediate then
  959. push_int(ref.offset)
  960. else
  961. begin
  962. if not(aktoptprocessor in [Class386, ClassP6]) and
  963. not(cs_littlesize in aktglobalswitches)
  964. then
  965. begin
  966. getexplicitregister32(R_EDI);
  967. emit_ref_reg(A_MOV,S_L,newreference(ref),R_EDI);
  968. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
  969. ungetregister32(R_EDI);
  970. end
  971. else exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,newreference(ref))));
  972. end;
  973. end;
  974. procedure emitpushreferenceaddr(const ref : treference);
  975. var
  976. href : treference;
  977. begin
  978. { this will fail for references to other segments !!! }
  979. if ref.is_immediate then
  980. { is this right ? }
  981. begin
  982. { push_int(ref.offset)}
  983. gettempofsizereference(4,href);
  984. emit_const_ref(A_MOV,S_L,ref.offset,newreference(href));
  985. emitpushreferenceaddr(href);
  986. del_reference(href);
  987. end
  988. else
  989. begin
  990. if ref.segment<>R_NO then
  991. CGMessage(cg_e_cant_use_far_pointer_there);
  992. if (ref.base=R_NO) and (ref.index=R_NO) then
  993. exprasmlist^.concat(new(paicpu,op_sym_ofs(A_PUSH,S_L,ref.symbol,ref.offset)))
  994. else if (ref.base=R_NO) and (ref.index<>R_NO) and
  995. (ref.offset=0) and (ref.scalefactor=0) and (ref.symbol=nil) then
  996. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,ref.index)))
  997. else if (ref.base<>R_NO) and (ref.index=R_NO) and
  998. (ref.offset=0) and (ref.symbol=nil) then
  999. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,ref.base)))
  1000. else
  1001. begin
  1002. getexplicitregister32(R_EDI);
  1003. emit_ref_reg(A_LEA,S_L,newreference(ref),R_EDI);
  1004. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
  1005. ungetregister32(R_EDI);
  1006. end;
  1007. end;
  1008. end;
  1009. {*****************************************************************************
  1010. Emit Float Functions
  1011. *****************************************************************************}
  1012. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  1013. begin
  1014. case t of
  1015. s32real : begin
  1016. op:=A_FLD;
  1017. s:=S_FS;
  1018. end;
  1019. s64real : begin
  1020. op:=A_FLD;
  1021. { ???? }
  1022. s:=S_FL;
  1023. end;
  1024. s80real : begin
  1025. op:=A_FLD;
  1026. s:=S_FX;
  1027. end;
  1028. s64comp : begin
  1029. op:=A_FILD;
  1030. s:=S_IQ;
  1031. end;
  1032. else internalerror(17);
  1033. end;
  1034. end;
  1035. procedure floatload(t : tfloattype;const ref : treference);
  1036. var
  1037. op : tasmop;
  1038. s : topsize;
  1039. begin
  1040. floatloadops(t,op,s);
  1041. exprasmlist^.concat(new(paicpu,op_ref(op,s,
  1042. newreference(ref))));
  1043. inc(fpuvaroffset);
  1044. end;
  1045. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  1046. begin
  1047. case t of
  1048. s32real : begin
  1049. op:=A_FSTP;
  1050. s:=S_FS;
  1051. end;
  1052. s64real : begin
  1053. op:=A_FSTP;
  1054. s:=S_FL;
  1055. end;
  1056. s80real : begin
  1057. op:=A_FSTP;
  1058. s:=S_FX;
  1059. end;
  1060. s64comp : begin
  1061. op:=A_FISTP;
  1062. s:=S_IQ;
  1063. end;
  1064. else
  1065. internalerror(17);
  1066. end;
  1067. end;
  1068. procedure floatstore(t : tfloattype;const ref : treference);
  1069. var
  1070. op : tasmop;
  1071. s : topsize;
  1072. begin
  1073. floatstoreops(t,op,s);
  1074. exprasmlist^.concat(new(paicpu,op_ref(op,s,
  1075. newreference(ref))));
  1076. dec(fpuvaroffset);
  1077. end;
  1078. {*****************************************************************************
  1079. Emit Functions
  1080. *****************************************************************************}
  1081. procedure concatcopy(source,dest : treference;size : longint;delsource,loadref : boolean);
  1082. const
  1083. isizes : array[0..3] of topsize=(S_L,S_B,S_W,S_B);
  1084. ishr : array[0..3] of byte=(2,0,1,0);
  1085. var
  1086. ecxpushed : boolean;
  1087. helpsize : longint;
  1088. i : byte;
  1089. reg8,reg32 : tregister;
  1090. swap : boolean;
  1091. procedure maybepushecx;
  1092. begin
  1093. if not(R_ECX in unused) then
  1094. begin
  1095. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_ECX)));
  1096. ecxpushed:=true;
  1097. end
  1098. else getexplicitregister32(R_ECX);
  1099. end;
  1100. begin
  1101. {$IfNDef regallocfix}
  1102. If delsource then
  1103. del_reference(source);
  1104. {$EndIf regallocfix}
  1105. if (not loadref) and
  1106. ((size<=8) or
  1107. (not(cs_littlesize in aktglobalswitches ) and (size<=12))) then
  1108. begin
  1109. helpsize:=size shr 2;
  1110. getexplicitregister32(R_EDI);
  1111. for i:=1 to helpsize do
  1112. begin
  1113. emit_ref_reg(A_MOV,S_L,newreference(source),R_EDI);
  1114. {$ifdef regallocfix}
  1115. If (size = 4) and delsource then
  1116. del_reference(source);
  1117. {$endif regallocfix}
  1118. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,newreference(dest))));
  1119. inc(source.offset,4);
  1120. inc(dest.offset,4);
  1121. dec(size,4);
  1122. end;
  1123. if size>1 then
  1124. begin
  1125. emit_ref_reg(A_MOV,S_W,newreference(source),R_DI);
  1126. {$ifdef regallocfix}
  1127. If (size = 2) and delsource then
  1128. del_reference(source);
  1129. {$endif regallocfix}
  1130. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_W,R_DI,newreference(dest))));
  1131. inc(source.offset,2);
  1132. inc(dest.offset,2);
  1133. dec(size,2);
  1134. end;
  1135. ungetregister32(R_EDI);
  1136. if size>0 then
  1137. begin
  1138. { and now look for an 8 bit register }
  1139. swap:=false;
  1140. if R_EAX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EAX))
  1141. else if R_EDX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EDX))
  1142. else if R_EBX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EBX))
  1143. else if R_ECX in unused then reg8:=reg32toreg8(getexplicitregister32(R_ECX))
  1144. else
  1145. begin
  1146. swap:=true;
  1147. { we need only to check 3 registers, because }
  1148. { one is always not index or base }
  1149. if (dest.base<>R_EAX) and (dest.index<>R_EAX) then
  1150. begin
  1151. reg8:=R_AL;
  1152. reg32:=R_EAX;
  1153. end
  1154. else if (dest.base<>R_EBX) and (dest.index<>R_EBX) then
  1155. begin
  1156. reg8:=R_BL;
  1157. reg32:=R_EBX;
  1158. end
  1159. else if (dest.base<>R_ECX) and (dest.index<>R_ECX) then
  1160. begin
  1161. reg8:=R_CL;
  1162. reg32:=R_ECX;
  1163. end;
  1164. end;
  1165. if swap then
  1166. { was earlier XCHG, of course nonsense }
  1167. begin
  1168. getexplicitregister32(R_EDI);
  1169. emit_reg_reg(A_MOV,S_L,reg32,R_EDI);
  1170. end;
  1171. emit_ref_reg(A_MOV,S_B,newreference(source),reg8);
  1172. {$ifdef regallocfix}
  1173. If delsource then
  1174. del_reference(source);
  1175. {$endif regallocfix}
  1176. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_B,reg8,newreference(dest))));
  1177. if swap then
  1178. begin
  1179. emit_reg_reg(A_MOV,S_L,R_EDI,reg32);
  1180. ungetregister32(R_EDI);
  1181. end
  1182. else
  1183. ungetregister(reg8);
  1184. end;
  1185. end
  1186. else
  1187. begin
  1188. getexplicitregister32(R_EDI);
  1189. emit_ref_reg(A_LEA,S_L,newreference(dest),R_EDI);
  1190. {$ifdef regallocfix}
  1191. {is this ok?? (JM)}
  1192. del_reference(dest);
  1193. {$endif regallocfix}
  1194. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  1195. if loadref then
  1196. emit_ref_reg(A_MOV,S_L,newreference(source),R_ESI)
  1197. else
  1198. begin
  1199. emit_ref_reg(A_LEA,S_L,newreference(source),R_ESI);
  1200. {$ifdef regallocfix}
  1201. if delsource then
  1202. del_reference(source);
  1203. {$endif regallocfix}
  1204. end;
  1205. exprasmlist^.concat(new(paicpu,op_none(A_CLD,S_NO)));
  1206. ecxpushed:=false;
  1207. if cs_littlesize in aktglobalswitches then
  1208. begin
  1209. maybepushecx;
  1210. emit_const_reg(A_MOV,S_L,size,R_ECX);
  1211. exprasmlist^.concat(new(paicpu,op_none(A_REP,S_NO)));
  1212. exprasmlist^.concat(new(paicpu,op_none(A_MOVSB,S_NO)));
  1213. end
  1214. else
  1215. begin
  1216. helpsize:=size shr 2;
  1217. size:=size and 3;
  1218. if helpsize>1 then
  1219. begin
  1220. maybepushecx;
  1221. emit_const_reg(A_MOV,S_L,helpsize,R_ECX);
  1222. exprasmlist^.concat(new(paicpu,op_none(A_REP,S_NO)));
  1223. end;
  1224. if helpsize>0 then
  1225. exprasmlist^.concat(new(paicpu,op_none(A_MOVSD,S_NO)));
  1226. if size>1 then
  1227. begin
  1228. dec(size,2);
  1229. exprasmlist^.concat(new(paicpu,op_none(A_MOVSW,S_NO)));
  1230. end;
  1231. if size=1 then
  1232. exprasmlist^.concat(new(paicpu,op_none(A_MOVSB,S_NO)));
  1233. end;
  1234. ungetregister32(R_EDI);
  1235. exprasmlist^.concat(new(pairegalloc,dealloc(R_ESI)));
  1236. if ecxpushed then
  1237. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_ECX)))
  1238. else
  1239. ungetregister32(R_ECX);
  1240. { loading SELF-reference again }
  1241. maybe_loadesi;
  1242. end;
  1243. if delsource then
  1244. ungetiftemp(source);
  1245. end;
  1246. procedure emitloadord2reg(const location:Tlocation;orddef:Porddef;
  1247. destreg:Tregister;delloc:boolean);
  1248. {A lot smaller and less bug sensitive than the original unfolded loads.}
  1249. var tai:Paicpu;
  1250. r:Preference;
  1251. begin
  1252. tai := nil;
  1253. case location.loc of
  1254. LOC_REGISTER,LOC_CREGISTER:
  1255. begin
  1256. case orddef^.typ of
  1257. u8bit:
  1258. tai:=new(paicpu,op_reg_reg(A_MOVZX,S_BL,location.register,destreg));
  1259. s8bit:
  1260. tai:=new(paicpu,op_reg_reg(A_MOVSX,S_BL,location.register,destreg));
  1261. u16bit:
  1262. tai:=new(paicpu,op_reg_reg(A_MOVZX,S_WL,location.register,destreg));
  1263. s16bit:
  1264. tai:=new(paicpu,op_reg_reg(A_MOVSX,S_WL,location.register,destreg));
  1265. u32bit,s32bit:
  1266. if location.register <> destreg then
  1267. tai:=new(paicpu,op_reg_reg(A_MOV,S_L,location.register,destreg));
  1268. end;
  1269. if delloc then
  1270. ungetregister(location.register);
  1271. end;
  1272. LOC_MEM,
  1273. LOC_REFERENCE:
  1274. begin
  1275. if location.reference.is_immediate then
  1276. tai:=new(paicpu,op_const_reg(A_MOV,S_L,location.reference.offset,destreg))
  1277. else
  1278. begin
  1279. r:=newreference(location.reference);
  1280. case orddef^.typ of
  1281. u8bit:
  1282. tai:=new(paicpu,op_ref_reg(A_MOVZX,S_BL,r,destreg));
  1283. s8bit:
  1284. tai:=new(paicpu,op_ref_reg(A_MOVSX,S_BL,r,destreg));
  1285. u16bit:
  1286. tai:=new(paicpu,op_ref_reg(A_MOVZX,S_WL,r,destreg));
  1287. s16bit:
  1288. tai:=new(paicpu,op_ref_reg(A_MOVSX,S_WL,r,destreg));
  1289. u32bit:
  1290. tai:=new(paicpu,op_ref_reg(A_MOV,S_L,r,destreg));
  1291. s32bit:
  1292. tai:=new(paicpu,op_ref_reg(A_MOV,S_L,r,destreg));
  1293. end;
  1294. end;
  1295. if delloc then
  1296. del_reference(location.reference);
  1297. end
  1298. else
  1299. internalerror(6);
  1300. end;
  1301. if assigned(tai) then
  1302. exprasmlist^.concat(tai);
  1303. end;
  1304. { if necessary ESI is reloaded after a call}
  1305. procedure maybe_loadesi;
  1306. var
  1307. hp : preference;
  1308. p : pprocinfo;
  1309. i : longint;
  1310. begin
  1311. if assigned(procinfo^._class) then
  1312. begin
  1313. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  1314. if lexlevel>normal_function_level then
  1315. begin
  1316. new(hp);
  1317. reset_reference(hp^);
  1318. hp^.offset:=procinfo^.framepointer_offset;
  1319. hp^.base:=procinfo^.framepointer;
  1320. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1321. p:=procinfo^.parent;
  1322. for i:=3 to lexlevel-1 do
  1323. begin
  1324. new(hp);
  1325. reset_reference(hp^);
  1326. hp^.offset:=p^.framepointer_offset;
  1327. hp^.base:=R_ESI;
  1328. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1329. p:=p^.parent;
  1330. end;
  1331. new(hp);
  1332. reset_reference(hp^);
  1333. hp^.offset:=p^.selfpointer_offset;
  1334. hp^.base:=R_ESI;
  1335. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1336. end
  1337. else
  1338. begin
  1339. new(hp);
  1340. reset_reference(hp^);
  1341. hp^.offset:=procinfo^.selfpointer_offset;
  1342. hp^.base:=procinfo^.framepointer;
  1343. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1344. end;
  1345. end;
  1346. end;
  1347. {*****************************************************************************
  1348. Entry/Exit Code Functions
  1349. *****************************************************************************}
  1350. procedure genprofilecode;
  1351. var
  1352. pl : pasmlabel;
  1353. begin
  1354. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1355. exit;
  1356. case target_info.target of
  1357. target_i386_freebsd,
  1358. target_i386_linux:
  1359. begin
  1360. getaddrlabel(pl);
  1361. emitinsertcall('mcount');
  1362. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  1363. exprasmlist^.insert(new(paicpu,op_sym_ofs_reg(A_MOV,S_L,pl,0,R_EDX)));
  1364. exprasmlist^.insert(new(pai_section,init(sec_code)));
  1365. exprasmlist^.insert(new(pai_const,init_32bit(0)));
  1366. exprasmlist^.insert(new(pai_label,init(pl)));
  1367. exprasmlist^.insert(new(pai_align,init(4)));
  1368. exprasmlist^.insert(new(pai_section,init(sec_data)));
  1369. end;
  1370. target_i386_go32v2:
  1371. begin
  1372. emitinsertcall('MCOUNT');
  1373. end;
  1374. end;
  1375. end;
  1376. procedure generate_interrupt_stackframe_entry;
  1377. begin
  1378. { save the registers of an interrupt procedure }
  1379. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EAX)));
  1380. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EBX)));
  1381. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_ECX)));
  1382. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EDX)));
  1383. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_ESI)));
  1384. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
  1385. { .... also the segment registers }
  1386. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_W,R_DS)));
  1387. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_W,R_ES)));
  1388. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_W,R_FS)));
  1389. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_W,R_GS)));
  1390. end;
  1391. procedure generate_interrupt_stackframe_exit;
  1392. begin
  1393. { restore the registers of an interrupt procedure }
  1394. { this was all with entrycode instead of exitcode !!}
  1395. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_EAX)));
  1396. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_EBX)));
  1397. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_ECX)));
  1398. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_EDX)));
  1399. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_ESI)));
  1400. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_EDI)));
  1401. { .... also the segment registers }
  1402. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_W,R_DS)));
  1403. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_W,R_ES)));
  1404. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_W,R_FS)));
  1405. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_W,R_GS)));
  1406. { this restores the flags }
  1407. procinfo^.aktexitcode^.concat(new(paicpu,op_none(A_IRET,S_NO)));
  1408. end;
  1409. { generates the code for threadvar initialisation }
  1410. procedure initialize_threadvar(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  1411. var
  1412. hr : treference;
  1413. begin
  1414. if (psym(p)^.typ=varsym) and
  1415. (vo_is_thread_var in pvarsym(p)^.varoptions) then
  1416. begin
  1417. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,pvarsym(p)^.getsize)));
  1418. reset_reference(hr);
  1419. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  1420. emitpushreferenceaddr(hr);
  1421. emitcall('FPC_INIT_THREADVAR');
  1422. end;
  1423. end;
  1424. { initilizes data of type t }
  1425. { if is_already_ref is true then the routines assumes }
  1426. { that r points to the data to initialize }
  1427. procedure initialize(t : pdef;const ref : treference;is_already_ref : boolean);
  1428. var
  1429. hr : treference;
  1430. begin
  1431. if is_ansistring(t) or
  1432. is_widestring(t) or
  1433. is_interfacecom(t) then
  1434. begin
  1435. emit_const_ref(A_MOV,S_L,0,
  1436. newreference(ref));
  1437. end
  1438. else
  1439. begin
  1440. reset_reference(hr);
  1441. hr.symbol:=pstoreddef(t)^.get_inittable_label;
  1442. emitpushreferenceaddr(hr);
  1443. if is_already_ref then
  1444. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,
  1445. newreference(ref))))
  1446. else
  1447. emitpushreferenceaddr(ref);
  1448. emitcall('FPC_INITIALIZE');
  1449. end;
  1450. end;
  1451. { finalizes data of type t }
  1452. { if is_already_ref is true then the routines assumes }
  1453. { that r points to the data to finalizes }
  1454. procedure finalize(t : pdef;const ref : treference;is_already_ref : boolean);
  1455. var
  1456. r : treference;
  1457. begin
  1458. if is_ansistring(t) or
  1459. is_widestring(t) then
  1460. begin
  1461. decrstringref(t,ref);
  1462. end
  1463. else if is_interfacecom(t) then
  1464. begin
  1465. decrcomintfref(t,ref);
  1466. end
  1467. else
  1468. begin
  1469. reset_reference(r);
  1470. r.symbol:=pstoreddef(t)^.get_inittable_label;
  1471. emitpushreferenceaddr(r);
  1472. if is_already_ref then
  1473. exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,
  1474. newreference(ref))))
  1475. else
  1476. emitpushreferenceaddr(ref);
  1477. emitcall('FPC_FINALIZE');
  1478. end;
  1479. end;
  1480. { generates the code for initialisation of local data }
  1481. procedure initialize_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  1482. var
  1483. hr : treference;
  1484. begin
  1485. if (psym(p)^.typ=varsym) and
  1486. assigned(pvarsym(p)^.vartype.def) and
  1487. not(is_class(pvarsym(p)^.vartype.def)) and
  1488. pvarsym(p)^.vartype.def^.needs_inittable then
  1489. begin
  1490. if assigned(procinfo) then
  1491. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1492. reset_reference(hr);
  1493. if psym(p)^.owner^.symtabletype in [localsymtable,inlinelocalsymtable] then
  1494. begin
  1495. hr.base:=procinfo^.framepointer;
  1496. hr.offset:=-pvarsym(p)^.address+pvarsym(p)^.owner^.address_fixup;
  1497. end
  1498. else
  1499. begin
  1500. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  1501. end;
  1502. initialize(pvarsym(p)^.vartype.def,hr,false);
  1503. end;
  1504. end;
  1505. { generates the code for incrementing the reference count of parameters and
  1506. initialize out parameters }
  1507. procedure init_paras(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  1508. var
  1509. hrv : treference;
  1510. hr: treference;
  1511. begin
  1512. if (psym(p)^.typ=varsym) and
  1513. not is_class(pvarsym(p)^.vartype.def) and
  1514. pvarsym(p)^.vartype.def^.needs_inittable then
  1515. begin
  1516. if (pvarsym(p)^.varspez=vs_value) then
  1517. begin
  1518. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1519. reset_reference(hrv);
  1520. hrv.base:=procinfo^.framepointer;
  1521. hrv.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1522. if is_ansistring(pvarsym(p)^.vartype.def) or
  1523. is_widestring(pvarsym(p)^.vartype.def) then
  1524. begin
  1525. incrstringref(pvarsym(p)^.vartype.def,hrv)
  1526. end
  1527. else if is_interfacecom(pvarsym(p)^.vartype.def) then
  1528. begin
  1529. incrcomintfref(pvarsym(p)^.vartype.def,hrv)
  1530. end
  1531. else
  1532. begin
  1533. reset_reference(hr);
  1534. hr.symbol:=pstoreddef(pvarsym(p)^.vartype.def)^.get_inittable_label;
  1535. emitpushreferenceaddr(hr);
  1536. emitpushreferenceaddr(hrv);
  1537. emitcall('FPC_ADDREF');
  1538. end;
  1539. end
  1540. else if (pvarsym(p)^.varspez=vs_out) then
  1541. begin
  1542. reset_reference(hrv);
  1543. hrv.base:=procinfo^.framepointer;
  1544. hrv.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1545. {$ifndef noAllocEdi}
  1546. getexplicitregister32(R_EDI);
  1547. {$endif noAllocEdi}
  1548. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOV,S_L,newreference(hrv),R_EDI)));
  1549. reset_reference(hr);
  1550. hr.base:=R_EDI;
  1551. initialize(pvarsym(p)^.vartype.def,hr,false);
  1552. end;
  1553. end;
  1554. end;
  1555. { generates the code for decrementing the reference count of parameters }
  1556. procedure final_paras(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  1557. var
  1558. hrv : treference;
  1559. hr: treference;
  1560. begin
  1561. if (psym(p)^.typ=varsym) and
  1562. not is_class(pvarsym(p)^.vartype.def) and
  1563. pvarsym(p)^.vartype.def^.needs_inittable then
  1564. begin
  1565. if (pvarsym(p)^.varspez=vs_value) then
  1566. begin
  1567. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1568. reset_reference(hrv);
  1569. hrv.base:=procinfo^.framepointer;
  1570. hrv.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1571. if is_ansistring(pvarsym(p)^.vartype.def) or
  1572. is_widestring(pvarsym(p)^.vartype.def) then
  1573. begin
  1574. decrstringref(pvarsym(p)^.vartype.def,hrv)
  1575. end
  1576. else if is_interfacecom(pvarsym(p)^.vartype.def) then
  1577. begin
  1578. decrcomintfref(pvarsym(p)^.vartype.def,hrv)
  1579. end
  1580. else
  1581. begin
  1582. reset_reference(hr);
  1583. hr.symbol:=pstoreddef(pvarsym(p)^.vartype.def)^.get_inittable_label;
  1584. emitpushreferenceaddr(hr);
  1585. emitpushreferenceaddr(hrv);
  1586. emitcall('FPC_DECREF');
  1587. end;
  1588. end;
  1589. end;
  1590. end;
  1591. { generates the code for finalisation of local data }
  1592. procedure finalize_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  1593. var
  1594. hr : treference;
  1595. begin
  1596. if (psym(p)^.typ=varsym) and
  1597. assigned(pvarsym(p)^.vartype.def) and
  1598. not(is_class(pvarsym(p)^.vartype.def)) and
  1599. pvarsym(p)^.vartype.def^.needs_inittable then
  1600. begin
  1601. if assigned(procinfo) then
  1602. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1603. reset_reference(hr);
  1604. case psym(p)^.owner^.symtabletype of
  1605. localsymtable,inlinelocalsymtable:
  1606. begin
  1607. hr.base:=procinfo^.framepointer;
  1608. hr.offset:=-pvarsym(p)^.address+pvarsym(p)^.owner^.address_fixup;
  1609. end;
  1610. else
  1611. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  1612. end;
  1613. finalize(pvarsym(p)^.vartype.def,hr,false);
  1614. end;
  1615. end;
  1616. { generates the code to make local copies of the value parameters }
  1617. procedure copyvalueparas(p : pnamedindexobject);{$ifndef fpc}far;{$endif}
  1618. var
  1619. href1,href2 : treference;
  1620. r : preference;
  1621. power,len : longint;
  1622. opsize : topsize;
  1623. again,ok : pasmlabel;
  1624. begin
  1625. if (psym(p)^.typ=varsym) and
  1626. (pvarsym(p)^.varspez=vs_value) and
  1627. (push_addr_param(pvarsym(p)^.vartype.def)) then
  1628. begin
  1629. if is_open_array(pvarsym(p)^.vartype.def) or
  1630. is_array_of_const(pvarsym(p)^.vartype.def) then
  1631. begin
  1632. { get stack space }
  1633. new(r);
  1634. reset_reference(r^);
  1635. r^.base:=procinfo^.framepointer;
  1636. r^.offset:=pvarsym(p)^.address+4+procinfo^.para_offset;
  1637. getexplicitregister32(R_EDI);
  1638. exprasmlist^.concat(new(paicpu,
  1639. op_ref_reg(A_MOV,S_L,r,R_EDI)));
  1640. exprasmlist^.concat(new(paicpu,
  1641. op_reg(A_INC,S_L,R_EDI)));
  1642. if (parraydef(pvarsym(p)^.vartype.def)^.elesize<>1) then
  1643. begin
  1644. if ispowerof2(parraydef(pvarsym(p)^.vartype.def)^.elesize, power) then
  1645. exprasmlist^.concat(new(paicpu,
  1646. op_const_reg(A_SHL,S_L,
  1647. power,R_EDI)))
  1648. else
  1649. exprasmlist^.concat(new(paicpu,
  1650. op_const_reg(A_IMUL,S_L,
  1651. parraydef(pvarsym(p)^.vartype.def)^.elesize,R_EDI)));
  1652. end;
  1653. {$ifndef NOTARGETWIN32}
  1654. { windows guards only a few pages for stack growing, }
  1655. { so we have to access every page first }
  1656. if target_os.id=os_i386_win32 then
  1657. begin
  1658. getlabel(again);
  1659. getlabel(ok);
  1660. emitlab(again);
  1661. exprasmlist^.concat(new(paicpu,
  1662. op_const_reg(A_CMP,S_L,winstackpagesize,R_EDI)));
  1663. emitjmp(C_C,ok);
  1664. exprasmlist^.concat(new(paicpu,
  1665. op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP)));
  1666. exprasmlist^.concat(new(paicpu,
  1667. op_reg(A_PUSH,S_L,R_EAX)));
  1668. exprasmlist^.concat(new(paicpu,
  1669. op_const_reg(A_SUB,S_L,winstackpagesize,R_EDI)));
  1670. emitjmp(C_None,again);
  1671. emitlab(ok);
  1672. exprasmlist^.concat(new(paicpu,
  1673. op_reg_reg(A_SUB,S_L,R_EDI,R_ESP)));
  1674. ungetregister32(R_EDI);
  1675. { now reload EDI }
  1676. new(r);
  1677. reset_reference(r^);
  1678. r^.base:=procinfo^.framepointer;
  1679. r^.offset:=pvarsym(p)^.address+4+procinfo^.para_offset;
  1680. getexplicitregister32(R_EDI);
  1681. exprasmlist^.concat(new(paicpu,
  1682. op_ref_reg(A_MOV,S_L,r,R_EDI)));
  1683. exprasmlist^.concat(new(paicpu,
  1684. op_reg(A_INC,S_L,R_EDI)));
  1685. if (parraydef(pvarsym(p)^.vartype.def)^.elesize<>1) then
  1686. begin
  1687. if ispowerof2(parraydef(pvarsym(p)^.vartype.def)^.elesize, power) then
  1688. exprasmlist^.concat(new(paicpu,
  1689. op_const_reg(A_SHL,S_L,
  1690. power,R_EDI)))
  1691. else
  1692. exprasmlist^.concat(new(paicpu,
  1693. op_const_reg(A_IMUL,S_L,
  1694. parraydef(pvarsym(p)^.vartype.def)^.elesize,R_EDI)));
  1695. end;
  1696. end
  1697. else
  1698. {$endif NOTARGETWIN32}
  1699. exprasmlist^.concat(new(paicpu,
  1700. op_reg_reg(A_SUB,S_L,R_EDI,R_ESP)));
  1701. { load destination }
  1702. exprasmlist^.concat(new(paicpu,
  1703. op_reg_reg(A_MOV,S_L,R_ESP,R_EDI)));
  1704. { don't destroy the registers! }
  1705. exprasmlist^.concat(new(paicpu,
  1706. op_reg(A_PUSH,S_L,R_ECX)));
  1707. exprasmlist^.concat(new(paicpu,
  1708. op_reg(A_PUSH,S_L,R_ESI)));
  1709. { load count }
  1710. new(r);
  1711. reset_reference(r^);
  1712. r^.base:=procinfo^.framepointer;
  1713. r^.offset:=pvarsym(p)^.address+4+procinfo^.para_offset;
  1714. exprasmlist^.concat(new(paicpu,
  1715. op_ref_reg(A_MOV,S_L,r,R_ECX)));
  1716. { load source }
  1717. new(r);
  1718. reset_reference(r^);
  1719. r^.base:=procinfo^.framepointer;
  1720. r^.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1721. exprasmlist^.concat(new(paicpu,
  1722. op_ref_reg(A_MOV,S_L,r,R_ESI)));
  1723. { scheduled .... }
  1724. exprasmlist^.concat(new(paicpu,
  1725. op_reg(A_INC,S_L,R_ECX)));
  1726. { calculate size }
  1727. len:=parraydef(pvarsym(p)^.vartype.def)^.elesize;
  1728. opsize:=S_B;
  1729. if (len and 3)=0 then
  1730. begin
  1731. opsize:=S_L;
  1732. len:=len shr 2;
  1733. end
  1734. else
  1735. if (len and 1)=0 then
  1736. begin
  1737. opsize:=S_W;
  1738. len:=len shr 1;
  1739. end;
  1740. if ispowerof2(len, power) then
  1741. exprasmlist^.concat(new(paicpu,
  1742. op_const_reg(A_SHL,S_L,
  1743. power,R_ECX)))
  1744. else
  1745. exprasmlist^.concat(new(paicpu,
  1746. op_const_reg(A_IMUL,S_L,len,R_ECX)));
  1747. exprasmlist^.concat(new(paicpu,
  1748. op_none(A_REP,S_NO)));
  1749. case opsize of
  1750. S_B : exprasmlist^.concat(new(paicpu,op_none(A_MOVSB,S_NO)));
  1751. S_W : exprasmlist^.concat(new(paicpu,op_none(A_MOVSW,S_NO)));
  1752. S_L : exprasmlist^.concat(new(paicpu,op_none(A_MOVSD,S_NO)));
  1753. end;
  1754. ungetregister32(R_EDI);
  1755. exprasmlist^.concat(new(paicpu,
  1756. op_reg(A_POP,S_L,R_ESI)));
  1757. exprasmlist^.concat(new(paicpu,
  1758. op_reg(A_POP,S_L,R_ECX)));
  1759. { patch the new address }
  1760. new(r);
  1761. reset_reference(r^);
  1762. r^.base:=procinfo^.framepointer;
  1763. r^.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1764. exprasmlist^.concat(new(paicpu,
  1765. op_reg_ref(A_MOV,S_L,R_ESP,r)));
  1766. end
  1767. else
  1768. if is_shortstring(pvarsym(p)^.vartype.def) then
  1769. begin
  1770. reset_reference(href1);
  1771. href1.base:=procinfo^.framepointer;
  1772. href1.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1773. reset_reference(href2);
  1774. href2.base:=procinfo^.framepointer;
  1775. href2.offset:=-pvarsym(p)^.localvarsym^.address+pvarsym(p)^.localvarsym^.owner^.address_fixup;
  1776. copyshortstring(href2,href1,pstringdef(pvarsym(p)^.vartype.def)^.len,true,false);
  1777. end
  1778. else
  1779. begin
  1780. reset_reference(href1);
  1781. href1.base:=procinfo^.framepointer;
  1782. href1.offset:=pvarsym(p)^.address+procinfo^.para_offset;
  1783. reset_reference(href2);
  1784. href2.base:=procinfo^.framepointer;
  1785. href2.offset:=-pvarsym(p)^.localvarsym^.address+pvarsym(p)^.localvarsym^.owner^.address_fixup;
  1786. concatcopy(href1,href2,pvarsym(p)^.vartype.def^.size,true,true);
  1787. end;
  1788. end;
  1789. end;
  1790. procedure inittempvariables;
  1791. var
  1792. hp : ptemprecord;
  1793. r : preference;
  1794. begin
  1795. hp:=templist;
  1796. while assigned(hp) do
  1797. begin
  1798. if hp^.temptype in [tt_ansistring,tt_freeansistring,tt_interfacecom] then
  1799. begin
  1800. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1801. new(r);
  1802. reset_reference(r^);
  1803. r^.base:=procinfo^.framepointer;
  1804. r^.offset:=hp^.pos;
  1805. emit_const_ref(A_MOV,S_L,0,r);
  1806. end;
  1807. hp:=hp^.next;
  1808. end;
  1809. end;
  1810. procedure finalizetempvariables;
  1811. var
  1812. hp : ptemprecord;
  1813. hr : treference;
  1814. begin
  1815. hp:=templist;
  1816. while assigned(hp) do
  1817. begin
  1818. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  1819. begin
  1820. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1821. reset_reference(hr);
  1822. hr.base:=procinfo^.framepointer;
  1823. hr.offset:=hp^.pos;
  1824. emitpushreferenceaddr(hr);
  1825. emitcall('FPC_ANSISTR_DECR_REF');
  1826. end
  1827. else if hp^.temptype=tt_interfacecom then
  1828. begin
  1829. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1830. reset_reference(hr);
  1831. hr.base:=procinfo^.framepointer;
  1832. hr.offset:=hp^.pos;
  1833. emitpushreferenceaddr(hr);
  1834. emitcall('FPC_INTF_DECR_REF');
  1835. end;
  1836. hp:=hp^.next;
  1837. end;
  1838. end;
  1839. var
  1840. ls : longint;
  1841. procedure largest_size(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  1842. begin
  1843. if (psym(p)^.typ=varsym) and
  1844. (pvarsym(p)^.getvaluesize>ls) then
  1845. ls:=pvarsym(p)^.getvaluesize;
  1846. end;
  1847. procedure alignstack(alist : paasmoutput);
  1848. begin
  1849. {$ifdef dummy}
  1850. if (cs_optimize in aktglobalswitches) and
  1851. (aktoptprocessor in [classp5,classp6]) then
  1852. begin
  1853. ls:=0;
  1854. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}largest_size);
  1855. if ls>=8 then
  1856. alist^.insert(new(paicpu,op_const_reg(A_AND,S_L,-8,R_ESP)));
  1857. end;
  1858. {$endif dummy}
  1859. end;
  1860. procedure genentrycode(alist : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
  1861. stackframe:longint;
  1862. var parasize:longint;var nostackframe:boolean;
  1863. inlined : boolean);
  1864. {
  1865. Generates the entry code for a procedure
  1866. }
  1867. var
  1868. hs : string;
  1869. {$ifdef GDB}
  1870. stab_function_name : Pai_stab_function_name;
  1871. {$endif GDB}
  1872. hr : preference;
  1873. p : psymtable;
  1874. r : treference;
  1875. oldlist,
  1876. oldexprasmlist : paasmoutput;
  1877. again : pasmlabel;
  1878. i : longint;
  1879. begin
  1880. oldexprasmlist:=exprasmlist;
  1881. exprasmlist:=alist;
  1882. if (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
  1883. begin
  1884. emitinsertcall('FPC_INITIALIZEUNITS');
  1885. oldlist:=exprasmlist;
  1886. exprasmlist:=new(paasmoutput,init);
  1887. p:=symtablestack;
  1888. while assigned(p) do
  1889. begin
  1890. p^.foreach({$ifndef TP}@{$endif}initialize_threadvar);
  1891. p:=p^.next;
  1892. end;
  1893. oldlist^.insertlist(exprasmlist);
  1894. dispose(exprasmlist,done);
  1895. exprasmlist:=oldlist;
  1896. end;
  1897. {$ifdef GDB}
  1898. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  1899. exprasmlist^.insert(new(pai_force_line,init));
  1900. {$endif GDB}
  1901. { a constructor needs a help procedure }
  1902. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1903. begin
  1904. if is_class(procinfo^._class) then
  1905. begin
  1906. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1907. exprasmlist^.insert(new(paicpu,op_cond_sym(A_Jcc,C_Z,S_NO,faillabel)));
  1908. emitinsertcall('FPC_NEW_CLASS');
  1909. end
  1910. else if is_object(procinfo^._class) then
  1911. begin
  1912. exprasmlist^.insert(new(paicpu,op_cond_sym(A_Jcc,C_Z,S_NO,faillabel)));
  1913. emitinsertcall('FPC_HELP_CONSTRUCTOR');
  1914. getexplicitregister32(R_EDI);
  1915. exprasmlist^.insert(new(paicpu,op_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI)));
  1916. end
  1917. else
  1918. Internalerror(200006161);
  1919. end;
  1920. { don't load ESI, does the caller }
  1921. { we must do it for local function }
  1922. { that can be called from a foreach }
  1923. { of another object than self !! PM }
  1924. if assigned(procinfo^._class) and { !!!!! shouldn't we load ESI always? }
  1925. (lexlevel>normal_function_level) then
  1926. maybe_loadesi;
  1927. { When message method contains self as a parameter,
  1928. we must load it into ESI }
  1929. If (po_containsself in aktprocsym^.definition^.procoptions) then
  1930. begin
  1931. new(hr);
  1932. reset_reference(hr^);
  1933. hr^.offset:=procinfo^.selfpointer_offset;
  1934. hr^.base:=procinfo^.framepointer;
  1935. exprasmlist^.insert(new(paicpu,op_ref_reg(A_MOV,S_L,hr,R_ESI)));
  1936. exprasmlist^.insert(new(pairegalloc,alloc(R_ESI)));
  1937. end;
  1938. { should we save edi,esi,ebx like C ? }
  1939. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  1940. begin
  1941. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  1942. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EBX)));
  1943. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_ESI)));
  1944. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
  1945. end;
  1946. { for the save all registers we can simply use a pusha,popa which
  1947. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1948. if (po_saveregisters in aktprocsym^.definition^.procoptions) then
  1949. begin
  1950. exprasmlist^.insert(new(paicpu,op_none(A_PUSHA,S_L)));
  1951. end;
  1952. { omit stack frame ? }
  1953. if (not inlined) and (procinfo^.framepointer=stack_pointer) then
  1954. begin
  1955. CGMessage(cg_d_stackframe_omited);
  1956. nostackframe:=true;
  1957. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1958. parasize:=0
  1959. else
  1960. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.para_offset-4;
  1961. if stackframe<>0 then
  1962. exprasmlist^.insert(new(paicpu,
  1963. op_const_reg(A_SUB,S_L,stackframe,R_ESP)));
  1964. end
  1965. else
  1966. begin
  1967. alignstack(alist);
  1968. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1969. parasize:=0
  1970. else
  1971. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.para_offset-8;
  1972. nostackframe:=false;
  1973. if stackframe<>0 then
  1974. begin
  1975. {$ifdef unused}
  1976. if (cs_littlesize in aktglobalswitches) and (stackframe<=65535) then
  1977. begin
  1978. if (cs_check_stack in aktlocalswitches) and
  1979. not(target_info.target in [target_386_freebsd,
  1980. target_i386_linux,target_i386_win32]) then
  1981. begin
  1982. emitinsertcall('FPC_STACKCHECK');
  1983. exprasmlist^.insert(new(paicpu,op_const(A_PUSH,S_L,stackframe)));
  1984. end;
  1985. if cs_profile in aktmoduleswitches then
  1986. genprofilecode;
  1987. { %edi is already saved when pocdecl is used
  1988. if ((target_info.target=target_linux) or (target_info.target=target_freebsd)) and
  1989. ((aktprocsym^.definition^.options and poexports)<>0) then
  1990. exprasmlist^.insert(new(Paicpu,op_reg(A_PUSH,S_L,R_EDI))); }
  1991. { ATTENTION:
  1992. never use ENTER in linux !!! (or freebsd MvdV)
  1993. the stack page fault does not support it PM }
  1994. exprasmlist^.insert(new(paicpu,op_const_const(A_ENTER,S_NO,stackframe,0)))
  1995. end
  1996. else
  1997. {$endif unused}
  1998. begin
  1999. { windows guards only a few pages for stack growing, }
  2000. { so we have to access every page first }
  2001. if (target_os.id=os_i386_win32) and
  2002. (stackframe>=winstackpagesize) then
  2003. begin
  2004. if stackframe div winstackpagesize<=5 then
  2005. begin
  2006. exprasmlist^.insert(new(paicpu,op_const_reg(A_SUB,S_L,stackframe-4,R_ESP)));
  2007. for i:=1 to stackframe div winstackpagesize do
  2008. begin
  2009. hr:=new_reference(R_ESP,stackframe-i*winstackpagesize);
  2010. exprasmlist^.concat(new(paicpu,
  2011. op_const_ref(A_MOV,S_L,0,hr)));
  2012. end;
  2013. exprasmlist^.concat(new(paicpu,
  2014. op_reg(A_PUSH,S_L,R_EAX)));
  2015. end
  2016. else
  2017. begin
  2018. getlabel(again);
  2019. getexplicitregister32(R_EDI);
  2020. exprasmlist^.concat(new(paicpu,
  2021. op_const_reg(A_MOV,S_L,stackframe div winstackpagesize,R_EDI)));
  2022. emitlab(again);
  2023. exprasmlist^.concat(new(paicpu,
  2024. op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP)));
  2025. exprasmlist^.concat(new(paicpu,
  2026. op_reg(A_PUSH,S_L,R_EAX)));
  2027. exprasmlist^.concat(new(paicpu,
  2028. op_reg(A_DEC,S_L,R_EDI)));
  2029. emitjmp(C_NZ,again);
  2030. ungetregister32(R_EDI);
  2031. exprasmlist^.concat(new(paicpu,
  2032. op_const_reg(A_SUB,S_L,stackframe mod winstackpagesize,R_ESP)));
  2033. end
  2034. end
  2035. else
  2036. exprasmlist^.insert(new(paicpu,op_const_reg(A_SUB,S_L,stackframe,R_ESP)));
  2037. if (cs_check_stack in aktlocalswitches) and
  2038. not(target_info.target in [target_i386_freebsd,
  2039. target_i386_linux,target_i386_win32]) then
  2040. begin
  2041. emitinsertcall('FPC_STACKCHECK');
  2042. exprasmlist^.insert(new(paicpu,op_const(A_PUSH,S_L,stackframe)));
  2043. end;
  2044. if cs_profile in aktmoduleswitches then
  2045. genprofilecode;
  2046. exprasmlist^.insert(new(paicpu,op_reg_reg(A_MOV,S_L,R_ESP,R_EBP)));
  2047. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EBP)));
  2048. end;
  2049. end { endif stackframe <> 0 }
  2050. else
  2051. begin
  2052. if cs_profile in aktmoduleswitches then
  2053. genprofilecode;
  2054. exprasmlist^.insert(new(paicpu,op_reg_reg(A_MOV,S_L,R_ESP,R_EBP)));
  2055. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EBP)));
  2056. end;
  2057. end;
  2058. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  2059. generate_interrupt_stackframe_entry;
  2060. { initialize return value }
  2061. if (procinfo^.returntype.def<>pdef(voiddef)) and
  2062. (procinfo^.returntype.def^.needs_inittable) then
  2063. begin
  2064. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  2065. reset_reference(r);
  2066. r.offset:=procinfo^.return_offset;
  2067. r.base:=procinfo^.framepointer;
  2068. initialize(procinfo^.returntype.def,r,ret_in_param(procinfo^.returntype.def));
  2069. end;
  2070. { initialisize local data like ansistrings }
  2071. case aktprocsym^.definition^.proctypeoption of
  2072. potype_unitinit:
  2073. begin
  2074. { using current_module^.globalsymtable is hopefully }
  2075. { more robust than symtablestack and symtablestack^.next }
  2076. psymtable(current_module^.globalsymtable)^.foreach({$ifndef TP}@{$endif}initialize_data);
  2077. psymtable(current_module^.localsymtable)^.foreach({$ifndef TP}@{$endif}initialize_data);
  2078. end;
  2079. { units have seperate code for initilization and finalization }
  2080. potype_unitfinalize: ;
  2081. else
  2082. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}initialize_data);
  2083. end;
  2084. { initialisizes temp. ansi/wide string data }
  2085. inittempvariables;
  2086. { generate copies of call by value parameters }
  2087. if not(po_assembler in aktprocsym^.definition^.procoptions) and
  2088. (([pocall_cdecl,pocall_cppdecl]*aktprocsym^.definition^.proccalloptions)=[]) then
  2089. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}copyvalueparas);
  2090. if assigned( aktprocsym^.definition^.parast) then
  2091. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}init_paras);
  2092. { do we need an exception frame because of ansi/widestrings/interfaces ? }
  2093. if not inlined and
  2094. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2095. { but it's useless in init/final code of units }
  2096. not(aktprocsym^.definition^.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2097. begin
  2098. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  2099. { Type of stack-frame must be pushed}
  2100. exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,1)));
  2101. emitcall('FPC_PUSHEXCEPTADDR');
  2102. exprasmlist^.concat(new(paicpu,
  2103. op_reg(A_PUSH,S_L,R_EAX)));
  2104. emitcall('FPC_SETJMP');
  2105. exprasmlist^.concat(new(paicpu,
  2106. op_reg(A_PUSH,S_L,R_EAX)));
  2107. exprasmlist^.concat(new(paicpu,
  2108. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  2109. emitjmp(C_NE,aktexitlabel);
  2110. { probably we've to reload self here }
  2111. maybe_loadesi;
  2112. end;
  2113. if not inlined then
  2114. begin
  2115. if (cs_profile in aktmoduleswitches) or
  2116. (aktprocsym^.definition^.owner^.symtabletype=globalsymtable) or
  2117. (assigned(procinfo^._class) and (procinfo^._class^.owner^.symtabletype=globalsymtable)) then
  2118. make_global:=true;
  2119. hs:=proc_names.get;
  2120. {$ifdef GDB}
  2121. if (cs_debuginfo in aktmoduleswitches) and target_os.use_function_relative_addresses then
  2122. stab_function_name := new(pai_stab_function_name,init(strpnew(hs)));
  2123. {$EndIf GDB}
  2124. while hs<>'' do
  2125. begin
  2126. if make_global then
  2127. exprasmlist^.insert(new(pai_symbol,initname_global(hs,0)))
  2128. else
  2129. exprasmlist^.insert(new(pai_symbol,initname(hs,0)));
  2130. {$ifdef GDB}
  2131. if (cs_debuginfo in aktmoduleswitches) and
  2132. target_os.use_function_relative_addresses then
  2133. exprasmlist^.insert(new(pai_stab_function_name,init(strpnew(hs))));
  2134. {$endif GDB}
  2135. hs:=proc_names.get;
  2136. end;
  2137. if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
  2138. aktprocsym^.is_global := True;
  2139. {$ifdef GDB}
  2140. if (cs_debuginfo in aktmoduleswitches) then
  2141. begin
  2142. if target_os.use_function_relative_addresses then
  2143. exprasmlist^.insert(stab_function_name);
  2144. exprasmlist^.insert(new(pai_stabs,init(aktprocsym^.stabstring)));
  2145. aktprocsym^.isstabwritten:=true;
  2146. end;
  2147. {$endif GDB}
  2148. { Align, gprof uses 16 byte granularity }
  2149. if (cs_profile in aktmoduleswitches) then
  2150. exprasmlist^.insert(new(pai_align,init_op(16,$90)))
  2151. else
  2152. if not(cs_littlesize in aktglobalswitches) then
  2153. exprasmlist^.insert(new(pai_align,init(16)));
  2154. end;
  2155. if inlined then
  2156. load_regvars(exprasmlist,nil);
  2157. exprasmlist:=oldexprasmlist;
  2158. end;
  2159. procedure handle_return_value(inlined : boolean;var uses_eax,uses_edx : boolean);
  2160. var
  2161. hr : preference;
  2162. op : Tasmop;
  2163. s : Topsize;
  2164. begin
  2165. uses_eax:=false;
  2166. uses_edx:=false;
  2167. if procinfo^.returntype.def<>pdef(voiddef) then
  2168. begin
  2169. {if ((procinfo^.flags and pi_operator)<>0) and
  2170. assigned(opsym) then
  2171. procinfo^.funcret_is_valid:=
  2172. procinfo^.funcret_is_valid or (opsym^.refs>0);}
  2173. if (procinfo^.funcret_state<>vs_assigned) and not inlined { and
  2174. ((procinfo^.flags and pi_uses_asm)=0)} then
  2175. CGMessage(sym_w_function_result_not_set);
  2176. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset);
  2177. if (procinfo^.returntype.def^.deftype in [orddef,enumdef]) then
  2178. begin
  2179. uses_eax:=true;
  2180. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  2181. case procinfo^.returntype.def^.size of
  2182. 8:
  2183. begin
  2184. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2185. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset+4);
  2186. exprasmlist^.concat(new(pairegalloc,alloc(R_EDX)));
  2187. emit_ref_reg(A_MOV,S_L,hr,R_EDX);
  2188. uses_edx:=true;
  2189. end;
  2190. 4:
  2191. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2192. 2:
  2193. emit_ref_reg(A_MOV,S_W,hr,R_AX);
  2194. 1:
  2195. emit_ref_reg(A_MOV,S_B,hr,R_AL);
  2196. end;
  2197. end
  2198. else
  2199. if ret_in_acc(procinfo^.returntype.def) then
  2200. begin
  2201. uses_eax:=true;
  2202. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  2203. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2204. end
  2205. else
  2206. if (procinfo^.returntype.def^.deftype=floatdef) then
  2207. begin
  2208. floatloadops(pfloatdef(procinfo^.returntype.def)^.typ,op,s);
  2209. exprasmlist^.concat(new(paicpu,op_ref(op,s,hr)))
  2210. end
  2211. else
  2212. dispose(hr);
  2213. end
  2214. end;
  2215. procedure genexitcode(alist : paasmoutput;parasize:longint;nostackframe,inlined:boolean);
  2216. var
  2217. {$ifdef GDB}
  2218. mangled_length : longint;
  2219. p : pchar;
  2220. st : string[2];
  2221. {$endif GDB}
  2222. nofinal,okexitlabel,noreraiselabel,nodestroycall : pasmlabel;
  2223. hr : treference;
  2224. uses_eax,uses_edx,uses_esi : boolean;
  2225. oldexprasmlist : paasmoutput;
  2226. ai : paicpu;
  2227. pd : pprocdef;
  2228. begin
  2229. oldexprasmlist:=exprasmlist;
  2230. exprasmlist:=alist;
  2231. if aktexitlabel^.is_used then
  2232. exprasmlist^.insert(new(pai_label,init(aktexitlabel)));
  2233. { call the destructor help procedure }
  2234. if (aktprocsym^.definition^.proctypeoption=potype_destructor) and
  2235. assigned(procinfo^._class) then
  2236. begin
  2237. if is_class(procinfo^._class) then
  2238. begin
  2239. emitinsertcall('FPC_DISPOSE_CLASS');
  2240. end
  2241. else if is_object(procinfo^._class) then
  2242. begin
  2243. emitinsertcall('FPC_HELP_DESTRUCTOR');
  2244. getexplicitregister32(R_EDI);
  2245. exprasmlist^.insert(new(paicpu,op_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI)));
  2246. { must the object be finalized ? }
  2247. if procinfo^._class^.needs_inittable then
  2248. begin
  2249. getlabel(nofinal);
  2250. exprasmlist^.insert(new(pai_label,init(nofinal)));
  2251. emitinsertcall('FPC_FINALIZE');
  2252. ungetregister32(R_EDI);
  2253. exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_ESI)));
  2254. exprasmlist^.insert(new(paicpu,op_sym(A_PUSH,S_L,procinfo^._class^.get_inittable_label)));
  2255. ai:=new(paicpu,op_sym(A_Jcc,S_NO,nofinal));
  2256. ai^.SetCondition(C_Z);
  2257. exprasmlist^.insert(ai);
  2258. reset_reference(hr);
  2259. hr.base:=R_EBP;
  2260. hr.offset:=8;
  2261. exprasmlist^.insert(new(paicpu,op_const_ref(A_CMP,S_L,0,newreference(hr))));
  2262. end;
  2263. end
  2264. else
  2265. begin
  2266. Internalerror(200006161);
  2267. end;
  2268. end;
  2269. { finalize temporary data }
  2270. finalizetempvariables;
  2271. { finalize local data like ansistrings}
  2272. case aktprocsym^.definition^.proctypeoption of
  2273. potype_unitfinalize:
  2274. begin
  2275. { using current_module^.globalsymtable is hopefully }
  2276. { more robust than symtablestack and symtablestack^.next }
  2277. psymtable(current_module^.globalsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  2278. psymtable(current_module^.localsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  2279. end;
  2280. { units have seperate code for initialization and finalization }
  2281. potype_unitinit: ;
  2282. else
  2283. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}finalize_data);
  2284. end;
  2285. { finalize paras data }
  2286. if assigned(aktprocsym^.definition^.parast) then
  2287. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}final_paras);
  2288. { do we need to handle exceptions because of ansi/widestrings ? }
  2289. if not inlined and
  2290. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2291. { but it's useless in init/final code of units }
  2292. not(aktprocsym^.definition^.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2293. begin
  2294. { the exception helper routines modify all registers }
  2295. aktprocsym^.definition^.usedregisters:=$ff;
  2296. getlabel(noreraiselabel);
  2297. emitcall('FPC_POPADDRSTACK');
  2298. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  2299. exprasmlist^.concat(new(paicpu,
  2300. op_reg(A_POP,S_L,R_EAX)));
  2301. exprasmlist^.concat(new(paicpu,
  2302. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  2303. ungetregister32(R_EAX);
  2304. emitjmp(C_E,noreraiselabel);
  2305. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  2306. begin
  2307. if assigned(procinfo^._class) then
  2308. begin
  2309. pd:=procinfo^._class^.searchdestructor;
  2310. if assigned(pd) then
  2311. begin
  2312. getlabel(nodestroycall);
  2313. emit_const_ref(A_CMP,S_L,0,new_reference(procinfo^.framepointer,
  2314. procinfo^.selfpointer_offset));
  2315. emitjmp(C_E,nodestroycall);
  2316. if is_class(procinfo^._class) then
  2317. begin
  2318. emit_const(A_PUSH,S_L,1);
  2319. emit_reg(A_PUSH,S_L,R_ESI);
  2320. end
  2321. else if is_object(procinfo^._class) then
  2322. begin
  2323. emit_reg(A_PUSH,S_L,R_ESI);
  2324. emit_sym(A_PUSH,S_L,newasmsymbol(procinfo^._class^.vmt_mangledname));
  2325. end
  2326. else
  2327. begin
  2328. Internalerror(200006161);
  2329. end;
  2330. if (po_virtualmethod in pd^.procoptions) then
  2331. begin
  2332. emit_ref_reg(A_MOV,S_L,new_reference(R_ESI,0),R_EDI);
  2333. emit_ref(A_CALL,S_NO,new_reference(R_EDI,procinfo^._class^.vmtmethodoffset(pd^.extnumber)));
  2334. end
  2335. else
  2336. emitcall(pd^.mangledname);
  2337. { not necessary because the result is never assigned in the
  2338. case of an exception (FK)
  2339. emit_const_reg(A_MOV,S_L,0,R_ESI);
  2340. emit_const_ref(A_MOV,S_L,0,new_reference(procinfo^.framepointer,8));
  2341. }
  2342. emitlab(nodestroycall);
  2343. end;
  2344. end
  2345. end
  2346. else
  2347. { must be the return value finalized before reraising the exception? }
  2348. if (procinfo^.returntype.def<>pdef(voiddef)) and
  2349. (procinfo^.returntype.def^.needs_inittable) and
  2350. ((procinfo^.returntype.def^.deftype<>objectdef) or
  2351. not is_class(procinfo^.returntype.def)) then
  2352. begin
  2353. reset_reference(hr);
  2354. hr.offset:=procinfo^.return_offset;
  2355. hr.base:=procinfo^.framepointer;
  2356. finalize(procinfo^.returntype.def,hr,ret_in_param(procinfo^.returntype.def));
  2357. end;
  2358. emitcall('FPC_RERAISE');
  2359. emitlab(noreraiselabel);
  2360. end;
  2361. { call __EXIT for main program }
  2362. if (not DLLsource) and (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
  2363. begin
  2364. emitcall('FPC_DO_EXIT');
  2365. end;
  2366. { handle return value }
  2367. uses_eax:=false;
  2368. uses_edx:=false;
  2369. uses_esi:=false;
  2370. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  2371. if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
  2372. handle_return_value(inlined,uses_eax,uses_edx)
  2373. else
  2374. begin
  2375. { successful constructor deletes the zero flag }
  2376. { and returns self in eax }
  2377. { eax must be set to zero if the allocation failed !!! }
  2378. getlabel(okexitlabel);
  2379. emitjmp(C_NONE,okexitlabel);
  2380. emitlab(faillabel);
  2381. if is_class(procinfo^._class) then
  2382. begin
  2383. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,8),R_ESI);
  2384. emitcall('FPC_HELP_FAIL_CLASS');
  2385. end
  2386. else if is_object(procinfo^._class) then
  2387. begin
  2388. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,12),R_ESI);
  2389. getexplicitregister32(R_EDI);
  2390. emit_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI);
  2391. emitcall('FPC_HELP_FAIL');
  2392. ungetregister32(R_EDI);
  2393. end
  2394. else
  2395. Internalerror(200006161);
  2396. emitlab(okexitlabel);
  2397. { for classes this is done after the call to }
  2398. { AfterConstruction }
  2399. if is_object(procinfo^._class) then
  2400. begin
  2401. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  2402. emit_reg_reg(A_MOV,S_L,R_ESI,R_EAX);
  2403. uses_eax:=true;
  2404. end;
  2405. emit_reg_reg(A_TEST,S_L,R_ESI,R_ESI);
  2406. uses_esi:=true;
  2407. end;
  2408. { stabs uses the label also ! }
  2409. if aktexit2label^.is_used or
  2410. ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  2411. emitlab(aktexit2label);
  2412. { gives problems for long mangled names }
  2413. {list^.concat(new(pai_symbol,init(aktprocsym^.definition^.mangledname+'_end')));}
  2414. { should we restore edi ? }
  2415. { for all i386 gcc implementations }
  2416. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  2417. begin
  2418. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  2419. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EBX)));
  2420. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_ESI)));
  2421. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EDI)));
  2422. { here we could reset R_EBX
  2423. but that is risky because it only works
  2424. if genexitcode is called after genentrycode
  2425. so lets skip this for the moment PM
  2426. aktprocsym^.definition^.usedregisters:=
  2427. aktprocsym^.definition^.usedregisters or not ($80 shr byte(R_EBX));
  2428. }
  2429. end;
  2430. { for the save all registers we can simply use a pusha,popa which
  2431. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  2432. if (po_saveregisters in aktprocsym^.definition^.procoptions) then
  2433. begin
  2434. if uses_esi then
  2435. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,4))));
  2436. if uses_edx then
  2437. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,20))));
  2438. if uses_eax then
  2439. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,28))));
  2440. exprasmlist^.concat(new(paicpu,op_none(A_POPA,S_L)))
  2441. end;
  2442. if not(nostackframe) then
  2443. begin
  2444. if not inlined then
  2445. exprasmlist^.concat(new(paicpu,op_none(A_LEAVE,S_NO)));
  2446. end
  2447. else
  2448. begin
  2449. if (gettempsize<>0) and not inlined then
  2450. exprasmlist^.insert(new(paicpu,
  2451. op_const_reg(A_ADD,S_L,gettempsize,R_ESP)));
  2452. end;
  2453. { parameters are limited to 65535 bytes because }
  2454. { ret allows only imm16 }
  2455. if (parasize>65535) and not(pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  2456. CGMessage(cg_e_parasize_too_big);
  2457. { at last, the return is generated }
  2458. if not inlined then
  2459. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  2460. begin
  2461. if uses_esi then
  2462. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,16))));
  2463. if uses_edx then
  2464. begin
  2465. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  2466. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,12))));
  2467. end;
  2468. if uses_eax then
  2469. begin
  2470. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  2471. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,0))));
  2472. end;
  2473. generate_interrupt_stackframe_exit;
  2474. end
  2475. else
  2476. begin
  2477. {Routines with the poclearstack flag set use only a ret.}
  2478. { also routines with parasize=0 }
  2479. if (pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  2480. begin
  2481. {$ifndef OLD_C_STACK}
  2482. { complex return values are removed from stack in C code PM }
  2483. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  2484. exprasmlist^.concat(new(paicpu,op_const(A_RET,S_NO,4)))
  2485. else
  2486. {$endif not OLD_C_STACK}
  2487. exprasmlist^.concat(new(paicpu,op_none(A_RET,S_NO)));
  2488. end
  2489. else if (parasize=0) then
  2490. exprasmlist^.concat(new(paicpu,op_none(A_RET,S_NO)))
  2491. else
  2492. exprasmlist^.concat(new(paicpu,op_const(A_RET,S_NO,parasize)));
  2493. end;
  2494. if not inlined then
  2495. exprasmlist^.concat(new(pai_symbol_end,initname(aktprocsym^.definition^.mangledname)));
  2496. {$ifdef GDB}
  2497. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  2498. begin
  2499. aktprocsym^.concatstabto(exprasmlist);
  2500. if assigned(procinfo^._class) then
  2501. if (not assigned(procinfo^.parent) or
  2502. not assigned(procinfo^.parent^._class)) then
  2503. begin
  2504. if not(is_class(procinfo^._class)) then
  2505. st:='v'
  2506. else
  2507. st:='p';
  2508. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2509. '"$t:'+st+procinfo^._class^.numberstring+'",'+
  2510. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.selfpointer_offset)))))
  2511. end
  2512. else
  2513. begin
  2514. if not is_class(procinfo^._class) then
  2515. st:='*'
  2516. else
  2517. st:='';
  2518. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2519. '"$t:r'+st+procinfo^._class^.numberstring+'",'+
  2520. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI])))));
  2521. end;
  2522. { define calling EBP as pseudo local var PM }
  2523. { this enables test if the function is a local one !! }
  2524. if assigned(procinfo^.parent) and (lexlevel>normal_function_level) then
  2525. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2526. '"parent_ebp:'+voidpointerdef^.numberstring+'",'+
  2527. tostr(N_LSYM)+',0,0,'+tostr(procinfo^.framepointer_offset)))));
  2528. if (pdef(aktprocsym^.definition^.rettype.def) <> pdef(voiddef)) then
  2529. begin
  2530. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  2531. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2532. '"'+aktprocsym^.name+':X*'+pstoreddef(aktprocsym^.definition^.rettype.def)^.numberstring+'",'+
  2533. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))))
  2534. else
  2535. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2536. '"'+aktprocsym^.name+':X'+pstoreddef(aktprocsym^.definition^.rettype.def)^.numberstring+'",'+
  2537. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))));
  2538. if (m_result in aktmodeswitches) then
  2539. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  2540. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2541. '"RESULT:X*'+pstoreddef(aktprocsym^.definition^.rettype.def)^.numberstring+'",'+
  2542. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))))
  2543. else
  2544. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2545. '"RESULT:X'+pstoreddef(aktprocsym^.definition^.rettype.def)^.numberstring+'",'+
  2546. tostr(N_PSYM)+',0,0,'+tostr(procinfo^.return_offset)))));
  2547. end;
  2548. mangled_length:=length(aktprocsym^.definition^.mangledname);
  2549. getmem(p,2*mangled_length+50);
  2550. strpcopy(p,'192,0,0,');
  2551. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  2552. if (target_os.use_function_relative_addresses) then
  2553. begin
  2554. strpcopy(strend(p),'-');
  2555. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  2556. end;
  2557. exprasmlist^.concat(new(pai_stabn,init(strnew(p))));
  2558. {list^.concat(new(pai_stabn,init(strpnew('192,0,0,'
  2559. +aktprocsym^.definition^.mangledname))));
  2560. p[0]:='2';p[1]:='2';p[2]:='4';
  2561. strpcopy(strend(p),'_end');}
  2562. strpcopy(p,'224,0,0,'+aktexit2label^.name);
  2563. if (target_os.use_function_relative_addresses) then
  2564. begin
  2565. strpcopy(strend(p),'-');
  2566. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  2567. end;
  2568. exprasmlist^.concatlist(withdebuglist);
  2569. exprasmlist^.concat(new(pai_stabn,init(
  2570. strnew(p))));
  2571. { strpnew('224,0,0,'
  2572. +aktprocsym^.definition^.mangledname+'_end'))));}
  2573. freemem(p,2*mangled_length+50);
  2574. end;
  2575. {$endif GDB}
  2576. if inlined then
  2577. cleanup_regvars(exprasmlist);
  2578. exprasmlist:=oldexprasmlist;
  2579. end;
  2580. procedure genimplicitunitfinal(alist : paasmoutput);
  2581. begin
  2582. { using current_module^.globalsymtable is hopefully }
  2583. { more robust than symtablestack and symtablestack^.next }
  2584. psymtable(current_module^.globalsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  2585. psymtable(current_module^.localsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  2586. exprasmlist^.insert(new(pai_symbol,initname_global('FINALIZE$$'+current_module^.modulename^,0)));
  2587. exprasmlist^.insert(new(pai_symbol,initname_global(target_os.cprefix+current_module^.modulename^+'_finalize',0)));
  2588. {$ifdef GDB}
  2589. if (cs_debuginfo in aktmoduleswitches) and
  2590. target_os.use_function_relative_addresses then
  2591. exprasmlist^.insert(new(pai_stab_function_name,init(strpnew('FINALIZE$$'+current_module^.modulename^))));
  2592. {$endif GDB}
  2593. exprasmlist^.concat(new(paicpu,op_none(A_RET,S_NO)));
  2594. alist^.concatlist(exprasmlist);
  2595. end;
  2596. procedure genimplicitunitinit(alist : paasmoutput);
  2597. begin
  2598. { using current_module^.globalsymtable is hopefully }
  2599. { more robust than symtablestack and symtablestack^.next }
  2600. psymtable(current_module^.globalsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  2601. psymtable(current_module^.localsymtable)^.foreach({$ifndef TP}@{$endif}finalize_data);
  2602. exprasmlist^.insert(new(pai_symbol,initname_global('INIT$$'+current_module^.modulename^,0)));
  2603. exprasmlist^.insert(new(pai_symbol,initname_global(target_os.cprefix+current_module^.modulename^+'_init',0)));
  2604. {$ifdef GDB}
  2605. if (cs_debuginfo in aktmoduleswitches) and
  2606. target_os.use_function_relative_addresses then
  2607. exprasmlist^.insert(new(pai_stab_function_name,init(strpnew('INIT$$'+current_module^.modulename^))));
  2608. {$endif GDB}
  2609. exprasmlist^.concat(new(paicpu,op_none(A_RET,S_NO)));
  2610. alist^.concatlist(exprasmlist);
  2611. end;
  2612. {$ifdef test_dest_loc}
  2613. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  2614. begin
  2615. if (dest_loc.loc=LOC_CREGISTER) or (dest_loc.loc=LOC_REGISTER) then
  2616. begin
  2617. emit_reg_reg(A_MOV,s,reg,dest_loc.register);
  2618. set_location(p^.location,dest_loc);
  2619. in_dest_loc:=true;
  2620. end
  2621. else
  2622. if (dest_loc.loc=LOC_REFERENCE) or (dest_loc.loc=LOC_MEM) then
  2623. begin
  2624. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,s,reg,newreference(dest_loc.reference))));
  2625. set_location(p^.location,dest_loc);
  2626. in_dest_loc:=true;
  2627. end
  2628. else
  2629. internalerror(20080);
  2630. end;
  2631. {$endif test_dest_loc}
  2632. end.
  2633. {
  2634. $Log$
  2635. Revision 1.10 2000-11-07 23:40:48 florian
  2636. + AfterConstruction and BeforeDestruction impemented
  2637. Revision 1.9 2000/11/06 23:49:20 florian
  2638. * fixed init_paras call
  2639. Revision 1.8 2000/11/06 23:15:01 peter
  2640. * added copyvaluepara call again
  2641. Revision 1.7 2000/11/04 14:25:23 florian
  2642. + merged Attila's changes for interfaces, not tested yet
  2643. Revision 1.6 2000/10/31 22:02:55 peter
  2644. * symtable splitted, no real code changes
  2645. Revision 1.5 2000/10/24 22:23:04 peter
  2646. * emitcall -> emitinsertcall for profiling (merged)
  2647. Revision 1.4 2000/10/24 12:47:45 jonas
  2648. * allocate registers which hold function result
  2649. Revision 1.3 2000/10/24 08:54:25 michael
  2650. + Extra patch from peter
  2651. Revision 1.2 2000/10/24 07:20:03 pierre
  2652. * fix for bug 1193 (merged)
  2653. Revision 1.1 2000/10/15 09:47:42 peter
  2654. * moved to i386/
  2655. Revision 1.19 2000/10/14 10:14:46 peter
  2656. * moehrendorf oct 2000 rewrite
  2657. Revision 1.18 2000/10/10 14:55:28 jonas
  2658. * added missing regallocs for edi in emit_mov_ref_reg64 (merged)
  2659. Revision 1.17 2000/10/01 19:48:23 peter
  2660. * lot of compile updates for cg11
  2661. Revision 1.16 2000/09/30 16:08:45 peter
  2662. * more cg11 updates
  2663. Revision 1.15 2000/09/24 15:06:12 peter
  2664. * use defines.inc
  2665. Revision 1.14 2000/09/16 12:22:52 peter
  2666. * freebsd support merged
  2667. Revision 1.13 2000/08/27 16:11:49 peter
  2668. * moved some util functions from globals,cobjects to cutils
  2669. * splitted files into finput,fmodule
  2670. Revision 1.12 2000/08/24 19:07:54 peter
  2671. * don't initialize if localvarsym is set because that varsym will
  2672. already be initialized
  2673. * first initialize local data before copy of value para's (merged)
  2674. Revision 1.11 2000/08/19 20:09:33 peter
  2675. * check size after checking openarray in push_value_para (merged)
  2676. Revision 1.10 2000/08/16 13:06:06 florian
  2677. + support of 64 bit integer constants
  2678. Revision 1.9 2000/08/10 18:42:03 peter
  2679. * fixed for constants in emit_push_mem_size for go32v2 (merged)
  2680. Revision 1.8 2000/08/07 11:29:40 jonas
  2681. + emit_push_mem_size() which pushes a value in memory of a certain size
  2682. * pushsetelement() and pushvaluepara() use this new procedure, because
  2683. otherwise they could sometimes try to push data past the end of the
  2684. heap, causing a crash
  2685. (merged from fixes branch)
  2686. Revision 1.7 2000/08/03 13:17:25 jonas
  2687. + allow regvars to be used inside inlined procs, which required the
  2688. following changes:
  2689. + load regvars in genentrycode/free them in genexitcode (cgai386)
  2690. * moved all regvar related code to new regvars unit
  2691. + added pregvarinfo type to hcodegen
  2692. + added regvarinfo field to tprocinfo (symdef/symdefh)
  2693. * deallocate the regvars of the caller in secondprocinline before
  2694. inlining the called procedure and reallocate them afterwards
  2695. Revision 1.6 2000/08/02 08:05:04 jonas
  2696. * fixed web bug1087
  2697. * allocate R_ECX explicitely if it's used
  2698. (merged from fixes branch)
  2699. Revision 1.5 2000/07/27 09:25:05 jonas
  2700. * moved locflags2reg() procedure from cg386add to cgai386
  2701. + added locjump2reg() procedure to cgai386
  2702. * fixed internalerror(2002) when the result of a case expression has
  2703. LOC_JUMP
  2704. (all merged from fixes branch)
  2705. Revision 1.4 2000/07/21 15:14:02 jonas
  2706. + added is_addr field for labels, if they are only used for getting the address
  2707. (e.g. for io checks) and corresponding getaddrlabel() procedure
  2708. Revision 1.3 2000/07/13 12:08:25 michael
  2709. + patched to 1.1.0 with former 1.09patch from peter
  2710. Revision 1.2 2000/07/13 11:32:37 michael
  2711. + removed logs
  2712. }