12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542 |
- {
- $Id$
- Copyright (c) 1993-98 by Florian Klaempfl
- Helper routines for the i386 code generator
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************}
- unit cgai386;
- interface
- uses
- cobjects,tree,
- cpubase,cpuasm,
- symconst,symtable,aasm;
- {$define TESTGETTEMP to store const that
- are written into temps for later release PM }
- function def_opsize(p1:pdef):topsize;
- function def2def_opsize(p1,p2:pdef):topsize;
- function def_getreg(p1:pdef):tregister;
- function makereg8(r:tregister):tregister;
- function makereg16(r:tregister):tregister;
- function makereg32(r:tregister):tregister;
- procedure emitlab(var l : pasmlabel);
- procedure emitjmp(c : tasmcond;var l : pasmlabel);
- procedure emit_flag2reg(flag:tresflags;hregister:tregister);
- procedure emit_none(i : tasmop;s : topsize);
- procedure emit_const(i : tasmop;s : topsize;c : longint);
- procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
- procedure emit_ref(i : tasmop;s : topsize;ref : preference);
- procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
- procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
- procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
- procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
- procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
- procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
- procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
- procedure emit_sym(i : tasmop;s : topsize;op : pasmsymbol);
- procedure emit_sym_ofs(i : tasmop;s : topsize;op : pasmsymbol;ofs : longint);
- procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;reg : tregister);
- procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;ref : preference);
- procedure emitcall(const routine:string);
- procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize);
- procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
- procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
- procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
- procedure emit_push_loc(const t:tlocation);
- { pushes qword location to the stack }
- procedure emit_pushq_loc(const t : tlocation);
- procedure release_qword_loc(const t : tlocation);
- { releases the registers of a location }
- procedure release_loc(const t : tlocation);
- procedure emit_pushw_loc(const t:tlocation);
- procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
- procedure emit_to_mem(var p:ptree);
- procedure emit_to_reg16(var hr:tregister);
- procedure emit_to_reg32(var hr:tregister);
- procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
- procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
- procedure copyshortstring(const dref,sref : treference;len : byte;loadref:boolean);
- procedure loadansistring(p : ptree);
- procedure finalize(t : pdef;const ref : treference;is_already_ref : boolean);
- procedure decrstringref(t : pdef;const ref : treference);
- function maybe_push(needed : byte;p : ptree;isint64 : boolean) : boolean;
- procedure push_int(l : longint);
- procedure emit_push_mem(const ref : treference);
- procedure emitpushreferenceaddr(const ref : treference);
- procedure pushsetelement(p : ptree);
- procedure restore(p : ptree;isint64 : boolean);
- procedure push_value_para(p:ptree;inlined:boolean;para_offset:longint;alignment : longint);
- {$ifdef TEMPS_NOT_PUSH}
- { does the same as restore/, but uses temp. space instead of pushing }
- function maybe_push(needed : byte;p : ptree;isint64 : boolean) : boolean;
- procedure restorefromtemp(p : ptree;isint64 : boolean);
- {$endif TEMPS_NOT_PUSH}
- procedure floatload(t : tfloattype;const ref : treference);
- procedure floatstore(t : tfloattype;const ref : treference);
- procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
- procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
- procedure maybe_loadesi;
- procedure maketojumpbool(p : ptree);
- procedure emitloadord2reg(const location:Tlocation;orddef:Porddef;destreg:Tregister;delloc:boolean);
- procedure emitoverflowcheck(p:ptree);
- procedure emitrangecheck(p:ptree;todef:pdef);
- procedure concatcopy(source,dest : treference;size : longint;delsource : boolean;loadref:boolean);
- procedure firstcomplex(p : ptree);
- procedure genentrycode(alist : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
- stackframe:longint;
- var parasize:longint;var nostackframe:boolean;
- inlined : boolean);
- procedure genexitcode(alist : paasmoutput;parasize:longint;
- nostackframe,inlined:boolean);
- {$ifdef test_dest_loc}
- const
- { used to avoid temporary assignments }
- dest_loc_known : boolean = false;
- in_dest_loc : boolean = false;
- dest_loc_tree : ptree = nil;
- var
- dest_loc : tlocation;
- procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
- {$endif test_dest_loc}
- implementation
- uses
- strings,globtype,systems,globals,verbose,files,types,pbase,
- tgeni386,temp_gen,hcodegen,ppu
- {$ifdef GDB}
- ,gdb
- {$endif}
- {$ifndef NOTARGETWIN32}
- ,t_win32
- {$endif}
- ;
- {*****************************************************************************
- Helpers
- *****************************************************************************}
- function def_opsize(p1:pdef):topsize;
- begin
- case p1^.size of
- 1 : def_opsize:=S_B;
- 2 : def_opsize:=S_W;
- 4 : def_opsize:=S_L;
- else
- internalerror(78);
- end;
- end;
- function def2def_opsize(p1,p2:pdef):topsize;
- var
- o1 : topsize;
- begin
- case p1^.size of
- 1 : o1:=S_B;
- 2 : o1:=S_W;
- 4 : o1:=S_L;
- { I don't know if we need it (FK) }
- 8 : o1:=S_L;
- else
- internalerror(78);
- end;
- if assigned(p2) then
- begin
- case p2^.size of
- 1 : o1:=S_B;
- 2 : begin
- if o1=S_B then
- o1:=S_BW
- else
- o1:=S_W;
- end;
- 4,8:
- begin
- case o1 of
- S_B : o1:=S_BL;
- S_W : o1:=S_WL;
- end;
- end;
- end;
- end;
- def2def_opsize:=o1;
- end;
- function def_getreg(p1:pdef):tregister;
- begin
- case p1^.size of
- 1 : def_getreg:=reg32toreg8(getregister32);
- 2 : def_getreg:=reg32toreg16(getregister32);
- 4 : def_getreg:=getregister32;
- else
- internalerror(78);
- end;
- end;
- function makereg8(r:tregister):tregister;
- begin
- case r of
- R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
- makereg8:=reg32toreg8(r);
- R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
- makereg8:=reg16toreg8(r);
- R_AL,R_BL,R_CL,R_DL :
- makereg8:=r;
- end;
- end;
- function makereg16(r:tregister):tregister;
- begin
- case r of
- R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
- makereg16:=reg32toreg16(r);
- R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
- makereg16:=r;
- R_AL,R_BL,R_CL,R_DL :
- makereg16:=reg8toreg16(r);
- end;
- end;
- function makereg32(r:tregister):tregister;
- begin
- case r of
- R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
- makereg32:=r;
- R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
- makereg32:=reg16toreg32(r);
- R_AL,R_BL,R_CL,R_DL :
- makereg32:=reg8toreg32(r);
- end;
- end;
- {*****************************************************************************
- Emit Assembler
- *****************************************************************************}
- procedure emitlab(var l : pasmlabel);
- begin
- if not l^.is_set then
- exprasmlist^.concat(new(pai_label,init(l)))
- else
- internalerror(7453984);
- end;
- {$ifdef nojmpfix}
- procedure emitjmp(c : tasmcond;var l : pasmlabel);
- var
- ai : Paicpu;
- begin
- if c=C_None then
- exprasmlist^.concat(new(paicpu,op_sym(A_JMP,S_NO,l)))
- else
- begin
- ai:=new(paicpu,op_sym(A_Jcc,S_NO,l));
- ai^.SetCondition(c);
- ai^.is_jmp:=true;
- exprasmlist^.concat(ai);
- end;
- end;
- {$else nojmpfix}
- procedure emitjmp(c : tasmcond;var l : pasmlabel);
- var
- ai : Paicpu;
- begin
- if c=C_None then
- ai := new(paicpu,op_sym(A_JMP,S_NO,l))
- else
- begin
- ai:=new(paicpu,op_sym(A_Jcc,S_NO,l));
- ai^.SetCondition(c);
- end;
- ai^.is_jmp:=true;
- exprasmlist^.concat(ai);
- end;
- {$endif nojmpfix}
- procedure emit_flag2reg(flag:tresflags;hregister:tregister);
- var
- ai : paicpu;
- hreg : tregister;
- begin
- hreg:=makereg8(hregister);
- ai:=new(paicpu,op_reg(A_Setcc,S_B,hreg));
- ai^.SetCondition(flag_2_cond[flag]);
- exprasmlist^.concat(ai);
- if hreg<>hregister then
- begin
- if hregister in regset16bit then
- emit_to_reg16(hreg)
- else
- emit_to_reg32(hreg);
- end;
- end;
- procedure emit_none(i : tasmop;s : topsize);
- begin
- exprasmlist^.concat(new(paicpu,op_none(i,s)));
- end;
- procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
- begin
- exprasmlist^.concat(new(paicpu,op_reg(i,s,reg)));
- end;
- procedure emit_ref(i : tasmop;s : topsize;ref : preference);
- begin
- exprasmlist^.concat(new(paicpu,op_ref(i,s,ref)));
- end;
- procedure emit_const(i : tasmop;s : topsize;c : longint);
- begin
- exprasmlist^.concat(new(paicpu,op_const(i,s,c)));
- end;
- procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
- begin
- exprasmlist^.concat(new(paicpu,op_const_reg(i,s,c,reg)));
- end;
- procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
- begin
- exprasmlist^.concat(new(paicpu,op_const_ref(i,s,c,ref)));
- end;
- procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
- begin
- exprasmlist^.concat(new(paicpu,op_ref_reg(i,s,ref,reg)));
- end;
- procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
- begin
- exprasmlist^.concat(new(paicpu,op_reg_ref(i,s,reg,ref)));
- end;
- procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
- begin
- if (reg1<>reg2) or (i<>A_MOV) then
- exprasmlist^.concat(new(paicpu,op_reg_reg(i,s,reg1,reg2)));
- end;
- procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
- begin
- exprasmlist^.concat(new(paicpu,op_const_reg_reg(i,s,c,reg1,reg2)));
- end;
- procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
- begin
- exprasmlist^.concat(new(paicpu,op_reg_reg_reg(i,s,reg1,reg2,reg3)));
- end;
- procedure emit_sym(i : tasmop;s : topsize;op : pasmsymbol);
- begin
- exprasmlist^.concat(new(paicpu,op_sym(i,s,op)));
- end;
- procedure emit_sym_ofs(i : tasmop;s : topsize;op : pasmsymbol;ofs : longint);
- begin
- exprasmlist^.concat(new(paicpu,op_sym_ofs(i,s,op,ofs)));
- end;
- procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;reg : tregister);
- begin
- exprasmlist^.concat(new(paicpu,op_sym_ofs_reg(i,s,op,ofs,reg)));
- end;
- procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : pasmsymbol;ofs:longint;ref : preference);
- begin
- exprasmlist^.concat(new(paicpu,op_sym_ofs_ref(i,s,op,ofs,ref)));
- end;
- procedure emitcall(const routine:string);
- begin
- exprasmlist^.concat(new(paicpu,op_sym(A_CALL,S_NO,newasmsymbol(routine))));
- end;
- { only usefull in startup code }
- procedure emitinsertcall(const routine:string);
- begin
- exprasmlist^.insert(new(paicpu,op_sym(A_CALL,S_NO,newasmsymbol(routine))));
- end;
- procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize);
- var
- hreg : tregister;
- pushedeax : boolean;
- begin
- pushedeax:=false;
- case t.loc of
- LOC_REGISTER,
- LOC_CREGISTER : begin
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,siz,
- t.register,newreference(ref))));
- ungetregister32(t.register); { the register is not needed anymore }
- end;
- LOC_MEM,
- LOC_REFERENCE : begin
- if t.reference.is_immediate then
- emit_const_ref(A_MOV,siz,
- t.reference.offset,newreference(ref))
- else
- begin
- case siz of
- S_B : begin
- { we can't do a getregister in the code generator }
- { without problems!!! }
- if usablereg32>0 then
- hreg:=reg32toreg8(getregister32)
- else
- begin
- emit_reg(A_PUSH,S_L,R_EAX);
- pushedeax:=true;
- hreg:=R_AL;
- end;
- end;
- S_W : hreg:=R_DI;
- S_L : hreg:=R_EDI;
- end;
- emit_ref_reg(A_MOV,siz,
- newreference(t.reference),hreg);
- del_reference(t.reference);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,siz,
- hreg,newreference(ref))));
- if siz=S_B then
- begin
- if pushedeax then
- emit_reg(A_POP,S_L,R_EAX)
- else
- ungetregister(hreg);
- end;
- { we can release the registers }
- { but only AFTER the MOV! Important for the optimizer!
- (JM)}
- del_reference(ref);
- end;
- ungetiftemp(t.reference);
- end;
- else
- internalerror(330);
- end;
- end;
- procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
- begin
- case t.loc of
- LOC_REGISTER,
- LOC_CREGISTER : begin
- emit_reg_reg(A_MOV,S_L,t.register,reg);
- ungetregister32(t.register); { the register is not needed anymore }
- end;
- LOC_MEM,
- LOC_REFERENCE : begin
- if t.reference.is_immediate then
- emit_const_reg(A_MOV,S_L,
- t.reference.offset,reg)
- else
- begin
- emit_ref_reg(A_MOV,S_L,
- newreference(t.reference),reg);
- end;
- end;
- else
- internalerror(330);
- end;
- end;
- procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
- begin
- case t.loc of
- LOC_REGISTER,
- LOC_CREGISTER : begin
- emit_reg_reg(A_MOV,RegSize(Reg),
- reg,t.register);
- end;
- LOC_MEM,
- LOC_REFERENCE : begin
- if t.reference.is_immediate then
- internalerror(334)
- else
- begin
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,RegSize(Reg),
- Reg,newreference(t.reference))));
- end;
- end;
- else
- internalerror(330);
- end;
- end;
- procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
- begin
- case t.loc of
- LOC_MEM,
- LOC_REFERENCE : begin
- if t.reference.is_immediate then
- internalerror(331)
- else
- begin
- emit_ref_reg(A_LEA,S_L,
- newreference(t.reference),reg);
- end;
- if freetemp then
- ungetiftemp(t.reference);
- end;
- else
- internalerror(332);
- end;
- end;
- procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
- begin
- case t.loc of
- LOC_REGISTER,
- LOC_CREGISTER : begin
- emit_reg_reg(A_MOV,S_L,
- reglow,t.registerlow);
- emit_reg_reg(A_MOV,S_L,
- reghigh,t.registerhigh);
- end;
- LOC_MEM,
- LOC_REFERENCE : begin
- if t.reference.is_immediate then
- internalerror(334)
- else
- begin
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
- Reglow,newreference(t.reference))));
- inc(t.reference.offset,4);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
- Reghigh,newreference(t.reference))));
- end;
- end;
- else
- internalerror(330);
- end;
- end;
- procedure emit_pushq_loc(const t : tlocation);
- var
- hr : preference;
- begin
- case t.loc of
- LOC_REGISTER,
- LOC_CREGISTER:
- begin
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,
- t.registerhigh)));
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,
- t.registerlow)));
- end;
- LOC_MEM,
- LOC_REFERENCE:
- begin
- hr:=newreference(t.reference);
- inc(hr^.offset,4);
- exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,
- hr)));
- exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,
- newreference(t.reference))));
- ungetiftemp(t.reference);
- end;
- else internalerror(331);
- end;
- end;
- procedure release_loc(const t : tlocation);
- begin
- case t.loc of
- LOC_REGISTER,
- LOC_CREGISTER:
- begin
- ungetregister32(t.register);
- end;
- LOC_MEM,
- LOC_REFERENCE:
- del_reference(t.reference);
- else internalerror(332);
- end;
- end;
- procedure release_qword_loc(const t : tlocation);
- begin
- case t.loc of
- LOC_REGISTER,
- LOC_CREGISTER:
- begin
- ungetregister32(t.registerhigh);
- ungetregister32(t.registerlow);
- end;
- LOC_MEM,
- LOC_REFERENCE:
- del_reference(t.reference);
- else internalerror(331);
- end;
- end;
- procedure emit_push_loc(const t:tlocation);
- begin
- case t.loc of
- LOC_REGISTER,
- LOC_CREGISTER : begin
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,makereg32(t.register))));
- ungetregister(t.register); { the register is not needed anymore }
- end;
- LOC_MEM,
- LOC_REFERENCE : begin
- if t.reference.is_immediate then
- exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,t.reference.offset)))
- else
- exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,newreference(t.reference))));
- del_reference(t.reference);
- ungetiftemp(t.reference);
- end;
- else
- internalerror(330);
- end;
- end;
- procedure emit_pushw_loc(const t:tlocation);
- var
- opsize : topsize;
- begin
- case t.loc of
- LOC_REGISTER,
- LOC_CREGISTER : begin
- if target_os.stackalignment=4 then
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,makereg32(t.register))))
- else
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_W,makereg16(t.register))));
- ungetregister(t.register); { the register is not needed anymore }
- end;
- LOC_MEM,
- LOC_REFERENCE : begin
- if target_os.stackalignment=4 then
- opsize:=S_L
- else
- opsize:=S_W;
- if t.reference.is_immediate then
- exprasmlist^.concat(new(paicpu,op_const(A_PUSH,opsize,t.reference.offset)))
- else
- exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,opsize,newreference(t.reference))));
- del_reference(t.reference);
- ungetiftemp(t.reference);
- end;
- else
- internalerror(330);
- end;
- end;
- procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
- begin
- case t.loc of
- LOC_MEM,
- LOC_REFERENCE : begin
- if t.reference.is_immediate then
- internalerror(331)
- else
- begin
- emit_ref_reg(A_LEA,S_L,
- newreference(t.reference),R_EDI);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
- R_EDI,newreference(ref))));
- end;
- { release the registers }
- del_reference(t.reference);
- if freetemp then
- ungetiftemp(t.reference);
- end;
- else
- internalerror(332);
- end;
- end;
- procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
- begin
- case t.loc of
- LOC_MEM,
- LOC_REFERENCE : begin
- if t.reference.is_immediate then
- internalerror(331)
- else
- begin
- emit_ref_reg(A_LEA,S_L,
- newreference(t.reference),R_EDI);
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
- end;
- if freetemp then
- ungetiftemp(t.reference);
- end;
- else
- internalerror(332);
- end;
- end;
- procedure emit_to_mem(var p:ptree);
- begin
- case p^.location.loc of
- LOC_FPU : begin
- reset_reference(p^.location.reference);
- gettempofsizereference(10,p^.location.reference);
- floatstore(pfloatdef(p^.resulttype)^.typ,p^.location.reference);
- { This can't be never a l-value! (FK)
- p^.location.loc:=LOC_REFERENCE; }
- end;
- LOC_MEM,
- LOC_REFERENCE : ;
- LOC_CFPUREGISTER : begin
- emit_reg(A_FLD,S_NO,correct_fpuregister(p^.location.register,fpuvaroffset));
- inc(fpuvaroffset);
- reset_reference(p^.location.reference);
- gettempofsizereference(10,p^.location.reference);
- floatstore(pfloatdef(p^.resulttype)^.typ,p^.location.reference);
- { This can't be never a l-value! (FK)
- p^.location.loc:=LOC_REFERENCE; }
- end;
- else
- internalerror(333);
- end;
- p^.location.loc:=LOC_MEM;
- end;
- procedure emit_to_reg16(var hr:tregister);
- begin
- { ranges are a little bit bug sensitive ! }
- case hr of
- R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP,R_EBP:
- begin
- hr:=reg32toreg16(hr);
- end;
- R_AL,R_BL,R_CL,R_DL:
- begin
- hr:=reg8toreg16(hr);
- emit_const_reg(A_AND,S_W,$ff,hr);
- end;
- R_AH,R_BH,R_CH,R_DH:
- begin
- hr:=reg8toreg16(hr);
- emit_const_reg(A_AND,S_W,$ff00,hr);
- end;
- end;
- end;
- procedure emit_to_reg32(var hr:tregister);
- begin
- { ranges are a little bit bug sensitive ! }
- case hr of
- R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP,R_BP:
- begin
- hr:=reg16toreg32(hr);
- emit_const_reg(A_AND,S_L,$ffff,hr);
- end;
- R_AL,R_BL,R_CL,R_DL:
- begin
- hr:=reg8toreg32(hr);
- emit_const_reg(A_AND,S_L,$ff,hr);
- end;
- R_AH,R_BH,R_CH,R_DH:
- begin
- hr:=reg8toreg32(hr);
- emit_const_reg(A_AND,S_L,$ff00,hr);
- end;
- end;
- end;
- {*****************************************************************************
- Emit String Functions
- *****************************************************************************}
- procedure copyshortstring(const dref,sref : treference;len : byte;loadref:boolean);
- begin
- emitpushreferenceaddr(dref);
- if loadref then
- emit_push_mem(sref)
- else
- emitpushreferenceaddr(sref);
- push_int(len);
- emitcall('FPC_SHORTSTR_COPY');
- maybe_loadesi;
- end;
- procedure copylongstring(const dref,sref : treference;len : longint;loadref:boolean);
- begin
- emitpushreferenceaddr(dref);
- if loadref then
- emit_push_mem(sref)
- else
- emitpushreferenceaddr(sref);
- push_int(len);
- emitcall('FPC_LONGSTR_COPY');
- maybe_loadesi;
- end;
- procedure decrstringref(t : pdef;const ref : treference);
- var
- pushedregs : tpushed;
- begin
- pushusedregisters(pushedregs,$ff);
- emitpushreferenceaddr(ref);
- if is_ansistring(t) then
- begin
- emitcall('FPC_ANSISTR_DECR_REF');
- end
- else if is_widestring(t) then
- begin
- emitcall('FPC_WIDESTR_DECR_REF');
- end
- else internalerror(1859);
- popusedregisters(pushedregs);
- end;
- procedure loadansistring(p : ptree);
- {
- copies an ansistring from p^.right to p^.left, we
- assume, that both sides are ansistring, firstassignement have
- to take care of that, an ansistring can't be a register variable
- }
- var
- pushed : tpushed;
- ungettemp : boolean;
- begin
- { before pushing any parameter, we have to save all used }
- { registers, but before that we have to release the }
- { registers of that node to save uneccessary pushed }
- { so be careful, if you think you can optimize that code (FK) }
- { nevertheless, this has to be changed, because otherwise the }
- { register is released before it's contents are pushed -> }
- { problems with the optimizer (JM) }
- del_reference(p^.left^.location.reference);
- ungettemp:=false;
- case p^.right^.location.loc of
- LOC_REGISTER,LOC_CREGISTER:
- begin
- {$IfNDef regallocfix}
- ungetregister32(p^.right^.location.register);
- pushusedregisters(pushed,$ff);
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,p^.right^.location.register)));
- {$Else regallocfix}
- pushusedregisters(pushed, $ff xor ($80 shr byte(p^.right^.location.register)));
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,p^.right^.location.register)));
- ungetregister32(p^.right^.location.register);
- {$EndIf regallocfix}
- end;
- LOC_REFERENCE,LOC_MEM:
- begin
- {$IfNDef regallocfix}
- del_reference(p^.right^.location.reference);
- pushusedregisters(pushed,$ff);
- emit_push_mem(p^.right^.location.reference);
- {$Else regallocfix}
- pushusedregisters(pushed,$ff
- xor ($80 shr byte(p^.right^.location.reference.base))
- xor ($80 shr byte(p^.right^.location.reference.index)));
- emit_push_mem(p^.right^.location.reference);
- del_reference(p^.right^.location.reference);
- {$EndIf regallocfix}
- ungettemp:=true;
- end;
- end;
- emitpushreferenceaddr(p^.left^.location.reference);
- del_reference(p^.left^.location.reference);
- emitcall('FPC_ANSISTR_ASSIGN');
- maybe_loadesi;
- popusedregisters(pushed);
- if ungettemp then
- ungetiftemp(p^.right^.location.reference);
- end;
- {*****************************************************************************
- Emit Push Functions
- *****************************************************************************}
- function maybe_push(needed : byte;p : ptree;isint64 : boolean) : boolean;
- var
- pushed : boolean;
- {hregister : tregister; }
- {$ifdef TEMPS_NOT_PUSH}
- href : treference;
- {$endif TEMPS_NOT_PUSH}
- begin
- if needed>usablereg32 then
- begin
- if (p^.location.loc=LOC_REGISTER) then
- begin
- if isint64 then
- begin
- {$ifdef TEMPS_NOT_PUSH}
- gettempofsizereference(href,8);
- p^.temp_offset:=href.offset;
- href.offset:=href.offset+4;
- exprasmlist^.concat(new(paicpu,op_reg(A_MOV,S_L,p^.location.registerhigh,href)));
- href.offset:=href.offset-4;
- {$else TEMPS_NOT_PUSH}
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,p^.location.registerhigh)));
- {$endif TEMPS_NOT_PUSH}
- ungetregister32(p^.location.registerhigh);
- end
- {$ifdef TEMPS_NOT_PUSH}
- else
- begin
- gettempofsizereference(href,4);
- p^.temp_offset:=href.offset;
- end
- {$endif TEMPS_NOT_PUSH}
- ;
- pushed:=true;
- {$ifdef TEMPS_NOT_PUSH}
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,p^.location.register,href)));
- {$else TEMPS_NOT_PUSH}
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,p^.location.register)));
- {$endif TEMPS_NOT_PUSH}
- ungetregister32(p^.location.register);
- end
- else if (p^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
- ((p^.location.reference.base<>R_NO) or
- (p^.location.reference.index<>R_NO)
- ) then
- begin
- del_reference(p^.location.reference);
- emit_ref_reg(A_LEA,S_L,newreference(p^.location.reference),
- R_EDI);
- {$ifdef TEMPS_NOT_PUSH}
- gettempofsizereference(href,4);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,href)));
- p^.temp_offset:=href.offset;
- {$else TEMPS_NOT_PUSH}
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
- {$endif TEMPS_NOT_PUSH}
- pushed:=true;
- end
- else pushed:=false;
- end
- else pushed:=false;
- maybe_push:=pushed;
- end;
- {$ifdef TEMPS_NOT_PUSH}
- function maybe_savetotemp(needed : byte;p : ptree;isint64 : boolean) : boolean;
- var
- pushed : boolean;
- href : treference;
- begin
- if needed>usablereg32 then
- begin
- if (p^.location.loc=LOC_REGISTER) then
- begin
- if isint64(p^.resulttype) then
- begin
- gettempofsizereference(href,8);
- p^.temp_offset:=href.offset;
- href.offset:=href.offset+4;
- exprasmlist^.concat(new(paicpu,op_reg(A_MOV,S_L,p^.location.registerhigh,href)));
- href.offset:=href.offset-4;
- ungetregister32(p^.location.registerhigh);
- end
- else
- begin
- gettempofsizereference(href,4);
- p^.temp_offset:=href.offset;
- end;
- pushed:=true;
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,p^.location.register,href)));
- ungetregister32(p^.location.register);
- end
- else if (p^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
- ((p^.location.reference.base<>R_NO) or
- (p^.location.reference.index<>R_NO)
- ) then
- begin
- del_reference(p^.location.reference);
- emit_ref_reg(A_LEA,S_L,newreference(p^.location.reference),
- R_EDI);
- gettempofsizereference(href,4);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,href)));
- p^.temp_offset:=href.offset;
- pushed:=true;
- end
- else pushed:=false;
- end
- else pushed:=false;
- maybe_push:=pushed;
- end;
- {$endif TEMPS_NOT_PUSH}
- procedure push_int(l : longint);
- begin
- if (l = 0) and
- not(aktoptprocessor in [Class386, ClassP6]) and
- not(cs_littlesize in aktglobalswitches)
- Then
- begin
- emit_reg_reg(A_XOR,S_L,R_EDI,R_EDI);
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
- end
- else
- exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,l)));
- end;
- procedure emit_push_mem(const ref : treference);
- begin
- if ref.is_immediate then
- push_int(ref.offset)
- else
- begin
- if not(aktoptprocessor in [Class386, ClassP6]) and
- not(cs_littlesize in aktglobalswitches)
- then
- begin
- emit_ref_reg(A_MOV,S_L,newreference(ref),R_EDI);
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
- end
- else exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,newreference(ref))));
- end;
- end;
- procedure emitpushreferenceaddr(const ref : treference);
- var
- href : treference;
- begin
- { this will fail for references to other segments !!! }
- if ref.is_immediate then
- { is this right ? }
- begin
- { push_int(ref.offset)}
- gettempofsizereference(4,href);
- emit_const_ref(A_MOV,S_L,ref.offset,newreference(href));
- emitpushreferenceaddr(href);
- del_reference(href);
- end
- else
- begin
- if ref.segment<>R_NO then
- CGMessage(cg_e_cant_use_far_pointer_there);
- if (ref.base=R_NO) and (ref.index=R_NO) then
- exprasmlist^.concat(new(paicpu,op_sym_ofs(A_PUSH,S_L,ref.symbol,ref.offset)))
- else if (ref.base=R_NO) and (ref.index<>R_NO) and
- (ref.offset=0) and (ref.scalefactor=0) and (ref.symbol=nil) then
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,ref.index)))
- else if (ref.base<>R_NO) and (ref.index=R_NO) and
- (ref.offset=0) and (ref.symbol=nil) then
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,ref.base)))
- else
- begin
- emit_ref_reg(A_LEA,S_L,newreference(ref),R_EDI);
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
- end;
- end;
- end;
- procedure pushsetelement(p : ptree);
- {
- copies p a set element on the stack
- }
- var
- hr,hr16,hr32 : tregister;
- begin
- { copy the element on the stack, slightly complicated }
- if p^.treetype=ordconstn then
- begin
- if target_os.stackalignment=4 then
- exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,p^.value)))
- else
- exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_W,p^.value)));
- end
- else
- begin
- case p^.location.loc of
- LOC_REGISTER,
- LOC_CREGISTER :
- begin
- hr:=p^.location.register;
- case hr of
- R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
- begin
- hr16:=reg32toreg16(hr);
- hr32:=hr;
- end;
- R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
- begin
- hr16:=hr;
- hr32:=reg16toreg32(hr);
- end;
- R_AL,R_BL,R_CL,R_DL :
- begin
- hr16:=reg8toreg16(hr);
- hr32:=reg8toreg32(hr);
- end;
- end;
- if target_os.stackalignment=4 then
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,hr32)))
- else
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_W,hr16)));
- ungetregister32(hr32);
- end;
- else
- begin
- if target_os.stackalignment=4 then
- exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,newreference(p^.location.reference))))
- else
- exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_W,newreference(p^.location.reference))));
- del_reference(p^.location.reference);
- end;
- end;
- end;
- end;
- procedure restore(p : ptree;isint64 : boolean);
- var
- hregister : tregister;
- {$ifdef TEMPS_NOT_PUSH}
- href : treference;
- {$endif TEMPS_NOT_PUSH}
- begin
- hregister:=getregister32;
- {$ifdef TEMPS_NOT_PUSH}
- reset_reference(href);
- href.base:=procinfo^.frame_pointer;
- href.offset:=p^.temp_offset;
- emit_ref_reg(A_MOV,S_L,href,hregister);
- {$else TEMPS_NOT_PUSH}
- exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,hregister)));
- {$endif TEMPS_NOT_PUSH}
- if (p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
- begin
- p^.location.register:=hregister;
- if isint64 then
- begin
- p^.location.registerhigh:=getregister32;
- {$ifdef TEMPS_NOT_PUSH}
- href.offset:=p^.temp_offset+4;
- emit_ref_reg(A_MOV,S_L,p^.location.registerhigh);
- { set correctly for release ! }
- href.offset:=p^.temp_offset;
- {$else TEMPS_NOT_PUSH}
- exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,p^.location.registerhigh)));
- {$endif TEMPS_NOT_PUSH}
- end;
- end
- else
- begin
- reset_reference(p^.location.reference);
- { any reasons why this was moved into the index register ? }
- { normally usage of base register is much better (FK) }
- p^.location.reference.base:=hregister;
- { Why is this done? We can never be sure about p^.left
- because otherwise secondload fails !!!
- set_location(p^.left^.location,p^.location);}
- end;
- {$ifdef TEMPS_NOT_PUSH}
- ungetiftemp(href);
- {$endif TEMPS_NOT_PUSH}
- end;
- {$ifdef TEMPS_NOT_PUSH}
- procedure restorefromtemp(p : ptree;isint64 : boolean);
- var
- hregister : tregister;
- href : treference;
- begin
- hregister:=getregister32;
- reset_reference(href);
- href.base:=procinfo^.frame_pointer;
- href.offset:=p^.temp_offset;
- emit_ref_reg(A_MOV,S_L,href,hregister);
- if (p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
- begin
- p^.location.register:=hregister;
- if isint64 then
- begin
- p^.location.registerhigh:=getregister32;
- href.offset:=p^.temp_offset+4;
- emit_ref_reg(A_MOV,S_L,p^.location.registerhigh);
- { set correctly for release ! }
- href.offset:=p^.temp_offset;
- end;
- end
- else
- begin
- reset_reference(p^.location.reference);
- p^.location.reference.base:=hregister;
- { Why is this done? We can never be sure about p^.left
- because otherwise secondload fails PM
- set_location(p^.left^.location,p^.location);}
- end;
- ungetiftemp(href);
- end;
- {$endif TEMPS_NOT_PUSH}
- procedure push_value_para(p:ptree;inlined:boolean;para_offset:longint;alignment : longint);
- var
- tempreference : treference;
- r : preference;
- opsize : topsize;
- op : tasmop;
- hreg : tregister;
- size : longint;
- hlabel : pasmlabel;
- begin
- case p^.location.loc of
- LOC_REGISTER,
- LOC_CREGISTER:
- begin
- case p^.location.register of
- R_EAX,R_EBX,R_ECX,R_EDX,R_ESI,
- R_EDI,R_ESP,R_EBP :
- begin
- if p^.resulttype^.size=8 then
- begin
- inc(pushedparasize,8);
- if inlined then
- begin
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
- p^.location.registerlow,r)));
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize+4);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
- p^.location.registerhigh,r)));
- end
- else
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,p^.location.registerhigh)));
- ungetregister32(p^.location.registerhigh);
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,p^.location.registerlow)));
- ungetregister32(p^.location.registerlow);
- end
- else
- begin
- inc(pushedparasize,4);
- if inlined then
- begin
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,
- p^.location.register,r)));
- end
- else
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,p^.location.register)));
- ungetregister32(p^.location.register);
- end;
- end;
- R_AX,R_BX,R_CX,R_DX,R_SI,R_DI:
- begin
- if alignment=4 then
- begin
- opsize:=S_L;
- hreg:=reg16toreg32(p^.location.register);
- inc(pushedparasize,4);
- end
- else
- begin
- opsize:=S_W;
- hreg:=p^.location.register;
- inc(pushedparasize,2);
- end;
- if inlined then
- begin
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,opsize,hreg,r)));
- end
- else
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,opsize,hreg)));
- ungetregister32(reg16toreg32(p^.location.register));
- end;
- R_AL,R_BL,R_CL,R_DL:
- begin
- if alignment=4 then
- begin
- opsize:=S_L;
- hreg:=reg8toreg32(p^.location.register);
- inc(pushedparasize,4);
- end
- else
- begin
- opsize:=S_W;
- hreg:=reg8toreg16(p^.location.register);
- inc(pushedparasize,2);
- end;
- { we must push always 16 bit }
- if inlined then
- begin
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,opsize,hreg,r)));
- end
- else
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,opsize,hreg)));
- ungetregister32(reg8toreg32(p^.location.register));
- end;
- else internalerror(1899);
- end;
- end;
- LOC_FPU:
- begin
- size:=align(pfloatdef(p^.resulttype)^.size,alignment);
- inc(pushedparasize,size);
- if not inlined then
- emit_const_reg(A_SUB,S_L,size,R_ESP);
- {$ifdef GDB}
- if (cs_debuginfo in aktmoduleswitches) and
- (exprasmlist^.first=exprasmlist^.last) then
- exprasmlist^.concat(new(pai_force_line,init));
- {$endif GDB}
- r:=new_reference(R_ESP,0);
- floatstoreops(pfloatdef(p^.resulttype)^.typ,op,opsize);
- { this is the easiest case for inlined !! }
- if inlined then
- begin
- r^.base:=procinfo^.framepointer;
- r^.offset:=para_offset-pushedparasize;
- end;
- exprasmlist^.concat(new(paicpu,op_ref(op,opsize,r)));
- dec(fpuvaroffset);
- end;
- LOC_CFPUREGISTER:
- begin
- exprasmlist^.concat(new(paicpu,op_reg(A_FLD,S_NO,
- correct_fpuregister(p^.location.register,fpuvaroffset))));
- size:=align(pfloatdef(p^.resulttype)^.size,alignment);
- inc(pushedparasize,size);
- if not inlined then
- emit_const_reg(A_SUB,S_L,size,R_ESP);
- {$ifdef GDB}
- if (cs_debuginfo in aktmoduleswitches) and
- (exprasmlist^.first=exprasmlist^.last) then
- exprasmlist^.concat(new(pai_force_line,init));
- {$endif GDB}
- r:=new_reference(R_ESP,0);
- floatstoreops(pfloatdef(p^.resulttype)^.typ,op,opsize);
- { this is the easiest case for inlined !! }
- if inlined then
- begin
- r^.base:=procinfo^.framepointer;
- r^.offset:=para_offset-pushedparasize;
- end;
- exprasmlist^.concat(new(paicpu,op_ref(op,opsize,r)));
- end;
- LOC_REFERENCE,LOC_MEM:
- begin
- tempreference:=p^.location.reference;
- del_reference(p^.location.reference);
- case p^.resulttype^.deftype of
- enumdef,
- orddef :
- begin
- case p^.resulttype^.size of
- 8 : begin
- inc(pushedparasize,8);
- if inlined then
- begin
- emit_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI);
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- inc(tempreference.offset,4);
- emit_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI);
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize+4);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- begin
- inc(tempreference.offset,4);
- emit_push_mem(tempreference);
- dec(tempreference.offset,4);
- emit_push_mem(tempreference);
- end;
- end;
- 4 : begin
- inc(pushedparasize,4);
- if inlined then
- begin
- emit_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI);
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- end;
- 1,2 : begin
- if alignment=4 then
- begin
- opsize:=S_L;
- hreg:=R_EDI;
- inc(pushedparasize,4);
- end
- else
- begin
- opsize:=S_W;
- hreg:=R_DI;
- inc(pushedparasize,2);
- end;
- if inlined then
- begin
- emit_ref_reg(A_MOV,opsize,
- newreference(tempreference),hreg);
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,opsize,hreg,r)));
- end
- else
- exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,opsize,
- newreference(tempreference))));
- end;
- else
- internalerror(234231);
- end;
- end;
- floatdef :
- begin
- case pfloatdef(p^.resulttype)^.typ of
- f32bit,
- s32real :
- begin
- inc(pushedparasize,4);
- if inlined then
- begin
- emit_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI);
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- end;
- s64real,
- s64comp :
- begin
- inc(pushedparasize,4);
- inc(tempreference.offset,4);
- if inlined then
- begin
- emit_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI);
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- inc(pushedparasize,4);
- dec(tempreference.offset,4);
- if inlined then
- begin
- emit_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI);
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- end;
- s80real :
- begin
- inc(pushedparasize,4);
- if alignment=4 then
- inc(tempreference.offset,8)
- else
- inc(tempreference.offset,6);
- if inlined then
- begin
- emit_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI);
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- dec(tempreference.offset,4);
- inc(pushedparasize,4);
- if inlined then
- begin
- emit_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI);
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- if alignment=4 then
- begin
- opsize:=S_L;
- hreg:=R_EDI;
- inc(pushedparasize,4);
- dec(tempreference.offset,4);
- end
- else
- begin
- opsize:=S_W;
- hreg:=R_DI;
- inc(pushedparasize,2);
- dec(tempreference.offset,2);
- end;
- if inlined then
- begin
- emit_ref_reg(A_MOV,opsize,
- newreference(tempreference),hreg);
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,opsize,hreg,r)));
- end
- else
- exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,opsize,
- newreference(tempreference))));
- end;
- end;
- end;
- pointerdef,
- procvardef,
- classrefdef:
- begin
- inc(pushedparasize,4);
- if inlined then
- begin
- emit_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI);
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- end;
- arraydef,
- recorddef,
- stringdef,
- setdef,
- objectdef :
- begin
- { even some structured types are 32 bit }
- if is_widestring(p^.resulttype) or
- is_ansistring(p^.resulttype) or
- is_smallset(p^.resulttype) or
- ((p^.resulttype^.deftype in [recorddef,arraydef]) and (p^.resulttype^.size<=4)
- and ((p^.resulttype^.deftype<>arraydef) or not
- (parraydef(p^.resulttype)^.IsConstructor or
- parraydef(p^.resulttype)^.isArrayOfConst or
- is_open_array(p^.resulttype)))
- ) or
- ((p^.resulttype^.deftype=objectdef) and
- pobjectdef(p^.resulttype)^.is_class) then
- begin
- if (p^.resulttype^.size>2) or
- (alignment=4) then
- begin
- inc(pushedparasize,4);
- if inlined then
- begin
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- concatcopy(tempreference,r^,4,false,false);
- end
- else
- emit_push_mem(tempreference);
- end
- else
- begin
- if p^.resulttype^.size>0 then
- begin
- inc(pushedparasize,2);
- if inlined then
- begin
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- concatcopy(tempreference,r^,2,false,false);
- end
- else
- exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_W,newreference(tempreference))));
- end;
- end;
- end
- { call by value open array ? }
- else
- internalerror(8954);
- end;
- else
- CGMessage(cg_e_illegal_expression);
- end;
- end;
- LOC_JUMP:
- begin
- getlabel(hlabel);
- if alignment=4 then
- begin
- opsize:=S_L;
- inc(pushedparasize,4);
- end
- else
- begin
- opsize:=S_W;
- inc(pushedparasize,2);
- end;
- emitlab(truelabel);
- if inlined then
- begin
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- emit_const_ref(A_MOV,opsize,1,r);
- end
- else
- exprasmlist^.concat(new(paicpu,op_const(A_PUSH,opsize,1)));
- emitjmp(C_None,hlabel);
- emitlab(falselabel);
- if inlined then
- begin
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- emit_const_ref(A_MOV,opsize,0,r);
- end
- else
- exprasmlist^.concat(new(paicpu,op_const(A_PUSH,opsize,0)));
- emitlab(hlabel);
- end;
- LOC_FLAGS:
- begin
- if not(R_EAX in unused) then
- emit_reg_reg(A_MOV,S_L,R_EAX,R_EDI);
- emit_flag2reg(p^.location.resflags,R_AL);
- emit_reg_reg(A_MOVZX,S_BW,R_AL,R_AX);
- if alignment=4 then
- begin
- opsize:=S_L;
- hreg:=R_EAX;
- inc(pushedparasize,4);
- end
- else
- begin
- opsize:=S_W;
- hreg:=R_AX;
- inc(pushedparasize,2);
- end;
- if inlined then
- begin
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,opsize,hreg,r)));
- end
- else
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,opsize,hreg)));
- if not(R_EAX in unused) then
- emit_reg_reg(A_MOV,S_L,R_EDI,R_EAX);
- end;
- {$ifdef SUPPORT_MMX}
- LOC_MMXREGISTER,
- LOC_CMMXREGISTER:
- begin
- inc(pushedparasize,8); { was missing !!! (PM) }
- emit_const_reg(
- A_SUB,S_L,8,R_ESP);
- {$ifdef GDB}
- if (cs_debuginfo in aktmoduleswitches) and
- (exprasmlist^.first=exprasmlist^.last) then
- exprasmlist^.concat(new(pai_force_line,init));
- {$endif GDB}
- if inlined then
- begin
- r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOVQ,S_NO,
- p^.location.register,r)));
- end
- else
- begin
- r:=new_reference(R_ESP,0);
- exprasmlist^.concat(new(paicpu,op_reg_ref(
- A_MOVQ,S_NO,p^.location.register,r)));
- end;
- end;
- {$endif SUPPORT_MMX}
- end;
- end;
- {*****************************************************************************
- Emit Float Functions
- *****************************************************************************}
- procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
- begin
- case t of
- s32real : begin
- op:=A_FLD;
- s:=S_FS;
- end;
- s64real : begin
- op:=A_FLD;
- { ???? }
- s:=S_FL;
- end;
- s80real : begin
- op:=A_FLD;
- s:=S_FX;
- end;
- s64comp : begin
- op:=A_FILD;
- s:=S_IQ;
- end;
- else internalerror(17);
- end;
- end;
- procedure floatload(t : tfloattype;const ref : treference);
- var
- op : tasmop;
- s : topsize;
- begin
- floatloadops(t,op,s);
- exprasmlist^.concat(new(paicpu,op_ref(op,s,
- newreference(ref))));
- inc(fpuvaroffset);
- end;
- procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
- begin
- case t of
- s32real : begin
- op:=A_FSTP;
- s:=S_FS;
- end;
- s64real : begin
- op:=A_FSTP;
- s:=S_FL;
- end;
- s80real : begin
- op:=A_FSTP;
- s:=S_FX;
- end;
- s64comp : begin
- op:=A_FISTP;
- s:=S_IQ;
- end;
- else
- internalerror(17);
- end;
- end;
- procedure floatstore(t : tfloattype;const ref : treference);
- var
- op : tasmop;
- s : topsize;
- begin
- floatstoreops(t,op,s);
- exprasmlist^.concat(new(paicpu,op_ref(op,s,
- newreference(ref))));
- dec(fpuvaroffset);
- end;
- {*****************************************************************************
- Emit Functions
- *****************************************************************************}
- procedure maketojumpbool(p : ptree);
- {
- produces jumps to true respectively false labels using boolean expressions
- }
- var
- opsize : topsize;
- storepos : tfileposinfo;
- begin
- if p^.error then
- exit;
- storepos:=aktfilepos;
- aktfilepos:=p^.fileinfo;
- if is_boolean(p^.resulttype) then
- begin
- if is_constboolnode(p) then
- begin
- if p^.value<>0 then
- emitjmp(C_None,truelabel)
- else
- emitjmp(C_None,falselabel);
- end
- else
- begin
- opsize:=def_opsize(p^.resulttype);
- case p^.location.loc of
- LOC_CREGISTER,LOC_REGISTER : begin
- emit_reg_reg(A_OR,opsize,p^.location.register,
- p^.location.register);
- ungetregister(p^.location.register);
- emitjmp(C_NZ,truelabel);
- emitjmp(C_None,falselabel);
- end;
- LOC_MEM,LOC_REFERENCE : begin
- emit_const_ref(
- A_CMP,opsize,0,newreference(p^.location.reference));
- del_reference(p^.location.reference);
- emitjmp(C_NZ,truelabel);
- emitjmp(C_None,falselabel);
- end;
- LOC_FLAGS : begin
- emitjmp(flag_2_cond[p^.location.resflags],truelabel);
- emitjmp(C_None,falselabel);
- end;
- end;
- end;
- end
- else
- CGMessage(type_e_mismatch);
- aktfilepos:=storepos;
- end;
- { produces if necessary overflowcode }
- procedure emitoverflowcheck(p:ptree);
- var
- hl : pasmlabel;
- begin
- if not(cs_check_overflow in aktlocalswitches) then
- exit;
- getlabel(hl);
- if not ((p^.resulttype^.deftype=pointerdef) or
- ((p^.resulttype^.deftype=orddef) and
- (porddef(p^.resulttype)^.typ in [u64bit,u16bit,u32bit,u8bit,uchar,
- bool8bit,bool16bit,bool32bit]))) then
- emitjmp(C_NO,hl)
- else
- emitjmp(C_NB,hl);
- emitcall('FPC_OVERFLOW');
- emitlab(hl);
- end;
- { produces range check code, while one of the operands is a 64 bit
- integer }
- procedure emitrangecheck64(p : ptree;todef : pdef);
- begin
- internalerror(28699);
- end;
- { produces if necessary rangecheckcode }
- procedure emitrangecheck(p:ptree;todef:pdef);
- {
- generate range checking code for the value at location t. The
- type used is the checked against todefs ranges. fromdef (p.resulttype)
- is the original type used at that location, when both defs are
- equal the check is also insert (needed for succ,pref,inc,dec)
- }
- var
- neglabel,
- poslabel : pasmlabel;
- href : treference;
- rstr : string;
- hreg : tregister;
- opsize : topsize;
- op : tasmop;
- fromdef : pdef;
- lto,hto,
- lfrom,hfrom : longint;
- doublebound,
- is_reg,
- popecx : boolean;
- begin
- { range checking on and range checkable value? }
- if not(cs_check_range in aktlocalswitches) or
- not(todef^.deftype in [orddef,enumdef,arraydef]) then
- exit;
- { only check when assigning to scalar, subranges are different,
- when todef=fromdef then the check is always generated }
- fromdef:=p^.resulttype;
- if is_64bitint(fromdef) or is_64bitint(todef) then
- begin
- emitrangecheck64(p,todef);
- exit;
- end;
- {we also need lto and hto when checking if we need to use doublebound!
- (JM)}
- getrange(todef,lto,hto);
- if todef<>fromdef then
- begin
- getrange(p^.resulttype,lfrom,hfrom);
- { first check for not being u32bit, then if the to is bigger than
- from }
- if (lto<hto) and (lfrom<hfrom) and
- (lto<=lfrom) and (hto>=hfrom) then
- exit;
- end;
- { generate the rangecheck code for the def where we are going to
- store the result }
- doublebound:=false;
- case todef^.deftype of
- orddef :
- begin
- porddef(todef)^.genrangecheck;
- rstr:=porddef(todef)^.getrangecheckstring;
- doublebound:=(porddef(todef)^.typ=u32bit) and (lto>hto);
- end;
- enumdef :
- begin
- penumdef(todef)^.genrangecheck;
- rstr:=penumdef(todef)^.getrangecheckstring;
- end;
- arraydef :
- begin
- parraydef(todef)^.genrangecheck;
- rstr:=parraydef(todef)^.getrangecheckstring;
- end;
- end;
- { get op and opsize }
- opsize:=def2def_opsize(fromdef,u32bitdef);
- if opsize in [S_B,S_W,S_L] then
- op:=A_MOV
- else
- if is_signed(fromdef) then
- op:=A_MOVSX
- else
- op:=A_MOVZX;
- is_reg:=(p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]);
- if is_reg then
- hreg:=p^.location.register;
- if not target_os.use_bound_instruction then
- begin
- { FPC_BOUNDCHECK needs to be called with
- %ecx - value
- %edi - pointer to the ranges }
- popecx:=false;
- if not(is_reg) or
- (p^.location.register<>R_ECX) then
- begin
- if not(R_ECX in unused) then
- begin
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_ECX)));
- popecx:=true;
- end;
- if is_reg then
- emit_reg_reg(op,opsize,p^.location.register,R_ECX)
- else
- emit_ref_reg(op,opsize,newreference(p^.location.reference),R_ECX);
- end;
- if doublebound then
- begin
- getlabel(neglabel);
- getlabel(poslabel);
- emit_reg_reg(A_OR,S_L,R_ECX,R_ECX);
- emitjmp(C_L,neglabel);
- end;
- { insert bound instruction only }
- exprasmlist^.concat(new(paicpu,op_sym_ofs_reg(A_MOV,S_L,newasmsymbol(rstr),0,R_EDI)));
- emitcall('FPC_BOUNDCHECK');
- { u32bit needs 2 checks }
- if doublebound then
- begin
- emitjmp(C_None,poslabel);
- emitlab(neglabel);
- exprasmlist^.concat(new(paicpu,op_sym_ofs_reg(A_MOV,S_L,newasmsymbol(rstr),8,R_EDI)));
- emitcall('FPC_BOUNDCHECK');
- emitlab(poslabel);
- end;
- if popecx then
- exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_ECX)));
- end
- else
- begin
- reset_reference(href);
- href.symbol:=newasmsymbol(rstr);
- { load the value in a register }
- if is_reg then
- begin
- { be sure that hreg is a 32 bit reg, if not load it in %edi }
- if p^.location.register in [R_EAX..R_EDI] then
- hreg:=p^.location.register
- else
- begin
- emit_reg_reg(op,opsize,p^.location.register,R_EDI);
- hreg:=R_EDI;
- end;
- end
- else
- begin
- emit_ref_reg(op,opsize,newreference(p^.location.reference),R_EDI);
- hreg:=R_EDI;
- end;
- if doublebound then
- begin
- getlabel(neglabel);
- getlabel(poslabel);
- emit_reg_reg(A_OR,S_L,hreg,hreg);
- emitjmp(C_L,neglabel);
- end;
- { insert bound instruction only }
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_BOUND,S_L,hreg,newreference(href))));
- { u32bit needs 2 checks }
- if doublebound then
- begin
- href.offset:=8;
- emitjmp(C_None,poslabel);
- emitlab(neglabel);
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_BOUND,S_L,hreg,newreference(href))));
- emitlab(poslabel);
- end;
- end;
- end;
- procedure concatcopy(source,dest : treference;size : longint;delsource,loadref : boolean);
- const
- isizes : array[0..3] of topsize=(S_L,S_B,S_W,S_B);
- ishr : array[0..3] of byte=(2,0,1,0);
- var
- ecxpushed : boolean;
- helpsize : longint;
- i : byte;
- reg8,reg32 : tregister;
- swap : boolean;
- procedure maybepushecx;
- begin
- if not(R_ECX in unused) then
- begin
- exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,R_ECX)));
- ecxpushed:=true;
- end;
- end;
- begin
- {$IfNDef regallocfix}
- If delsource then
- del_reference(source);
- {$EndIf regallocfix}
- if (not loadref) and
- ((size<=8) or
- (not(cs_littlesize in aktglobalswitches ) and (size<=12))) then
- begin
- helpsize:=size shr 2;
- for i:=1 to helpsize do
- begin
- emit_ref_reg(A_MOV,S_L,newreference(source),R_EDI);
- {$ifdef regallocfix}
- If (size = 4) and delsource then
- del_reference(source);
- {$endif regallocfix}
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,R_EDI,newreference(dest))));
- inc(source.offset,4);
- inc(dest.offset,4);
- dec(size,4);
- end;
- if size>1 then
- begin
- emit_ref_reg(A_MOV,S_W,newreference(source),R_DI);
- {$ifdef regallocfix}
- If (size = 2) and delsource then
- del_reference(source);
- {$endif regallocfix}
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_W,R_DI,newreference(dest))));
- inc(source.offset,2);
- inc(dest.offset,2);
- dec(size,2);
- end;
- if size>0 then
- begin
- { and now look for an 8 bit register }
- swap:=false;
- if R_EAX in unused then reg8:=R_AL
- else if R_EBX in unused then reg8:=R_BL
- else if R_ECX in unused then reg8:=R_CL
- else if R_EDX in unused then reg8:=R_DL
- else
- begin
- swap:=true;
- { we need only to check 3 registers, because }
- { one is always not index or base }
- if (dest.base<>R_EAX) and (dest.index<>R_EAX) then
- begin
- reg8:=R_AL;
- reg32:=R_EAX;
- end
- else if (dest.base<>R_EBX) and (dest.index<>R_EBX) then
- begin
- reg8:=R_BL;
- reg32:=R_EBX;
- end
- else if (dest.base<>R_ECX) and (dest.index<>R_ECX) then
- begin
- reg8:=R_CL;
- reg32:=R_ECX;
- end;
- end;
- if swap then
- { was earlier XCHG, of course nonsense }
- emit_reg_reg(A_MOV,S_L,reg32,R_EDI);
- emit_ref_reg(A_MOV,S_B,newreference(source),reg8);
- {$ifdef regallocfix}
- If delsource then
- del_reference(source);
- {$endif regallocfix}
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_B,reg8,newreference(dest))));
- if swap then
- emit_reg_reg(A_MOV,S_L,R_EDI,reg32);
- end;
- end
- else
- begin
- emit_ref_reg(A_LEA,S_L,newreference(dest),R_EDI);
- {$ifdef regallocfix}
- {is this ok?? (JM)}
- del_reference(dest);
- {$endif regallocfix}
- if loadref then
- emit_ref_reg(A_MOV,S_L,newreference(source),R_ESI)
- else
- begin
- emit_ref_reg(A_LEA,S_L,newreference(source),R_ESI);
- {$ifdef regallocfix}
- if delsource then
- del_reference(source);
- {$endif regallocfix}
- end;
- exprasmlist^.concat(new(paicpu,op_none(A_CLD,S_NO)));
- ecxpushed:=false;
- if cs_littlesize in aktglobalswitches then
- begin
- maybepushecx;
- emit_const_reg(A_MOV,S_L,size,R_ECX);
- exprasmlist^.concat(new(paicpu,op_none(A_REP,S_NO)));
- exprasmlist^.concat(new(paicpu,op_none(A_MOVSB,S_NO)));
- end
- else
- begin
- helpsize:=size shr 2;
- size:=size and 3;
- if helpsize>1 then
- begin
- maybepushecx;
- emit_const_reg(A_MOV,S_L,helpsize,R_ECX);
- exprasmlist^.concat(new(paicpu,op_none(A_REP,S_NO)));
- end;
- if helpsize>0 then
- exprasmlist^.concat(new(paicpu,op_none(A_MOVSD,S_NO)));
- if size>1 then
- begin
- dec(size,2);
- exprasmlist^.concat(new(paicpu,op_none(A_MOVSW,S_NO)));
- end;
- if size=1 then
- exprasmlist^.concat(new(paicpu,op_none(A_MOVSB,S_NO)));
- end;
- if ecxpushed then
- exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_ECX)));
- { loading SELF-reference again }
- maybe_loadesi;
- end;
- if delsource then
- ungetiftemp(source);
- end;
- procedure emitloadord2reg(const location:Tlocation;orddef:Porddef;
- destreg:Tregister;delloc:boolean);
- {A lot smaller and less bug sensitive than the original unfolded loads.}
- var tai:Paicpu;
- r:Preference;
- begin
- case location.loc of
- LOC_REGISTER,LOC_CREGISTER:
- begin
- case orddef^.typ of
- u8bit:
- tai:=new(paicpu,op_reg_reg(A_MOVZX,S_BL,location.register,destreg));
- s8bit:
- tai:=new(paicpu,op_reg_reg(A_MOVSX,S_BL,location.register,destreg));
- u16bit:
- tai:=new(paicpu,op_reg_reg(A_MOVZX,S_WL,location.register,destreg));
- s16bit:
- tai:=new(paicpu,op_reg_reg(A_MOVSX,S_WL,location.register,destreg));
- u32bit:
- tai:=new(paicpu,op_reg_reg(A_MOV,S_L,location.register,destreg));
- s32bit:
- tai:=new(paicpu,op_reg_reg(A_MOV,S_L,location.register,destreg));
- end;
- if delloc then
- ungetregister(location.register);
- end;
- LOC_MEM,
- LOC_REFERENCE:
- begin
- if location.reference.is_immediate then
- tai:=new(paicpu,op_const_reg(A_MOV,S_L,location.reference.offset,destreg))
- else
- begin
- r:=newreference(location.reference);
- case orddef^.typ of
- u8bit:
- tai:=new(paicpu,op_ref_reg(A_MOVZX,S_BL,r,destreg));
- s8bit:
- tai:=new(paicpu,op_ref_reg(A_MOVSX,S_BL,r,destreg));
- u16bit:
- tai:=new(paicpu,op_ref_reg(A_MOVZX,S_WL,r,destreg));
- s16bit:
- tai:=new(paicpu,op_ref_reg(A_MOVSX,S_WL,r,destreg));
- u32bit:
- tai:=new(paicpu,op_ref_reg(A_MOV,S_L,r,destreg));
- s32bit:
- tai:=new(paicpu,op_ref_reg(A_MOV,S_L,r,destreg));
- end;
- end;
- if delloc then
- del_reference(location.reference);
- end
- else
- internalerror(6);
- end;
- exprasmlist^.concat(tai);
- end;
- { if necessary ESI is reloaded after a call}
- procedure maybe_loadesi;
- var
- hp : preference;
- p : pprocinfo;
- i : longint;
- begin
- if assigned(procinfo^._class) then
- begin
- if lexlevel>normal_function_level then
- begin
- new(hp);
- reset_reference(hp^);
- hp^.offset:=procinfo^.framepointer_offset;
- hp^.base:=procinfo^.framepointer;
- emit_ref_reg(A_MOV,S_L,hp,R_ESI);
- p:=procinfo^.parent;
- for i:=3 to lexlevel-1 do
- begin
- new(hp);
- reset_reference(hp^);
- hp^.offset:=p^.framepointer_offset;
- hp^.base:=R_ESI;
- emit_ref_reg(A_MOV,S_L,hp,R_ESI);
- p:=p^.parent;
- end;
- new(hp);
- reset_reference(hp^);
- hp^.offset:=p^.selfpointer_offset;
- hp^.base:=R_ESI;
- emit_ref_reg(A_MOV,S_L,hp,R_ESI);
- end
- else
- begin
- new(hp);
- reset_reference(hp^);
- hp^.offset:=procinfo^.selfpointer_offset;
- hp^.base:=procinfo^.framepointer;
- emit_ref_reg(A_MOV,S_L,hp,R_ESI);
- end;
- end;
- end;
- procedure firstcomplex(p : ptree);
- var
- hp : ptree;
- begin
- { always calculate boolean AND and OR from left to right }
- if (p^.treetype in [orn,andn]) and
- (p^.left^.resulttype^.deftype=orddef) and
- (porddef(p^.left^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit]) then
- p^.swaped:=false
- else
- if (p^.left^.registers32<p^.right^.registers32) and
- { the following check is appropriate, because all }
- { 4 registers are rarely used and it is thereby }
- { achieved that the extra code is being dropped }
- { by exchanging not commutative operators }
- (p^.right^.registers32<=4) then
- begin
- hp:=p^.left;
- p^.left:=p^.right;
- p^.right:=hp;
- p^.swaped:=true;
- end
- else
- p^.swaped:=false;
- end;
- {*****************************************************************************
- Entry/Exit Code Functions
- *****************************************************************************}
- procedure genprofilecode;
- var
- pl : pasmlabel;
- begin
- if (po_assembler in aktprocsym^.definition^.procoptions) then
- exit;
- case target_info.target of
- target_i386_linux:
- begin
- getlabel(pl);
- emitcall('mcount');
- exprasmlist^.insert(new(paicpu,op_sym_ofs_reg(A_MOV,S_L,pl,0,R_EDX)));
- exprasmlist^.insert(new(pai_section,init(sec_code)));
- exprasmlist^.insert(new(pai_const,init_32bit(0)));
- exprasmlist^.insert(new(pai_label,init(pl)));
- exprasmlist^.insert(new(pai_align,init(4)));
- exprasmlist^.insert(new(pai_section,init(sec_data)));
- end;
- target_i386_go32v2:
- begin
- emitinsertcall('MCOUNT');
- end;
- end;
- end;
- procedure generate_interrupt_stackframe_entry;
- begin
- { save the registers of an interrupt procedure }
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EAX)));
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EBX)));
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_ECX)));
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EDX)));
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_ESI)));
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
- { .... also the segment registers }
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_W,R_DS)));
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_W,R_ES)));
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_W,R_FS)));
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_W,R_GS)));
- end;
- procedure generate_interrupt_stackframe_exit;
- begin
- { restore the registers of an interrupt procedure }
- { this was all with entrycode instead of exitcode !!}
- procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_EAX)));
- procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_EBX)));
- procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_ECX)));
- procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_EDX)));
- procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_ESI)));
- procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_L,R_EDI)));
- { .... also the segment registers }
- procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_W,R_DS)));
- procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_W,R_ES)));
- procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_W,R_FS)));
- procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_POP,S_W,R_GS)));
- { this restores the flags }
- procinfo^.aktexitcode^.concat(new(paicpu,op_none(A_IRET,S_NO)));
- end;
- { generates the code for threadvar initialisation }
- procedure initialize_threadvar(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
- var
- hr : treference;
- begin
- if (psym(p)^.typ=varsym) and
- (vo_is_thread_var in pvarsym(p)^.varoptions) then
- begin
- exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,pvarsym(p)^.getsize)));
- reset_reference(hr);
- hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
- emitpushreferenceaddr(hr);
- emitcall('FPC_INIT_THREADVAR');
- end;
- end;
- { initilizes data of type t }
- { if is_already_ref is true then the routines assumes }
- { that r points to the data to initialize }
- procedure initialize(t : pdef;const ref : treference;is_already_ref : boolean);
- var
- hr : treference;
- begin
- if is_ansistring(t) or
- is_widestring(t) then
- begin
- emit_const_ref(A_MOV,S_L,0,
- newreference(ref));
- end
- else
- begin
- reset_reference(hr);
- hr.symbol:=t^.get_inittable_label;
- emitpushreferenceaddr(hr);
- if is_already_ref then
- exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,
- newreference(ref))))
- else
- emitpushreferenceaddr(ref);
- emitcall('FPC_INITIALIZE');
- end;
- end;
- { finalizes data of type t }
- { if is_already_ref is true then the routines assumes }
- { that r points to the data to finalizes }
- procedure finalize(t : pdef;const ref : treference;is_already_ref : boolean);
- var
- r : treference;
- begin
- if is_ansistring(t) or
- is_widestring(t) then
- begin
- decrstringref(t,ref);
- end
- else
- begin
- reset_reference(r);
- r.symbol:=t^.get_inittable_label;
- emitpushreferenceaddr(r);
- if is_already_ref then
- exprasmlist^.concat(new(paicpu,op_ref(A_PUSH,S_L,
- newreference(ref))))
- else
- emitpushreferenceaddr(ref);
- emitcall('FPC_FINALIZE');
- end;
- end;
- { generates the code for initialisation of local data }
- procedure initialize_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
- var
- hr : treference;
- begin
- if (psym(p)^.typ=varsym) and
- assigned(pvarsym(p)^.definition) and
- not((pvarsym(p)^.definition^.deftype=objectdef) and
- pobjectdef(pvarsym(p)^.definition)^.is_class) and
- pvarsym(p)^.definition^.needs_inittable then
- begin
- procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
- reset_reference(hr);
- if psym(p)^.owner^.symtabletype=localsymtable then
- begin
- hr.base:=procinfo^.framepointer;
- hr.offset:=-pvarsym(p)^.address;
- end
- else
- begin
- hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
- end;
- initialize(pvarsym(p)^.definition,hr,false);
- end;
- end;
- { generates the code for incrementing the reference count of parameters }
- procedure incr_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
- var
- hr : treference;
- begin
- if (psym(p)^.typ=varsym) and
- not((pvarsym(p)^.definition^.deftype=objectdef) and
- pobjectdef(pvarsym(p)^.definition)^.is_class) and
- pvarsym(p)^.definition^.needs_inittable and
- ((pvarsym(p)^.varspez=vs_value) {or
- (pvarsym(p)^.varspez=vs_const) and
- not(dont_copy_const_param(pvarsym(p)^.definition))}) then
- begin
- procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
- reset_reference(hr);
- hr.symbol:=pvarsym(p)^.definition^.get_inittable_label;
- emitpushreferenceaddr(hr);
- reset_reference(hr);
- hr.base:=procinfo^.framepointer;
- hr.offset:=pvarsym(p)^.address+procinfo^.call_offset;
- emitpushreferenceaddr(hr);
- reset_reference(hr);
- emitcall('FPC_ADDREF');
- end;
- end;
- { generates the code for finalisation of local data }
- procedure finalize_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
- var
- hr : treference;
- begin
- if (psym(p)^.typ=varsym) and
- assigned(pvarsym(p)^.definition) and
- not((pvarsym(p)^.definition^.deftype=objectdef) and
- pobjectdef(pvarsym(p)^.definition)^.is_class) and
- pvarsym(p)^.definition^.needs_inittable then
- begin
- { not all kind of parameters need to be finalized }
- if (psym(p)^.owner^.symtabletype=parasymtable) and
- ((pvarsym(p)^.varspez=vs_var) or
- (pvarsym(p)^.varspez=vs_const) { and
- (dont_copy_const_param(pvarsym(p)^.definition)) } ) then
- exit;
- procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
- reset_reference(hr);
- case psym(p)^.owner^.symtabletype of
- localsymtable:
- begin
- hr.base:=procinfo^.framepointer;
- hr.offset:=-pvarsym(p)^.address;
- end;
- parasymtable:
- begin
- hr.base:=procinfo^.framepointer;
- hr.offset:=pvarsym(p)^.address+procinfo^.call_offset;
- end;
- else
- hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
- end;
- finalize(pvarsym(p)^.definition,hr,false);
- end;
- end;
- { generates the code to make local copies of the value parameters }
- procedure copyvalueparas(p : pnamedindexobject);{$ifndef fpc}far;{$endif}
- var
- href1,href2 : treference;
- r : preference;
- len : longint;
- opsize : topsize;
- again,ok : pasmlabel;
- begin
- if (psym(p)^.typ=varsym) and
- (pvarsym(p)^.varspez=vs_value) and
- (push_addr_param(pvarsym(p)^.definition)) then
- begin
- if is_open_array(pvarsym(p)^.definition) or
- is_array_of_const(pvarsym(p)^.definition) then
- begin
- { get stack space }
- new(r);
- reset_reference(r^);
- r^.base:=procinfo^.framepointer;
- r^.offset:=pvarsym(p)^.address+4+procinfo^.call_offset;
- exprasmlist^.concat(new(paicpu,
- op_ref_reg(A_MOV,S_L,r,R_EDI)));
- exprasmlist^.concat(new(paicpu,
- op_reg(A_INC,S_L,R_EDI)));
- exprasmlist^.concat(new(paicpu,
- op_const_reg(A_IMUL,S_L,
- parraydef(pvarsym(p)^.definition)^.definition^.size,R_EDI)));
- {$ifndef NOTARGETWIN32}
- { windows guards only a few pages for stack growing, }
- { so we have to access every page first }
- if target_os.id=os_i386_win32 then
- begin
- getlabel(again);
- getlabel(ok);
- emitlab(again);
- exprasmlist^.concat(new(paicpu,
- op_const_reg(A_CMP,S_L,winstackpagesize,R_EDI)));
- emitjmp(C_C,ok);
- exprasmlist^.concat(new(paicpu,
- op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP)));
- exprasmlist^.concat(new(paicpu,
- op_reg(A_PUSH,S_L,R_EAX)));
- exprasmlist^.concat(new(paicpu,
- op_const_reg(A_SUB,S_L,winstackpagesize,R_EDI)));
- emitjmp(C_None,again);
- emitlab(ok);
- exprasmlist^.concat(new(paicpu,
- op_reg_reg(A_SUB,S_L,R_EDI,R_ESP)));
- { now reload EDI }
- new(r);
- reset_reference(r^);
- r^.base:=procinfo^.framepointer;
- r^.offset:=pvarsym(p)^.address+4+procinfo^.call_offset;
- exprasmlist^.concat(new(paicpu,
- op_ref_reg(A_MOV,S_L,r,R_EDI)));
- exprasmlist^.concat(new(paicpu,
- op_reg(A_INC,S_L,R_EDI)));
- exprasmlist^.concat(new(paicpu,
- op_const_reg(A_IMUL,S_L,
- parraydef(pvarsym(p)^.definition)^.definition^.size,R_EDI)));
- end
- else
- {$endif NOTARGETWIN32}
- begin
- exprasmlist^.concat(new(paicpu,
- op_reg_reg(A_SUB,S_L,R_EDI,R_ESP)));
- { load destination }
- exprasmlist^.concat(new(paicpu,
- op_reg_reg(A_MOV,S_L,R_ESP,R_EDI)));
- end;
- { don't destroy the registers! }
- exprasmlist^.concat(new(paicpu,
- op_reg(A_PUSH,S_L,R_ECX)));
- exprasmlist^.concat(new(paicpu,
- op_reg(A_PUSH,S_L,R_ESI)));
- { load count }
- new(r);
- reset_reference(r^);
- r^.base:=procinfo^.framepointer;
- r^.offset:=pvarsym(p)^.address+4+procinfo^.call_offset;
- exprasmlist^.concat(new(paicpu,
- op_ref_reg(A_MOV,S_L,r,R_ECX)));
- { load source }
- new(r);
- reset_reference(r^);
- r^.base:=procinfo^.framepointer;
- r^.offset:=pvarsym(p)^.address+procinfo^.call_offset;
- exprasmlist^.concat(new(paicpu,
- op_ref_reg(A_MOV,S_L,r,R_ESI)));
- { scheduled .... }
- exprasmlist^.concat(new(paicpu,
- op_reg(A_INC,S_L,R_ECX)));
- { calculate size }
- len:=parraydef(pvarsym(p)^.definition)^.definition^.size;
- opsize:=S_B;
- if (len and 3)=0 then
- begin
- opsize:=S_L;
- len:=len shr 2;
- end
- else
- if (len and 1)=0 then
- begin
- opsize:=S_W;
- len:=len shr 1;
- end;
- exprasmlist^.concat(new(paicpu,
- op_const_reg(A_IMUL,S_L,len,R_ECX)));
- exprasmlist^.concat(new(paicpu,
- op_none(A_REP,S_NO)));
- case opsize of
- S_B : exprasmlist^.concat(new(paicpu,op_none(A_MOVSB,S_NO)));
- S_W : exprasmlist^.concat(new(paicpu,op_none(A_MOVSW,S_NO)));
- S_L : exprasmlist^.concat(new(paicpu,op_none(A_MOVSD,S_NO)));
- end;
- exprasmlist^.concat(new(paicpu,
- op_reg(A_POP,S_L,R_ESI)));
- exprasmlist^.concat(new(paicpu,
- op_reg(A_POP,S_L,R_ECX)));
- { patch the new address }
- new(r);
- reset_reference(r^);
- r^.base:=procinfo^.framepointer;
- r^.offset:=pvarsym(p)^.address+procinfo^.call_offset;
- exprasmlist^.concat(new(paicpu,
- op_reg_ref(A_MOV,S_L,R_ESP,r)));
- end
- else
- if is_shortstring(pvarsym(p)^.definition) then
- begin
- reset_reference(href1);
- href1.base:=procinfo^.framepointer;
- href1.offset:=pvarsym(p)^.address+procinfo^.call_offset;
- reset_reference(href2);
- href2.base:=procinfo^.framepointer;
- href2.offset:=-pvarsym(p)^.localvarsym^.address;
- copyshortstring(href2,href1,pstringdef(pvarsym(p)^.definition)^.len,true);
- end
- else
- begin
- reset_reference(href1);
- href1.base:=procinfo^.framepointer;
- href1.offset:=pvarsym(p)^.address+procinfo^.call_offset;
- reset_reference(href2);
- href2.base:=procinfo^.framepointer;
- href2.offset:=-pvarsym(p)^.localvarsym^.address;
- concatcopy(href1,href2,pvarsym(p)^.definition^.size,true,true);
- end;
- end;
- end;
- procedure inittempansistrings;
- var
- hp : ptemprecord;
- r : preference;
- begin
- hp:=templist;
- while assigned(hp) do
- begin
- if hp^.temptype in [tt_ansistring,tt_freeansistring] then
- begin
- procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
- new(r);
- reset_reference(r^);
- r^.base:=procinfo^.framepointer;
- r^.offset:=hp^.pos;
- emit_const_ref(A_MOV,S_L,0,r);
- end;
- hp:=hp^.next;
- end;
- end;
- procedure finalizetempansistrings;
- var
- hp : ptemprecord;
- hr : treference;
- begin
- hp:=templist;
- while assigned(hp) do
- begin
- if hp^.temptype in [tt_ansistring,tt_freeansistring] then
- begin
- procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
- reset_reference(hr);
- hr.base:=procinfo^.framepointer;
- hr.offset:=hp^.pos;
- emitpushreferenceaddr(hr);
- emitcall('FPC_ANSISTR_DECR_REF');
- end;
- hp:=hp^.next;
- end;
- end;
- var
- ls : longint;
- procedure largest_size(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
- begin
- if (psym(p)^.typ=varsym) and
- (pvarsym(p)^.getsize>ls) then
- ls:=pvarsym(p)^.getsize;
- end;
- procedure alignstack(alist : paasmoutput);
- begin
- {$ifdef dummy}
- if (cs_optimize in aktglobalswitches) and
- (aktoptprocessor in [classp5,classp6]) then
- begin
- ls:=0;
- aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}largest_size);
- if ls>=8 then
- alist^.insert(new(paicpu,op_const_reg(A_AND,S_L,-8,R_ESP)));
- end;
- {$endif dummy}
- end;
- procedure genentrycode(alist : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
- stackframe:longint;
- var parasize:longint;var nostackframe:boolean;
- inlined : boolean);
- {
- Generates the entry code for a procedure
- }
- var
- hs : string;
- {$ifdef GDB}
- stab_function_name : Pai_stab_function_name;
- {$endif GDB}
- hr : preference;
- p : psymtable;
- r : treference;
- oldlist,
- oldexprasmlist : paasmoutput;
- again : pasmlabel;
- i : longint;
- begin
- oldexprasmlist:=exprasmlist;
- exprasmlist:=alist;
- if (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
- begin
- emitinsertcall('FPC_INITIALIZEUNITS');
- if target_info.target=target_I386_WIN32 then
- begin
- new(hr);
- reset_reference(hr^);
- hr^.symbol:=newasmsymbol('U_SYSWIN32_ISCONSOLE');
- if apptype=at_cui then
- exprasmlist^.insert(new(paicpu,op_const_ref(A_MOV,S_B,
- 1,hr)))
- else
- exprasmlist^.insert(new(paicpu,op_const_ref(A_MOV,S_B,
- 0,hr)));
- end;
- oldlist:=exprasmlist;
- exprasmlist:=new(paasmoutput,init);
- p:=symtablestack;
- while assigned(p) do
- begin
- p^.foreach({$ifndef TP}@{$endif}initialize_threadvar);
- p:=p^.next;
- end;
- oldlist^.insertlist(exprasmlist);
- dispose(exprasmlist,done);
- exprasmlist:=oldlist;
- end;
- { a constructor needs a help procedure }
- if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
- begin
- if procinfo^._class^.is_class then
- begin
- exprasmlist^.insert(new(paicpu,op_cond_sym(A_Jcc,C_Z,S_NO,faillabel)));
- emitinsertcall('FPC_NEW_CLASS');
- end
- else
- begin
- exprasmlist^.insert(new(paicpu,op_cond_sym(A_Jcc,C_Z,S_NO,faillabel)));
- emitinsertcall('FPC_HELP_CONSTRUCTOR');
- exprasmlist^.insert(new(paicpu,op_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI)));
- end;
- end;
- { don't load ESI, does the caller }
- { When message method contains self as a parameter,
- we must load it into ESI }
- If (po_containsself in aktprocsym^.definition^.procoptions) then
- begin
- new(hr);
- reset_reference(hr^);
- hr^.offset:=procinfo^.selfpointer_offset;
- hr^.base:=procinfo^.framepointer;
- exprasmlist^.insert(new(paicpu,op_ref_reg(A_MOV,S_L,hr,R_ESI)));
- end;
- { should we save edi,esi,ebx like C ? }
- if (po_savestdregs in aktprocsym^.definition^.procoptions) then
- begin
- if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EBX)));
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_ESI)));
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EDI)));
- end;
- { for the save all registers we can simply use a pusha,popa which
- push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
- if (po_saveregisters in aktprocsym^.definition^.procoptions) then
- begin
- exprasmlist^.insert(new(paicpu,op_none(A_PUSHA,S_L)));
- end;
- { omit stack frame ? }
- if not inlined then
- if procinfo^.framepointer=stack_pointer then
- begin
- CGMessage(cg_d_stackframe_omited);
- nostackframe:=true;
- if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
- parasize:=0
- else
- parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.call_offset-4;
- if stackframe<>0 then
- exprasmlist^.insert(new(paicpu,
- op_const_reg(A_SUB,S_L,gettempsize,R_ESP)));
- end
- else
- begin
- alignstack(alist);
- if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
- parasize:=0
- else
- parasize:=aktprocsym^.definition^.parast^.datasize+procinfo^.call_offset-8;
- nostackframe:=false;
- if stackframe<>0 then
- begin
- {$ifdef unused}
- if (cs_littlesize in aktglobalswitches) and (stackframe<=65535) then
- begin
- if (cs_check_stack in aktlocalswitches) and
- not(target_info.target in [target_i386_linux,target_i386_win32]) then
- begin
- emitinsertcall('FPC_STACKCHECK');
- exprasmlist^.insert(new(paicpu,op_const(A_PUSH,S_L,stackframe)));
- end;
- if cs_profile in aktmoduleswitches then
- genprofilecode;
- { %edi is already saved when pocdecl is used
- if (target_info.target=target_linux) and
- ((aktprocsym^.definition^.options and poexports)<>0) then
- exprasmlist^.insert(new(Paicpu,op_reg(A_PUSH,S_L,R_EDI))); }
- exprasmlist^.insert(new(paicpu,op_const_const(A_ENTER,S_NO,stackframe,0)))
- end
- else
- {$endif unused}
- begin
- { windows guards only a few pages for stack growing, }
- { so we have to access every page first }
- if (target_os.id=os_i386_win32) and
- (stackframe>=winstackpagesize) then
- begin
- if stackframe div winstackpagesize<=5 then
- begin
- exprasmlist^.insert(new(paicpu,op_const_reg(A_SUB,S_L,stackframe-4,R_ESP)));
- for i:=1 to stackframe div winstackpagesize do
- begin
- hr:=new_reference(R_ESP,stackframe-i*winstackpagesize);
- exprasmlist^.concat(new(paicpu,
- op_const_ref(A_MOV,S_L,0,hr)));
- end;
- exprasmlist^.concat(new(paicpu,
- op_reg(A_PUSH,S_L,R_EAX)));
- end
- else
- begin
- getlabel(again);
- exprasmlist^.concat(new(paicpu,
- op_const_reg(A_MOV,S_L,stackframe div winstackpagesize,R_EDI)));
- emitlab(again);
- exprasmlist^.concat(new(paicpu,
- op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP)));
- exprasmlist^.concat(new(paicpu,
- op_reg(A_PUSH,S_L,R_EAX)));
- exprasmlist^.concat(new(paicpu,
- op_reg(A_DEC,S_L,R_EDI)));
- emitjmp(C_NZ,again);
- exprasmlist^.concat(new(paicpu,
- op_const_reg(A_SUB,S_L,stackframe mod winstackpagesize,R_ESP)));
- end
- end
- else
- exprasmlist^.insert(new(paicpu,op_const_reg(A_SUB,S_L,stackframe,R_ESP)));
- if (cs_check_stack in aktlocalswitches) and
- not(target_info.target in [target_i386_linux,target_i386_win32]) then
- begin
- emitinsertcall('FPC_STACKCHECK');
- exprasmlist^.insert(new(paicpu,op_const(A_PUSH,S_L,stackframe)));
- end;
- if cs_profile in aktmoduleswitches then
- genprofilecode;
- exprasmlist^.insert(new(paicpu,op_reg_reg(A_MOV,S_L,R_ESP,R_EBP)));
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EBP)));
- end;
- end { endif stackframe <> 0 }
- else
- begin
- if cs_profile in aktmoduleswitches then
- genprofilecode;
- exprasmlist^.insert(new(paicpu,op_reg_reg(A_MOV,S_L,R_ESP,R_EBP)));
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_EBP)));
- end;
- end;
- if (po_interrupt in aktprocsym^.definition^.procoptions) then
- generate_interrupt_stackframe_entry;
- { initialize return value }
- if (procinfo^.retdef<>pdef(voiddef)) and
- (procinfo^.retdef^.needs_inittable) and
- ((procinfo^.retdef^.deftype<>objectdef) or
- not(pobjectdef(procinfo^.retdef)^.is_class)) then
- begin
- procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
- reset_reference(r);
- r.offset:=procinfo^.retoffset;
- r.base:=procinfo^.framepointer;
- initialize(procinfo^.retdef,r,ret_in_param(procinfo^.retdef));
- end;
- { generate copies of call by value parameters }
- if not(po_assembler in aktprocsym^.definition^.procoptions) then
- aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}copyvalueparas);
- { initialisizes local data }
- aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}initialize_data);
- { add a reference to all call by value/const parameters }
- aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}incr_data);
- { initilisizes temp. ansi/wide string data }
- inittempansistrings;
- { do we need an exception frame because of ansi/widestrings ? }
- if (procinfo^.flags and pi_needs_implicit_finally)<>0 then
- begin
- usedinproc:=usedinproc or ($80 shr byte(R_EAX));
- { Type of stack-frame must be pushed}
- exprasmlist^.concat(new(paicpu,op_const(A_PUSH,S_L,1)));
- emitcall('FPC_PUSHEXCEPTADDR');
- exprasmlist^.concat(new(paicpu,
- op_reg(A_PUSH,S_L,R_EAX)));
- emitcall('FPC_SETJMP');
- exprasmlist^.concat(new(paicpu,
- op_reg(A_PUSH,S_L,R_EAX)));
- exprasmlist^.concat(new(paicpu,
- op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
- emitjmp(C_NE,aktexitlabel);
- end;
- if (cs_profile in aktmoduleswitches) or
- (aktprocsym^.definition^.owner^.symtabletype=globalsymtable) or
- (assigned(procinfo^._class) and (procinfo^._class^.owner^.symtabletype=globalsymtable)) then
- make_global:=true;
- if not inlined then
- begin
- hs:=proc_names.get;
- {$ifdef GDB}
- if (cs_debuginfo in aktmoduleswitches) then
- exprasmlist^.insert(new(pai_force_line,init));
- if (cs_debuginfo in aktmoduleswitches) and target_os.use_function_relative_addresses then
- stab_function_name := new(pai_stab_function_name,init(strpnew(hs)));
- {$EndIf GDB}
- while hs<>'' do
- begin
- if make_global then
- exprasmlist^.insert(new(pai_symbol,initname_global(hs,0)))
- else
- exprasmlist^.insert(new(pai_symbol,initname(hs,0)));
- {$ifdef GDB}
- if (cs_debuginfo in aktmoduleswitches) and
- target_os.use_function_relative_addresses then
- exprasmlist^.insert(new(pai_stab_function_name,init(strpnew(hs))));
- {$endif GDB}
- hs:=proc_names.get;
- end;
- end;
- {$ifdef GDB}
- if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
- begin
- if target_os.use_function_relative_addresses then
- exprasmlist^.insert(stab_function_name);
- if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
- aktprocsym^.is_global := True;
- exprasmlist^.insert(new(pai_stabs,init(aktprocsym^.stabstring)));
- aktprocsym^.isstabwritten:=true;
- end;
- {$endif GDB}
- { Align }
- if (not inlined) then
- begin
- { gprof uses 16 byte granularity !! }
- if (cs_profile in aktmoduleswitches) then
- exprasmlist^.insert(new(pai_align,init_op(16,$90)))
- else
- if not(cs_littlesize in aktglobalswitches) then
- exprasmlist^.insert(new(pai_align,init_op(16,$90)));
- end;
- exprasmlist:=oldexprasmlist;
- end;
- procedure handle_return_value(inlined : boolean);
- var
- hr : preference;
- op : Tasmop;
- s : Topsize;
- begin
- if procinfo^.retdef<>pdef(voiddef) then
- begin
- {if ((procinfo^.flags and pi_operator)<>0) and
- assigned(opsym) then
- procinfo^.funcret_is_valid:=
- procinfo^.funcret_is_valid or (opsym^.refs>0);}
- if (procinfo^.funcret_state<>vs_assigned) and not inlined { and
- ((procinfo^.flags and pi_uses_asm)=0)} then
- CGMessage(sym_w_function_result_not_set);
- hr:=new_reference(procinfo^.framepointer,procinfo^.retoffset);
- if (procinfo^.retdef^.deftype in [orddef,enumdef]) then
- begin
- case procinfo^.retdef^.size of
- 8:
- begin
- emit_ref_reg(A_MOV,S_L,hr,R_EAX);
- hr:=new_reference(procinfo^.framepointer,procinfo^.retoffset+4);
- emit_ref_reg(A_MOV,S_L,hr,R_EDX);
- end;
- 4:
- emit_ref_reg(A_MOV,S_L,hr,R_EAX);
- 2:
- emit_ref_reg(A_MOV,S_W,hr,R_AX);
- 1:
- emit_ref_reg(A_MOV,S_B,hr,R_AL);
- end;
- end
- else
- if ret_in_acc(procinfo^.retdef) then
- emit_ref_reg(A_MOV,S_L,hr,R_EAX)
- else
- if (procinfo^.retdef^.deftype=floatdef) then
- begin
- floatloadops(pfloatdef(procinfo^.retdef)^.typ,op,s);
- exprasmlist^.concat(new(paicpu,op_ref(op,s,hr)))
- end
- else
- dispose(hr);
- end
- end;
- procedure genexitcode(alist : paasmoutput;parasize:longint;nostackframe,inlined:boolean);
- var
- {$ifdef GDB}
- mangled_length : longint;
- p : pchar;
- {$endif GDB}
- nofinal,okexitlabel,noreraiselabel : pasmlabel;
- hr : treference;
- oldexprasmlist : paasmoutput;
- ai : paicpu;
- begin
- oldexprasmlist:=exprasmlist;
- exprasmlist:=alist;
- if aktexitlabel^.is_used then
- exprasmlist^.insert(new(pai_label,init(aktexitlabel)));
- { call the destructor help procedure }
- if (aktprocsym^.definition^.proctypeoption=potype_destructor) and
- assigned(procinfo^._class) then
- begin
- if procinfo^._class^.is_class then
- begin
- emitinsertcall('FPC_DISPOSE_CLASS');
- end
- else
- begin
- emitinsertcall('FPC_HELP_DESTRUCTOR');
- exprasmlist^.insert(new(paicpu,op_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI)));
- { must the object be finalized ? }
- if procinfo^._class^.needs_inittable then
- begin
- getlabel(nofinal);
- exprasmlist^.insert(new(pai_label,init(nofinal)));
- emitinsertcall('FPC_FINALIZE');
- exprasmlist^.insert(new(paicpu,op_reg(A_PUSH,S_L,R_ESI)));
- exprasmlist^.insert(new(paicpu,op_sym(A_PUSH,S_L,procinfo^._class^.get_inittable_label)));
- ai:=new(paicpu,op_sym(A_Jcc,S_NO,nofinal));
- ai^.SetCondition(C_Z);
- exprasmlist^.insert(ai);
- reset_reference(hr);
- hr.base:=R_EBP;
- hr.offset:=8;
- exprasmlist^.insert(new(paicpu,op_const_ref(A_CMP,S_L,0,newreference(hr))));
- end;
- end;
- end;
- { finalize temporary data }
- finalizetempansistrings;
- { finalize local data }
- aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}finalize_data);
- { finalize paras data }
- if assigned(aktprocsym^.definition^.parast) then
- aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}finalize_data);
- { do we need to handle exceptions because of ansi/widestrings ? }
- if (procinfo^.flags and pi_needs_implicit_finally)<>0 then
- begin
- getlabel(noreraiselabel);
- emitcall('FPC_POPADDRSTACK');
- exprasmlist^.concat(new(paicpu,
- op_reg(A_POP,S_L,R_EAX)));
- exprasmlist^.concat(new(paicpu,
- op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
- emitjmp(C_E,noreraiselabel);
- { must be the return value finalized before reraising the exception? }
- if (procinfo^.retdef<>pdef(voiddef)) and
- (procinfo^.retdef^.needs_inittable) and
- ((procinfo^.retdef^.deftype<>objectdef) or
- not(pobjectdef(procinfo^.retdef)^.is_class)) then
- begin
- reset_reference(hr);
- hr.offset:=procinfo^.retoffset;
- hr.base:=procinfo^.framepointer;
- finalize(procinfo^.retdef,hr,ret_in_param(procinfo^.retdef));
- end;
- emitcall('FPC_RERAISE');
- emitlab(noreraiselabel);
- end;
- { call __EXIT for main program }
- if (not DLLsource) and (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
- begin
- emitcall('FPC_DO_EXIT');
- end;
- { handle return value }
- if not(po_assembler in aktprocsym^.definition^.procoptions) then
- if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
- handle_return_value(inlined)
- else
- begin
- { successful constructor deletes the zero flag }
- { and returns self in eax }
- { eax must be set to zero if the allocation failed !!! }
- getlabel(okexitlabel);
- emitjmp(C_NONE,okexitlabel);
- emitlab(faillabel);
- if procinfo^._class^.is_class then
- begin
- emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,8),R_ESI);
- emitcall('FPC_HELP_FAIL_CLASS');
- end
- else
- begin
- emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,12),R_ESI);
- emit_const_reg(A_MOV,S_L,procinfo^._class^.vmt_offset,R_EDI);
- emitcall('FPC_HELP_FAIL');
- end;
- emitlab(okexitlabel);
- emit_reg_reg(A_MOV,S_L,R_ESI,R_EAX);
- emit_reg_reg(A_OR,S_L,R_EAX,R_EAX);
- end;
- { stabs uses the label also ! }
- if aktexit2label^.is_used or
- ((cs_debuginfo in aktmoduleswitches) and not inlined) then
- emitlab(aktexit2label);
- { gives problems for long mangled names }
- {list^.concat(new(pai_symbol,init(aktprocsym^.definition^.mangledname+'_end')));}
- { should we restore edi ? }
- { for all i386 gcc implementations }
- if (po_savestdregs in aktprocsym^.definition^.procoptions) then
- begin
- if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
- exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EBX)));
- exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_ESI)));
- exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,R_EDI)));
- { here we could reset R_EBX
- but that is risky because it only works
- if genexitcode is called after genentrycode
- so lets skip this for the moment PM
- aktprocsym^.definition^.usedregisters:=
- aktprocsym^.definition^.usedregisters or not ($80 shr byte(R_EBX));
- }
- end;
- { for the save all registers we can simply use a pusha,popa which
- push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
- if (po_saveregisters in aktprocsym^.definition^.procoptions) then
- begin
- exprasmlist^.concat(new(paicpu,op_none(A_POPA,S_L)));
- end;
- if not(nostackframe) then
- begin
- if not inlined then
- exprasmlist^.concat(new(paicpu,op_none(A_LEAVE,S_NO)));
- end
- else
- begin
- if (gettempsize<>0) and not inlined then
- exprasmlist^.insert(new(paicpu,
- op_const_reg(A_ADD,S_L,gettempsize,R_ESP)));
- end;
- { parameters are limited to 65535 bytes because }
- { ret allows only imm16 }
- if (parasize>65535) and not(pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
- CGMessage(cg_e_parasize_too_big);
- { at last, the return is generated }
- if not inlined then
- if (po_interrupt in aktprocsym^.definition^.procoptions) then
- generate_interrupt_stackframe_exit
- else
- begin
- {Routines with the poclearstack flag set use only a ret.}
- { also routines with parasize=0 }
- if (pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
- begin
- {$ifndef OLD_C_STACK}
- { complex return values are removed from stack in C code PM }
- if ret_in_param(aktprocsym^.definition^.retdef) then
- exprasmlist^.concat(new(paicpu,op_const(A_RET,S_NO,4)))
- else
- {$endif not OLD_C_STACK}
- exprasmlist^.concat(new(paicpu,op_none(A_RET,S_NO)));
- end
- else if (parasize=0) then
- exprasmlist^.concat(new(paicpu,op_none(A_RET,S_NO)))
- else
- exprasmlist^.concat(new(paicpu,op_const(A_RET,S_NO,parasize)));
- end;
- exprasmlist^.concat(new(pai_symbol_end,initname(aktprocsym^.definition^.mangledname)));
- {$ifdef GDB}
- if (cs_debuginfo in aktmoduleswitches) and not inlined then
- begin
- aktprocsym^.concatstabto(exprasmlist);
- if assigned(procinfo^._class) then
- if (not assigned(procinfo^.parent) or
- not assigned(procinfo^.parent^._class)) then
- exprasmlist^.concat(new(pai_stabs,init(strpnew(
- '"$t:v'+procinfo^._class^.numberstring+'",'+
- tostr(N_PSYM)+',0,0,'+tostr(procinfo^.selfpointer_offset)))))
- else
- exprasmlist^.concat(new(pai_stabs,init(strpnew(
- '"$t:r*'+procinfo^._class^.numberstring+'",'+
- tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI])))));
- if (pdef(aktprocsym^.definition^.retdef) <> pdef(voiddef)) then
- begin
- if ret_in_param(aktprocsym^.definition^.retdef) then
- exprasmlist^.concat(new(pai_stabs,init(strpnew(
- '"'+aktprocsym^.name+':X*'+aktprocsym^.definition^.retdef^.numberstring+'",'+
- tostr(N_PSYM)+',0,0,'+tostr(procinfo^.retoffset)))))
- else
- exprasmlist^.concat(new(pai_stabs,init(strpnew(
- '"'+aktprocsym^.name+':X'+aktprocsym^.definition^.retdef^.numberstring+'",'+
- tostr(N_PSYM)+',0,0,'+tostr(procinfo^.retoffset)))));
- if (m_result in aktmodeswitches) then
- if ret_in_param(aktprocsym^.definition^.retdef) then
- exprasmlist^.concat(new(pai_stabs,init(strpnew(
- '"RESULT:X*'+aktprocsym^.definition^.retdef^.numberstring+'",'+
- tostr(N_PSYM)+',0,0,'+tostr(procinfo^.retoffset)))))
- else
- exprasmlist^.concat(new(pai_stabs,init(strpnew(
- '"RESULT:X'+aktprocsym^.definition^.retdef^.numberstring+'",'+
- tostr(N_PSYM)+',0,0,'+tostr(procinfo^.retoffset)))));
- end;
- mangled_length:=length(aktprocsym^.definition^.mangledname);
- getmem(p,mangled_length+50);
- strpcopy(p,'192,0,0,');
- strpcopy(strend(p),aktprocsym^.definition^.mangledname);
- exprasmlist^.concat(new(pai_stabn,init(strnew(p))));
- {list^.concat(new(pai_stabn,init(strpnew('192,0,0,'
- +aktprocsym^.definition^.mangledname))));
- p[0]:='2';p[1]:='2';p[2]:='4';
- strpcopy(strend(p),'_end');}
- freemem(p,mangled_length+50);
- exprasmlist^.concat(new(pai_stabn,init(
- strpnew('224,0,0,'+aktexit2label^.name))));
- { strpnew('224,0,0,'
- +aktprocsym^.definition^.mangledname+'_end'))));}
- end;
- {$endif GDB}
- exprasmlist:=oldexprasmlist;
- end;
- {$ifdef test_dest_loc}
- procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
- begin
- if (dest_loc.loc=LOC_CREGISTER) or (dest_loc.loc=LOC_REGISTER) then
- begin
- emit_reg_reg(A_MOV,s,reg,dest_loc.register);
- set_location(p^.location,dest_loc);
- in_dest_loc:=true;
- end
- else
- if (dest_loc.loc=LOC_REFERENCE) or (dest_loc.loc=LOC_MEM) then
- begin
- exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,s,reg,newreference(dest_loc.reference))));
- set_location(p^.location,dest_loc);
- in_dest_loc:=true;
- end
- else
- internalerror(20080);
- end;
- {$endif test_dest_loc}
- end.
- {
- $Log$
- Revision 1.60 1999-11-17 17:04:58 pierre
- * Notes/hints changes
- Revision 1.59 1999/11/15 14:04:00 pierre
- * self pointer stabs for local function was wrong
- Revision 1.58 1999/11/09 23:06:44 peter
- * esi_offset -> selfpointer_offset to be newcg compatible
- * hcogegen -> cgbase fixes for newcg
- Revision 1.57 1999/11/06 14:34:19 peter
- * truncated log to 20 revs
- Revision 1.56 1999/10/25 12:18:11 peter
- * fixed bug 301
- Revision 1.55 1999/10/21 16:41:38 florian
- * problems with readln fixed: esi wasn't restored correctly when
- reading ordinal fields of objects futher the register allocation
- didn't take care of the extra register when reading ordinal values
- * enumerations can now be used in constant indexes of properties
- Revision 1.54 1999/10/21 14:29:32 peter
- * redesigned linker object
- + library support for linux (only procedures can be exported)
- Revision 1.53 1999/10/13 22:09:29 pierre
- * fix for uggly bug of Marco
- Revision 1.52 1999/10/08 15:40:47 pierre
- * use and remember that C functions with complex data results use ret $4
- Revision 1.51 1999/10/05 22:01:52 pierre
- * bug exit('test') + fail for classes
- Revision 1.50 1999/09/29 11:46:18 florian
- * fixed bug 292 from bugs directory
- Revision 1.49 1999/09/28 21:07:53 florian
- * fixed bug 608
- Revision 1.48 1999/09/28 19:43:47 florian
- * the maybe_push fix of Pierre wasn't 100%, the tree parameter
- must contain a valid location (which is saved if necessary)
- Revision 1.47 1999/09/27 23:44:50 peter
- * procinfo is now a pointer
- * support for result setting in sub procedure
- Revision 1.46 1999/09/26 13:26:07 florian
- * exception patch of Romio nevertheless the excpetion handling
- needs some corections regarding register saving
- * gettempansistring is again a procedure
- Revision 1.45 1999/09/20 16:35:43 peter
- * restored old alignment, saves 40k on ppc386
- Revision 1.44 1999/09/16 07:58:14 pierre
- + RESULT pseudo var added in GDB debug info
- Revision 1.43 1999/09/15 20:35:38 florian
- * small fix to operator overloading when in MMX mode
- + the compiler uses now fldz and fld1 if possible
- + some fixes to floating point registers
- + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
- * .... ???
- Revision 1.42 1999/09/14 07:59:47 florian
- * finally!? fixed
- with <function with result in temp> do
- My last and also Peter's fix before were wrong :(
- Revision 1.41 1999/09/12 08:48:04 florian
- * bugs 593 and 607 fixed
- * some other potential bugs with array constructors fixed
- * for classes compiled in $M+ and it's childs, the default access method
- is now published
- * fixed copyright message (it is now 1993-99)
- Revision 1.40 1999/09/11 11:23:58 florian
- * bug 603 fixed
- Revision 1.39 1999/09/10 15:42:51 peter
- * fixed with <calln> do
- * fixed finalize/initialize call for new/dispose
- Revision 1.38 1999/09/04 20:50:08 florian
- * bug 580 fixed
- Revision 1.37 1999/09/02 17:07:38 florian
- * problems with -Or fixed: tdef.isfpuregable was wrong!
- }
|