cga.pas 115 KB

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