pass_1.pas 217 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 by Florian Klaempfl
  4. This unit implements the first pass of the code generator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef tp}
  19. {$F+}
  20. {$endif tp}
  21. unit pass_1;
  22. interface
  23. uses tree;
  24. procedure firstpass(var p : ptree);
  25. function do_firstpass(var p : ptree) : boolean;
  26. implementation
  27. uses
  28. cobjects,verbose,comphook,systems,globals,
  29. aasm,symtable,types,strings,hcodegen,files
  30. {$ifdef i386}
  31. ,i386
  32. ,tgeni386
  33. {$endif}
  34. {$ifdef m68k}
  35. ,m68k
  36. ,tgen68k
  37. {$endif}
  38. {$ifdef UseBrowser}
  39. ,browser
  40. {$endif UseBrowser}
  41. ;
  42. { firstcallparan without varspez
  43. we don't count the ref }
  44. const
  45. count_ref : boolean = true;
  46. { marks an lvalue as "unregable" }
  47. procedure make_not_regable(p : ptree);
  48. begin
  49. case p^.treetype of
  50. typeconvn :
  51. make_not_regable(p^.left);
  52. loadn :
  53. if p^.symtableentry^.typ=varsym then
  54. pvarsym(p^.symtableentry)^.var_options :=
  55. pvarsym(p^.symtableentry)^.var_options and not vo_regable;
  56. end;
  57. end;
  58. procedure left_right_max(p : ptree);
  59. begin
  60. if assigned(p^.left) then
  61. begin
  62. if assigned(p^.right) then
  63. begin
  64. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  65. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  66. {$ifdef SUPPORT_MMX}
  67. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  68. {$endif SUPPORT_MMX}
  69. end
  70. else
  71. begin
  72. p^.registers32:=p^.left^.registers32;
  73. p^.registersfpu:=p^.left^.registersfpu;
  74. {$ifdef SUPPORT_MMX}
  75. p^.registersmmx:=p^.left^.registersmmx;
  76. {$endif SUPPORT_MMX}
  77. end;
  78. end;
  79. end;
  80. { calculates the needed registers for a binary operator }
  81. procedure calcregisters(p : ptree;r32,fpu,mmx : word);
  82. begin
  83. left_right_max(p);
  84. { Only when the difference between the left and right registers < the
  85. wanted registers allocate the amount of registers }
  86. if assigned(p^.left) then
  87. begin
  88. if assigned(p^.right) then
  89. begin
  90. if (abs(p^.left^.registers32-p^.right^.registers32)<r32) then
  91. inc(p^.registers32,r32);
  92. if (abs(p^.left^.registersfpu-p^.right^.registersfpu)<fpu) then
  93. inc(p^.registersfpu,fpu);
  94. {$ifdef SUPPORT_MMX}
  95. if (abs(p^.left^.registersmmx-p^.right^.registersmmx)<mmx) then
  96. inc(p^.registersmmx,mmx);
  97. {$endif SUPPORT_MMX}
  98. end
  99. else
  100. begin
  101. if (p^.left^.registers32<r32) then
  102. inc(p^.registers32,r32);
  103. if (p^.left^.registersfpu<fpu) then
  104. inc(p^.registersfpu,fpu);
  105. {$ifdef SUPPORT_MMX}
  106. if (p^.left^.registersmmx<mmx) then
  107. inc(p^.registersmmx,mmx);
  108. {$endif SUPPORT_MMX}
  109. end;
  110. end;
  111. { error message, if more than 8 floating point }
  112. { registers are needed }
  113. if p^.registersfpu>8 then
  114. Message(cg_e_too_complex_expr);
  115. end;
  116. function both_rm(p : ptree) : boolean;
  117. begin
  118. both_rm:=(p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  119. (p^.right^.location.loc in [LOC_MEM,LOC_REFERENCE]);
  120. end;
  121. function is_assignment_overloaded(from_def,to_def : pdef) : boolean;forward;
  122. function isconvertable(def_from,def_to : pdef;
  123. var doconv : tconverttype;fromtreetype : ttreetyp;
  124. explicit : boolean) : boolean;
  125. const
  126. { Tbasetype: uauto,uvoid,uchar,
  127. u8bit,u16bit,u32bit,
  128. s8bit,s16bit,s32,
  129. bool8bit,bool16bit,boot32bit }
  130. basedefconverts : array[tbasetype,tbasetype] of tconverttype =
  131. {uauto}
  132. ((tc_not_possible,tc_not_possible,tc_not_possible,
  133. tc_not_possible,tc_not_possible,tc_not_possible,
  134. tc_not_possible,tc_not_possible,tc_not_possible,
  135. tc_not_possible,tc_not_possible,tc_not_possible),
  136. {uvoid}
  137. (tc_not_possible,tc_not_possible,tc_not_possible,
  138. tc_not_possible,tc_not_possible,tc_not_possible,
  139. tc_not_possible,tc_not_possible,tc_not_possible,
  140. tc_not_possible,tc_not_possible,tc_not_possible),
  141. {uchar}
  142. (tc_not_possible,tc_not_possible,tc_only_rangechecks32bit,
  143. tc_not_possible,tc_not_possible,tc_not_possible,
  144. tc_not_possible,tc_not_possible,tc_not_possible,
  145. tc_not_possible,tc_not_possible,tc_not_possible),
  146. {u8bit}
  147. (tc_not_possible,tc_not_possible,tc_not_possible,
  148. tc_only_rangechecks32bit,tc_u8bit_2_u16bit,tc_u8bit_2_u32bit,
  149. tc_only_rangechecks32bit,tc_u8bit_2_s16bit,tc_u8bit_2_s32bit,
  150. tc_int_2_bool,tc_int_2_bool,tc_int_2_bool),
  151. {u16bit}
  152. (tc_not_possible,tc_not_possible,tc_not_possible,
  153. tc_u16bit_2_u8bit,tc_only_rangechecks32bit,tc_u16bit_2_u32bit,
  154. tc_u16bit_2_s8bit,tc_only_rangechecks32bit,tc_u16bit_2_s32bit,
  155. tc_int_2_bool,tc_int_2_bool,tc_int_2_bool),
  156. {u32bit}
  157. (tc_not_possible,tc_not_possible,tc_not_possible,
  158. tc_u32bit_2_u8bit,tc_u32bit_2_u16bit,tc_only_rangechecks32bit,
  159. tc_u32bit_2_s8bit,tc_u32bit_2_s16bit,tc_only_rangechecks32bit,
  160. tc_int_2_bool,tc_int_2_bool,tc_int_2_bool),
  161. {s8bit}
  162. (tc_not_possible,tc_not_possible,tc_not_possible,
  163. tc_only_rangechecks32bit,tc_s8bit_2_u16bit,tc_s8bit_2_u32bit,
  164. tc_only_rangechecks32bit,tc_s8bit_2_s16bit,tc_s8bit_2_s32bit,
  165. tc_int_2_bool,tc_int_2_bool,tc_int_2_bool),
  166. {s16bit}
  167. (tc_not_possible,tc_not_possible,tc_not_possible,
  168. tc_s16bit_2_u8bit,tc_only_rangechecks32bit,tc_s16bit_2_u32bit,
  169. tc_s16bit_2_s8bit,tc_only_rangechecks32bit,tc_s16bit_2_s32bit,
  170. tc_int_2_bool,tc_int_2_bool,tc_int_2_bool),
  171. {s32bit}
  172. (tc_not_possible,tc_not_possible,tc_not_possible,
  173. tc_s32bit_2_u8bit,tc_s32bit_2_u16bit,tc_only_rangechecks32bit,
  174. tc_s32bit_2_s8bit,tc_s32bit_2_s16bit,tc_only_rangechecks32bit,
  175. tc_int_2_bool,tc_int_2_bool,tc_int_2_bool),
  176. {bool8bit}
  177. (tc_not_possible,tc_not_possible,tc_not_possible,
  178. tc_bool_2_int,tc_bool_2_int,tc_bool_2_int,
  179. tc_bool_2_int,tc_bool_2_int,tc_bool_2_int,
  180. tc_only_rangechecks32bit,tc_bool_2_int,tc_bool_2_int),
  181. {bool16bit}
  182. (tc_not_possible,tc_not_possible,tc_not_possible,
  183. tc_bool_2_int,tc_bool_2_int,tc_bool_2_int,
  184. tc_bool_2_int,tc_bool_2_int,tc_bool_2_int,
  185. tc_bool_2_int,tc_only_rangechecks32bit,tc_bool_2_int),
  186. {bool32bit}
  187. (tc_not_possible,tc_not_possible,tc_not_possible,
  188. tc_bool_2_int,tc_bool_2_int,tc_bool_2_int,
  189. tc_bool_2_int,tc_bool_2_int,tc_bool_2_int,
  190. tc_bool_2_int,tc_bool_2_int,tc_only_rangechecks32bit));
  191. var
  192. b : boolean;
  193. hd1,hd2 : pdef;
  194. begin
  195. b:=false;
  196. if (not assigned(def_from)) or (not assigned(def_to)) then
  197. begin
  198. isconvertable:=false;
  199. exit;
  200. end;
  201. { handle ord to ord first }
  202. if (def_from^.deftype=orddef) and (def_to^.deftype=orddef) then
  203. begin
  204. doconv:=basedefconverts[porddef(def_from)^.typ,porddef(def_to)^.typ];
  205. { Don't allow automatic int->bool.
  206. Very Bad Hack !!!! (PFV) }
  207. if (doconv=tc_int_2_bool) and (not explicit) then
  208. b:=false
  209. else
  210. if doconv<>tc_not_possible then
  211. b:=true;
  212. end
  213. else
  214. if (def_from^.deftype=orddef) and (def_to^.deftype=floatdef) then
  215. begin
  216. if pfloatdef(def_to)^.typ=f32bit then
  217. doconv:=tc_int_2_fix
  218. else
  219. doconv:=tc_int_2_real;
  220. b:=true;
  221. end
  222. else
  223. { 2 float types ? }
  224. if (def_from^.deftype=floatdef) and (def_to^.deftype=floatdef) then
  225. begin
  226. if pfloatdef(def_from)^.typ=pfloatdef(def_to)^.typ then
  227. doconv:=tc_equal
  228. else
  229. begin
  230. if pfloatdef(def_from)^.typ=f32bit then
  231. doconv:=tc_fix_2_real
  232. else if pfloatdef(def_to)^.typ=f32bit then
  233. doconv:=tc_real_2_fix
  234. else
  235. doconv:=tc_real_2_real;
  236. { comp isn't a floating type }
  237. {$ifdef i386}
  238. if (pfloatdef(def_to)^.typ=s64bit) and
  239. (pfloatdef(def_from)^.typ<>s64bit) and
  240. not (explicit) then
  241. Message(type_w_convert_real_2_comp);
  242. {$endif}
  243. end;
  244. b:=true;
  245. end
  246. else
  247. { enum to enum }
  248. if (def_from^.deftype=enumdef) and (def_to^.deftype=enumdef) then
  249. begin
  250. if assigned(penumdef(def_from)^.basedef) then
  251. hd1:=penumdef(def_from)^.basedef
  252. else
  253. hd1:=def_from;
  254. if assigned(penumdef(def_to)^.basedef) then
  255. hd2:=penumdef(def_to)^.basedef
  256. else
  257. hd2:=def_to;
  258. b:=(hd1=hd2);
  259. end
  260. else
  261. { assignment overwritten ?? }
  262. if is_assignment_overloaded(def_from,def_to) then
  263. b:=true
  264. else
  265. if (def_from^.deftype=pointerdef) and (def_to^.deftype=arraydef) and
  266. (parraydef(def_to)^.lowrange=0) and
  267. is_equal(ppointerdef(def_from)^.definition,parraydef(def_to)^.definition) then
  268. begin
  269. doconv:=tc_pointer_to_array;
  270. b:=true;
  271. end
  272. else
  273. if (def_from^.deftype=arraydef) and (def_to^.deftype=pointerdef) and
  274. (parraydef(def_from)^.lowrange=0) and
  275. is_equal(parraydef(def_from)^.definition,ppointerdef(def_to)^.definition) then
  276. begin
  277. doconv:=tc_array_to_pointer;
  278. b:=true;
  279. end
  280. else
  281. { typed files are all equal to the abstract file type
  282. name TYPEDFILE in system.pp in is_equal in types.pas
  283. the problem is that it sholud be also compatible to FILE
  284. but this would leed to a problem for ASSIGN RESET and REWRITE
  285. when trying to find the good overloaded function !!
  286. so all file function are doubled in system.pp
  287. this is not very beautiful !!}
  288. if (def_from^.deftype=filedef) and (def_to^.deftype=filedef) and
  289. (
  290. (
  291. (pfiledef(def_from)^.filetype = ft_typed) and
  292. (pfiledef(def_to)^.filetype = ft_typed) and
  293. (
  294. (pfiledef(def_from)^.typed_as = pdef(voiddef)) or
  295. (pfiledef(def_to)^.typed_as = pdef(voiddef))
  296. )
  297. ) or
  298. (
  299. (
  300. (pfiledef(def_from)^.filetype = ft_untyped) and
  301. (pfiledef(def_to)^.filetype = ft_typed)
  302. ) or
  303. (
  304. (pfiledef(def_from)^.filetype = ft_typed) and
  305. (pfiledef(def_to)^.filetype = ft_untyped)
  306. )
  307. )
  308. ) then
  309. begin
  310. doconv:=tc_equal;
  311. b:=true;
  312. end
  313. else
  314. { object pascal objects }
  315. if (def_from^.deftype=objectdef) and (def_to^.deftype=objectdef) {and
  316. pobjectdef(def_from)^.isclass and pobjectdef(def_to)^.isclass }then
  317. begin
  318. doconv:=tc_equal;
  319. b:=pobjectdef(def_from)^.isrelated(
  320. pobjectdef(def_to));
  321. end
  322. else
  323. { class types and class reference type
  324. can be assigned to void pointers }
  325. if (((def_from^.deftype=objectdef) and
  326. pobjectdef(def_from)^.isclass) or
  327. (def_from^.deftype=classrefdef)
  328. ) and
  329. (def_to^.deftype=pointerdef) and
  330. (ppointerdef(def_to)^.definition^.deftype=orddef) and
  331. (porddef(ppointerdef(def_to)^.definition)^.typ=uvoid) then
  332. begin
  333. doconv:=tc_equal;
  334. b:=true;
  335. end
  336. else
  337. { class reference types }
  338. if (def_from^.deftype=classrefdef) and (def_from^.deftype=classrefdef) then
  339. begin
  340. doconv:=tc_equal;
  341. b:=pobjectdef(pclassrefdef(def_from)^.definition)^.isrelated(
  342. pobjectdef(pclassrefdef(def_to)^.definition));
  343. end
  344. else
  345. if (def_from^.deftype=pointerdef) and (def_to^.deftype=pointerdef) then
  346. begin
  347. { child class pointer can be assigned to anchestor pointers }
  348. if (
  349. (ppointerdef(def_from)^.definition^.deftype=objectdef) and
  350. (ppointerdef(def_to)^.definition^.deftype=objectdef) and
  351. pobjectdef(ppointerdef(def_from)^.definition)^.isrelated(
  352. pobjectdef(ppointerdef(def_to)^.definition))
  353. ) or
  354. { all pointers can be assigned to void-pointer }
  355. is_equal(ppointerdef(def_to)^.definition,voiddef) or
  356. { in my opnion, is this not clean pascal }
  357. { well, but it's handy to use, it isn't ? (FK) }
  358. is_equal(ppointerdef(def_from)^.definition,voiddef) then
  359. begin
  360. doconv:=tc_equal;
  361. b:=true;
  362. end
  363. end
  364. else
  365. if (def_from^.deftype=stringdef) and (def_to^.deftype=stringdef) then
  366. begin
  367. doconv:=tc_string_to_string;
  368. b:=true;
  369. end
  370. else
  371. { char to string}
  372. if is_equal(def_from,cchardef) and (def_to^.deftype=stringdef) then
  373. begin
  374. doconv:=tc_char_to_string;
  375. b:=true;
  376. end
  377. else
  378. { string constant to zero terminated string constant }
  379. if (fromtreetype=stringconstn) and
  380. ((def_to^.deftype=pointerdef) and is_equal(Ppointerdef(def_to)^.definition,cchardef)) then
  381. begin
  382. doconv:=tc_cstring_charpointer;
  383. b:=true;
  384. end
  385. else
  386. { array of char to string, the length check is done by the firstpass of this node }
  387. if (def_from^.deftype=stringdef) and
  388. ((def_to^.deftype=arraydef) and is_equal(parraydef(def_to)^.definition,cchardef)) then
  389. begin
  390. doconv:=tc_string_chararray;
  391. b:=true;
  392. end
  393. else
  394. { string to array of char, the length check is done by the firstpass of this node }
  395. if ((def_from^.deftype=arraydef) and is_equal(parraydef(def_from)^.definition,cchardef)) and
  396. (def_to^.deftype=stringdef) then
  397. begin
  398. doconv:=tc_chararray_2_string;
  399. b:=true;
  400. end
  401. else
  402. if (fromtreetype=ordconstn) and is_equal(def_from,cchardef) then
  403. begin
  404. if (def_to^.deftype=pointerdef) and
  405. is_equal(ppointerdef(def_to)^.definition,cchardef) then
  406. begin
  407. doconv:=tc_cchar_charpointer;
  408. b:=true;
  409. end;
  410. end
  411. else
  412. if (def_to^.deftype=procvardef) and (def_from^.deftype=procdef) then
  413. begin
  414. def_from^.deftype:=procvardef;
  415. doconv:=tc_proc2procvar;
  416. b:=is_equal(def_from,def_to);
  417. def_from^.deftype:=procdef;
  418. end
  419. else
  420. { nil is compatible with class instances }
  421. if (fromtreetype=niln) and (def_to^.deftype=objectdef)
  422. and (pobjectdef(def_to)^.isclass) then
  423. begin
  424. doconv:=tc_equal;
  425. b:=true;
  426. end
  427. else
  428. { nil is compatible with class references }
  429. if (fromtreetype=niln) and (def_to^.deftype=classrefdef) then
  430. begin
  431. doconv:=tc_equal;
  432. b:=true;
  433. end
  434. else
  435. { nil is compatible with procvars }
  436. if (fromtreetype=niln) and (def_to^.deftype=procvardef) then
  437. begin
  438. doconv:=tc_equal;
  439. b:=true;
  440. end
  441. else
  442. { nil is compatible with ansi- and wide strings }
  443. if (fromtreetype=niln) and (def_to^.deftype=stringdef)
  444. and (pstringdef(def_to)^.string_typ in [st_ansistring,st_widestring]) then
  445. begin
  446. doconv:=tc_equal;
  447. b:=true;
  448. end
  449. else
  450. { ansi- and wide strings can be assigned to void pointers }
  451. if (def_from^.deftype=stringdef) and
  452. (pstringdef(def_from)^.string_typ in [st_ansistring,st_widestring]) and
  453. (def_to^.deftype=pointerdef) and
  454. (ppointerdef(def_to)^.definition^.deftype=orddef) and
  455. (porddef(ppointerdef(def_to)^.definition)^.typ=uvoid) then
  456. begin
  457. doconv:=tc_equal;
  458. b:=true;
  459. end
  460. else
  461. { ansistrings can be assigned to pchar }
  462. if is_ansistring(def_from) and
  463. (def_to^.deftype=pointerdef) and
  464. (ppointerdef(def_to)^.definition^.deftype=orddef) and
  465. (porddef(ppointerdef(def_to)^.definition)^.typ=uchar) then
  466. begin
  467. doconv:=tc_ansistring_2_pchar;
  468. b:=true;
  469. end
  470. else
  471. { pchar can be assigned to ansistrings }
  472. if ((def_from^.deftype=pointerdef) and
  473. (ppointerdef(def_from)^.definition^.deftype=orddef) and
  474. (porddef(ppointerdef(def_from)^.definition)^.typ=uchar)) and
  475. is_ansistring(def_to) then
  476. begin
  477. doconv:=tc_pchar_2_ansistring;
  478. b:=true;
  479. end
  480. else
  481. { procedure variable can be assigned to an void pointer }
  482. { Not anymore. Use the @ operator now.}
  483. if not (cs_tp_compatible in aktmoduleswitches) then
  484. begin
  485. if (def_from^.deftype=procvardef) and
  486. (def_to^.deftype=pointerdef) and
  487. (ppointerdef(def_to)^.definition^.deftype=orddef) and
  488. (porddef(ppointerdef(def_to)^.definition)^.typ=uvoid) then
  489. begin
  490. doconv:=tc_equal;
  491. b:=true;
  492. end;
  493. end;
  494. isconvertable:=b;
  495. end;
  496. procedure firsterror(var p : ptree);
  497. begin
  498. p^.error:=true;
  499. codegenerror:=true;
  500. p^.resulttype:=generrordef;
  501. end;
  502. procedure firstload(var p : ptree);
  503. begin
  504. p^.location.loc:=LOC_REFERENCE;
  505. p^.registers32:=0;
  506. p^.registersfpu:=0;
  507. {$ifdef SUPPORT_MMX}
  508. p^.registersmmx:=0;
  509. {$endif SUPPORT_MMX}
  510. clear_reference(p^.location.reference);
  511. if p^.symtableentry^.typ=funcretsym then
  512. begin
  513. putnode(p);
  514. p:=genzeronode(funcretn);
  515. p^.funcretprocinfo:=pprocinfo(pfuncretsym(p^.symtableentry)^.funcretprocinfo);
  516. p^.retdef:=pfuncretsym(p^.symtableentry)^.funcretdef;
  517. firstpass(p);
  518. exit;
  519. end;
  520. if p^.symtableentry^.typ=absolutesym then
  521. begin
  522. p^.resulttype:=pabsolutesym(p^.symtableentry)^.definition;
  523. if pabsolutesym(p^.symtableentry)^.abstyp=tovar then
  524. p^.symtableentry:=pabsolutesym(p^.symtableentry)^.ref;
  525. p^.symtable:=p^.symtableentry^.owner;
  526. p^.is_absolute:=true;
  527. end;
  528. case p^.symtableentry^.typ of
  529. absolutesym :;
  530. varsym :
  531. begin
  532. if not(p^.is_absolute) and (p^.resulttype=nil) then
  533. p^.resulttype:=pvarsym(p^.symtableentry)^.definition;
  534. if ((p^.symtable^.symtabletype=parasymtable) or
  535. (p^.symtable^.symtabletype=localsymtable)) and
  536. (lexlevel>p^.symtable^.symtablelevel) then
  537. begin
  538. { sollte sich die Variable in einem anderen Stackframe }
  539. { befinden, so brauchen wir ein Register zum Dereferenceieren }
  540. if (p^.symtable^.symtablelevel)>0 then
  541. begin
  542. p^.registers32:=1;
  543. { further, the variable can't be put into a register }
  544. pvarsym(p^.symtableentry)^.var_options:=
  545. pvarsym(p^.symtableentry)^.var_options and not vo_regable;
  546. end;
  547. end;
  548. if (pvarsym(p^.symtableentry)^.varspez=vs_const) then
  549. p^.location.loc:=LOC_MEM;
  550. { we need a register for call by reference parameters }
  551. if (pvarsym(p^.symtableentry)^.varspez=vs_var) or
  552. ((pvarsym(p^.symtableentry)^.varspez=vs_const) and
  553. dont_copy_const_param(pvarsym(p^.symtableentry)^.definition)
  554. ) or
  555. { call by value open arrays are also indirect addressed }
  556. is_open_array(pvarsym(p^.symtableentry)^.definition) then
  557. p^.registers32:=1;
  558. if p^.symtable^.symtabletype=withsymtable then
  559. inc(p^.registers32);
  560. { a class variable is a pointer !!!
  561. yes, but we have to resolve the reference in an
  562. appropriate tree node (FK)
  563. if (pvarsym(p^.symtableentry)^.definition^.deftype=objectdef) and
  564. ((pobjectdef(pvarsym(p^.symtableentry)^.definition)^.options and oois_class)<>0) then
  565. p^.registers32:=1;
  566. }
  567. { count variable references }
  568. if must_be_valid and p^.is_first then
  569. begin
  570. if pvarsym(p^.symtableentry)^.is_valid=2 then
  571. if (assigned(pvarsym(p^.symtableentry)^.owner) and assigned(aktprocsym)
  572. and (pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst)) then
  573. Message1(sym_n_uninitialized_local_variable,pvarsym(p^.symtableentry)^.name);
  574. end;
  575. if count_ref then
  576. begin
  577. if (p^.is_first) then
  578. begin
  579. if (pvarsym(p^.symtableentry)^.is_valid=2) then
  580. pvarsym(p^.symtableentry)^.is_valid:=1;
  581. p^.is_first:=false;
  582. end;
  583. end;
  584. { this will create problem with local var set by
  585. under_procedures
  586. if (assigned(pvarsym(p^.symtableentry)^.owner) and assigned(aktprocsym)
  587. and ((pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst)
  588. or (pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst))) then }
  589. if t_times<1 then
  590. inc(pvarsym(p^.symtableentry)^.refs)
  591. else
  592. inc(pvarsym(p^.symtableentry)^.refs,t_times);
  593. end;
  594. typedconstsym :
  595. if not p^.is_absolute then
  596. p^.resulttype:=ptypedconstsym(p^.symtableentry)^.definition;
  597. procsym :
  598. begin
  599. if assigned(pprocsym(p^.symtableentry)^.definition^.nextoverloaded) then
  600. Message(parser_e_no_overloaded_procvars);
  601. p^.resulttype:=pprocsym(p^.symtableentry)^.definition;
  602. end;
  603. else internalerror(3);
  604. end;
  605. end;
  606. procedure firstadd(var p : ptree);
  607. procedure make_bool_equal_size(var p:ptree);
  608. begin
  609. if porddef(p^.left^.resulttype)^.typ>porddef(p^.right^.resulttype)^.typ then
  610. begin
  611. p^.right:=gentypeconvnode(p^.right,porddef(p^.left^.resulttype));
  612. p^.right^.convtyp:=tc_bool_2_int;
  613. p^.right^.explizit:=true;
  614. firstpass(p^.right);
  615. end
  616. else
  617. if porddef(p^.left^.resulttype)^.typ<porddef(p^.right^.resulttype)^.typ then
  618. begin
  619. p^.left:=gentypeconvnode(p^.left,porddef(p^.right^.resulttype));
  620. p^.left^.convtyp:=tc_bool_2_int;
  621. p^.left^.explizit:=true;
  622. firstpass(p^.left);
  623. end;
  624. end;
  625. var
  626. t : ptree;
  627. lt,rt : ttreetyp;
  628. rv,lv : longint;
  629. rvd,lvd : bestreal;
  630. rd,ld : pdef;
  631. tempdef : pdef;
  632. concatstrings : boolean;
  633. { to evalute const sets }
  634. resultset : pconstset;
  635. i : longint;
  636. b : boolean;
  637. convdone : boolean;
  638. {$ifndef UseAnsiString}
  639. s1,s2:^string;
  640. {$else UseAnsiString}
  641. s1,s2 : pchar;
  642. l1,l2 : longint;
  643. {$endif UseAnsiString}
  644. { this totally forgets to set the pi_do_call flag !! }
  645. label
  646. no_overload;
  647. begin
  648. { first do the two subtrees }
  649. firstpass(p^.left);
  650. firstpass(p^.right);
  651. lt:=p^.left^.treetype;
  652. rt:=p^.right^.treetype;
  653. rd:=p^.right^.resulttype;
  654. ld:=p^.left^.resulttype;
  655. convdone:=false;
  656. if codegenerror then
  657. exit;
  658. { overloaded operator ? }
  659. if (p^.treetype=starstarn) or
  660. (ld^.deftype=recorddef) or
  661. { <> and = are defined for classes }
  662. ((ld^.deftype=objectdef) and
  663. (not(pobjectdef(ld)^.isclass) or
  664. not(p^.treetype in [equaln,unequaln])
  665. )
  666. ) or
  667. (rd^.deftype=recorddef) or
  668. { <> and = are defined for classes }
  669. ((rd^.deftype=objectdef) and
  670. (not(pobjectdef(rd)^.isclass) or
  671. not(p^.treetype in [equaln,unequaln])
  672. )
  673. ) then
  674. begin
  675. {!!!!!!!!! handle paras }
  676. case p^.treetype of
  677. { the nil as symtable signs firstcalln that this is
  678. an overloaded operator }
  679. addn:
  680. t:=gencallnode(overloaded_operators[plus],nil);
  681. subn:
  682. t:=gencallnode(overloaded_operators[minus],nil);
  683. muln:
  684. t:=gencallnode(overloaded_operators[star],nil);
  685. starstarn:
  686. t:=gencallnode(overloaded_operators[starstar],nil);
  687. slashn:
  688. t:=gencallnode(overloaded_operators[slash],nil);
  689. ltn:
  690. t:=gencallnode(overloaded_operators[globals.lt],nil);
  691. gtn:
  692. t:=gencallnode(overloaded_operators[gt],nil);
  693. lten:
  694. t:=gencallnode(overloaded_operators[lte],nil);
  695. gten:
  696. t:=gencallnode(overloaded_operators[gte],nil);
  697. equaln,unequaln :
  698. t:=gencallnode(overloaded_operators[equal],nil);
  699. else goto no_overload;
  700. end;
  701. { we have to convert p^.left and p^.right into
  702. callparanodes }
  703. t^.left:=gencallparanode(p^.left,nil);
  704. t^.left:=gencallparanode(p^.right,t^.left);
  705. if t^.symtableprocentry=nil then
  706. Message(parser_e_operator_not_overloaded);
  707. if p^.treetype=unequaln then
  708. t:=gensinglenode(notn,t);
  709. firstpass(t);
  710. putnode(p);
  711. p:=t;
  712. exit;
  713. end;
  714. no_overload:
  715. { compact consts }
  716. { convert int consts to real consts, if the }
  717. { other operand is a real const }
  718. if (rt=realconstn) and is_constintnode(p^.left) then
  719. begin
  720. t:=genrealconstnode(p^.left^.value);
  721. disposetree(p^.left);
  722. p^.left:=t;
  723. lt:=realconstn;
  724. end;
  725. if (lt=realconstn) and is_constintnode(p^.right) then
  726. begin
  727. t:=genrealconstnode(p^.right^.value);
  728. disposetree(p^.right);
  729. p^.right:=t;
  730. rt:=realconstn;
  731. end;
  732. { both are int constants ? }
  733. if is_constintnode(p^.left) and is_constintnode(p^.right) then
  734. begin
  735. lv:=p^.left^.value;
  736. rv:=p^.right^.value;
  737. case p^.treetype of
  738. addn : t:=genordinalconstnode(lv+rv,s32bitdef);
  739. subn : t:=genordinalconstnode(lv-rv,s32bitdef);
  740. muln : t:=genordinalconstnode(lv*rv,s32bitdef);
  741. xorn : t:=genordinalconstnode(lv xor rv,s32bitdef);
  742. orn : t:=genordinalconstnode(lv or rv,s32bitdef);
  743. andn : t:=genordinalconstnode(lv and rv,s32bitdef);
  744. ltn : t:=genordinalconstnode(ord(lv<rv),booldef);
  745. lten : t:=genordinalconstnode(ord(lv<=rv),booldef);
  746. gtn : t:=genordinalconstnode(ord(lv>rv),booldef);
  747. gten : t:=genordinalconstnode(ord(lv>=rv),booldef);
  748. equaln : t:=genordinalconstnode(ord(lv=rv),booldef);
  749. unequaln : t:=genordinalconstnode(ord(lv<>rv),booldef);
  750. slashn : begin
  751. { int/int becomes a real }
  752. t:=genrealconstnode(int(lv)/int(rv));
  753. firstpass(t);
  754. end;
  755. else
  756. Message(type_e_mismatch);
  757. end;
  758. disposetree(p);
  759. firstpass(t);
  760. p:=t;
  761. exit;
  762. end;
  763. { both real constants ? }
  764. if (lt=realconstn) and (rt=realconstn) then
  765. begin
  766. lvd:=p^.left^.value_real;
  767. rvd:=p^.right^.value_real;
  768. case p^.treetype of
  769. addn : t:=genrealconstnode(lvd+rvd);
  770. subn : t:=genrealconstnode(lvd-rvd);
  771. muln : t:=genrealconstnode(lvd*rvd);
  772. caretn : t:=genrealconstnode(exp(ln(lvd)*rvd));
  773. slashn : t:=genrealconstnode(lvd/rvd);
  774. ltn : t:=genordinalconstnode(ord(lvd<rvd),booldef);
  775. lten : t:=genordinalconstnode(ord(lvd<=rvd),booldef);
  776. gtn : t:=genordinalconstnode(ord(lvd>rvd),booldef);
  777. gten : t:=genordinalconstnode(ord(lvd>=rvd),booldef);
  778. equaln : t:=genordinalconstnode(ord(lvd=rvd),booldef);
  779. unequaln : t:=genordinalconstnode(ord(lvd<>rvd),booldef);
  780. else
  781. Message(type_e_mismatch);
  782. end;
  783. disposetree(p);
  784. p:=t;
  785. firstpass(p);
  786. exit;
  787. end;
  788. { concating strings ? }
  789. concatstrings:=false;
  790. {$ifdef UseAnsiString}
  791. s1:=nil;
  792. s2:=nil;
  793. {$else UseAnsiString}
  794. new(s1);
  795. new(s2);
  796. {$endif UseAnsiString}
  797. if (lt=ordconstn) and (rt=ordconstn) and
  798. (ld^.deftype=orddef) and (porddef(ld)^.typ=uchar) and
  799. (rd^.deftype=orddef) and (porddef(rd)^.typ=uchar) then
  800. begin
  801. {$ifdef UseAnsiString}
  802. s1:=strpnew(char(byte(p^.left^.value)));
  803. s2:=strpnew(char(byte(p^.right^.value)));
  804. l1:=1;l2:=1;
  805. {$else UseAnsiString}
  806. s1^:=char(byte(p^.left^.value));
  807. s2^:=char(byte(p^.right^.value));
  808. {$endif UseAnsiString}
  809. concatstrings:=true;
  810. end
  811. else
  812. if (lt=stringconstn) and (rt=ordconstn) and
  813. (rd^.deftype=orddef) and (porddef(rd)^.typ=uchar) then
  814. begin
  815. {$ifdef UseAnsiString}
  816. { here there is allways the damn #0 problem !! }
  817. s1:=getpcharcopy(p^.left);
  818. l1:=p^.left^.length;
  819. s2:=strpnew(char(byte(p^.right^.value)));
  820. l2:=1;
  821. {$else UseAnsiString}
  822. s1^:=p^.left^.value_str^;
  823. s2^:=char(byte(p^.right^.value));
  824. {$endif UseAnsiString}
  825. concatstrings:=true;
  826. end
  827. else if (lt=ordconstn) and (rt=stringconstn) and
  828. (ld^.deftype=orddef) and
  829. (porddef(ld)^.typ=uchar) then
  830. begin
  831. {$ifdef UseAnsiString}
  832. { here there is allways the damn #0 problem !! }
  833. s1:=strpnew(char(byte(p^.left^.value)));
  834. l1:=1;
  835. s2:=getpcharcopy(p^.right);
  836. l2:=p^.right^.length;
  837. {$else UseAnsiString}
  838. s1^:=char(byte(p^.left^.value));
  839. s2^:=p^.right^.value_str^;
  840. {$endif UseAnsiString}
  841. concatstrings:=true;
  842. end
  843. else if (lt=stringconstn) and (rt=stringconstn) then
  844. begin
  845. {$ifdef UseAnsiString}
  846. s1:=getpcharcopy(p^.left);
  847. l1:=p^.left^.length;
  848. s2:=getpcharcopy(p^.right);
  849. l2:=p^.right^.length;
  850. {$else UseAnsiString}
  851. s1^:=p^.left^.value_str^;
  852. s2^:=p^.right^.value_str^;
  853. {$endif UseAnsiString}
  854. concatstrings:=true;
  855. end;
  856. { I will need to translate all this to ansistrings !!! }
  857. if concatstrings then
  858. begin
  859. case p^.treetype of
  860. {$ifndef UseAnsiString}
  861. addn : t:=genstringconstnode(s1^+s2^);
  862. ltn : t:=genordinalconstnode(byte(s1^<s2^),booldef);
  863. lten : t:=genordinalconstnode(byte(s1^<=s2^),booldef);
  864. gtn : t:=genordinalconstnode(byte(s1^>s2^),booldef);
  865. gten : t:=genordinalconstnode(byte(s1^>=s2^),booldef);
  866. equaln : t:=genordinalconstnode(byte(s1^=s2^),booldef);
  867. unequaln : t:=genordinalconstnode(byte(s1^<>s2^),booldef);
  868. {$else UseAnsiString}
  869. addn : t:=genpcharconstnode(
  870. concatansistrings(s1,s2,l1,l2),l1+l2);
  871. ltn : t:=genordinalconstnode(
  872. byte(compareansistrings(s1,s2,l1,l2)<0),booldef);
  873. lten : t:=genordinalconstnode(
  874. byte(compareansistrings(s1,s2,l1,l2)<=0),booldef);
  875. gtn : t:=genordinalconstnode(
  876. byte(compareansistrings(s1,s2,l1,l2)>0),booldef);
  877. gten : t:=genordinalconstnode(
  878. byte(compareansistrings(s1,s2,l1,l2)>=0),booldef);
  879. equaln : t:=genordinalconstnode(
  880. byte(compareansistrings(s1,s2,l1,l2)=0),booldef);
  881. unequaln : t:=genordinalconstnode(
  882. byte(compareansistrings(s1,s2,l1,l2)<>0),booldef);
  883. {$endif UseAnsiString}
  884. end;
  885. {$ifdef UseAnsiString}
  886. ansistringdispose(s1,l1);
  887. ansistringdispose(s2,l2);
  888. {$else UseAnsiString}
  889. dispose(s1);
  890. dispose(s2);
  891. {$endif UseAnsiString}
  892. disposetree(p);
  893. firstpass(t);
  894. p:=t;
  895. exit;
  896. end;
  897. {$ifdef UseAnsiString}
  898. ansistringdispose(s1,l1);
  899. ansistringdispose(s2,l2);
  900. {$else UseAnsiString}
  901. dispose(s1);
  902. dispose(s2);
  903. {$endif UseAnsiString}
  904. { if both are orddefs then check sub types }
  905. if (ld^.deftype=orddef) and (rd^.deftype=orddef) then
  906. begin
  907. { 2 booleans ? }
  908. if (porddef(ld)^.typ in [bool8bit,bool16bit,bool32bit]) and
  909. (porddef(rd)^.typ in [bool8bit,bool16bit,bool32bit]) then
  910. begin
  911. case p^.treetype of
  912. andn,orn : begin
  913. calcregisters(p,0,0,0);
  914. p^.location.loc:=LOC_JUMP;
  915. end;
  916. unequaln,
  917. equaln,xorn : begin
  918. { this forces a better code generation (TEST }
  919. { instead of CMP) }
  920. if p^.treetype<>xorn then
  921. begin
  922. if (p^.left^.treetype=ordconstn) and
  923. (p^.left^.value<>0) then
  924. begin
  925. p^.left^.value:=0;
  926. if p^.treetype=equaln then
  927. p^.treetype:=unequaln
  928. else
  929. p^.treetype:=equaln;
  930. end;
  931. if (p^.right^.treetype=ordconstn) and
  932. (p^.right^.value<>0) then
  933. begin
  934. p^.right^.value:=0;
  935. if p^.treetype=equaln then
  936. p^.treetype:=unequaln
  937. else
  938. p^.treetype:=equaln;
  939. end;
  940. end;
  941. make_bool_equal_size(p);
  942. calcregisters(p,1,0,0);
  943. end
  944. else
  945. Message(type_e_mismatch);
  946. end;
  947. convdone:=true;
  948. end
  949. else
  950. { Both are chars? only convert to strings for addn }
  951. if (porddef(rd)^.typ=uchar) and (porddef(ld)^.typ=uchar) then
  952. begin
  953. if p^.treetype=addn then
  954. begin
  955. p^.left:=gentypeconvnode(p^.left,cstringdef);
  956. firstpass(p^.left);
  957. p^.right:=gentypeconvnode(p^.right,cstringdef);
  958. firstpass(p^.right);
  959. { here we call STRCOPY }
  960. procinfo.flags:=procinfo.flags or pi_do_call;
  961. calcregisters(p,0,0,0);
  962. p^.location.loc:=LOC_MEM;
  963. end
  964. else
  965. calcregisters(p,1,0,0);
  966. convdone:=true;
  967. end;
  968. end
  969. else
  970. { is one of the sides a string ? }
  971. if (ld^.deftype=stringdef) or (rd^.deftype=stringdef) then
  972. begin
  973. { convert other side to a string, if not both site are strings,
  974. the typeconv will put give an error if it's not possible }
  975. if not((rd^.deftype=stringdef) and (ld^.deftype=stringdef)) then
  976. begin
  977. if ld^.deftype=stringdef then
  978. p^.right:=gentypeconvnode(p^.right,cstringdef)
  979. else
  980. p^.left:=gentypeconvnode(p^.left,cstringdef);
  981. firstpass(p^.left);
  982. firstpass(p^.right);
  983. end;
  984. { here we call STRCONCAT or STRCMP or STRCOPY }
  985. procinfo.flags:=procinfo.flags or pi_do_call;
  986. calcregisters(p,0,0,0);
  987. p^.location.loc:=LOC_MEM;
  988. convdone:=true;
  989. end
  990. else
  991. { left side a setdef ? }
  992. if (ld^.deftype=setdef) then
  993. begin
  994. { right site must also be a setdef, unless addn is used }
  995. if not(p^.treetype in [subn,symdifn,addn,muln,equaln,unequaln]) or
  996. ((rd^.deftype<>setdef) and (p^.treetype<>addn)) then
  997. Message(type_e_mismatch);
  998. if ((rd^.deftype=setdef) and not(is_equal(rd,ld))) and
  999. not((rt=setelementn) and is_equal(psetdef(ld)^.setof,rd)) then
  1000. Message(type_e_set_element_are_not_comp);
  1001. { ranges require normsets }
  1002. if (psetdef(ld)^.settype=smallset) and
  1003. (rt=setelementn) and
  1004. assigned(p^.right^.right) then
  1005. begin
  1006. { generate a temporary normset def }
  1007. tempdef:=new(psetdef,init(psetdef(ld)^.setof,255));
  1008. p^.left:=gentypeconvnode(p^.left,tempdef);
  1009. firstpass(p^.left);
  1010. dispose(tempdef,done);
  1011. ld:=p^.left^.resulttype;
  1012. end;
  1013. { if the destination is not a smallset then insert a typeconv
  1014. which loads a smallset into a normal set }
  1015. if (psetdef(ld)^.settype<>smallset) and
  1016. (psetdef(rd)^.settype=smallset) then
  1017. begin
  1018. p^.right:=gentypeconvnode(p^.right,psetdef(p^.left^.resulttype));
  1019. firstpass(p^.right);
  1020. end;
  1021. { do constant evalution }
  1022. if (p^.right^.treetype=setconstn) and
  1023. (p^.left^.treetype=setconstn) then
  1024. begin
  1025. new(resultset);
  1026. case p^.treetype of
  1027. addn : begin
  1028. for i:=0 to 31 do
  1029. resultset^[i]:=
  1030. p^.right^.value_set^[i] or p^.left^.value_set^[i];
  1031. t:=gensetconstnode(resultset,psetdef(ld));
  1032. end;
  1033. muln : begin
  1034. for i:=0 to 31 do
  1035. resultset^[i]:=
  1036. p^.right^.value_set^[i] and p^.left^.value_set^[i];
  1037. t:=gensetconstnode(resultset,psetdef(ld));
  1038. end;
  1039. subn : begin
  1040. for i:=0 to 31 do
  1041. resultset^[i]:=
  1042. p^.left^.value_set^[i] and not(p^.right^.value_set^[i]);
  1043. t:=gensetconstnode(resultset,psetdef(ld));
  1044. end;
  1045. symdifn : begin
  1046. for i:=0 to 31 do
  1047. resultset^[i]:=
  1048. p^.left^.value_set^[i] xor p^.right^.value_set^[i];
  1049. t:=gensetconstnode(resultset,psetdef(ld));
  1050. end;
  1051. unequaln : begin
  1052. b:=true;
  1053. for i:=0 to 31 do
  1054. if p^.right^.value_set^[i]=p^.left^.value_set^[i] then
  1055. begin
  1056. b:=false;
  1057. break;
  1058. end;
  1059. t:=genordinalconstnode(ord(b),booldef);
  1060. end;
  1061. equaln : begin
  1062. b:=true;
  1063. for i:=0 to 31 do
  1064. if p^.right^.value_set^[i]<>p^.left^.value_set^[i] then
  1065. begin
  1066. b:=false;
  1067. break;
  1068. end;
  1069. t:=genordinalconstnode(ord(b),booldef);
  1070. end;
  1071. end;
  1072. dispose(resultset);
  1073. disposetree(p);
  1074. p:=t;
  1075. firstpass(p);
  1076. exit;
  1077. end
  1078. else
  1079. if psetdef(ld)^.settype=smallset then
  1080. begin
  1081. calcregisters(p,1,0,0);
  1082. p^.location.loc:=LOC_REGISTER;
  1083. end
  1084. else
  1085. begin
  1086. calcregisters(p,0,0,0);
  1087. { here we call SET... }
  1088. procinfo.flags:=procinfo.flags or pi_do_call;
  1089. p^.location.loc:=LOC_MEM;
  1090. end;
  1091. convdone:=true;
  1092. end
  1093. else
  1094. { is one a real float ? }
  1095. if (rd^.deftype=floatdef) or (ld^.deftype=floatdef) then
  1096. begin
  1097. { if one is a fixed, then convert to f32bit }
  1098. if ((rd^.deftype=floatdef) and (pfloatdef(rd)^.typ=f32bit)) or
  1099. ((ld^.deftype=floatdef) and (pfloatdef(ld)^.typ=f32bit)) then
  1100. begin
  1101. if not(porddef(rd)^.typ in [u8bit,s8bit,u16bit,s16bit,s32bit,u32bit]) or (p^.treetype<>muln) then
  1102. p^.right:=gentypeconvnode(p^.right,s32fixeddef);
  1103. if not(porddef(rd)^.typ in [u8bit,s8bit,u16bit,s16bit,s32bit,u32bit]) or (p^.treetype<>muln) then
  1104. p^.left:=gentypeconvnode(p^.left,s32fixeddef);
  1105. firstpass(p^.left);
  1106. firstpass(p^.right);
  1107. calcregisters(p,1,0,0);
  1108. p^.location.loc:=LOC_REGISTER;
  1109. end
  1110. else
  1111. { convert both to c64float }
  1112. begin
  1113. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  1114. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  1115. firstpass(p^.left);
  1116. firstpass(p^.right);
  1117. calcregisters(p,1,1,0);
  1118. p^.location.loc:=LOC_FPU;
  1119. end;
  1120. convdone:=true;
  1121. end
  1122. else
  1123. { pointer comperation and subtraction }
  1124. if (rd^.deftype=pointerdef) and (ld^.deftype=pointerdef) then
  1125. begin
  1126. p^.location.loc:=LOC_REGISTER;
  1127. p^.right:=gentypeconvnode(p^.right,ld);
  1128. firstpass(p^.right);
  1129. calcregisters(p,1,0,0);
  1130. case p^.treetype of
  1131. equaln,unequaln : ;
  1132. ltn,lten,gtn,gten:
  1133. begin
  1134. if not(cs_extsyntax in aktmoduleswitches) then
  1135. Message(type_e_mismatch);
  1136. end;
  1137. subn:
  1138. begin
  1139. if not(cs_extsyntax in aktmoduleswitches) then
  1140. Message(type_e_mismatch);
  1141. p^.resulttype:=s32bitdef;
  1142. exit;
  1143. end;
  1144. else Message(type_e_mismatch);
  1145. end;
  1146. convdone:=true;
  1147. end
  1148. else
  1149. if (rd^.deftype=objectdef) and (ld^.deftype=objectdef) and
  1150. pobjectdef(rd)^.isclass and pobjectdef(ld)^.isclass then
  1151. begin
  1152. p^.location.loc:=LOC_REGISTER;
  1153. if pobjectdef(rd)^.isrelated(pobjectdef(ld)) then
  1154. p^.right:=gentypeconvnode(p^.right,ld)
  1155. else
  1156. p^.left:=gentypeconvnode(p^.left,rd);
  1157. firstpass(p^.right);
  1158. firstpass(p^.left);
  1159. calcregisters(p,1,0,0);
  1160. case p^.treetype of
  1161. equaln,unequaln : ;
  1162. else Message(type_e_mismatch);
  1163. end;
  1164. convdone:=true;
  1165. end
  1166. else
  1167. if (rd^.deftype=classrefdef) and (ld^.deftype=classrefdef) then
  1168. begin
  1169. p^.location.loc:=LOC_REGISTER;
  1170. if pobjectdef(pclassrefdef(rd)^.definition)^.isrelated(pobjectdef(
  1171. pclassrefdef(ld)^.definition)) then
  1172. p^.right:=gentypeconvnode(p^.right,ld)
  1173. else
  1174. p^.left:=gentypeconvnode(p^.left,rd);
  1175. firstpass(p^.right);
  1176. firstpass(p^.left);
  1177. calcregisters(p,1,0,0);
  1178. case p^.treetype of
  1179. equaln,unequaln : ;
  1180. else Message(type_e_mismatch);
  1181. end;
  1182. convdone:=true;
  1183. end
  1184. else
  1185. { allows comperasion with nil pointer }
  1186. if (rd^.deftype=objectdef) and
  1187. pobjectdef(rd)^.isclass then
  1188. begin
  1189. p^.location.loc:=LOC_REGISTER;
  1190. p^.left:=gentypeconvnode(p^.left,rd);
  1191. firstpass(p^.left);
  1192. calcregisters(p,1,0,0);
  1193. case p^.treetype of
  1194. equaln,unequaln : ;
  1195. else Message(type_e_mismatch);
  1196. end;
  1197. convdone:=true;
  1198. end
  1199. else
  1200. if (ld^.deftype=objectdef) and
  1201. pobjectdef(ld)^.isclass then
  1202. begin
  1203. p^.location.loc:=LOC_REGISTER;
  1204. p^.right:=gentypeconvnode(p^.right,ld);
  1205. firstpass(p^.right);
  1206. calcregisters(p,1,0,0);
  1207. case p^.treetype of
  1208. equaln,unequaln : ;
  1209. else Message(type_e_mismatch);
  1210. end;
  1211. convdone:=true;
  1212. end
  1213. else
  1214. if (rd^.deftype=classrefdef) then
  1215. begin
  1216. p^.left:=gentypeconvnode(p^.left,rd);
  1217. firstpass(p^.left);
  1218. calcregisters(p,1,0,0);
  1219. case p^.treetype of
  1220. equaln,unequaln : ;
  1221. else Message(type_e_mismatch);
  1222. end;
  1223. convdone:=true;
  1224. end
  1225. else
  1226. if (ld^.deftype=classrefdef) then
  1227. begin
  1228. p^.right:=gentypeconvnode(p^.right,ld);
  1229. firstpass(p^.right);
  1230. calcregisters(p,1,0,0);
  1231. case p^.treetype of
  1232. equaln,unequaln : ;
  1233. else
  1234. Message(type_e_mismatch);
  1235. end;
  1236. convdone:=true;
  1237. end
  1238. else
  1239. if (rd^.deftype=pointerdef) then
  1240. begin
  1241. p^.location.loc:=LOC_REGISTER;
  1242. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1243. firstpass(p^.left);
  1244. calcregisters(p,1,0,0);
  1245. if p^.treetype=addn then
  1246. begin
  1247. if not(cs_extsyntax in aktmoduleswitches) then
  1248. Message(type_e_mismatch);
  1249. end
  1250. else
  1251. Message(type_e_mismatch);
  1252. convdone:=true;
  1253. end
  1254. else
  1255. if (ld^.deftype=pointerdef) then
  1256. begin
  1257. p^.location.loc:=LOC_REGISTER;
  1258. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1259. firstpass(p^.right);
  1260. calcregisters(p,1,0,0);
  1261. case p^.treetype of
  1262. addn,subn : if not(cs_extsyntax in aktmoduleswitches) then
  1263. Message(type_e_mismatch);
  1264. else
  1265. Message(type_e_mismatch);
  1266. end;
  1267. convdone:=true;
  1268. end
  1269. else
  1270. if (rd^.deftype=procvardef) and (ld^.deftype=procvardef) and is_equal(rd,ld) then
  1271. begin
  1272. calcregisters(p,1,0,0);
  1273. p^.location.loc:=LOC_REGISTER;
  1274. case p^.treetype of
  1275. equaln,unequaln : ;
  1276. else
  1277. Message(type_e_mismatch);
  1278. end;
  1279. convdone:=true;
  1280. end
  1281. else
  1282. {$ifdef SUPPORT_MMX}
  1283. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  1284. is_mmx_able_array(rd) and is_equal(ld,rd) then
  1285. begin
  1286. firstpass(p^.right);
  1287. firstpass(p^.left);
  1288. case p^.treetype of
  1289. addn,subn,xorn,orn,andn:
  1290. ;
  1291. { mul is a little bit restricted }
  1292. muln:
  1293. if not(mmx_type(p^.left^.resulttype) in
  1294. [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1295. Message(type_e_mismatch);
  1296. else
  1297. Message(type_e_mismatch);
  1298. end;
  1299. p^.location.loc:=LOC_MMXREGISTER;
  1300. calcregisters(p,0,0,1);
  1301. convdone:=true;
  1302. end
  1303. else
  1304. {$endif SUPPORT_MMX}
  1305. if (ld^.deftype=enumdef) and (rd^.deftype=enumdef) and (is_equal(ld,rd)) then
  1306. begin
  1307. calcregisters(p,1,0,0);
  1308. case p^.treetype of
  1309. equaln,unequaln,
  1310. ltn,lten,gtn,gten : ;
  1311. else Message(type_e_mismatch);
  1312. end;
  1313. convdone:=true;
  1314. end;
  1315. { the general solution is to convert to 32 bit int }
  1316. if not convdone then
  1317. begin
  1318. { but an int/int gives real/real! }
  1319. if p^.treetype=slashn then
  1320. begin
  1321. Message(type_w_int_slash_int);
  1322. Message(type_h_use_div_for_int);
  1323. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  1324. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  1325. firstpass(p^.left);
  1326. firstpass(p^.right);
  1327. { maybe we need an integer register to save }
  1328. { a reference }
  1329. if ((p^.left^.location.loc<>LOC_FPU) or
  1330. (p^.right^.location.loc<>LOC_FPU)) and
  1331. (p^.left^.registers32=p^.right^.registers32) then
  1332. calcregisters(p,1,1,0)
  1333. else
  1334. calcregisters(p,0,1,0);
  1335. p^.location.loc:=LOC_FPU;
  1336. end
  1337. else
  1338. begin
  1339. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1340. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1341. firstpass(p^.left);
  1342. firstpass(p^.right);
  1343. calcregisters(p,1,0,0);
  1344. p^.location.loc:=LOC_REGISTER;
  1345. end;
  1346. end;
  1347. if codegenerror then
  1348. exit;
  1349. { determines result type for comparions }
  1350. { here the is a problem with multiple passes }
  1351. { example length(s)+1 gets internal 'longint' type first }
  1352. { if it is a arg it is converted to 'LONGINT' }
  1353. { but a second first pass will reset this to 'longint' }
  1354. case p^.treetype of
  1355. ltn,lten,gtn,gten,equaln,unequaln:
  1356. begin
  1357. if not assigned(p^.resulttype) then
  1358. p^.resulttype:=booldef;
  1359. p^.location.loc:=LOC_FLAGS;
  1360. end;
  1361. xorn:
  1362. begin
  1363. if not assigned(p^.resulttype) then
  1364. p^.resulttype:=p^.left^.resulttype;
  1365. p^.location.loc:=LOC_REGISTER;
  1366. end;
  1367. addn:
  1368. begin
  1369. { the result of a string addition is a string of length 255 }
  1370. if (p^.left^.resulttype^.deftype=stringdef) or
  1371. (p^.right^.resulttype^.deftype=stringdef) then
  1372. begin
  1373. {$ifndef UseAnsiString}
  1374. if not assigned(p^.resulttype) then
  1375. p^.resulttype:=cstringdef
  1376. {$else UseAnsiString}
  1377. if is_ansistring(p^.left^.resulttype) or
  1378. is_ansistring(p^.right^.resulttype) then
  1379. p^.resulttype:=cansistringdef
  1380. else
  1381. p^.resulttype:=cstringdef;
  1382. {$endif UseAnsiString}
  1383. end
  1384. else
  1385. if not assigned(p^.resulttype) then
  1386. p^.resulttype:=p^.left^.resulttype;
  1387. end;
  1388. else if not assigned(p^.resulttype) then
  1389. p^.resulttype:=p^.left^.resulttype;
  1390. end;
  1391. end;
  1392. procedure firstmoddiv(var p : ptree);
  1393. var
  1394. t : ptree;
  1395. {power : longint; }
  1396. begin
  1397. firstpass(p^.left);
  1398. firstpass(p^.right);
  1399. if codegenerror then
  1400. exit;
  1401. if is_constintnode(p^.left) and is_constintnode(p^.right) then
  1402. begin
  1403. case p^.treetype of
  1404. modn : t:=genordinalconstnode(p^.left^.value mod p^.right^.value,s32bitdef);
  1405. divn : t:=genordinalconstnode(p^.left^.value div p^.right^.value,s32bitdef);
  1406. end;
  1407. disposetree(p);
  1408. firstpass(t);
  1409. p:=t;
  1410. exit;
  1411. end;
  1412. { !!!!!! u32bit }
  1413. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1414. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1415. firstpass(p^.left);
  1416. firstpass(p^.right);
  1417. if codegenerror then
  1418. exit;
  1419. left_right_max(p);
  1420. if p^.left^.registers32<=p^.right^.registers32 then
  1421. inc(p^.registers32);
  1422. p^.resulttype:=s32bitdef;
  1423. p^.location.loc:=LOC_REGISTER;
  1424. end;
  1425. procedure firstshlshr(var p : ptree);
  1426. var
  1427. t : ptree;
  1428. begin
  1429. firstpass(p^.left);
  1430. firstpass(p^.right);
  1431. if codegenerror then
  1432. exit;
  1433. if is_constintnode(p^.left) and is_constintnode(p^.right) then
  1434. begin
  1435. case p^.treetype of
  1436. shrn : t:=genordinalconstnode(p^.left^.value shr p^.right^.value,s32bitdef);
  1437. shln : t:=genordinalconstnode(p^.left^.value shl p^.right^.value,s32bitdef);
  1438. end;
  1439. disposetree(p);
  1440. firstpass(t);
  1441. p:=t;
  1442. exit;
  1443. end;
  1444. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1445. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1446. firstpass(p^.left);
  1447. firstpass(p^.right);
  1448. if codegenerror then
  1449. exit;
  1450. calcregisters(p,2,0,0);
  1451. {
  1452. p^.registers32:=p^.left^.registers32;
  1453. if p^.registers32<p^.right^.registers32 then
  1454. p^.registers32:=p^.right^.registers32;
  1455. if p^.registers32<1 then p^.registers32:=1;
  1456. }
  1457. p^.resulttype:=s32bitdef;
  1458. p^.location.loc:=LOC_REGISTER;
  1459. end;
  1460. procedure firstrealconst(var p : ptree);
  1461. begin
  1462. p^.location.loc:=LOC_MEM;
  1463. end;
  1464. procedure firstfixconst(var p : ptree);
  1465. begin
  1466. p^.location.loc:=LOC_MEM;
  1467. end;
  1468. procedure firstordconst(var p : ptree);
  1469. begin
  1470. p^.location.loc:=LOC_MEM;
  1471. end;
  1472. procedure firstniln(var p : ptree);
  1473. begin
  1474. p^.resulttype:=voidpointerdef;
  1475. p^.location.loc:=LOC_MEM;
  1476. end;
  1477. procedure firststringconst(var p : ptree);
  1478. begin
  1479. {why this !!! lost of dummy type definitions
  1480. one per const string !!!
  1481. p^.resulttype:=new(pstringdef,init(length(p^.value_str^)));}
  1482. if cs_ansistrings in aktlocalswitches then
  1483. p^.resulttype:=cansistringdef
  1484. else
  1485. p^.resulttype:=cstringdef;
  1486. p^.location.loc:=LOC_MEM;
  1487. end;
  1488. procedure firstumminus(var p : ptree);
  1489. var
  1490. t : ptree;
  1491. minusdef : pprocdef;
  1492. begin
  1493. firstpass(p^.left);
  1494. p^.registers32:=p^.left^.registers32;
  1495. p^.registersfpu:=p^.left^.registersfpu;
  1496. {$ifdef SUPPORT_MMX}
  1497. p^.registersmmx:=p^.left^.registersmmx;
  1498. {$endif SUPPORT_MMX}
  1499. p^.resulttype:=p^.left^.resulttype;
  1500. if codegenerror then
  1501. exit;
  1502. if is_constintnode(p^.left) then
  1503. begin
  1504. t:=genordinalconstnode(-p^.left^.value,s32bitdef);
  1505. disposetree(p);
  1506. firstpass(t);
  1507. p:=t;
  1508. exit;
  1509. end;
  1510. { nasm can not cope with negativ reals !! }
  1511. if is_constrealnode(p^.left)
  1512. {$ifdef i386}
  1513. and not(aktoutputformat in [as_nasmcoff,as_nasmelf,as_nasmobj])
  1514. {$endif}
  1515. then
  1516. begin
  1517. t:=genrealconstnode(-p^.left^.value_real);
  1518. disposetree(p);
  1519. firstpass(t);
  1520. p:=t;
  1521. exit;
  1522. end;
  1523. if (p^.left^.resulttype^.deftype=floatdef) then
  1524. begin
  1525. if pfloatdef(p^.left^.resulttype)^.typ=f32bit then
  1526. begin
  1527. if (p^.left^.location.loc<>LOC_REGISTER) and
  1528. (p^.registers32<1) then
  1529. p^.registers32:=1;
  1530. p^.location.loc:=LOC_REGISTER;
  1531. end
  1532. else
  1533. p^.location.loc:=LOC_FPU;
  1534. end
  1535. {$ifdef SUPPORT_MMX}
  1536. else if (cs_mmx in aktlocalswitches) and
  1537. is_mmx_able_array(p^.left^.resulttype) then
  1538. begin
  1539. if (p^.left^.location.loc<>LOC_MMXREGISTER) and
  1540. (p^.registersmmx<1) then
  1541. p^.registersmmx:=1;
  1542. { if saturation is on, p^.left^.resulttype isn't
  1543. "mmx able" (FK)
  1544. if (cs_mmx_saturation in aktlocalswitches^) and
  1545. (porddef(parraydef(p^.resulttype)^.definition)^.typ in
  1546. [s32bit,u32bit]) then
  1547. Message(type_e_mismatch);
  1548. }
  1549. end
  1550. {$endif SUPPORT_MMX}
  1551. else if (p^.left^.resulttype^.deftype=orddef) then
  1552. begin
  1553. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1554. firstpass(p^.left);
  1555. p^.registersfpu:=p^.left^.registersfpu;
  1556. {$ifdef SUPPORT_MMX}
  1557. p^.registersmmx:=p^.left^.registersmmx;
  1558. {$endif SUPPORT_MMX}
  1559. p^.registers32:=p^.left^.registers32;
  1560. if codegenerror then
  1561. exit;
  1562. if (p^.left^.location.loc<>LOC_REGISTER) and
  1563. (p^.registers32<1) then
  1564. p^.registers32:=1;
  1565. p^.location.loc:=LOC_REGISTER;
  1566. p^.resulttype:=p^.left^.resulttype;
  1567. end
  1568. else
  1569. begin
  1570. if assigned(overloaded_operators[minus]) then
  1571. minusdef:=overloaded_operators[minus]^.definition
  1572. else
  1573. minusdef:=nil;
  1574. while assigned(minusdef) do
  1575. begin
  1576. if (minusdef^.para1^.data=p^.left^.resulttype) and
  1577. (minusdef^.para1^.next=nil) then
  1578. begin
  1579. t:=gencallnode(overloaded_operators[minus],nil);
  1580. t^.left:=gencallparanode(p^.left,nil);
  1581. putnode(p);
  1582. p:=t;
  1583. firstpass(p);
  1584. exit;
  1585. end;
  1586. minusdef:=minusdef^.nextoverloaded;
  1587. end;
  1588. Message(type_e_mismatch);
  1589. end;
  1590. end;
  1591. procedure firstaddr(var p : ptree);
  1592. var
  1593. hp : ptree;
  1594. hp2 : pdefcoll;
  1595. store_valid : boolean;
  1596. hp3 : pabstractprocdef;
  1597. begin
  1598. make_not_regable(p^.left);
  1599. if not(assigned(p^.resulttype)) then
  1600. begin
  1601. if p^.left^.treetype=calln then
  1602. begin
  1603. hp:=genloadnode(pvarsym(p^.left^.symtableprocentry),p^.left^.symtableproc);
  1604. { result is a procedure variable }
  1605. { No, to be TP compatible, you must return a pointer to
  1606. the procedure that is stored in the procvar.}
  1607. if not(cs_tp_compatible in aktmoduleswitches) then
  1608. begin
  1609. p^.resulttype:=new(pprocvardef,init);
  1610. { it could also be a procvar, not only pprocsym ! }
  1611. if p^.left^.symtableprocentry^.typ=varsym then
  1612. hp3:=pabstractprocdef(pvarsym(p^.left^.symtableprocentry)^.definition)
  1613. else
  1614. hp3:=pabstractprocdef(pprocsym(p^.left^.symtableprocentry)^.definition);
  1615. pprocvardef(p^.resulttype)^.options:=hp3^.options;
  1616. pprocvardef(p^.resulttype)^.retdef:=hp3^.retdef;
  1617. hp2:=hp3^.para1;
  1618. while assigned(hp2) do
  1619. begin
  1620. pprocvardef(p^.resulttype)^.concatdef(hp2^.data,hp2^.paratyp);
  1621. hp2:=hp2^.next;
  1622. end;
  1623. end
  1624. else
  1625. p^.resulttype:=voidpointerdef;
  1626. disposetree(p^.left);
  1627. p^.left:=hp;
  1628. end
  1629. else
  1630. begin
  1631. if not(cs_typed_addresses in aktlocalswitches) then
  1632. p^.resulttype:=voidpointerdef
  1633. else p^.resulttype:=new(ppointerdef,init(p^.left^.resulttype));
  1634. end;
  1635. end;
  1636. store_valid:=must_be_valid;
  1637. must_be_valid:=false;
  1638. firstpass(p^.left);
  1639. must_be_valid:=store_valid;
  1640. if codegenerror then
  1641. exit;
  1642. { we should allow loc_mem for @string }
  1643. if (p^.left^.location.loc<>LOC_REFERENCE) and
  1644. (p^.left^.location.loc<>LOC_MEM) then
  1645. Message(cg_e_illegal_expression);
  1646. p^.registers32:=p^.left^.registers32;
  1647. p^.registersfpu:=p^.left^.registersfpu;
  1648. {$ifdef SUPPORT_MMX}
  1649. p^.registersmmx:=p^.left^.registersmmx;
  1650. {$endif SUPPORT_MMX}
  1651. if p^.registers32<1 then
  1652. p^.registers32:=1;
  1653. p^.location.loc:=LOC_REGISTER;
  1654. end;
  1655. procedure firstdoubleaddr(var p : ptree);
  1656. begin
  1657. make_not_regable(p^.left);
  1658. firstpass(p^.left);
  1659. if p^.resulttype=nil then
  1660. p^.resulttype:=voidpointerdef;
  1661. if (p^.left^.resulttype^.deftype)<>procvardef then
  1662. Message(cg_e_illegal_expression);
  1663. if codegenerror then
  1664. exit;
  1665. if (p^.left^.location.loc<>LOC_REFERENCE) then
  1666. Message(cg_e_illegal_expression);
  1667. p^.registers32:=p^.left^.registers32;
  1668. p^.registersfpu:=p^.left^.registersfpu;
  1669. {$ifdef SUPPORT_MMX}
  1670. p^.registersmmx:=p^.left^.registersmmx;
  1671. {$endif SUPPORT_MMX}
  1672. if p^.registers32<1 then
  1673. p^.registers32:=1;
  1674. p^.location.loc:=LOC_REGISTER;
  1675. end;
  1676. procedure firstnot(var p : ptree);
  1677. var
  1678. t : ptree;
  1679. begin
  1680. firstpass(p^.left);
  1681. if codegenerror then
  1682. exit;
  1683. if (p^.left^.treetype=ordconstn) then
  1684. begin
  1685. t:=genordinalconstnode(not(p^.left^.value),p^.left^.resulttype);
  1686. disposetree(p);
  1687. firstpass(t);
  1688. p:=t;
  1689. exit;
  1690. end;
  1691. p^.resulttype:=p^.left^.resulttype;
  1692. p^.location.loc:=p^.left^.location.loc;
  1693. {$ifdef SUPPORT_MMX}
  1694. p^.registersmmx:=p^.left^.registersmmx;
  1695. {$endif SUPPORT_MMX}
  1696. if is_equal(p^.resulttype,booldef) then
  1697. begin
  1698. p^.registers32:=p^.left^.registers32;
  1699. if ((p^.location.loc=LOC_REFERENCE) or
  1700. (p^.location.loc=LOC_CREGISTER)) and
  1701. (p^.registers32<1) then
  1702. p^.registers32:=1;
  1703. end
  1704. else
  1705. {$ifdef SUPPORT_MMX}
  1706. if (cs_mmx in aktlocalswitches) and
  1707. is_mmx_able_array(p^.left^.resulttype) then
  1708. begin
  1709. if (p^.left^.location.loc<>LOC_MMXREGISTER) and
  1710. (p^.registersmmx<1) then
  1711. p^.registersmmx:=1;
  1712. end
  1713. else
  1714. {$endif SUPPORT_MMX}
  1715. begin
  1716. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1717. firstpass(p^.left);
  1718. if codegenerror then
  1719. exit;
  1720. p^.resulttype:=p^.left^.resulttype;
  1721. p^.registers32:=p^.left^.registers32;
  1722. {$ifdef SUPPORT_MMX}
  1723. p^.registersmmx:=p^.left^.registersmmx;
  1724. {$endif SUPPORT_MMX}
  1725. if (p^.left^.location.loc<>LOC_REGISTER) and
  1726. (p^.registers32<1) then
  1727. p^.registers32:=1;
  1728. p^.location.loc:=LOC_REGISTER;
  1729. end;
  1730. p^.registersfpu:=p^.left^.registersfpu;
  1731. end;
  1732. procedure firstnothing(var p : ptree);
  1733. begin
  1734. p^.resulttype:=voiddef;
  1735. end;
  1736. procedure firstassignment(var p : ptree);
  1737. var
  1738. store_valid : boolean;
  1739. hp : ptree;
  1740. begin
  1741. store_valid:=must_be_valid;
  1742. must_be_valid:=false;
  1743. firstpass(p^.left);
  1744. if codegenerror then
  1745. exit;
  1746. { assignements to open arrays aren't allowed }
  1747. if is_open_array(p^.left^.resulttype) then
  1748. Message(type_e_mismatch);
  1749. { test if we can avoid copying string to temp
  1750. as in s:=s+...; (PM) }
  1751. {$ifdef dummyi386}
  1752. if ((p^.right^.treetype=addn) or (p^.right^.treetype=subn)) and
  1753. equal_trees(p^.left,p^.right^.left) and
  1754. (ret_in_acc(p^.left^.resulttype)) and
  1755. (not cs_rangechecking in aktmoduleswitches^) then
  1756. begin
  1757. disposetree(p^.right^.left);
  1758. hp:=p^.right;
  1759. p^.right:=p^.right^.right;
  1760. if hp^.treetype=addn then
  1761. p^.assigntyp:=at_plus
  1762. else
  1763. p^.assigntyp:=at_minus;
  1764. putnode(hp);
  1765. end;
  1766. if p^.assigntyp<>at_normal then
  1767. begin
  1768. { for fpu type there is no faster way }
  1769. if is_fpu(p^.left^.resulttype) then
  1770. case p^.assigntyp of
  1771. at_plus : p^.right:=gennode(addn,getcopy(p^.left),p^.right);
  1772. at_minus : p^.right:=gennode(subn,getcopy(p^.left),p^.right);
  1773. at_star : p^.right:=gennode(muln,getcopy(p^.left),p^.right);
  1774. at_slash : p^.right:=gennode(slashn,getcopy(p^.left),p^.right);
  1775. end;
  1776. end;
  1777. {$endif i386}
  1778. must_be_valid:=true;
  1779. firstpass(p^.right);
  1780. must_be_valid:=store_valid;
  1781. if codegenerror then
  1782. exit;
  1783. { some string functions don't need conversion, so treat them separatly }
  1784. if is_shortstring(p^.left^.resulttype) and (assigned(p^.right^.resulttype)) then
  1785. begin
  1786. if not ((p^.right^.resulttype^.deftype=stringdef) or
  1787. ((p^.right^.resulttype^.deftype=orddef) and (porddef(p^.right^.resulttype)^.typ=uchar))) then
  1788. begin
  1789. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  1790. firstpass(p^.right);
  1791. if codegenerror then
  1792. exit;
  1793. end;
  1794. { we call STRCOPY }
  1795. procinfo.flags:=procinfo.flags or pi_do_call;
  1796. hp:=p^.right;
  1797. { test for s:=s+anything ... }
  1798. { the problem is for
  1799. s:=s+s+s;
  1800. this is broken here !! }
  1801. { while hp^.treetype=addn do hp:=hp^.left;
  1802. if equal_trees(p^.left,hp) then
  1803. begin
  1804. p^.concat_string:=true;
  1805. hp:=p^.right;
  1806. while hp^.treetype=addn do
  1807. begin
  1808. hp^.use_strconcat:=true;
  1809. hp:=hp^.left;
  1810. end;
  1811. end; }
  1812. end
  1813. else
  1814. begin
  1815. if (p^.right^.treetype=realconstn) then
  1816. begin
  1817. if p^.left^.resulttype^.deftype=floatdef then
  1818. begin
  1819. case pfloatdef(p^.left^.resulttype)^.typ of
  1820. s32real : p^.right^.realtyp:=ait_real_32bit;
  1821. s64real : p^.right^.realtyp:=ait_real_64bit;
  1822. s80real : p^.right^.realtyp:=ait_real_extended;
  1823. { what about f32bit and s64bit }
  1824. else
  1825. begin
  1826. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  1827. { nochmal firstpass wegen der Typkonvertierung aufrufen }
  1828. firstpass(p^.right);
  1829. if codegenerror then
  1830. exit;
  1831. end;
  1832. end;
  1833. end;
  1834. end
  1835. else
  1836. begin
  1837. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  1838. firstpass(p^.right);
  1839. if codegenerror then
  1840. exit;
  1841. end;
  1842. end;
  1843. p^.resulttype:=voiddef;
  1844. {
  1845. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  1846. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  1847. }
  1848. p^.registers32:=p^.left^.registers32+p^.right^.registers32;
  1849. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  1850. {$ifdef SUPPORT_MMX}
  1851. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  1852. {$endif SUPPORT_MMX}
  1853. end;
  1854. procedure firstlr(var p : ptree);
  1855. begin
  1856. firstpass(p^.left);
  1857. firstpass(p^.right);
  1858. end;
  1859. procedure firstderef(var p : ptree);
  1860. begin
  1861. firstpass(p^.left);
  1862. if codegenerror then
  1863. begin
  1864. p^.resulttype:=generrordef;
  1865. exit;
  1866. end;
  1867. p^.registers32:=max(p^.left^.registers32,1);
  1868. p^.registersfpu:=p^.left^.registersfpu;
  1869. {$ifdef SUPPORT_MMX}
  1870. p^.registersmmx:=p^.left^.registersmmx;
  1871. {$endif SUPPORT_MMX}
  1872. if p^.left^.resulttype^.deftype<>pointerdef then
  1873. Message(cg_e_invalid_qualifier);
  1874. p^.resulttype:=ppointerdef(p^.left^.resulttype)^.definition;
  1875. p^.location.loc:=LOC_REFERENCE;
  1876. end;
  1877. procedure firstrange(var p : ptree);
  1878. var
  1879. ct : tconverttype;
  1880. begin
  1881. firstpass(p^.left);
  1882. firstpass(p^.right);
  1883. if codegenerror then
  1884. exit;
  1885. { both types must be compatible }
  1886. if not(is_equal(p^.left^.resulttype,p^.right^.resulttype)) and
  1887. not(isconvertable(p^.left^.resulttype,p^.right^.resulttype,ct,ordconstn,false)) then
  1888. Message(type_e_mismatch);
  1889. { Check if only when its a constant set }
  1890. if (p^.left^.treetype=ordconstn) and (p^.right^.treetype=ordconstn) then
  1891. begin
  1892. { upper limit must be greater or equal than lower limit }
  1893. { not if u32bit }
  1894. if (p^.left^.value>p^.right^.value) and
  1895. (( p^.left^.value<0) or (p^.right^.value>=0)) then
  1896. Message(cg_e_upper_lower_than_lower);
  1897. end;
  1898. left_right_max(p);
  1899. p^.resulttype:=p^.left^.resulttype;
  1900. set_location(p^.location,p^.left^.location);
  1901. end;
  1902. procedure firstvecn(var p : ptree);
  1903. var
  1904. harr : pdef;
  1905. ct : tconverttype;
  1906. begin
  1907. firstpass(p^.left);
  1908. firstpass(p^.right);
  1909. if codegenerror then
  1910. exit;
  1911. { range check only for arrays }
  1912. if (p^.left^.resulttype^.deftype=arraydef) then
  1913. begin
  1914. if not(isconvertable(p^.right^.resulttype,
  1915. parraydef(p^.left^.resulttype)^.rangedef,
  1916. ct,ordconstn,false)) and
  1917. not(is_equal(p^.right^.resulttype,
  1918. parraydef(p^.left^.resulttype)^.rangedef)) then
  1919. Message(type_e_mismatch);
  1920. end;
  1921. { Never convert a boolean or a char !}
  1922. { maybe type conversion }
  1923. if (p^.right^.resulttype^.deftype<>enumdef) and
  1924. not ((p^.right^.resulttype^.deftype=orddef) and
  1925. (Porddef(p^.right^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit,uchar])) then
  1926. begin
  1927. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1928. { once more firstpass }
  1929. {?? It's better to only firstpass when the tree has
  1930. changed, isn't it ?}
  1931. firstpass(p^.right);
  1932. end;
  1933. if codegenerror then
  1934. exit;
  1935. { determine return type }
  1936. if not assigned(p^.resulttype) then
  1937. if p^.left^.resulttype^.deftype=arraydef then
  1938. p^.resulttype:=parraydef(p^.left^.resulttype)^.definition
  1939. else if (p^.left^.resulttype^.deftype=pointerdef) then
  1940. begin
  1941. { convert pointer to array }
  1942. harr:=new(parraydef,init(0,$7fffffff,s32bitdef));
  1943. parraydef(harr)^.definition:=ppointerdef(p^.left^.resulttype)^.definition;
  1944. p^.left:=gentypeconvnode(p^.left,harr);
  1945. firstpass(p^.left);
  1946. if codegenerror then
  1947. exit;
  1948. p^.resulttype:=parraydef(harr)^.definition
  1949. end
  1950. else if p^.left^.resulttype^.deftype=stringdef then
  1951. begin
  1952. { indexed access to strings }
  1953. case pstringdef(p^.left^.resulttype)^.string_typ of
  1954. {
  1955. st_widestring : p^.resulttype:=cwchardef;
  1956. }
  1957. st_ansistring : p^.resulttype:=cchardef;
  1958. st_longstring : p^.resulttype:=cchardef;
  1959. st_shortstring : p^.resulttype:=cchardef;
  1960. end;
  1961. end
  1962. else
  1963. Message(type_e_mismatch);
  1964. { the register calculation is easy if a const index is used }
  1965. if p^.right^.treetype=ordconstn then
  1966. begin
  1967. p^.registers32:=p^.left^.registers32;
  1968. { for ansi/wide strings, we need at least one register }
  1969. if is_ansistring(p^.left^.resulttype) or
  1970. is_widestring(p^.left^.resulttype) then
  1971. p^.registers32:=max(p^.registers32,1);
  1972. end
  1973. else
  1974. begin
  1975. { this rules are suboptimal, but they should give }
  1976. { good results }
  1977. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  1978. { for ansi/wide strings, we need at least one register }
  1979. if is_ansistring(p^.left^.resulttype) or
  1980. is_widestring(p^.left^.resulttype) then
  1981. p^.registers32:=max(p^.registers32,1);
  1982. { need we an extra register when doing the restore ? }
  1983. if (p^.left^.registers32<=p^.right^.registers32) and
  1984. { only if the node needs less than 3 registers }
  1985. { two for the right node and one for the }
  1986. { left address }
  1987. (p^.registers32<3) then
  1988. inc(p^.registers32);
  1989. { need we an extra register for the index ? }
  1990. if (p^.right^.location.loc<>LOC_REGISTER)
  1991. { only if the right node doesn't need a register }
  1992. and (p^.right^.registers32<1) then
  1993. inc(p^.registers32);
  1994. { not correct, but what works better ?
  1995. if p^.left^.registers32>0 then
  1996. p^.registers32:=max(p^.registers32,2)
  1997. else
  1998. min. one register
  1999. p^.registers32:=max(p^.registers32,1);
  2000. }
  2001. end;
  2002. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  2003. {$ifdef SUPPORT_MMX}
  2004. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  2005. {$endif SUPPORT_MMX}
  2006. p^.location.loc:=p^.left^.location.loc;
  2007. end;
  2008. type
  2009. tfirstconvproc = procedure(var p : ptree);
  2010. procedure first_bigger_smaller(var p : ptree);
  2011. begin
  2012. if (p^.left^.location.loc<>LOC_REGISTER) and (p^.registers32=0) then
  2013. p^.registers32:=1;
  2014. p^.location.loc:=LOC_REGISTER;
  2015. end;
  2016. procedure first_cstring_charpointer(var p : ptree);
  2017. begin
  2018. p^.registers32:=1;
  2019. p^.location.loc:=LOC_REGISTER;
  2020. end;
  2021. procedure first_string_chararray(var p : ptree);
  2022. begin
  2023. p^.registers32:=1;
  2024. p^.location.loc:=LOC_REGISTER;
  2025. end;
  2026. procedure first_string_string(var p : ptree);
  2027. begin
  2028. if pstringdef(p^.resulttype)^.string_typ<>
  2029. pstringdef(p^.left^.resulttype)^.string_typ then
  2030. begin
  2031. if p^.left^.treetype=stringconstn then
  2032. p^.left^.stringtype:=pstringdef(p^.resulttype)^.string_typ
  2033. else
  2034. procinfo.flags:=procinfo.flags or pi_do_call;
  2035. end;
  2036. { for simplicity lets first keep all ansistrings
  2037. as LOC_MEM, could also become LOC_REGISTER }
  2038. p^.location.loc:=LOC_MEM;
  2039. end;
  2040. procedure first_char_to_string(var p : ptree);
  2041. var
  2042. hp : ptree;
  2043. begin
  2044. if p^.left^.treetype=ordconstn then
  2045. begin
  2046. hp:=genstringconstnode(chr(p^.left^.value));
  2047. firstpass(hp);
  2048. disposetree(p);
  2049. p:=hp;
  2050. end
  2051. else
  2052. p^.location.loc:=LOC_MEM;
  2053. end;
  2054. procedure first_nothing(var p : ptree);
  2055. begin
  2056. p^.location.loc:=LOC_MEM;
  2057. end;
  2058. procedure first_array_to_pointer(var p : ptree);
  2059. begin
  2060. if p^.registers32<1 then
  2061. p^.registers32:=1;
  2062. p^.location.loc:=LOC_REGISTER;
  2063. end;
  2064. procedure first_int_real(var p : ptree);
  2065. var t : ptree;
  2066. begin
  2067. if p^.left^.treetype=ordconstn then
  2068. begin
  2069. { convert constants direct }
  2070. { not because of type conversion }
  2071. t:=genrealconstnode(p^.left^.value);
  2072. { do a first pass here
  2073. because firstpass of typeconv does
  2074. not redo it for left field !! }
  2075. firstpass(t);
  2076. { the type can be something else than s64real !!}
  2077. t:=gentypeconvnode(t,p^.resulttype);
  2078. firstpass(t);
  2079. disposetree(p);
  2080. p:=t;
  2081. exit;
  2082. end
  2083. else
  2084. begin
  2085. if p^.registersfpu<1 then
  2086. p^.registersfpu:=1;
  2087. p^.location.loc:=LOC_FPU;
  2088. end;
  2089. end;
  2090. procedure first_int_fix(var p : ptree);
  2091. begin
  2092. if p^.left^.treetype=ordconstn then
  2093. begin
  2094. { convert constants direct }
  2095. p^.treetype:=fixconstn;
  2096. p^.value_fix:=p^.left^.value shl 16;
  2097. p^.disposetyp:=dt_nothing;
  2098. disposetree(p^.left);
  2099. p^.location.loc:=LOC_MEM;
  2100. end
  2101. else
  2102. begin
  2103. if p^.registers32<1 then
  2104. p^.registers32:=1;
  2105. p^.location.loc:=LOC_REGISTER;
  2106. end;
  2107. end;
  2108. procedure first_real_fix(var p : ptree);
  2109. begin
  2110. if p^.left^.treetype=realconstn then
  2111. begin
  2112. { convert constants direct }
  2113. p^.treetype:=fixconstn;
  2114. p^.value_fix:=round(p^.left^.value_real*65536);
  2115. p^.disposetyp:=dt_nothing;
  2116. disposetree(p^.left);
  2117. p^.location.loc:=LOC_MEM;
  2118. end
  2119. else
  2120. begin
  2121. { at least one fpu and int register needed }
  2122. if p^.registers32<1 then
  2123. p^.registers32:=1;
  2124. if p^.registersfpu<1 then
  2125. p^.registersfpu:=1;
  2126. p^.location.loc:=LOC_REGISTER;
  2127. end;
  2128. end;
  2129. procedure first_fix_real(var p : ptree);
  2130. begin
  2131. if p^.left^.treetype=fixconstn then
  2132. begin
  2133. { convert constants direct }
  2134. p^.treetype:=realconstn;
  2135. p^.value_real:=round(p^.left^.value_fix/65536.0);
  2136. p^.disposetyp:=dt_nothing;
  2137. disposetree(p^.left);
  2138. p^.location.loc:=LOC_MEM;
  2139. end
  2140. else
  2141. begin
  2142. if p^.registersfpu<1 then
  2143. p^.registersfpu:=1;
  2144. p^.location.loc:=LOC_FPU;
  2145. end;
  2146. end;
  2147. procedure first_real_real(var p : ptree);
  2148. begin
  2149. if p^.registersfpu<1 then
  2150. p^.registersfpu:=1;
  2151. p^.location.loc:=LOC_FPU;
  2152. end;
  2153. procedure first_pointer_to_array(var p : ptree);
  2154. begin
  2155. if p^.registers32<1 then
  2156. p^.registers32:=1;
  2157. p^.location.loc:=LOC_REFERENCE;
  2158. end;
  2159. procedure first_chararray_string(var p : ptree);
  2160. begin
  2161. { the only important information is the location of the }
  2162. { result }
  2163. { other stuff is done by firsttypeconv }
  2164. p^.location.loc:=LOC_MEM;
  2165. end;
  2166. procedure first_cchar_charpointer(var p : ptree);
  2167. begin
  2168. p^.left:=gentypeconvnode(p^.left,cstringdef);
  2169. { convert constant char to constant string }
  2170. firstpass(p^.left);
  2171. { evalute tree }
  2172. firstpass(p);
  2173. end;
  2174. procedure first_locmem(var p : ptree);
  2175. begin
  2176. p^.location.loc:=LOC_MEM;
  2177. end;
  2178. procedure first_bool_int(var p : ptree);
  2179. begin
  2180. p^.location.loc:=LOC_REGISTER;
  2181. { Florian I think this is overestimated
  2182. but I still do not really understand how to get this right (PM) }
  2183. { Hmmm, I think we need only one reg to return the result of }
  2184. { this node => so }
  2185. if p^.registers32<1 then
  2186. p^.registers32:=1;
  2187. { should work (FK)
  2188. p^.registers32:=p^.left^.registers32+1;}
  2189. end;
  2190. procedure first_int_bool(var p : ptree);
  2191. begin
  2192. p^.location.loc:=LOC_REGISTER;
  2193. { Florian I think this is overestimated
  2194. but I still do not really understand how to get this right (PM) }
  2195. { Hmmm, I think we need only one reg to return the result of }
  2196. { this node => so }
  2197. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  2198. firstpass(p^.left);
  2199. if p^.registers32<1 then
  2200. p^.registers32:=1;
  2201. { p^.resulttype:=booldef; }
  2202. { should work (FK)
  2203. p^.registers32:=p^.left^.registers32+1;}
  2204. end;
  2205. procedure first_proc_to_procvar(var p : ptree);
  2206. begin
  2207. { hmmm, I'am not sure if that is necessary (FK) }
  2208. firstpass(p^.left);
  2209. if codegenerror then
  2210. exit;
  2211. if (p^.left^.location.loc<>LOC_REFERENCE) then
  2212. Message(cg_e_illegal_expression);
  2213. p^.registers32:=p^.left^.registers32;
  2214. if p^.registers32<1 then
  2215. p^.registers32:=1;
  2216. p^.location.loc:=LOC_REGISTER;
  2217. end;
  2218. procedure first_load_smallset(var p : ptree);
  2219. begin
  2220. end;
  2221. procedure first_pchar_to_ansistring(var p : ptree);
  2222. begin
  2223. p^.location.loc:=LOC_REGISTER;
  2224. if p^.registers32<1 then
  2225. p^.registers32:=1;
  2226. end;
  2227. procedure first_ansistring_to_pchar(var p : ptree);
  2228. begin
  2229. p^.location.loc:=LOC_REGISTER;
  2230. if p^.registers32<1 then
  2231. p^.registers32:=1;
  2232. end;
  2233. function is_procsym_load(p:Ptree):boolean;
  2234. begin
  2235. is_procsym_load:=((p^.treetype=loadn) and (p^.symtableentry^.typ=procsym)) or
  2236. ((p^.treetype=addrn) and (p^.left^.treetype=loadn)
  2237. and (p^.left^.symtableentry^.typ=procsym)) ;
  2238. end;
  2239. { change a proc call to a procload for assignment to a procvar }
  2240. { this can only happen for proc/function without arguments }
  2241. function is_procsym_call(p:Ptree):boolean;
  2242. begin
  2243. is_procsym_call:=(p^.treetype=calln) and (p^.left=nil) and
  2244. (((p^.symtableprocentry^.typ=procsym) and (p^.right=nil)) or
  2245. ((p^.right<>nil) and (p^.right^.symtableprocentry^.typ=varsym)));
  2246. end;
  2247. {***}
  2248. function is_assignment_overloaded(from_def,to_def : pdef) : boolean;
  2249. var
  2250. passproc : pprocdef;
  2251. convtyp : tconverttype;
  2252. begin
  2253. is_assignment_overloaded:=false;
  2254. if assigned(overloaded_operators[assignment]) then
  2255. passproc:=overloaded_operators[assignment]^.definition
  2256. else
  2257. exit;
  2258. while passproc<>nil do
  2259. begin
  2260. if is_equal(passproc^.retdef,to_def) and
  2261. isconvertable(from_def,passproc^.para1^.data,convtyp,
  2262. ordconstn { nur Dummy},false ) then
  2263. begin
  2264. is_assignment_overloaded:=true;
  2265. break;
  2266. end;
  2267. passproc:=passproc^.nextoverloaded;
  2268. end;
  2269. end;
  2270. { Attention: do *** no *** recursive call of firstpass }
  2271. { because the child tree is always passed }
  2272. procedure firsttypeconv(var p : ptree);
  2273. var
  2274. hp : ptree;
  2275. aprocdef : pprocdef;
  2276. proctype : tdeftype;
  2277. const
  2278. firstconvert : array[tconverttype] of
  2279. tfirstconvproc = (first_nothing,first_nothing,
  2280. first_bigger_smaller,first_nothing,first_bigger_smaller,
  2281. first_bigger_smaller,first_bigger_smaller,
  2282. first_bigger_smaller,first_bigger_smaller,
  2283. first_bigger_smaller,first_string_string,
  2284. first_cstring_charpointer,first_string_chararray,
  2285. first_array_to_pointer,first_pointer_to_array,
  2286. first_char_to_string,first_bigger_smaller,
  2287. first_bigger_smaller,first_bigger_smaller,
  2288. first_bigger_smaller,first_bigger_smaller,
  2289. first_bigger_smaller,first_bigger_smaller,
  2290. first_bigger_smaller,first_bigger_smaller,
  2291. first_bigger_smaller,first_bigger_smaller,
  2292. first_bigger_smaller,first_bigger_smaller,
  2293. first_bigger_smaller,first_bigger_smaller,
  2294. first_bigger_smaller,first_bigger_smaller,
  2295. first_bigger_smaller,first_bigger_smaller,
  2296. first_bool_int,first_int_bool,
  2297. first_int_real,first_real_fix,
  2298. first_fix_real,first_int_fix,first_real_real,
  2299. first_locmem,first_proc_to_procvar,
  2300. first_cchar_charpointer,
  2301. first_load_smallset,
  2302. first_ansistring_to_pchar,
  2303. first_pchar_to_ansistring);
  2304. begin
  2305. aprocdef:=nil;
  2306. { if explicite type conversation, then run firstpass }
  2307. if p^.explizit then
  2308. firstpass(p^.left);
  2309. if codegenerror then
  2310. begin
  2311. p^.resulttype:=generrordef;
  2312. exit;
  2313. end;
  2314. if not assigned(p^.left^.resulttype) then
  2315. begin
  2316. codegenerror:=true;
  2317. internalerror(52349);
  2318. exit;
  2319. end;
  2320. { load the value_str from the left part }
  2321. p^.registers32:=p^.left^.registers32;
  2322. p^.registersfpu:=p^.left^.registersfpu;
  2323. {$ifdef SUPPORT_MMX}
  2324. p^.registersmmx:=p^.left^.registersmmx;
  2325. {$endif}
  2326. set_location(p^.location,p^.left^.location);
  2327. { remove obsolete type conversions }
  2328. if is_equal(p^.left^.resulttype,p^.resulttype) then
  2329. begin
  2330. { becuase is_equal only checks the basetype for sets we need to
  2331. check here if we are loading a smallset into a normalset }
  2332. if (p^.resulttype^.deftype=setdef) and
  2333. (p^.left^.resulttype^.deftype=setdef) and
  2334. (psetdef(p^.resulttype)^.settype<>smallset) and
  2335. (psetdef(p^.left^.resulttype)^.settype=smallset) then
  2336. begin
  2337. { try to define the set as a normalset if it's a constant set }
  2338. if p^.left^.treetype=setconstn then
  2339. begin
  2340. p^.resulttype:=p^.left^.resulttype;
  2341. psetdef(p^.resulttype)^.settype:=normset
  2342. end
  2343. else
  2344. p^.convtyp:=tc_load_smallset;
  2345. exit;
  2346. end
  2347. else
  2348. begin
  2349. hp:=p;
  2350. p:=p^.left;
  2351. p^.resulttype:=hp^.resulttype;
  2352. putnode(hp);
  2353. exit;
  2354. end;
  2355. end;
  2356. if is_assignment_overloaded(p^.left^.resulttype,p^.resulttype) then
  2357. begin
  2358. procinfo.flags:=procinfo.flags or pi_do_call;
  2359. hp:=gencallnode(overloaded_operators[assignment],nil);
  2360. hp^.left:=gencallparanode(p^.left,nil);
  2361. putnode(p);
  2362. p:=hp;
  2363. firstpass(p);
  2364. exit;
  2365. end;
  2366. if (not(isconvertable(p^.left^.resulttype,p^.resulttype,
  2367. p^.convtyp,p^.left^.treetype,p^.explizit))) then
  2368. begin
  2369. {Procedures have a resulttype of voiddef and functions of their
  2370. own resulttype. They will therefore always be incompatible with
  2371. a procvar. Because isconvertable cannot check for procedures we
  2372. use an extra check for them.}
  2373. if (cs_tp_compatible in aktmoduleswitches) and
  2374. ((is_procsym_load(p^.left) or is_procsym_call(p^.left)) and
  2375. (p^.resulttype^.deftype=procvardef)) then
  2376. begin
  2377. { just a test: p^.explizit:=false; }
  2378. if is_procsym_call(p^.left) then
  2379. begin
  2380. if p^.left^.right=nil then
  2381. begin
  2382. p^.left^.treetype:=loadn;
  2383. { are at same offset so this could be spared, but
  2384. it more secure to do it anyway }
  2385. p^.left^.symtableentry:=p^.left^.symtableprocentry;
  2386. p^.left^.resulttype:=pprocsym(p^.left^.symtableentry)^.definition;
  2387. aprocdef:=pprocdef(p^.left^.resulttype);
  2388. end
  2389. else
  2390. begin
  2391. p^.left^.right^.treetype:=loadn;
  2392. p^.left^.right^.symtableentry:=p^.left^.right^.symtableentry;
  2393. P^.left^.right^.resulttype:=pvarsym(p^.left^.symtableentry)^.definition;
  2394. hp:=p^.left^.right;
  2395. putnode(p^.left);
  2396. p^.left:=hp;
  2397. { should we do that ? }
  2398. firstpass(p^.left);
  2399. if not is_equal(p^.left^.resulttype,p^.resulttype) then
  2400. begin
  2401. Message(type_e_mismatch);
  2402. exit;
  2403. end
  2404. else
  2405. begin
  2406. hp:=p;
  2407. p:=p^.left;
  2408. p^.resulttype:=hp^.resulttype;
  2409. putnode(hp);
  2410. exit;
  2411. end;
  2412. end;
  2413. end
  2414. else
  2415. begin
  2416. if p^.left^.treetype=addrn then
  2417. begin
  2418. hp:=p^.left;
  2419. p^.left:=p^.left^.left;
  2420. putnode(p^.left);
  2421. end
  2422. else
  2423. aprocdef:=pprocsym(p^.left^.symtableentry)^.definition;
  2424. end;
  2425. p^.convtyp:=tc_proc2procvar;
  2426. { Now check if the procedure we are going to assign to
  2427. the procvar, is compatible with the procvar's type.
  2428. Did the original procvar support do such a check?
  2429. I can't find any.}
  2430. { answer : is_equal works for procvardefs !! }
  2431. { but both must be procvardefs, so we cheet little }
  2432. if assigned(aprocdef) then
  2433. begin
  2434. proctype:=aprocdef^.deftype;
  2435. aprocdef^.deftype:=procvardef;
  2436. if not is_equal(aprocdef,p^.resulttype) then
  2437. begin
  2438. aprocdef^.deftype:=proctype;
  2439. Message(type_e_mismatch);
  2440. end;
  2441. aprocdef^.deftype:=proctype;
  2442. firstconvert[p^.convtyp](p);
  2443. end
  2444. else
  2445. Message(type_e_mismatch);
  2446. exit;
  2447. end
  2448. else
  2449. begin
  2450. if p^.explizit then
  2451. begin
  2452. { boolean to byte are special because the
  2453. location can be different }
  2454. if (p^.resulttype^.deftype=orddef) and
  2455. (porddef(p^.resulttype)^.typ=u8bit) and
  2456. (p^.left^.resulttype^.deftype=orddef) and
  2457. (porddef(p^.left^.resulttype)^.typ=bool8bit) then
  2458. begin
  2459. p^.convtyp:=tc_bool_2_int;
  2460. firstconvert[p^.convtyp](p);
  2461. exit;
  2462. end;
  2463. { normal tc_equal-Konvertierung durchf�hren }
  2464. p^.convtyp:=tc_equal;
  2465. { wenn Aufz„hltyp nach Ordinal konvertiert werden soll }
  2466. { dann Aufz„hltyp=s32bit }
  2467. if (p^.left^.resulttype^.deftype=enumdef) and
  2468. is_ordinal(p^.resulttype) then
  2469. begin
  2470. if p^.left^.treetype=ordconstn then
  2471. begin
  2472. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  2473. disposetree(p);
  2474. firstpass(hp);
  2475. p:=hp;
  2476. exit;
  2477. end
  2478. else
  2479. begin
  2480. if not isconvertable(s32bitdef,p^.resulttype,p^.convtyp,
  2481. ordconstn { nur Dummy},false ) then
  2482. Message(cg_e_illegal_type_conversion);
  2483. end;
  2484. end
  2485. { ordinal to enumeration }
  2486. else
  2487. if (p^.resulttype^.deftype=enumdef) and
  2488. is_ordinal(p^.left^.resulttype) then
  2489. begin
  2490. if p^.left^.treetype=ordconstn then
  2491. begin
  2492. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  2493. disposetree(p);
  2494. firstpass(hp);
  2495. p:=hp;
  2496. exit;
  2497. end
  2498. else
  2499. begin
  2500. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,
  2501. ordconstn { nur Dummy},false ) then
  2502. Message(cg_e_illegal_type_conversion);
  2503. end;
  2504. end
  2505. {Are we typecasting an ordconst to a char?}
  2506. else
  2507. if is_equal(p^.resulttype,cchardef) and
  2508. is_ordinal(p^.left^.resulttype) then
  2509. begin
  2510. if p^.left^.treetype=ordconstn then
  2511. begin
  2512. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  2513. firstpass(hp);
  2514. disposetree(p);
  2515. p:=hp;
  2516. exit;
  2517. end
  2518. else
  2519. begin
  2520. { this is wrong because it converts to a 4 byte long var !!
  2521. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,ordconstn nur Dummy ) then }
  2522. if not isconvertable(p^.left^.resulttype,u8bitdef,
  2523. p^.convtyp,ordconstn { nur Dummy},false ) then
  2524. Message(cg_e_illegal_type_conversion);
  2525. end;
  2526. end
  2527. { only if the same size or formal def }
  2528. { why do we allow typecasting of voiddef ?? (PM) }
  2529. else
  2530. if not(
  2531. (p^.left^.resulttype^.deftype=formaldef) or
  2532. (p^.left^.resulttype^.size=p^.resulttype^.size) or
  2533. (is_equal(p^.left^.resulttype,voiddef) and
  2534. (p^.left^.treetype=derefn))
  2535. ) then
  2536. Message(cg_e_illegal_type_conversion);
  2537. { the conversion into a strutured type is only }
  2538. { possible, if the source is no register }
  2539. if ((p^.resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  2540. ((p^.resulttype^.deftype=objectdef) and not(pobjectdef(p^.resulttype)^.isclass))
  2541. ) and (p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  2542. {it also works if the assignment is overloaded }
  2543. not is_assignment_overloaded(p^.left^.resulttype,p^.resulttype) then
  2544. Message(cg_e_illegal_type_conversion);
  2545. end
  2546. else
  2547. Message(type_e_mismatch);
  2548. end
  2549. end
  2550. else
  2551. begin
  2552. { ordinal contants can be directly converted }
  2553. if (p^.left^.treetype=ordconstn) and is_ordinal(p^.resulttype) then
  2554. begin
  2555. { perform range checking }
  2556. if not(p^.explizit and (cs_tp_compatible in aktmoduleswitches)) then
  2557. testrange(p^.resulttype,p^.left^.value);
  2558. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  2559. disposetree(p);
  2560. firstpass(hp);
  2561. p:=hp;
  2562. exit;
  2563. end;
  2564. if p^.convtyp<>tc_equal then
  2565. firstconvert[p^.convtyp](p);
  2566. end;
  2567. end;
  2568. { *************** subroutine handling **************** }
  2569. { protected field handling
  2570. protected field can not appear in
  2571. var parameters of function !!
  2572. this can only be done after we have determined the
  2573. overloaded function
  2574. this is the reason why it is not in the parser
  2575. PM }
  2576. procedure test_protected_sym(sym : psym);
  2577. begin
  2578. if ((sym^.properties and sp_protected)<>0) and
  2579. ((sym^.owner^.symtabletype=unitsymtable) or
  2580. ((sym^.owner^.symtabletype=objectsymtable) and
  2581. (pobjectdef(sym^.owner^.defowner)^.owner^.symtabletype=unitsymtable))) then
  2582. Message(parser_e_cant_access_protected_member);
  2583. end;
  2584. procedure test_protected(p : ptree);
  2585. begin
  2586. if p^.treetype=loadn then
  2587. begin
  2588. test_protected_sym(p^.symtableentry);
  2589. end
  2590. else if p^.treetype=typeconvn then
  2591. begin
  2592. test_protected(p^.left);
  2593. end
  2594. else if p^.treetype=derefn then
  2595. begin
  2596. test_protected(p^.left);
  2597. end
  2598. else if p^.treetype=subscriptn then
  2599. begin
  2600. { test_protected(p^.left);
  2601. Is a field of a protected var
  2602. also protected ??? PM }
  2603. test_protected_sym(p^.vs);
  2604. end;
  2605. end;
  2606. procedure firstcallparan(var p : ptree;defcoll : pdefcoll);
  2607. var store_valid : boolean;
  2608. convtyp : tconverttype;
  2609. begin
  2610. inc(parsing_para_level);
  2611. if assigned(p^.right) then
  2612. begin
  2613. if defcoll=nil then
  2614. firstcallparan(p^.right,nil)
  2615. else
  2616. firstcallparan(p^.right,defcoll^.next);
  2617. p^.registers32:=p^.right^.registers32;
  2618. p^.registersfpu:=p^.right^.registersfpu;
  2619. {$ifdef SUPPORT_MMX}
  2620. p^.registersmmx:=p^.right^.registersmmx;
  2621. {$endif}
  2622. end;
  2623. if defcoll=nil then
  2624. begin
  2625. { this breaks typeconversions in write !!! (PM) }
  2626. {if not(assigned(p^.resulttype)) then }
  2627. if not(assigned(p^.resulttype)) or
  2628. (p^.left^.treetype=typeconvn) then
  2629. firstpass(p^.left);
  2630. {else
  2631. exit; this broke the
  2632. value of registers32 !! }
  2633. if codegenerror then
  2634. begin
  2635. dec(parsing_para_level);
  2636. exit;
  2637. end;
  2638. p^.resulttype:=p^.left^.resulttype;
  2639. end
  2640. { if we know the routine which is called, then the type }
  2641. { conversions are inserted }
  2642. else
  2643. begin
  2644. if count_ref then
  2645. begin
  2646. store_valid:=must_be_valid;
  2647. if (defcoll^.paratyp=vs_var) then
  2648. test_protected(p^.left);
  2649. if (defcoll^.paratyp<>vs_var) then
  2650. must_be_valid:=true
  2651. else
  2652. must_be_valid:=false;
  2653. { here we must add something for the implicit type }
  2654. { conversion from array of char to pchar }
  2655. if isconvertable(p^.left^.resulttype,defcoll^.data,convtyp,
  2656. p^.left^.treetype,false) then
  2657. if convtyp=tc_array_to_pointer then
  2658. must_be_valid:=false;
  2659. firstpass(p^.left);
  2660. must_be_valid:=store_valid;
  2661. end;
  2662. if not(is_shortstring(p^.left^.resulttype) and
  2663. is_shortstring(defcoll^.data)) and
  2664. (defcoll^.data^.deftype<>formaldef) then
  2665. begin
  2666. if (defcoll^.paratyp=vs_var) and
  2667. { allows conversion from word to integer and
  2668. byte to shortint }
  2669. (not(
  2670. (p^.left^.resulttype^.deftype=orddef) and
  2671. (defcoll^.data^.deftype=orddef) and
  2672. (p^.left^.resulttype^.size=defcoll^.data^.size)
  2673. ) and
  2674. { an implicit pointer conversion is allowed }
  2675. not(
  2676. (p^.left^.resulttype^.deftype=pointerdef) and
  2677. (defcoll^.data^.deftype=pointerdef)
  2678. ) and
  2679. { child classes can be also passed }
  2680. not(
  2681. (p^.left^.resulttype^.deftype=objectdef) and
  2682. (defcoll^.data^.deftype=objectdef) and
  2683. pobjectdef(p^.left^.resulttype)^.isrelated(pobjectdef(defcoll^.data))
  2684. ) and
  2685. { an implicit file conversion is also allowed }
  2686. { from a typed file to an untyped one }
  2687. not(
  2688. (p^.left^.resulttype^.deftype=filedef) and
  2689. (defcoll^.data^.deftype=filedef) and
  2690. (pfiledef(defcoll^.data)^.filetype = ft_untyped) and
  2691. (pfiledef(p^.left^.resulttype)^.filetype = ft_typed)
  2692. ) and
  2693. not(is_equal(p^.left^.resulttype,defcoll^.data))) then
  2694. Message(parser_e_call_by_ref_without_typeconv);
  2695. { don't generate an type conversion for open arrays }
  2696. { else we loss the ranges }
  2697. if not(is_open_array(defcoll^.data)) then
  2698. begin
  2699. p^.left:=gentypeconvnode(p^.left,defcoll^.data);
  2700. firstpass(p^.left);
  2701. end;
  2702. if codegenerror then
  2703. begin
  2704. dec(parsing_para_level);
  2705. exit;
  2706. end;
  2707. end;
  2708. { check var strings }
  2709. if (cs_strict_var_strings in aktlocalswitches) and
  2710. is_shortstring(p^.left^.resulttype) and
  2711. is_shortstring(defcoll^.data) and
  2712. (defcoll^.paratyp=vs_var) and
  2713. not(is_equal(p^.left^.resulttype,defcoll^.data)) then
  2714. Message(type_e_strict_var_string_violation);
  2715. { Variablen, die call by reference �bergeben werden, }
  2716. { k”nnen nicht in ein Register kopiert werden }
  2717. { is this usefull here ? }
  2718. { this was missing in formal parameter list }
  2719. if defcoll^.paratyp=vs_var then
  2720. make_not_regable(p^.left);
  2721. p^.resulttype:=defcoll^.data;
  2722. end;
  2723. if p^.left^.registers32>p^.registers32 then
  2724. p^.registers32:=p^.left^.registers32;
  2725. if p^.left^.registersfpu>p^.registersfpu then
  2726. p^.registersfpu:=p^.left^.registersfpu;
  2727. {$ifdef SUPPORT_MMX}
  2728. if p^.left^.registersmmx>p^.registersmmx then
  2729. p^.registersmmx:=p^.left^.registersmmx;
  2730. {$endif SUPPORT_MMX}
  2731. dec(parsing_para_level);
  2732. end;
  2733. procedure firstcalln(var p : ptree);
  2734. type
  2735. pprocdefcoll = ^tprocdefcoll;
  2736. tprocdefcoll = record
  2737. data : pprocdef;
  2738. nextpara : pdefcoll;
  2739. firstpara : pdefcoll;
  2740. next : pprocdefcoll;
  2741. end;
  2742. var
  2743. hp,procs,hp2 : pprocdefcoll;
  2744. pd : pprocdef;
  2745. actprocsym : pprocsym;
  2746. def_from,def_to,conv_to : pdef;
  2747. pt,inlinecode : ptree;
  2748. exactmatch,inlined : boolean;
  2749. paralength,l : longint;
  2750. pdc : pdefcoll;
  2751. { only Dummy }
  2752. hcvt : tconverttype;
  2753. regi : tregister;
  2754. store_valid, old_count_ref : boolean;
  2755. { types.is_equal can't handle a formaldef ! }
  2756. function is_equal(def1,def2 : pdef) : boolean;
  2757. begin
  2758. { all types can be passed to a formaldef }
  2759. is_equal:=(def1^.deftype=formaldef) or
  2760. (assigned(def2) and types.is_equal(def1,def2))
  2761. { to support ansi/long/wide strings in a proper way }
  2762. { string and string[10] are assumed as equal }
  2763. { when searching the correct overloaded procedure }
  2764. or
  2765. (assigned(def1) and assigned(def2) and
  2766. (def1^.deftype=stringdef) and (def2^.deftype=stringdef) and
  2767. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ)
  2768. )
  2769. ;
  2770. end;
  2771. function is_in_limit(def_from,def_to : pdef) : boolean;
  2772. begin
  2773. is_in_limit:=(def_from^.deftype = orddef) and
  2774. (def_to^.deftype = orddef) and
  2775. (porddef(def_from)^.low>porddef(def_to)^.low) and
  2776. (porddef(def_from)^.high<porddef(def_to)^.high);
  2777. end;
  2778. var
  2779. is_const : boolean;
  2780. begin
  2781. { release registers! }
  2782. { if procdefinition<>nil then we called firstpass already }
  2783. { it seems to be bad because of the registers }
  2784. { at least we can avoid the overloaded search !! }
  2785. procs:=nil;
  2786. { made this global for disposing !! }
  2787. store_valid:=must_be_valid;
  2788. must_be_valid:=false;
  2789. inlined:=false;
  2790. if assigned(p^.procdefinition) and
  2791. ((p^.procdefinition^.options and poinline)<>0) then
  2792. begin
  2793. inlinecode:=p^.right;
  2794. if assigned(inlinecode) then
  2795. begin
  2796. inlined:=true;
  2797. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  2798. end;
  2799. p^.right:=nil;
  2800. end;
  2801. { procedure variable ? }
  2802. if assigned(p^.right) then
  2803. begin
  2804. { procedure does a call }
  2805. procinfo.flags:=procinfo.flags or pi_do_call;
  2806. { calc the correture value for the register }
  2807. {$ifdef i386}
  2808. for regi:=R_EAX to R_EDI do
  2809. inc(reg_pushes[regi],t_times*2);
  2810. {$endif}
  2811. {$ifdef m68k}
  2812. for regi:=R_D0 to R_A6 do
  2813. inc(reg_pushes[regi],t_times*2);
  2814. {$endif}
  2815. { calculate the type of the parameters }
  2816. if assigned(p^.left) then
  2817. begin
  2818. old_count_ref:=count_ref;
  2819. count_ref:=false;
  2820. firstcallparan(p^.left,nil);
  2821. count_ref:=old_count_ref;
  2822. if codegenerror then
  2823. exit;
  2824. end;
  2825. firstpass(p^.right);
  2826. { check the parameters }
  2827. pdc:=pprocvardef(p^.right^.resulttype)^.para1;
  2828. pt:=p^.left;
  2829. while assigned(pdc) and assigned(pt) do
  2830. begin
  2831. pt:=pt^.right;
  2832. pdc:=pdc^.next;
  2833. end;
  2834. if assigned(pt) or assigned(pdc) then
  2835. Message(parser_e_illegal_parameter_list);
  2836. { insert type conversions }
  2837. if assigned(p^.left) then
  2838. begin
  2839. old_count_ref:=count_ref;
  2840. count_ref:=true;
  2841. firstcallparan(p^.left,pprocvardef(p^.right^.resulttype)^.para1);
  2842. count_ref:=old_count_ref;
  2843. if codegenerror then
  2844. exit;
  2845. end;
  2846. p^.resulttype:=pprocvardef(p^.right^.resulttype)^.retdef;
  2847. { this was missing, leads to a bug below if
  2848. the procvar is a function }
  2849. p^.procdefinition:=pprocdef(p^.right^.resulttype);
  2850. end
  2851. else
  2852. { not a procedure variable }
  2853. begin
  2854. { determine the type of the parameters }
  2855. if assigned(p^.left) then
  2856. begin
  2857. old_count_ref:=count_ref;
  2858. count_ref:=false;
  2859. store_valid:=must_be_valid;
  2860. must_be_valid:=false;
  2861. firstcallparan(p^.left,nil);
  2862. count_ref:=old_count_ref;
  2863. must_be_valid:=store_valid;
  2864. if codegenerror then
  2865. exit;
  2866. end;
  2867. { do we know the procedure to call ? }
  2868. if not(assigned(p^.procdefinition)) then
  2869. begin
  2870. actprocsym:=pprocsym(p^.symtableprocentry);
  2871. { determine length of parameter list }
  2872. pt:=p^.left;
  2873. paralength:=0;
  2874. while assigned(pt) do
  2875. begin
  2876. inc(paralength);
  2877. pt:=pt^.right;
  2878. end;
  2879. { link all procedures which have the same # of parameters }
  2880. pd:=actprocsym^.definition;
  2881. while assigned(pd) do
  2882. begin
  2883. { we should also check that the overloaded function
  2884. has been declared in a unit that is in the uses !! }
  2885. { pd^.owner should be in the symtablestack !! }
  2886. { Laenge der deklarierten Parameterliste feststellen: }
  2887. { not necessary why nextprocsym field }
  2888. {st:=symtablestack;
  2889. if (pd^.owner^.symtabletype<>objectsymtable) then
  2890. while assigned(st) do
  2891. begin
  2892. if (st=pd^.owner) then break;
  2893. st:=st^.next;
  2894. end;
  2895. if assigned(st) then }
  2896. begin
  2897. pdc:=pd^.para1;
  2898. l:=0;
  2899. while assigned(pdc) do
  2900. begin
  2901. inc(l);
  2902. pdc:=pdc^.next;
  2903. end;
  2904. { only when the # of parameter are equal }
  2905. if l=paralength then
  2906. begin
  2907. new(hp);
  2908. hp^.data:=pd;
  2909. hp^.next:=procs;
  2910. hp^.nextpara:=pd^.para1;
  2911. hp^.firstpara:=pd^.para1;
  2912. procs:=hp;
  2913. end;
  2914. end;
  2915. pd:=pd^.nextoverloaded;
  2916. {$ifdef CHAINPROCSYMS}
  2917. if (pd=nil) and not (p^.unit_specific) then
  2918. begin
  2919. actprocsym:=actprocsym^.nextprocsym;
  2920. if assigned(actprocsym) then
  2921. pd:=actprocsym^.definition;
  2922. end;
  2923. {$endif CHAINPROCSYMS}
  2924. end;
  2925. { no procedures found? then there is something wrong
  2926. with the parameter size }
  2927. if not assigned(procs) and
  2928. ((parsing_para_level=0) or assigned(p^.left)) then
  2929. begin
  2930. Message(parser_e_wrong_parameter_size);
  2931. actprocsym^.write_parameter_lists;
  2932. exit;
  2933. end;
  2934. { now we can compare parameter after parameter }
  2935. pt:=p^.left;
  2936. while assigned(pt) do
  2937. begin
  2938. { matches a parameter of one procedure exact ? }
  2939. exactmatch:=false;
  2940. hp:=procs;
  2941. while assigned(hp) do
  2942. begin
  2943. if is_equal(hp^.nextpara^.data,pt^.resulttype) then
  2944. begin
  2945. if hp^.nextpara^.data=pt^.resulttype then
  2946. begin
  2947. pt^.exact_match_found:=true;
  2948. hp^.nextpara^.argconvtyp:=act_exact;
  2949. end
  2950. else
  2951. hp^.nextpara^.argconvtyp:=act_equal;
  2952. exactmatch:=true;
  2953. end
  2954. else
  2955. hp^.nextpara^.argconvtyp:=act_convertable;
  2956. hp:=hp^.next;
  2957. end;
  2958. { .... if yes, del all the other procedures }
  2959. if exactmatch then
  2960. begin
  2961. { the first .... }
  2962. while (assigned(procs)) and not(is_equal(procs^.nextpara^.data,pt^.resulttype)) do
  2963. begin
  2964. hp:=procs^.next;
  2965. dispose(procs);
  2966. procs:=hp;
  2967. end;
  2968. { and the others }
  2969. hp:=procs;
  2970. while (assigned(hp)) and assigned(hp^.next) do
  2971. begin
  2972. if not(is_equal(hp^.next^.nextpara^.data,pt^.resulttype)) then
  2973. begin
  2974. hp2:=hp^.next^.next;
  2975. dispose(hp^.next);
  2976. hp^.next:=hp2;
  2977. end
  2978. else
  2979. hp:=hp^.next;
  2980. end;
  2981. end
  2982. { when a parameter matches exact, remove all procs
  2983. which need typeconvs }
  2984. else
  2985. begin
  2986. { the first... }
  2987. while (assigned(procs)) and
  2988. not(isconvertable(pt^.resulttype,procs^.nextpara^.data,
  2989. hcvt,pt^.left^.treetype,false)) do
  2990. begin
  2991. hp:=procs^.next;
  2992. dispose(procs);
  2993. procs:=hp;
  2994. end;
  2995. { and the others }
  2996. hp:=procs;
  2997. while (assigned(hp)) and assigned(hp^.next) do
  2998. begin
  2999. if not(isconvertable(pt^.resulttype,hp^.next^.nextpara^.data,
  3000. hcvt,pt^.left^.treetype,false)) then
  3001. begin
  3002. hp2:=hp^.next^.next;
  3003. dispose(hp^.next);
  3004. hp^.next:=hp2;
  3005. end
  3006. else
  3007. hp:=hp^.next;
  3008. end;
  3009. end;
  3010. { update nextpara for all procedures }
  3011. hp:=procs;
  3012. while assigned(hp) do
  3013. begin
  3014. hp^.nextpara:=hp^.nextpara^.next;
  3015. hp:=hp^.next;
  3016. end;
  3017. { load next parameter }
  3018. pt:=pt^.right;
  3019. end;
  3020. if not assigned(procs) then
  3021. begin
  3022. { there is an error, must be wrong type, because
  3023. wrong size is already checked (PFV) }
  3024. if (parsing_para_level=0) or (p^.left<>nil) then
  3025. begin
  3026. Message(parser_e_wrong_parameter_type);
  3027. actprocsym^.write_parameter_lists;
  3028. exit;
  3029. end
  3030. else
  3031. begin
  3032. { try to convert to procvar }
  3033. p^.treetype:=loadn;
  3034. p^.resulttype:=pprocsym(p^.symtableprocentry)^.definition;
  3035. p^.symtableentry:=p^.symtableprocentry;
  3036. p^.is_first:=false;
  3037. p^.disposetyp:=dt_nothing;
  3038. firstpass(p);
  3039. exit;
  3040. end;
  3041. end;
  3042. { if there are several choices left then for orddef }
  3043. { if a type is totally included in the other }
  3044. { we don't fear an overflow , }
  3045. { so we can do as if it is an exact match }
  3046. { this will convert integer to longint }
  3047. { rather than to words }
  3048. { conversion of byte to integer or longint }
  3049. {would still not be solved }
  3050. if assigned(procs^.next) then
  3051. begin
  3052. hp:=procs;
  3053. while assigned(hp) do
  3054. begin
  3055. hp^.nextpara:=hp^.firstpara;
  3056. hp:=hp^.next;
  3057. end;
  3058. pt:=p^.left;
  3059. while assigned(pt) do
  3060. begin
  3061. { matches a parameter of one procedure exact ? }
  3062. exactmatch:=false;
  3063. def_from:=pt^.resulttype;
  3064. hp:=procs;
  3065. while assigned(hp) do
  3066. begin
  3067. if not is_equal(hp^.nextpara^.data,pt^.resulttype) then
  3068. begin
  3069. def_to:=hp^.nextpara^.data;
  3070. if ((def_from^.deftype=orddef) and (def_to^.deftype=orddef)) and
  3071. (is_in_limit(def_from,def_to) or
  3072. ((hp^.nextpara^.paratyp=vs_var) and
  3073. (def_from^.size=def_to^.size))) then
  3074. begin
  3075. exactmatch:=true;
  3076. conv_to:=def_to;
  3077. end;
  3078. end;
  3079. hp:=hp^.next;
  3080. end;
  3081. { .... if yes, del all the other procedures }
  3082. if exactmatch then
  3083. begin
  3084. { the first .... }
  3085. while (assigned(procs)) and not(is_in_limit(def_from,procs^.nextpara^.data)) do
  3086. begin
  3087. hp:=procs^.next;
  3088. dispose(procs);
  3089. procs:=hp;
  3090. end;
  3091. { and the others }
  3092. hp:=procs;
  3093. while (assigned(hp)) and assigned(hp^.next) do
  3094. begin
  3095. if not(is_in_limit(def_from,hp^.next^.nextpara^.data)) then
  3096. begin
  3097. hp2:=hp^.next^.next;
  3098. dispose(hp^.next);
  3099. hp^.next:=hp2;
  3100. end
  3101. else
  3102. begin
  3103. def_to:=hp^.next^.nextpara^.data;
  3104. if (conv_to^.size>def_to^.size) or
  3105. ((porddef(conv_to)^.low<porddef(def_to)^.low) and
  3106. (porddef(conv_to)^.high>porddef(def_to)^.high)) then
  3107. begin
  3108. hp2:=procs;
  3109. procs:=hp;
  3110. conv_to:=def_to;
  3111. dispose(hp2);
  3112. end
  3113. else
  3114. hp:=hp^.next;
  3115. end;
  3116. end;
  3117. end;
  3118. { update nextpara for all procedures }
  3119. hp:=procs;
  3120. while assigned(hp) do
  3121. begin
  3122. hp^.nextpara:=hp^.nextpara^.next;
  3123. hp:=hp^.next;
  3124. end;
  3125. pt:=pt^.right;
  3126. end;
  3127. end;
  3128. { let's try to eliminate equal is exact is there }
  3129. {if assigned(procs^.next) then
  3130. begin
  3131. pt:=p^.left;
  3132. while assigned(pt) do
  3133. begin
  3134. if pt^.exact_match_found then
  3135. begin
  3136. hp:=procs;
  3137. while (assigned(procs)) and (procs^.nextpara^.data<>pt^.resulttype) do
  3138. begin
  3139. hp:=procs^.next;
  3140. dispose(procs);
  3141. procs:=hp;
  3142. end;
  3143. end;
  3144. pt:=pt^.right;
  3145. end;
  3146. end; }
  3147. {$ifndef CHAINPROCSYMS}
  3148. if assigned(procs^.next) then
  3149. begin
  3150. Message(cg_e_cant_choose_overload_function);
  3151. actprocsym^.write_parameter_lists;
  3152. end;
  3153. {$else CHAINPROCSYMS}
  3154. if assigned(procs^.next) then
  3155. { if the last retained is the only one }
  3156. { from a unit it is OK PM }
  3157. { the last is the one coming from the first symtable }
  3158. { as the diff defcoll are inserted in front }
  3159. begin
  3160. hp2:=procs;
  3161. while assigned(hp2^.next) and assigned(hp2^.next^.next) do
  3162. hp2:=hp2^.next;
  3163. if (hp2^.data^.owner<>hp2^.next^.data^.owner) then
  3164. begin
  3165. hp:=procs^.next;
  3166. {hp2 is the correct one }
  3167. hp2:=hp2^.next;
  3168. while hp<>hp2 do
  3169. begin
  3170. dispose(procs);
  3171. procs:=hp;
  3172. hp:=procs^.next;
  3173. end;
  3174. procs:=hp2;
  3175. end
  3176. else
  3177. begin
  3178. Message(cg_e_cant_choose_overload_function);
  3179. actprocsym^.write_parameter_lists;
  3180. error(too_much_matches);
  3181. end;
  3182. end;
  3183. {$endif CHAINPROCSYMS}
  3184. {$ifdef UseBrowser}
  3185. if make_ref then
  3186. begin
  3187. procs^.data^.lastref:=new(pref,init(procs^.data^.lastref,@p^.fileinfo));
  3188. end;
  3189. {$endif UseBrowser}
  3190. p^.procdefinition:=procs^.data;
  3191. p^.resulttype:=procs^.data^.retdef;
  3192. { big error for with statements
  3193. p^.symtableproc:=p^.procdefinition^.owner; }
  3194. p^.location.loc:=LOC_MEM;
  3195. {$ifdef CHAINPROCSYMS}
  3196. { object with method read;
  3197. call to read(x) will be a usual procedure call }
  3198. if assigned(p^.methodpointer) and
  3199. (p^.procdefinition^._class=nil) then
  3200. begin
  3201. { not ok for extended }
  3202. case p^.methodpointer^.treetype of
  3203. typen,hnewn : fatalerror(no_para_match);
  3204. end;
  3205. disposetree(p^.methodpointer);
  3206. p^.methodpointer:=nil;
  3207. end;
  3208. {$endif CHAINPROCSYMS}
  3209. end;{ end of procedure to call determination }
  3210. is_const:=((p^.procdefinition^.options and pointernconst)<>0) and
  3211. (p^.left^.left^.treetype in [realconstn,ordconstn]);
  3212. { handle predefined procedures }
  3213. if ((p^.procdefinition^.options and pointernproc)<>0) or is_const then
  3214. begin
  3215. { settextbuf needs two args }
  3216. if assigned(p^.left^.right) then
  3217. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,is_const,p^.left)
  3218. else
  3219. begin
  3220. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,is_const,p^.left^.left);
  3221. putnode(p^.left);
  3222. end;
  3223. putnode(p);
  3224. firstpass(pt);
  3225. p:=pt;
  3226. must_be_valid:=store_valid;
  3227. if codegenerror then
  3228. exit;
  3229. dispose(procs);
  3230. exit;
  3231. end
  3232. else
  3233. { no intern procedure => we do a call }
  3234. { calc the correture value for the register }
  3235. { handle predefined procedures }
  3236. if (p^.procdefinition^.options and poinline)<>0 then
  3237. begin
  3238. if assigned(p^.methodpointer) then
  3239. Message(cg_e_unable_inline_object_methods);
  3240. if assigned(p^.right) and (p^.right^.treetype<>procinlinen) then
  3241. Message(cg_e_unable_inline_procvar);
  3242. { p^.treetype:=procinlinen; }
  3243. if not assigned(p^.right) then
  3244. begin
  3245. if assigned(p^.procdefinition^.code) then
  3246. inlinecode:=genprocinlinenode(p,ptree(p^.procdefinition^.code))
  3247. else
  3248. Message(cg_e_no_code_for_inline_stored);
  3249. if assigned(inlinecode) then
  3250. begin
  3251. { consider it has not inlined if called
  3252. again inside the args }
  3253. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  3254. firstpass(inlinecode);
  3255. inlined:=true;
  3256. end;
  3257. end;
  3258. end
  3259. else
  3260. procinfo.flags:=procinfo.flags or pi_do_call;
  3261. { work trough all parameters to insert the type conversions }
  3262. { !!! done now after internproc !! (PM) }
  3263. if assigned(p^.left) then
  3264. begin
  3265. old_count_ref:=count_ref;
  3266. count_ref:=true;
  3267. firstcallparan(p^.left,p^.procdefinition^.para1);
  3268. count_ref:=old_count_ref;
  3269. end;
  3270. {$ifdef i386}
  3271. for regi:=R_EAX to R_EDI do
  3272. begin
  3273. if (p^.procdefinition^.usedregisters and ($80 shr word(regi)))<>0 then
  3274. inc(reg_pushes[regi],t_times*2);
  3275. end;
  3276. {$endif}
  3277. {$ifdef m68k}
  3278. for regi:=R_D0 to R_A6 do
  3279. begin
  3280. if (p^.procdefinition^.usedregisters and ($800 shr word(regi)))<>0 then
  3281. inc(reg_pushes[regi],t_times*2);
  3282. end;
  3283. {$endif}
  3284. end;
  3285. { ensure that the result type is set }
  3286. p^.resulttype:=p^.procdefinition^.retdef;
  3287. { get a register for the return value }
  3288. if (p^.resulttype<>pdef(voiddef)) then
  3289. begin
  3290. if (p^.procdefinition^.options and poconstructor)<>0 then
  3291. begin
  3292. { extra handling of classes }
  3293. { p^.methodpointer should be assigned! }
  3294. if assigned(p^.methodpointer) and assigned(p^.methodpointer^.resulttype) and
  3295. (p^.methodpointer^.resulttype^.deftype=classrefdef) then
  3296. begin
  3297. p^.location.loc:=LOC_REGISTER;
  3298. p^.registers32:=1;
  3299. { the result type depends on the classref }
  3300. p^.resulttype:=pclassrefdef(p^.methodpointer^.resulttype)^.definition;
  3301. end
  3302. { a object constructor returns the result with the flags }
  3303. else
  3304. p^.location.loc:=LOC_FLAGS;
  3305. end
  3306. else
  3307. begin
  3308. {$ifdef SUPPORT_MMX}
  3309. if (cs_mmx in aktlocalswitches) and
  3310. is_mmx_able_array(p^.resulttype) then
  3311. begin
  3312. p^.location.loc:=LOC_MMXREGISTER;
  3313. p^.registersmmx:=1;
  3314. end
  3315. else
  3316. {$endif SUPPORT_MMX}
  3317. if ret_in_acc(p^.resulttype) then
  3318. begin
  3319. p^.location.loc:=LOC_REGISTER;
  3320. p^.registers32:=1;
  3321. end
  3322. else if (p^.resulttype^.deftype=floatdef) then
  3323. begin
  3324. p^.location.loc:=LOC_FPU;
  3325. p^.registersfpu:=1;
  3326. end
  3327. end;
  3328. end;
  3329. {$ifdef StoreFPULevel}
  3330. { a fpu can be used in any procedure !! }
  3331. p^.registersfpu:=p^.procdefinition^.fpu_used;
  3332. {$endif StoreFPULevel}
  3333. { if this is a call to a method calc the registers }
  3334. if (p^.methodpointer<>nil) then
  3335. begin
  3336. case p^.methodpointer^.treetype of
  3337. { but only, if this is not a supporting node }
  3338. typen,hnewn : ;
  3339. else
  3340. begin
  3341. { R.Assign is not a constructor !!! }
  3342. { but for R^.Assign, R must be valid !! }
  3343. if ((p^.procdefinition^.options and poconstructor) <> 0) or
  3344. ((p^.methodpointer^.treetype=loadn) and
  3345. ((pobjectdef(p^.methodpointer^.resulttype)^.options and oo_hasvirtual) = 0)) then
  3346. must_be_valid:=false
  3347. else
  3348. must_be_valid:=true;
  3349. firstpass(p^.methodpointer);
  3350. p^.registersfpu:=max(p^.methodpointer^.registersfpu,p^.registersfpu);
  3351. p^.registers32:=max(p^.methodpointer^.registers32,p^.registers32);
  3352. {$ifdef SUPPORT_MMX}
  3353. p^.registersmmx:=max(p^.methodpointer^.registersmmx,p^.registersmmx);
  3354. {$endif SUPPORT_MMX}
  3355. end;
  3356. end;
  3357. end;
  3358. if inlined then
  3359. begin
  3360. p^.right:=inlinecode;
  3361. p^.procdefinition^.options:=p^.procdefinition^.options or poinline;
  3362. end;
  3363. { determine the registers of the procedure variable }
  3364. { is this OK for inlined procs also ?? (PM) }
  3365. if assigned(p^.right) then
  3366. begin
  3367. p^.registersfpu:=max(p^.right^.registersfpu,p^.registersfpu);
  3368. p^.registers32:=max(p^.right^.registers32,p^.registers32);
  3369. {$ifdef SUPPORT_MMX}
  3370. p^.registersmmx:=max(p^.right^.registersmmx,p^.registersmmx);
  3371. {$endif SUPPORT_MMX}
  3372. end;
  3373. { determine the registers of the procedure }
  3374. if assigned(p^.left) then
  3375. begin
  3376. p^.registersfpu:=max(p^.left^.registersfpu,p^.registersfpu);
  3377. p^.registers32:=max(p^.left^.registers32,p^.registers32);
  3378. {$ifdef SUPPORT_MMX}
  3379. p^.registersmmx:=max(p^.left^.registersmmx,p^.registersmmx);
  3380. {$endif SUPPORT_MMX}
  3381. end;
  3382. if assigned(procs) then
  3383. dispose(procs);
  3384. must_be_valid:=store_valid;
  3385. end;
  3386. procedure firstfuncret(var p : ptree);
  3387. begin
  3388. p^.resulttype:=p^.retdef;
  3389. p^.location.loc:=LOC_REFERENCE;
  3390. if ret_in_param(p^.retdef) or
  3391. (@procinfo<>pprocinfo(p^.funcretprocinfo)) then
  3392. p^.registers32:=1;
  3393. { no claim if setting higher return value_str }
  3394. if must_be_valid and
  3395. (@procinfo=pprocinfo(p^.funcretprocinfo)) and
  3396. not procinfo.funcret_is_valid then
  3397. Message(sym_w_function_result_not_set);
  3398. if count_ref then
  3399. pprocinfo(p^.funcretprocinfo)^.funcret_is_valid:=true;
  3400. end;
  3401. { intern inline suborutines }
  3402. procedure firstinline(var p : ptree);
  3403. var
  3404. vl : longint;
  3405. vr : bestreal;
  3406. hp,hpp : ptree;
  3407. store_count_ref,
  3408. isreal,
  3409. dowrite,
  3410. store_valid,
  3411. file_is_typed : boolean;
  3412. procedure do_lowhigh(adef : pdef);
  3413. var
  3414. v : longint;
  3415. enum : penumsym;
  3416. begin
  3417. case Adef^.deftype of
  3418. orddef:
  3419. begin
  3420. if p^.inlinenumber=in_low_x then
  3421. v:=porddef(Adef)^.low
  3422. else
  3423. v:=porddef(Adef)^.high;
  3424. hp:=genordinalconstnode(v,adef);
  3425. firstpass(hp);
  3426. disposetree(p);
  3427. p:=hp;
  3428. end;
  3429. enumdef:
  3430. begin
  3431. enum:=Penumdef(Adef)^.first;
  3432. if p^.inlinenumber=in_high_x then
  3433. while enum^.next<>nil do
  3434. enum:=enum^.next;
  3435. hp:=genenumnode(enum);
  3436. disposetree(p);
  3437. p:=hp;
  3438. end
  3439. end;
  3440. end;
  3441. begin
  3442. store_valid:=must_be_valid;
  3443. store_count_ref:=count_ref;
  3444. count_ref:=false;
  3445. if not (p^.inlinenumber in [in_read_x,in_readln_x,in_sizeof_x,
  3446. in_typeof_x,in_ord_x,in_str_x_string,
  3447. in_reset_typedfile,in_rewrite_typedfile]) then
  3448. must_be_valid:=true
  3449. else
  3450. must_be_valid:=false;
  3451. { if we handle writeln; p^.left contains no valid address }
  3452. if assigned(p^.left) then
  3453. begin
  3454. if p^.left^.treetype=callparan then
  3455. firstcallparan(p^.left,nil)
  3456. else
  3457. firstpass(p^.left);
  3458. left_right_max(p);
  3459. set_location(p^.location,p^.left^.location);
  3460. end;
  3461. { handle intern constant functions in separate case }
  3462. if p^.inlineconst then
  3463. begin
  3464. isreal:=(p^.left^.treetype=realconstn);
  3465. vl:=p^.left^.value;
  3466. vr:=p^.left^.value_real;
  3467. case p^.inlinenumber of
  3468. in_const_trunc : begin
  3469. if isreal then
  3470. hp:=genordinalconstnode(trunc(vr),s32bitdef)
  3471. else
  3472. hp:=genordinalconstnode(trunc(vl),s32bitdef);
  3473. end;
  3474. in_const_round : begin
  3475. if isreal then
  3476. hp:=genordinalconstnode(round(vr),s32bitdef)
  3477. else
  3478. hp:=genordinalconstnode(round(vl),s32bitdef);
  3479. end;
  3480. in_const_frac : begin
  3481. if isreal then
  3482. hp:=genrealconstnode(frac(vr))
  3483. else
  3484. hp:=genrealconstnode(frac(vl));
  3485. end;
  3486. in_const_int : begin
  3487. if isreal then
  3488. hp:=genrealconstnode(int(vr))
  3489. else
  3490. hp:=genrealconstnode(int(vl));
  3491. end;
  3492. in_const_abs : begin
  3493. if isreal then
  3494. hp:=genrealconstnode(abs(vr))
  3495. else
  3496. hp:=genordinalconstnode(abs(vl),p^.left^.resulttype);
  3497. end;
  3498. in_const_sqr : begin
  3499. if isreal then
  3500. hp:=genrealconstnode(sqr(vr))
  3501. else
  3502. hp:=genordinalconstnode(sqr(vl),p^.left^.resulttype);
  3503. end;
  3504. in_const_odd : begin
  3505. if isreal then
  3506. Message(type_e_integer_expr_expected)
  3507. else
  3508. hp:=genordinalconstnode(byte(odd(vl)),booldef);
  3509. end;
  3510. in_const_swap_word : begin
  3511. if isreal then
  3512. Message(type_e_integer_expr_expected)
  3513. else
  3514. hp:=genordinalconstnode((vl and $ff) shl 8+(vl shr 8),p^.left^.resulttype);
  3515. end;
  3516. in_const_swap_long : begin
  3517. if isreal then
  3518. Message(type_e_mismatch)
  3519. else
  3520. hp:=genordinalconstnode((vl and $ffff) shl 16+(vl shr 16),p^.left^.resulttype);
  3521. end;
  3522. in_const_ptr : begin
  3523. if isreal then
  3524. Message(type_e_mismatch)
  3525. else
  3526. hp:=genordinalconstnode(vl,voidpointerdef);
  3527. end;
  3528. else
  3529. internalerror(88);
  3530. end;
  3531. disposetree(p);
  3532. firstpass(hp);
  3533. p:=hp;
  3534. end
  3535. else
  3536. begin
  3537. case p^.inlinenumber of
  3538. in_lo_long,in_hi_long,
  3539. in_lo_word,in_hi_word:
  3540. begin
  3541. if p^.registers32<1 then
  3542. p^.registers32:=1;
  3543. if p^.inlinenumber in [in_lo_word,in_hi_word] then
  3544. p^.resulttype:=u8bitdef
  3545. else
  3546. p^.resulttype:=u16bitdef;
  3547. p^.location.loc:=LOC_REGISTER;
  3548. if not is_integer(p^.left^.resulttype) then
  3549. Message(type_e_mismatch)
  3550. else
  3551. begin
  3552. if p^.left^.treetype=ordconstn then
  3553. begin
  3554. case p^.inlinenumber of
  3555. in_lo_word : hp:=genordinalconstnode(p^.left^.value and $ff,p^.left^.resulttype);
  3556. in_hi_word : hp:=genordinalconstnode(p^.left^.value shr 8,p^.left^.resulttype);
  3557. in_lo_long : hp:=genordinalconstnode(p^.left^.value and $ffff,p^.left^.resulttype);
  3558. in_hi_long : hp:=genordinalconstnode(p^.left^.value shr 16,p^.left^.resulttype);
  3559. end;
  3560. disposetree(p);
  3561. firstpass(hp);
  3562. p:=hp;
  3563. end;
  3564. end;
  3565. end;
  3566. in_sizeof_x:
  3567. begin
  3568. if p^.registers32<1 then
  3569. p^.registers32:=1;
  3570. p^.resulttype:=s32bitdef;
  3571. p^.location.loc:=LOC_REGISTER;
  3572. end;
  3573. in_typeof_x:
  3574. begin
  3575. if p^.registers32<1 then
  3576. p^.registers32:=1;
  3577. p^.location.loc:=LOC_REGISTER;
  3578. p^.resulttype:=voidpointerdef;
  3579. end;
  3580. in_ord_x:
  3581. begin
  3582. if (p^.left^.treetype=ordconstn) then
  3583. begin
  3584. hp:=genordinalconstnode(p^.left^.value,s32bitdef);
  3585. disposetree(p);
  3586. p:=hp;
  3587. firstpass(p);
  3588. end
  3589. else
  3590. begin
  3591. if (p^.left^.resulttype^.deftype=orddef) then
  3592. if (porddef(p^.left^.resulttype)^.typ in [uchar,bool8bit]) then
  3593. begin
  3594. if porddef(p^.left^.resulttype)^.typ=bool8bit then
  3595. begin
  3596. hp:=gentypeconvnode(p^.left,u8bitdef);
  3597. putnode(p);
  3598. p:=hp;
  3599. p^.convtyp:=tc_bool_2_int;
  3600. p^.explizit:=true;
  3601. firstpass(p);
  3602. end
  3603. else
  3604. begin
  3605. hp:=gentypeconvnode(p^.left,u8bitdef);
  3606. putnode(p);
  3607. p:=hp;
  3608. p^.explizit:=true;
  3609. firstpass(p);
  3610. end;
  3611. end
  3612. { can this happen ? }
  3613. else if (porddef(p^.left^.resulttype)^.typ=uvoid) then
  3614. Message(type_e_mismatch)
  3615. else
  3616. { all other orddef need no transformation }
  3617. begin
  3618. hp:=p^.left;
  3619. putnode(p);
  3620. p:=hp;
  3621. end
  3622. else if (p^.left^.resulttype^.deftype=enumdef) then
  3623. begin
  3624. hp:=gentypeconvnode(p^.left,s32bitdef);
  3625. putnode(p);
  3626. p:=hp;
  3627. p^.explizit:=true;
  3628. firstpass(p);
  3629. end
  3630. else
  3631. begin
  3632. { can anything else be ord() ?}
  3633. Message(type_e_mismatch);
  3634. end;
  3635. end;
  3636. end;
  3637. in_chr_byte:
  3638. begin
  3639. hp:=gentypeconvnode(p^.left,cchardef);
  3640. putnode(p);
  3641. p:=hp;
  3642. p^.explizit:=true;
  3643. firstpass(p);
  3644. end;
  3645. in_length_string:
  3646. begin
  3647. {$ifdef UseAnsiString}
  3648. if is_ansistring(p^.left^.resulttype) then
  3649. p^.resulttype:=s32bitdef
  3650. else
  3651. {$endif UseAnsiString}
  3652. p^.resulttype:=u8bitdef;
  3653. { wer don't need string conversations here }
  3654. if (p^.left^.treetype=typeconvn) and
  3655. (p^.left^.left^.resulttype^.deftype=stringdef) then
  3656. begin
  3657. hp:=p^.left^.left;
  3658. putnode(p^.left);
  3659. p^.left:=hp;
  3660. end;
  3661. { evalutes length of constant strings direct }
  3662. if (p^.left^.treetype=stringconstn) then
  3663. begin
  3664. {$ifdef UseAnsiString}
  3665. hp:=genordinalconstnode(p^.left^.length,s32bitdef);
  3666. {$else UseAnsiString}
  3667. hp:=genordinalconstnode(length(p^.left^.value_str^),s32bitdef);
  3668. {$endif UseAnsiString}
  3669. disposetree(p);
  3670. firstpass(hp);
  3671. p:=hp;
  3672. end;
  3673. end;
  3674. in_assigned_x:
  3675. begin
  3676. p^.resulttype:=booldef;
  3677. p^.location.loc:=LOC_FLAGS;
  3678. end;
  3679. in_pred_x,
  3680. in_succ_x:
  3681. begin
  3682. inc(p^.registers32);
  3683. p^.resulttype:=p^.left^.resulttype;
  3684. p^.location.loc:=LOC_REGISTER;
  3685. if not is_ordinal(p^.resulttype) then
  3686. Message(type_e_ordinal_expr_expected)
  3687. else
  3688. begin
  3689. if (p^.resulttype^.deftype=enumdef) and
  3690. (penumdef(p^.resulttype)^.has_jumps) then
  3691. Message(type_e_succ_and_pred_enums_with_assign_not_possible)
  3692. else
  3693. if p^.left^.treetype=ordconstn then
  3694. begin
  3695. if p^.inlinenumber=in_succ_x then
  3696. hp:=genordinalconstnode(p^.left^.value+1,p^.left^.resulttype)
  3697. else
  3698. hp:=genordinalconstnode(p^.left^.value-1,p^.left^.resulttype);
  3699. disposetree(p);
  3700. firstpass(hp);
  3701. p:=hp;
  3702. end;
  3703. end;
  3704. end;
  3705. in_inc_x,
  3706. in_dec_x:
  3707. begin
  3708. p^.resulttype:=voiddef;
  3709. if assigned(p^.left) then
  3710. begin
  3711. firstcallparan(p^.left,nil);
  3712. if codegenerror then
  3713. exit;
  3714. { first param must be var }
  3715. if is_constnode(p^.left^.left) then
  3716. Message(type_e_variable_id_expected);
  3717. { check type }
  3718. if (p^.left^.resulttype^.deftype in [enumdef,pointerdef]) or
  3719. is_ordinal(p^.left^.resulttype) then
  3720. begin
  3721. { two paras ? }
  3722. if assigned(p^.left^.right) then
  3723. begin
  3724. { insert a type conversion }
  3725. { the second param is always longint }
  3726. p^.left^.right^.left:=gentypeconvnode(p^.left^.right^.left,s32bitdef);
  3727. { check the type conversion }
  3728. firstpass(p^.left^.right^.left);
  3729. { need we an additional register ? }
  3730. if not(is_constintnode(p^.left^.right^.left)) and
  3731. (p^.left^.right^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  3732. (p^.left^.right^.left^.registers32<1) then
  3733. inc(p^.registers32);
  3734. if assigned(p^.left^.right^.right) then
  3735. Message(cg_e_illegal_expression);
  3736. end;
  3737. end
  3738. else
  3739. Message(type_e_ordinal_expr_expected);
  3740. end
  3741. else
  3742. Message(type_e_mismatch);
  3743. end;
  3744. in_read_x,
  3745. in_readln_x,
  3746. in_write_x,
  3747. in_writeln_x :
  3748. begin
  3749. { needs a call }
  3750. procinfo.flags:=procinfo.flags or pi_do_call;
  3751. p^.resulttype:=voiddef;
  3752. { we must know if it is a typed file or not }
  3753. { but we must first do the firstpass for it }
  3754. file_is_typed:=false;
  3755. if assigned(p^.left) then
  3756. begin
  3757. firstcallparan(p^.left,nil);
  3758. { now we can check }
  3759. hp:=p^.left;
  3760. while assigned(hp^.right) do
  3761. hp:=hp^.right;
  3762. { if resulttype is not assigned, then automatically }
  3763. { file is not typed. }
  3764. if assigned(hp) and assigned(hp^.resulttype) then
  3765. Begin
  3766. if (hp^.resulttype^.deftype=filedef) and
  3767. (pfiledef(hp^.resulttype)^.filetype=ft_typed) then
  3768. begin
  3769. file_is_typed:=true;
  3770. { test the type }
  3771. hpp:=p^.left;
  3772. while (hpp<>hp) do
  3773. begin
  3774. if (hpp^.left^.treetype=typen) then
  3775. Message(type_e_cant_read_write_type);
  3776. if not is_equal(hpp^.resulttype,pfiledef(hp^.resulttype)^.typed_as) then
  3777. Message(type_e_mismatch);
  3778. hpp:=hpp^.right;
  3779. end;
  3780. end;
  3781. end; { endif assigned(hp) }
  3782. { insert type conversions for write(ln) }
  3783. if (not file_is_typed) then
  3784. begin
  3785. dowrite:=(p^.inlinenumber in [in_write_x,in_writeln_x]);
  3786. hp:=p^.left;
  3787. while assigned(hp) do
  3788. begin
  3789. if (hp^.left^.treetype=typen) then
  3790. Message(type_e_cant_read_write_type);
  3791. if assigned(hp^.left^.resulttype) then
  3792. begin
  3793. isreal:=false;
  3794. case hp^.left^.resulttype^.deftype of
  3795. filedef : begin
  3796. { only allowed as first parameter }
  3797. if assigned(hp^.right) then
  3798. Message(type_e_cant_read_write_type);
  3799. end;
  3800. stringdef : ;
  3801. pointerdef : begin
  3802. if not is_equal(ppointerdef(hp^.left^.resulttype)^.definition,cchardef) then
  3803. Message(type_e_cant_read_write_type);
  3804. end;
  3805. floatdef : begin
  3806. isreal:=true;
  3807. end;
  3808. orddef : begin
  3809. case porddef(hp^.left^.resulttype)^.typ of
  3810. uchar,
  3811. u32bit,s32bit : ;
  3812. u8bit,s8bit,
  3813. u16bit,s16bit : if dowrite then
  3814. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  3815. bool8bit,
  3816. bool16bit,bool32bit : if dowrite then
  3817. hp^.left:=gentypeconvnode(hp^.left,booldef)
  3818. else
  3819. Message(type_e_cant_read_write_type);
  3820. else
  3821. Message(type_e_cant_read_write_type);
  3822. end;
  3823. end;
  3824. arraydef : begin
  3825. if not((parraydef(hp^.left^.resulttype)^.lowrange=0) and
  3826. is_equal(parraydef(hp^.left^.resulttype)^.definition,cchardef)) then
  3827. begin
  3828. { but we convert only if the first index<>0,
  3829. because in this case we have a ASCIIZ string }
  3830. if dowrite and
  3831. (parraydef(hp^.left^.resulttype)^.lowrange<>0) and
  3832. (parraydef(hp^.left^.resulttype)^.definition^.deftype=orddef) and
  3833. (porddef(parraydef(hp^.left^.resulttype)^.definition)^.typ=uchar) then
  3834. hp^.left:=gentypeconvnode(hp^.left,cstringdef)
  3835. else
  3836. Message(type_e_cant_read_write_type);
  3837. end;
  3838. end;
  3839. else
  3840. Message(type_e_cant_read_write_type);
  3841. end;
  3842. { some format options ? }
  3843. hpp:=hp^.right;
  3844. if assigned(hpp) and hpp^.is_colon_para then
  3845. begin
  3846. if (not is_integer(hpp^.resulttype)) then
  3847. Message(type_e_integer_expr_expected)
  3848. else
  3849. hpp^.left:=gentypeconvnode(hpp^.left,s32bitdef);
  3850. hpp:=hpp^.right;
  3851. if assigned(hpp) and hpp^.is_colon_para then
  3852. begin
  3853. if isreal then
  3854. begin
  3855. if (not is_integer(hpp^.resulttype)) then
  3856. Message(type_e_integer_expr_expected)
  3857. else
  3858. hpp^.left:=gentypeconvnode(hpp^.left,s32bitdef);
  3859. end
  3860. else
  3861. Message(parser_e_illegal_colon_qualifier);
  3862. end;
  3863. end;
  3864. end;
  3865. hp:=hp^.right;
  3866. end;
  3867. end;
  3868. { pass all parameters again for the typeconversions }
  3869. if codegenerror then
  3870. exit;
  3871. must_be_valid:=true;
  3872. firstcallparan(p^.left,nil);
  3873. { calc registers }
  3874. left_right_max(p);
  3875. end;
  3876. end;
  3877. in_settextbuf_file_x :
  3878. begin
  3879. { warning here p^.left is the callparannode
  3880. not the argument directly }
  3881. { p^.left^.left is text var }
  3882. { p^.left^.right^.left is the buffer var }
  3883. { firstcallparan(p^.left,nil);
  3884. already done in firstcalln }
  3885. { now we know the type of buffer }
  3886. getsymonlyin(systemunit,'SETTEXTBUF');
  3887. hp:=gencallnode(pprocsym(srsym),systemunit);
  3888. hp^.left:=gencallparanode(
  3889. genordinalconstnode(p^.left^.left^.resulttype^.size,s32bitdef),p^.left);
  3890. putnode(p);
  3891. p:=hp;
  3892. firstpass(p);
  3893. end;
  3894. { the firstpass of the arg has been done in firstcalln ? }
  3895. in_reset_typedfile,in_rewrite_typedfile :
  3896. begin
  3897. procinfo.flags:=procinfo.flags or pi_do_call;
  3898. { to be sure the right definition is loaded }
  3899. p^.left^.resulttype:=nil;
  3900. firstload(p^.left);
  3901. p^.resulttype:=voiddef;
  3902. end;
  3903. in_str_x_string :
  3904. begin
  3905. procinfo.flags:=procinfo.flags or pi_do_call;
  3906. p^.resulttype:=voiddef;
  3907. if assigned(p^.left) then
  3908. begin
  3909. hp:=p^.left^.right;
  3910. { first pass just the string for first local use }
  3911. must_be_valid:=false;
  3912. count_ref:=true;
  3913. p^.left^.right:=nil;
  3914. firstcallparan(p^.left,nil);
  3915. must_be_valid:=true;
  3916. p^.left^.right:=hp;
  3917. firstcallparan(p^.left^.right,nil);
  3918. hp:=p^.left;
  3919. { valid string ? }
  3920. if not assigned(hp) or
  3921. (hp^.left^.resulttype^.deftype<>stringdef) or
  3922. (hp^.right=nil) or
  3923. (hp^.left^.location.loc<>LOC_REFERENCE) then
  3924. Message(cg_e_illegal_expression);
  3925. { !!!! check length of string }
  3926. while assigned(hp^.right) do
  3927. hp:=hp^.right;
  3928. { check and convert the first param }
  3929. if hp^.is_colon_para then
  3930. Message(cg_e_illegal_expression);
  3931. isreal:=false;
  3932. case hp^.resulttype^.deftype of
  3933. orddef : begin
  3934. case porddef(hp^.left^.resulttype)^.typ of
  3935. u32bit,s32bit : ;
  3936. u8bit,s8bit,
  3937. u16bit,s16bit : hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  3938. else
  3939. Message(type_e_integer_or_real_expr_expected);
  3940. end;
  3941. end;
  3942. floatdef : begin
  3943. isreal:=true;
  3944. end;
  3945. else
  3946. Message(type_e_integer_or_real_expr_expected);
  3947. end;
  3948. { some format options ? }
  3949. hpp:=p^.left^.right;
  3950. if assigned(hpp) and hpp^.is_colon_para then
  3951. begin
  3952. if (not is_integer(hpp^.resulttype)) then
  3953. Message(type_e_integer_expr_expected)
  3954. else
  3955. hpp^.left:=gentypeconvnode(hpp^.left,s32bitdef);
  3956. hpp:=hpp^.right;
  3957. if assigned(hpp) and hpp^.is_colon_para then
  3958. begin
  3959. if isreal then
  3960. begin
  3961. if (not is_integer(hpp^.resulttype)) then
  3962. Message(type_e_integer_expr_expected)
  3963. else
  3964. hpp^.left:=gentypeconvnode(hpp^.left,s32bitdef);
  3965. end
  3966. else
  3967. Message(parser_e_illegal_colon_qualifier);
  3968. end;
  3969. end;
  3970. { for first local use }
  3971. must_be_valid:=false;
  3972. count_ref:=true;
  3973. end
  3974. else
  3975. Message(parser_e_illegal_parameter_list);
  3976. { pass all parameters again for the typeconversions }
  3977. if codegenerror then
  3978. exit;
  3979. must_be_valid:=true;
  3980. firstcallparan(p^.left,nil);
  3981. { calc registers }
  3982. left_right_max(p);
  3983. end;
  3984. in_include_x_y,
  3985. in_exclude_x_y:
  3986. begin
  3987. p^.resulttype:=voiddef;
  3988. if assigned(p^.left) then
  3989. begin
  3990. firstcallparan(p^.left,nil);
  3991. p^.registers32:=p^.left^.registers32;
  3992. p^.registersfpu:=p^.left^.registersfpu;
  3993. {$ifdef SUPPORT_MMX}
  3994. p^.registersmmx:=p^.left^.registersmmx;
  3995. {$endif SUPPORT_MMX}
  3996. { first param must be var }
  3997. if (p^.left^.left^.location.loc<>LOC_REFERENCE) and
  3998. (p^.left^.left^.location.loc<>LOC_CREGISTER) then
  3999. Message(cg_e_illegal_expression);
  4000. { check type }
  4001. if (p^.left^.resulttype^.deftype=setdef) then
  4002. begin
  4003. { two paras ? }
  4004. if assigned(p^.left^.right) then
  4005. begin
  4006. { insert a type conversion }
  4007. { to the type of the set elements }
  4008. p^.left^.right^.left:=gentypeconvnode(
  4009. p^.left^.right^.left,
  4010. psetdef(p^.left^.resulttype)^.setof);
  4011. { check the type conversion }
  4012. firstpass(p^.left^.right^.left);
  4013. { only three parameters are allowed }
  4014. if assigned(p^.left^.right^.right) then
  4015. Message(cg_e_illegal_expression);
  4016. end;
  4017. end
  4018. else
  4019. Message(type_e_mismatch);
  4020. end
  4021. else
  4022. Message(type_e_mismatch);
  4023. end;
  4024. in_low_x,in_high_x:
  4025. begin
  4026. if p^.left^.treetype in [typen,loadn] then
  4027. begin
  4028. case p^.left^.resulttype^.deftype of
  4029. orddef,enumdef:
  4030. begin
  4031. do_lowhigh(p^.left^.resulttype);
  4032. firstpass(p);
  4033. end;
  4034. setdef:
  4035. begin
  4036. do_lowhigh(Psetdef(p^.left^.resulttype)^.setof);
  4037. firstpass(p);
  4038. end;
  4039. arraydef:
  4040. begin
  4041. if is_open_array(p^.left^.resulttype) then
  4042. begin
  4043. if p^.inlinenumber=in_low_x then
  4044. begin
  4045. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.lowrange,s32bitdef);
  4046. disposetree(p);
  4047. p:=hp;
  4048. firstpass(p);
  4049. end
  4050. else
  4051. begin
  4052. p^.resulttype:=s32bitdef;
  4053. p^.registers32:=max(1,
  4054. p^.registers32);
  4055. p^.location.loc:=LOC_REGISTER;
  4056. end;
  4057. end
  4058. else
  4059. begin
  4060. if p^.inlinenumber=in_low_x then
  4061. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.lowrange,s32bitdef)
  4062. else
  4063. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.highrange,s32bitdef);
  4064. disposetree(p);
  4065. p:=hp;
  4066. firstpass(p);
  4067. end;
  4068. end;
  4069. stringdef:
  4070. begin
  4071. if p^.inlinenumber=in_low_x then
  4072. hp:=genordinalconstnode(0,u8bitdef)
  4073. else
  4074. hp:=genordinalconstnode(Pstringdef(p^.left^.resulttype)^.len,u8bitdef);
  4075. disposetree(p);
  4076. p:=hp;
  4077. firstpass(p);
  4078. end;
  4079. else
  4080. Message(type_e_mismatch);
  4081. end;
  4082. end
  4083. else
  4084. Message(type_e_varid_or_typeid_expected);
  4085. end
  4086. else internalerror(8);
  4087. end;
  4088. end;
  4089. must_be_valid:=store_valid;
  4090. count_ref:=store_count_ref;
  4091. end;
  4092. procedure firstsubscriptn(var p : ptree);
  4093. begin
  4094. firstpass(p^.left);
  4095. if codegenerror then
  4096. begin
  4097. p^.resulttype:=generrordef;
  4098. exit;
  4099. end;
  4100. p^.resulttype:=p^.vs^.definition;
  4101. { this must be done in the parser
  4102. if count_ref and not must_be_valid then
  4103. if (p^.vs^.properties and sp_protected)<>0 then
  4104. Message(parser_e_cant_write_protected_member);
  4105. }
  4106. p^.registers32:=p^.left^.registers32;
  4107. p^.registersfpu:=p^.left^.registersfpu;
  4108. {$ifdef SUPPORT_MMX}
  4109. p^.registersmmx:=p^.left^.registersmmx;
  4110. {$endif SUPPORT_MMX}
  4111. { classes must be dereferenced implicit }
  4112. if (p^.left^.resulttype^.deftype=objectdef) and
  4113. pobjectdef(p^.left^.resulttype)^.isclass then
  4114. begin
  4115. if p^.registers32=0 then
  4116. p^.registers32:=1;
  4117. p^.location.loc:=LOC_REFERENCE;
  4118. end
  4119. else
  4120. begin
  4121. if (p^.left^.location.loc<>LOC_MEM) and
  4122. (p^.left^.location.loc<>LOC_REFERENCE) then
  4123. Message(cg_e_illegal_expression);
  4124. set_location(p^.location,p^.left^.location);
  4125. end;
  4126. end;
  4127. procedure firstselfn(var p : ptree);
  4128. begin
  4129. if (p^.resulttype^.deftype=classrefdef) or
  4130. ((p^.resulttype^.deftype=objectdef)
  4131. and pobjectdef(p^.resulttype)^.isclass
  4132. ) then
  4133. p^.location.loc:=LOC_REGISTER
  4134. else
  4135. p^.location.loc:=LOC_REFERENCE;
  4136. end;
  4137. procedure firsttypen(var p : ptree);
  4138. begin
  4139. { DM: Why not allowed? For example: low(word) results in a type
  4140. id of word.
  4141. error(typeid_here_not_allowed);}
  4142. end;
  4143. procedure firsthnewn(var p : ptree);
  4144. begin
  4145. end;
  4146. procedure firsthdisposen(var p : ptree);
  4147. begin
  4148. firstpass(p^.left);
  4149. if codegenerror then
  4150. exit;
  4151. p^.registers32:=p^.left^.registers32;
  4152. p^.registersfpu:=p^.left^.registersfpu;
  4153. {$ifdef SUPPORT_MMX}
  4154. p^.registersmmx:=p^.left^.registersmmx;
  4155. {$endif SUPPORT_MMX}
  4156. if p^.registers32<1 then
  4157. p^.registers32:=1;
  4158. {
  4159. if p^.left^.location.loc<>LOC_REFERENCE then
  4160. Message(cg_e_illegal_expression);
  4161. }
  4162. p^.location.loc:=LOC_REFERENCE;
  4163. p^.resulttype:=ppointerdef(p^.left^.resulttype)^.definition;
  4164. end;
  4165. procedure firstnewn(var p : ptree);
  4166. begin
  4167. { Standardeinleitung }
  4168. firstpass(p^.left);
  4169. if codegenerror then
  4170. exit;
  4171. p^.registers32:=p^.left^.registers32;
  4172. p^.registersfpu:=p^.left^.registersfpu;
  4173. {$ifdef SUPPORT_MMX}
  4174. p^.registersmmx:=p^.left^.registersmmx;
  4175. {$endif SUPPORT_MMX}
  4176. { result type is already set }
  4177. procinfo.flags:=procinfo.flags or pi_do_call;
  4178. p^.location.loc:=LOC_REGISTER;
  4179. end;
  4180. procedure firstsimplenewdispose(var p : ptree);
  4181. begin
  4182. { this cannot be in a register !! }
  4183. make_not_regable(p^.left);
  4184. firstpass(p^.left);
  4185. { check the type }
  4186. if (p^.left^.resulttype=nil) or (p^.left^.resulttype^.deftype<>pointerdef) then
  4187. Message(type_e_pointer_type_expected);
  4188. if (p^.left^.location.loc<>LOC_REFERENCE) {and
  4189. (p^.left^.location.loc<>LOC_CREGISTER)} then
  4190. Message(cg_e_illegal_expression);
  4191. p^.registers32:=p^.left^.registers32;
  4192. p^.registersfpu:=p^.left^.registersfpu;
  4193. {$ifdef SUPPORT_MMX}
  4194. p^.registersmmx:=p^.left^.registersmmx;
  4195. {$endif SUPPORT_MMX}
  4196. p^.resulttype:=voiddef;
  4197. procinfo.flags:=procinfo.flags or pi_do_call;
  4198. end;
  4199. procedure firstsetele(var p : ptree);
  4200. begin
  4201. firstpass(p^.left);
  4202. if codegenerror then
  4203. exit;
  4204. if assigned(p^.right) then
  4205. begin
  4206. firstpass(p^.right);
  4207. if codegenerror then
  4208. exit;
  4209. end;
  4210. calcregisters(p,0,0,0);
  4211. p^.resulttype:=p^.left^.resulttype;
  4212. set_location(p^.location,p^.left^.location);
  4213. end;
  4214. procedure firstsetcons(var p : ptree);
  4215. begin
  4216. p^.location.loc:=LOC_MEM;
  4217. end;
  4218. procedure firstin(var p : ptree);
  4219. begin
  4220. p^.location.loc:=LOC_FLAGS;
  4221. p^.resulttype:=booldef;
  4222. firstpass(p^.right);
  4223. if codegenerror then
  4224. exit;
  4225. if p^.right^.resulttype^.deftype<>setdef then
  4226. Message(sym_e_set_expected);
  4227. firstpass(p^.left);
  4228. if codegenerror then
  4229. exit;
  4230. p^.left:=gentypeconvnode(p^.left,psetdef(p^.right^.resulttype)^.setof);
  4231. firstpass(p^.left);
  4232. if codegenerror then
  4233. exit;
  4234. left_right_max(p);
  4235. { this is not allways true due to optimization }
  4236. { but if we don't set this we get problems with optimizing self code }
  4237. if psetdef(p^.right^.resulttype)^.settype<>smallset then
  4238. procinfo.flags:=procinfo.flags or pi_do_call
  4239. else
  4240. begin
  4241. { a smallset needs maybe an misc. register }
  4242. if (p^.left^.treetype<>ordconstn) and
  4243. not(p^.right^.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  4244. (p^.right^.registers32<1) then
  4245. inc(p^.registers32);
  4246. end;
  4247. end;
  4248. procedure firststatement(var p : ptree);
  4249. begin
  4250. { left is the next statement in the list }
  4251. p^.resulttype:=voiddef;
  4252. { no temps over several statements }
  4253. cleartempgen;
  4254. { right is the statement itself calln assignn or a complex one }
  4255. firstpass(p^.right);
  4256. if (not (cs_extsyntax in aktmoduleswitches)) and
  4257. assigned(p^.right^.resulttype) and
  4258. (p^.right^.resulttype<>pdef(voiddef)) then
  4259. Message(cg_e_illegal_expression);
  4260. if codegenerror then
  4261. exit;
  4262. p^.registers32:=p^.right^.registers32;
  4263. p^.registersfpu:=p^.right^.registersfpu;
  4264. {$ifdef SUPPORT_MMX}
  4265. p^.registersmmx:=p^.right^.registersmmx;
  4266. {$endif SUPPORT_MMX}
  4267. { left is the next in the list }
  4268. firstpass(p^.left);
  4269. if codegenerror then
  4270. exit;
  4271. if p^.right^.registers32>p^.registers32 then
  4272. p^.registers32:=p^.right^.registers32;
  4273. if p^.right^.registersfpu>p^.registersfpu then
  4274. p^.registersfpu:=p^.right^.registersfpu;
  4275. {$ifdef SUPPORT_MMX}
  4276. if p^.right^.registersmmx>p^.registersmmx then
  4277. p^.registersmmx:=p^.right^.registersmmx;
  4278. {$endif}
  4279. end;
  4280. procedure firstblock(var p : ptree);
  4281. var
  4282. hp : ptree;
  4283. count : longint;
  4284. begin
  4285. count:=0;
  4286. hp:=p^.left;
  4287. while assigned(hp) do
  4288. begin
  4289. if cs_regalloc in aktglobalswitches then
  4290. begin
  4291. { Codeumstellungen }
  4292. { Funktionsresultate an exit anh„ngen }
  4293. { this is wrong for string or other complex
  4294. result types !!! }
  4295. if ret_in_acc(procinfo.retdef) and
  4296. assigned(hp^.left) and
  4297. (hp^.left^.right^.treetype=exitn) and
  4298. (hp^.right^.treetype=assignn) and
  4299. (hp^.right^.left^.treetype=funcretn) then
  4300. begin
  4301. if assigned(hp^.left^.right^.left) then
  4302. Message(cg_n_inefficient_code)
  4303. else
  4304. begin
  4305. hp^.left^.right^.left:=getcopy(hp^.right^.right);
  4306. disposetree(hp^.right);
  4307. hp^.right:=nil;
  4308. end;
  4309. end
  4310. { warning if unreachable code occurs and elimate this }
  4311. else if (hp^.right^.treetype in
  4312. [exitn,breakn,continuen,goton]) and
  4313. assigned(hp^.left) and
  4314. (hp^.left^.treetype<>labeln) then
  4315. begin
  4316. { use correct line number }
  4317. aktfilepos:=hp^.left^.fileinfo;
  4318. disposetree(hp^.left);
  4319. hp^.left:=nil;
  4320. Message(cg_w_unreachable_code);
  4321. { old lines }
  4322. aktfilepos:=hp^.right^.fileinfo;
  4323. end;
  4324. end;
  4325. if assigned(hp^.right) then
  4326. begin
  4327. cleartempgen;
  4328. firstpass(hp^.right);
  4329. if (not (cs_extsyntax in aktmoduleswitches)) and
  4330. assigned(hp^.right^.resulttype) and
  4331. (hp^.right^.resulttype<>pdef(voiddef)) then
  4332. Message(cg_e_illegal_expression);
  4333. if codegenerror then
  4334. exit;
  4335. hp^.registers32:=hp^.right^.registers32;
  4336. hp^.registersfpu:=hp^.right^.registersfpu;
  4337. {$ifdef SUPPORT_MMX}
  4338. hp^.registersmmx:=hp^.right^.registersmmx;
  4339. {$endif SUPPORT_MMX}
  4340. end
  4341. else
  4342. hp^.registers32:=0;
  4343. if hp^.registers32>p^.registers32 then
  4344. p^.registers32:=hp^.registers32;
  4345. if hp^.registersfpu>p^.registersfpu then
  4346. p^.registersfpu:=hp^.registersfpu;
  4347. {$ifdef SUPPORT_MMX}
  4348. if hp^.registersmmx>p^.registersmmx then
  4349. p^.registersmmx:=hp^.registersmmx;
  4350. {$endif}
  4351. inc(count);
  4352. hp:=hp^.left;
  4353. end;
  4354. { p^.registers32:=round(p^.registers32/count); }
  4355. end;
  4356. procedure first_while_repeat(var p : ptree);
  4357. var
  4358. old_t_times : longint;
  4359. begin
  4360. old_t_times:=t_times;
  4361. { Registergewichtung bestimmen }
  4362. if not(cs_littlesize in aktglobalswitches ) then
  4363. t_times:=t_times*8;
  4364. cleartempgen;
  4365. must_be_valid:=true;
  4366. firstpass(p^.left);
  4367. if codegenerror then
  4368. exit;
  4369. if not((p^.left^.resulttype^.deftype=orddef) and
  4370. (porddef(p^.left^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit])) then
  4371. begin
  4372. Message(type_e_mismatch);
  4373. exit;
  4374. end;
  4375. p^.registers32:=p^.left^.registers32;
  4376. p^.registersfpu:=p^.left^.registersfpu;
  4377. {$ifdef SUPPORT_MMX}
  4378. p^.registersmmx:=p^.left^.registersmmx;
  4379. {$endif SUPPORT_MMX}
  4380. { loop instruction }
  4381. if assigned(p^.right) then
  4382. begin
  4383. cleartempgen;
  4384. firstpass(p^.right);
  4385. if codegenerror then
  4386. exit;
  4387. if p^.registers32<p^.right^.registers32 then
  4388. p^.registers32:=p^.right^.registers32;
  4389. if p^.registersfpu<p^.right^.registersfpu then
  4390. p^.registersfpu:=p^.right^.registersfpu;
  4391. {$ifdef SUPPORT_MMX}
  4392. if p^.registersmmx<p^.right^.registersmmx then
  4393. p^.registersmmx:=p^.right^.registersmmx;
  4394. {$endif SUPPORT_MMX}
  4395. end;
  4396. t_times:=old_t_times;
  4397. end;
  4398. procedure firstif(var p : ptree);
  4399. var
  4400. old_t_times : longint;
  4401. hp : ptree;
  4402. begin
  4403. old_t_times:=t_times;
  4404. cleartempgen;
  4405. must_be_valid:=true;
  4406. firstpass(p^.left);
  4407. if codegenerror then
  4408. exit;
  4409. if not((p^.left^.resulttype^.deftype=orddef) and
  4410. (porddef(p^.left^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit])) then
  4411. begin
  4412. Message(type_e_mismatch);
  4413. exit;
  4414. end;
  4415. p^.registers32:=p^.left^.registers32;
  4416. p^.registersfpu:=p^.left^.registersfpu;
  4417. {$ifdef SUPPORT_MMX}
  4418. p^.registersmmx:=p^.left^.registersmmx;
  4419. {$endif SUPPORT_MMX}
  4420. { determines registers weigths }
  4421. if not(cs_littlesize in aktglobalswitches) then
  4422. t_times:=t_times div 2;
  4423. if t_times=0 then
  4424. t_times:=1;
  4425. { if path }
  4426. if assigned(p^.right) then
  4427. begin
  4428. cleartempgen;
  4429. firstpass(p^.right);
  4430. if codegenerror then
  4431. exit;
  4432. if p^.registers32<p^.right^.registers32 then
  4433. p^.registers32:=p^.right^.registers32;
  4434. if p^.registersfpu<p^.right^.registersfpu then
  4435. p^.registersfpu:=p^.right^.registersfpu;
  4436. {$ifdef SUPPORT_MMX}
  4437. if p^.registersmmx<p^.right^.registersmmx then
  4438. p^.registersmmx:=p^.right^.registersmmx;
  4439. {$endif SUPPORT_MMX}
  4440. end;
  4441. { else path }
  4442. if assigned(p^.t1) then
  4443. begin
  4444. cleartempgen;
  4445. firstpass(p^.t1);
  4446. if codegenerror then
  4447. exit;
  4448. if p^.registers32<p^.t1^.registers32 then
  4449. p^.registers32:=p^.t1^.registers32;
  4450. if p^.registersfpu<p^.t1^.registersfpu then
  4451. p^.registersfpu:=p^.t1^.registersfpu;
  4452. {$ifdef SUPPORT_MMX}
  4453. if p^.registersmmx<p^.t1^.registersmmx then
  4454. p^.registersmmx:=p^.t1^.registersmmx;
  4455. {$endif SUPPORT_MMX}
  4456. end;
  4457. if p^.left^.treetype=ordconstn then
  4458. begin
  4459. { optimize }
  4460. if p^.left^.value=1 then
  4461. begin
  4462. disposetree(p^.left);
  4463. hp:=p^.right;
  4464. disposetree(p^.t1);
  4465. { we cannot set p to nil !!! }
  4466. if assigned(hp) then
  4467. begin
  4468. putnode(p);
  4469. p:=hp;
  4470. end
  4471. else
  4472. begin
  4473. p^.left:=nil;
  4474. p^.t1:=nil;
  4475. p^.treetype:=nothingn;
  4476. end;
  4477. end
  4478. else
  4479. begin
  4480. disposetree(p^.left);
  4481. hp:=p^.t1;
  4482. disposetree(p^.right);
  4483. { we cannot set p to nil !!! }
  4484. if assigned(hp) then
  4485. begin
  4486. putnode(p);
  4487. p:=hp;
  4488. end
  4489. else
  4490. begin
  4491. p^.left:=nil;
  4492. p^.right:=nil;
  4493. p^.treetype:=nothingn;
  4494. end;
  4495. end;
  4496. end;
  4497. t_times:=old_t_times;
  4498. end;
  4499. procedure firstexitn(var p : ptree);
  4500. begin
  4501. if assigned(p^.left) then
  4502. begin
  4503. firstpass(p^.left);
  4504. p^.registers32:=p^.left^.registers32;
  4505. p^.registersfpu:=p^.left^.registersfpu;
  4506. {$ifdef SUPPORT_MMX}
  4507. p^.registersmmx:=p^.left^.registersmmx;
  4508. {$endif SUPPORT_MMX}
  4509. end;
  4510. end;
  4511. procedure firstfor(var p : ptree);
  4512. var
  4513. old_t_times : longint;
  4514. begin
  4515. { Registergewichtung bestimmen
  4516. (nicht genau), }
  4517. old_t_times:=t_times;
  4518. if not(cs_littlesize in aktglobalswitches) then
  4519. t_times:=t_times*8;
  4520. cleartempgen;
  4521. if assigned(p^.t1) then
  4522. begin
  4523. firstpass(p^.t1);
  4524. if codegenerror then
  4525. exit;
  4526. end;
  4527. p^.registers32:=p^.t1^.registers32;
  4528. p^.registersfpu:=p^.t1^.registersfpu;
  4529. {$ifdef SUPPORT_MMX}
  4530. p^.registersmmx:=p^.left^.registersmmx;
  4531. {$endif SUPPORT_MMX}
  4532. if p^.left^.treetype<>assignn then
  4533. Message(cg_e_illegal_expression);
  4534. { Laufvariable retten }
  4535. p^.t2:=getcopy(p^.left^.left);
  4536. { Check count var }
  4537. if (p^.t2^.treetype<>loadn) then
  4538. Message(cg_e_illegal_count_var);
  4539. if (not(is_ordinal(p^.t2^.resulttype))) then
  4540. Message(type_e_ordinal_expr_expected);
  4541. cleartempgen;
  4542. must_be_valid:=false;
  4543. firstpass(p^.left);
  4544. must_be_valid:=true;
  4545. if p^.left^.registers32>p^.registers32 then
  4546. p^.registers32:=p^.left^.registers32;
  4547. if p^.left^.registersfpu>p^.registersfpu then
  4548. p^.registersfpu:=p^.left^.registersfpu;
  4549. {$ifdef SUPPORT_MMX}
  4550. if p^.left^.registersmmx>p^.registersmmx then
  4551. p^.registersmmx:=p^.left^.registersmmx;
  4552. {$endif SUPPORT_MMX}
  4553. cleartempgen;
  4554. firstpass(p^.t2);
  4555. if p^.t2^.registers32>p^.registers32 then
  4556. p^.registers32:=p^.t2^.registers32;
  4557. if p^.t2^.registersfpu>p^.registersfpu then
  4558. p^.registersfpu:=p^.t2^.registersfpu;
  4559. {$ifdef SUPPORT_MMX}
  4560. if p^.t2^.registersmmx>p^.registersmmx then
  4561. p^.registersmmx:=p^.t2^.registersmmx;
  4562. {$endif SUPPORT_MMX}
  4563. cleartempgen;
  4564. firstpass(p^.right);
  4565. if p^.right^.treetype<>ordconstn then
  4566. begin
  4567. p^.right:=gentypeconvnode(p^.right,p^.t2^.resulttype);
  4568. cleartempgen;
  4569. firstpass(p^.right);
  4570. end;
  4571. if p^.right^.registers32>p^.registers32 then
  4572. p^.registers32:=p^.right^.registers32;
  4573. if p^.right^.registersfpu>p^.registersfpu then
  4574. p^.registersfpu:=p^.right^.registersfpu;
  4575. {$ifdef SUPPORT_MMX}
  4576. if p^.right^.registersmmx>p^.registersmmx then
  4577. p^.registersmmx:=p^.right^.registersmmx;
  4578. {$endif SUPPORT_MMX}
  4579. t_times:=old_t_times;
  4580. end;
  4581. procedure firstasm(var p : ptree);
  4582. begin
  4583. { it's a f... to determine the used registers }
  4584. { should be done by getnode
  4585. I think also, that all value_str should be set to their maximum (FK)
  4586. p^.registers32:=0;
  4587. p^.registersfpu:=0;
  4588. p^.registersmmx:=0;
  4589. }
  4590. procinfo.flags:=procinfo.flags or pi_uses_asm;
  4591. end;
  4592. procedure firstgoto(var p : ptree);
  4593. begin
  4594. {
  4595. p^.registers32:=0;
  4596. p^.registersfpu:=0;
  4597. }
  4598. p^.resulttype:=voiddef;
  4599. end;
  4600. procedure firstlabel(var p : ptree);
  4601. begin
  4602. cleartempgen;
  4603. firstpass(p^.left);
  4604. p^.registers32:=p^.left^.registers32;
  4605. p^.registersfpu:=p^.left^.registersfpu;
  4606. {$ifdef SUPPORT_MMX}
  4607. p^.registersmmx:=p^.left^.registersmmx;
  4608. {$endif SUPPORT_MMX}
  4609. p^.resulttype:=voiddef;
  4610. end;
  4611. procedure firstcase(var p : ptree);
  4612. var
  4613. old_t_times : longint;
  4614. hp : ptree;
  4615. begin
  4616. { evalutes the case expression }
  4617. cleartempgen;
  4618. must_be_valid:=true;
  4619. firstpass(p^.left);
  4620. if codegenerror then
  4621. exit;
  4622. p^.registers32:=p^.left^.registers32;
  4623. p^.registersfpu:=p^.left^.registersfpu;
  4624. {$ifdef SUPPORT_MMX}
  4625. p^.registersmmx:=p^.left^.registersmmx;
  4626. {$endif SUPPORT_MMX}
  4627. { walk through all instructions }
  4628. { estimates the repeat of each instruction }
  4629. old_t_times:=t_times;
  4630. if not(cs_littlesize in aktglobalswitches) then
  4631. begin
  4632. t_times:=t_times div case_count_labels(p^.nodes);
  4633. if t_times<1 then
  4634. t_times:=1;
  4635. end;
  4636. { first case }
  4637. hp:=p^.right;
  4638. while assigned(hp) do
  4639. begin
  4640. cleartempgen;
  4641. firstpass(hp^.right);
  4642. { searchs max registers }
  4643. if hp^.right^.registers32>p^.registers32 then
  4644. p^.registers32:=hp^.right^.registers32;
  4645. if hp^.right^.registersfpu>p^.registersfpu then
  4646. p^.registersfpu:=hp^.right^.registersfpu;
  4647. {$ifdef SUPPORT_MMX}
  4648. if hp^.right^.registersmmx>p^.registersmmx then
  4649. p^.registersmmx:=hp^.right^.registersmmx;
  4650. {$endif SUPPORT_MMX}
  4651. hp:=hp^.left;
  4652. end;
  4653. { may be handle else tree }
  4654. if assigned(p^.elseblock) then
  4655. begin
  4656. cleartempgen;
  4657. firstpass(p^.elseblock);
  4658. if codegenerror then
  4659. exit;
  4660. if p^.registers32<p^.elseblock^.registers32 then
  4661. p^.registers32:=p^.elseblock^.registers32;
  4662. if p^.registersfpu<p^.elseblock^.registersfpu then
  4663. p^.registersfpu:=p^.elseblock^.registersfpu;
  4664. {$ifdef SUPPORT_MMX}
  4665. if p^.registersmmx<p^.elseblock^.registersmmx then
  4666. p^.registersmmx:=p^.elseblock^.registersmmx;
  4667. {$endif SUPPORT_MMX}
  4668. end;
  4669. t_times:=old_t_times;
  4670. { there is one register required for the case expression }
  4671. if p^.registers32<1 then p^.registers32:=1;
  4672. end;
  4673. procedure firsttryexcept(var p : ptree);
  4674. begin
  4675. cleartempgen;
  4676. firstpass(p^.left);
  4677. { on statements }
  4678. if assigned(p^.right) then
  4679. begin
  4680. cleartempgen;
  4681. firstpass(p^.right);
  4682. p^.registers32:=max(p^.registers32,p^.right^.registers32);
  4683. p^.registersfpu:=max(p^.registersfpu,p^.right^.registersfpu);
  4684. {$ifdef SUPPORT_MMX}
  4685. p^.registersmmx:=max(p^.registersmmx,p^.right^.registersmmx);
  4686. {$endif SUPPORT_MMX}
  4687. end;
  4688. { else block }
  4689. if assigned(p^.t1) then
  4690. begin
  4691. firstpass(p^.t1);
  4692. p^.registers32:=max(p^.registers32,p^.t1^.registers32);
  4693. p^.registersfpu:=max(p^.registersfpu,p^.t1^.registersfpu);
  4694. {$ifdef SUPPORT_MMX}
  4695. p^.registersmmx:=max(p^.registersmmx,p^.t1^.registersmmx);
  4696. {$endif SUPPORT_MMX}
  4697. end;
  4698. end;
  4699. procedure firsttryfinally(var p : ptree);
  4700. begin
  4701. p^.resulttype:=voiddef;
  4702. cleartempgen;
  4703. must_be_valid:=true;
  4704. firstpass(p^.left);
  4705. cleartempgen;
  4706. must_be_valid:=true;
  4707. firstpass(p^.right);
  4708. if codegenerror then
  4709. exit;
  4710. left_right_max(p);
  4711. end;
  4712. procedure firstis(var p : ptree);
  4713. begin
  4714. firstpass(p^.left);
  4715. firstpass(p^.right);
  4716. if (p^.right^.resulttype^.deftype<>classrefdef) then
  4717. Message(type_e_mismatch);
  4718. if codegenerror then
  4719. exit;
  4720. left_right_max(p);
  4721. { left must be a class }
  4722. if (p^.left^.resulttype^.deftype<>objectdef) or
  4723. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  4724. Message(type_e_mismatch);
  4725. { the operands must be related }
  4726. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  4727. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  4728. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  4729. pobjectdef(p^.left^.resulttype)))) then
  4730. Message(type_e_mismatch);
  4731. p^.location.loc:=LOC_FLAGS;
  4732. p^.resulttype:=booldef;
  4733. end;
  4734. procedure firstas(var p : ptree);
  4735. begin
  4736. firstpass(p^.right);
  4737. firstpass(p^.left);
  4738. if (p^.right^.resulttype^.deftype<>classrefdef) then
  4739. Message(type_e_mismatch);
  4740. if codegenerror then
  4741. exit;
  4742. left_right_max(p);
  4743. { left must be a class }
  4744. if (p^.left^.resulttype^.deftype<>objectdef) or
  4745. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  4746. Message(type_e_mismatch);
  4747. { the operands must be related }
  4748. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  4749. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  4750. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  4751. pobjectdef(p^.left^.resulttype)))) then
  4752. Message(type_e_mismatch);
  4753. p^.location:=p^.left^.location;
  4754. p^.resulttype:=pclassrefdef(p^.right^.resulttype)^.definition;
  4755. end;
  4756. procedure firstloadvmt(var p : ptree);
  4757. begin
  4758. { resulttype must be set !
  4759. p^.registersfpu:=0;
  4760. }
  4761. p^.registers32:=1;
  4762. p^.location.loc:=LOC_REGISTER;
  4763. end;
  4764. procedure firstraise(var p : ptree);
  4765. begin
  4766. p^.resulttype:=voiddef;
  4767. {
  4768. p^.registersfpu:=0;
  4769. p^.registers32:=0;
  4770. }
  4771. if assigned(p^.left) then
  4772. begin
  4773. firstpass(p^.left);
  4774. { this must be a _class_ }
  4775. if (p^.left^.resulttype^.deftype<>objectdef) or
  4776. ((pobjectdef(p^.left^.resulttype)^.options and oois_class)=0) then
  4777. Message(type_e_mismatch);
  4778. p^.registersfpu:=p^.left^.registersfpu;
  4779. p^.registers32:=p^.left^.registers32;
  4780. {$ifdef SUPPORT_MMX}
  4781. p^.registersmmx:=p^.left^.registersmmx;
  4782. {$endif SUPPORT_MMX}
  4783. if assigned(p^.right) then
  4784. begin
  4785. firstpass(p^.right);
  4786. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  4787. firstpass(p^.right);
  4788. left_right_max(p);
  4789. end;
  4790. end;
  4791. end;
  4792. procedure firstwith(var p : ptree);
  4793. begin
  4794. if assigned(p^.left) and assigned(p^.right) then
  4795. begin
  4796. firstpass(p^.left);
  4797. if codegenerror then
  4798. exit;
  4799. firstpass(p^.right);
  4800. if codegenerror then
  4801. exit;
  4802. left_right_max(p);
  4803. p^.resulttype:=voiddef;
  4804. end
  4805. else
  4806. begin
  4807. { optimization }
  4808. disposetree(p);
  4809. p:=nil;
  4810. end;
  4811. end;
  4812. procedure firstonn(var p : ptree);
  4813. begin
  4814. { that's really an example procedure for a firstpass :) }
  4815. cleartempgen;
  4816. p^.resulttype:=voiddef;
  4817. p^.registers32:=0;
  4818. p^.registersfpu:=0;
  4819. {$ifdef SUPPORT_MMX}
  4820. p^.registersmmx:=0;
  4821. {$endif SUPPORT_MMX}
  4822. if assigned(p^.left) then
  4823. begin
  4824. firstpass(p^.left);
  4825. p^.registers32:=p^.left^.registers32;
  4826. p^.registersfpu:=p^.left^.registersfpu;
  4827. {$ifdef SUPPORT_MMX}
  4828. p^.registersmmx:=p^.left^.registersmmx;
  4829. {$endif SUPPORT_MMX}
  4830. end;
  4831. cleartempgen;
  4832. if assigned(p^.right) then
  4833. begin
  4834. firstpass(p^.right);
  4835. p^.registers32:=max(p^.registers32,p^.right^.registers32);
  4836. p^.registersfpu:=max(p^.registersfpu,p^.right^.registersfpu);
  4837. {$ifdef SUPPORT_MMX}
  4838. p^.registersmmx:=max(p^.registersmmx,p^.right^.registersmmx);
  4839. {$endif SUPPORT_MMX}
  4840. end;
  4841. end;
  4842. procedure firstprocinline(var p : ptree);
  4843. begin
  4844. {left contains the code in tree form }
  4845. { but it has already been firstpassed }
  4846. { so firstpass(p^.left); does not seem required }
  4847. { might be required later if we change the arg handling !! }
  4848. end;
  4849. type
  4850. firstpassproc = procedure(var p : ptree);
  4851. procedure firstpass(var p : ptree);
  4852. (* ttreetyp = (addn, {Represents the + operator.}
  4853. muln, {Represents the * operator.}
  4854. subn, {Represents the - operator.}
  4855. divn, {Represents the div operator.}
  4856. symdifn, {Represents the >< operator.}
  4857. modn, {Represents the mod operator.}
  4858. assignn, {Represents an assignment.}
  4859. loadn, {Represents the use of a variabele.}
  4860. rangen, {Represents a range (i.e. 0..9).}
  4861. ltn, {Represents the < operator.}
  4862. lten, {Represents the <= operator.}
  4863. gtn, {Represents the > operator.}
  4864. gten, {Represents the >= operator.}
  4865. equaln, {Represents the = operator.}
  4866. unequaln, {Represents the <> operator.}
  4867. inn, {Represents the in operator.}
  4868. orn, {Represents the or operator.}
  4869. xorn, {Represents the xor operator.}
  4870. shrn, {Represents the shr operator.}
  4871. shln, {Represents the shl operator.}
  4872. slashn, {Represents the / operator.}
  4873. andn, {Represents the and operator.}
  4874. subscriptn, {??? Field in a record/object?}
  4875. derefn, {Dereferences a pointer.}
  4876. addrn, {Represents the @ operator.}
  4877. doubleaddrn, {Represents the @@ operator.}
  4878. ordconstn, {Represents an ordinal value.}
  4879. typeconvn, {Represents type-conversion/typecast.}
  4880. calln, {Represents a call node.}
  4881. callparan, {Represents a parameter.}
  4882. realconstn, {Represents a real value.}
  4883. fixconstn, {Represents a fixed value.}
  4884. umminusn, {Represents a sign change (i.e. -2).}
  4885. asmn, {Represents an assembler node }
  4886. vecn, {Represents array indexing.}
  4887. stringconstn, {Represents a string constant.}
  4888. funcretn, {Represents the function result var.}
  4889. selfn, {Represents the self parameter.}
  4890. notn, {Represents the not operator.}
  4891. inlinen, {Internal procedures (i.e. writeln).}
  4892. niln, {Represents the nil pointer.}
  4893. errorn, {This part of the tree could not be
  4894. parsed because of a compiler error.}
  4895. typen, {A type name. Used for i.e. typeof(obj).}
  4896. hnewn, {The new operation, constructor call.}
  4897. hdisposen, {The dispose operation with destructor call.}
  4898. newn, {The new operation, constructor call.}
  4899. simpledisposen, {The dispose operation.}
  4900. setelen, {A set element (i.e. [a,b]).}
  4901. setconstrn, {A set constant (i.e. [1,2]).}
  4902. blockn, {A block of statements.}
  4903. statementn, {One statement in list of nodes.}
  4904. loopn, { used in genloopnode, must be converted }
  4905. ifn, {An if statement.}
  4906. breakn, {A break statement.}
  4907. continuen, {A continue statement.}
  4908. repeatn, {A repeat until block.}
  4909. whilen, {A while do statement.}
  4910. forn, {A for loop.}
  4911. exitn, {An exit statement.}
  4912. withn, {A with statement.}
  4913. casen, {A case statement.}
  4914. labeln, {A label.}
  4915. goton, {A goto statement.}
  4916. simplenewn, {The new operation.}
  4917. tryexceptn, {A try except block.}
  4918. raisen, {A raise statement.}
  4919. switchesn, {??? Currently unused...}
  4920. tryfinallyn, {A try finally statement.}
  4921. isn, {Represents the is operator.}
  4922. asn, {Represents the as typecast.}
  4923. caretn, {Represents the ^ operator.}
  4924. failn, {Represents the fail statement.}
  4925. starstarn, {Represents the ** operator exponentiation }
  4926. procinlinen, {Procedures that can be inlined }
  4927. { added for optimizations where we cannot suppress }
  4928. nothingn,
  4929. loadvmtn); {???.} *)
  4930. const
  4931. procedures : array[ttreetyp] of firstpassproc =
  4932. (firstadd,firstadd,firstadd,firstmoddiv,firstadd,
  4933. firstmoddiv,firstassignment,firstload,firstrange,
  4934. firstadd,firstadd,firstadd,firstadd,
  4935. firstadd,firstadd,firstin,firstadd,
  4936. firstadd,firstshlshr,firstshlshr,firstadd,
  4937. firstadd,firstsubscriptn,firstderef,firstaddr,firstdoubleaddr,
  4938. firstordconst,firsttypeconv,firstcalln,firstnothing,
  4939. firstrealconst,firstfixconst,firstumminus,firstasm,firstvecn,
  4940. firststringconst,firstfuncret,firstselfn,
  4941. firstnot,firstinline,firstniln,firsterror,
  4942. firsttypen,firsthnewn,firsthdisposen,firstnewn,
  4943. firstsimplenewdispose,firstsetele,firstsetcons,firstblock,
  4944. firststatement,firstnothing,firstif,firstnothing,
  4945. firstnothing,first_while_repeat,first_while_repeat,firstfor,
  4946. firstexitn,firstwith,firstcase,firstlabel,
  4947. firstgoto,firstsimplenewdispose,firsttryexcept,
  4948. firstraise,firstnothing,firsttryfinally,
  4949. firstonn,firstis,firstas,firstadd,
  4950. firstnothing,firstadd,firstprocinline,firstnothing,firstloadvmt);
  4951. var
  4952. oldcodegenerror : boolean;
  4953. oldlocalswitches : tlocalswitches;
  4954. oldpos : tfileposinfo;
  4955. {$ifdef extdebug}
  4956. str1,str2 : string;
  4957. oldp : ptree;
  4958. not_first : boolean;
  4959. {$endif extdebug}
  4960. begin
  4961. {$ifdef extdebug}
  4962. inc(total_of_firstpass);
  4963. if (p^.firstpasscount>0) and only_one_pass then
  4964. exit;
  4965. {$endif extdebug}
  4966. oldcodegenerror:=codegenerror;
  4967. oldpos:=aktfilepos;
  4968. oldlocalswitches:=aktlocalswitches;
  4969. {$ifdef extdebug}
  4970. if p^.firstpasscount>0 then
  4971. begin
  4972. move(p^,str1[1],sizeof(ttree));
  4973. str1[0]:=char(sizeof(ttree));
  4974. new(oldp);
  4975. oldp^:=p^;
  4976. not_first:=true;
  4977. inc(firstpass_several);
  4978. end
  4979. else
  4980. not_first:=false;
  4981. {$endif extdebug}
  4982. aktfilepos:=p^.fileinfo;
  4983. aktlocalswitches:=p^.localswitches;
  4984. if not p^.error then
  4985. begin
  4986. codegenerror:=false;
  4987. procedures[p^.treetype](p);
  4988. p^.error:=codegenerror;
  4989. codegenerror:=codegenerror or oldcodegenerror;
  4990. end
  4991. else
  4992. codegenerror:=true;
  4993. {$ifdef extdebug}
  4994. if not_first then
  4995. begin
  4996. { dirty trick to compare two ttree's (PM) }
  4997. move(p^,str2[1],sizeof(ttree));
  4998. str2[0]:=char(sizeof(ttree));
  4999. if str1<>str2 then
  5000. begin
  5001. comment(v_debug,'tree changed after first counting pass '
  5002. +tostr(longint(p^.treetype)));
  5003. compare_trees(oldp,p);
  5004. end;
  5005. dispose(oldp);
  5006. end;
  5007. if count_ref then
  5008. inc(p^.firstpasscount);
  5009. {$endif extdebug}
  5010. aktlocalswitches:=oldlocalswitches;
  5011. aktfilepos:=oldpos;
  5012. end;
  5013. function do_firstpass(var p : ptree) : boolean;
  5014. begin
  5015. codegenerror:=false;
  5016. firstpass(p);
  5017. do_firstpass:=codegenerror;
  5018. end;
  5019. { to be called only for a whole function }
  5020. { to insert code at entry and exit }
  5021. function function_firstpass(var p : ptree) : boolean;
  5022. begin
  5023. codegenerror:=false;
  5024. firstpass(p);
  5025. function_firstpass:=codegenerror;
  5026. end;
  5027. end.
  5028. {
  5029. $Log$
  5030. Revision 1.80 1998-09-08 14:10:11 pierre
  5031. * typen check in read write
  5032. there are probably other inline function that have the same bug !!
  5033. Revision 1.79 1998/09/08 13:50:17 peter
  5034. * removed last fix becasue it was not 100%
  5035. Revision 1.78 1998/09/08 13:36:24 peter
  5036. + can't write type syms anymore
  5037. Revision 1.77 1998/09/07 22:25:52 peter
  5038. * fixed str(boolean,string) which was allowed
  5039. * fixed write(' ':<int expression>) only constants where allowed :(
  5040. Revision 1.76 1998/09/07 18:46:05 peter
  5041. * update smartlinking, uses getdatalabel
  5042. * renamed ptree.value vars to value_str,value_real,value_set
  5043. Revision 1.75 1998/09/05 23:51:06 florian
  5044. * possible bug with too few registers in first/secondin fixed
  5045. Revision 1.74 1998/09/05 23:04:00 florian
  5046. * some fixes to get -Or work:
  5047. - inc/dec didn't take care of CREGISTER
  5048. - register calculcation of inc/dec was wrong
  5049. - var/const parameters get now assigned 32 bit register, but
  5050. const parameters only if they are passed by reference !
  5051. Revision 1.73 1998/09/05 22:29:57 florian
  5052. + the boolean comparision a=true generates now the same code as only a,
  5053. (a=1 was compiled to cmp 1,a now it is compiled to cmp 0,a)
  5054. Revision 1.72 1998/09/05 22:11:01 florian
  5055. + switch -vb
  5056. * while/repeat loops accept now also word/longbool conditions
  5057. * makebooltojump did an invalid ungetregister32, fixed
  5058. Revision 1.71 1998/09/04 11:55:18 florian
  5059. * problem with -Or fixed
  5060. Revision 1.70 1998/09/04 08:42:00 peter
  5061. * updated some error messages
  5062. Revision 1.69 1998/09/01 17:39:47 peter
  5063. + internal constant functions
  5064. Revision 1.68 1998/09/01 09:02:52 peter
  5065. * moved message() to hcodegen, so pass_2 also uses them
  5066. Revision 1.67 1998/09/01 07:54:20 pierre
  5067. * UseBrowser a little updated (might still be buggy !!)
  5068. * bug in psub.pas in function specifier removed
  5069. * stdcall allowed in interface and in implementation
  5070. (FPC will not yet complain if it is missing in either part
  5071. because stdcall is only a dummy !!)
  5072. Revision 1.66 1998/08/31 08:52:05 peter
  5073. * fixed error 10 with succ() and pref()
  5074. Revision 1.65 1998/08/28 12:51:40 florian
  5075. + ansistring to pchar type cast fixed
  5076. Revision 1.64 1998/08/28 10:54:22 peter
  5077. * fixed smallset generation from elements, it has never worked before!
  5078. Revision 1.63 1998/08/24 10:05:39 florian
  5079. + class types and class reference types are now compatible with void
  5080. pointers
  5081. + class can be stored now registers, even if a type conversation is applied
  5082. Revision 1.62 1998/08/23 16:07:22 florian
  5083. * internalerror with mod/div fixed
  5084. Revision 1.61 1998/08/21 14:08:47 pierre
  5085. + TEST_FUNCRET now default (old code removed)
  5086. works also for m68k (at least compiles)
  5087. Revision 1.60 1998/08/20 12:59:57 peter
  5088. - removed obsolete in_*
  5089. Revision 1.59 1998/08/20 09:26:39 pierre
  5090. + funcret setting in underproc testing
  5091. compile with _dTEST_FUNCRET
  5092. Revision 1.58 1998/08/19 16:07:51 jonas
  5093. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  5094. Revision 1.57 1998/08/19 00:42:39 peter
  5095. + subrange types for enums
  5096. + checking for bounds type with ranges
  5097. Revision 1.56 1998/08/18 09:24:42 pierre
  5098. * small warning position bug fixed
  5099. * support_mmx switches splitting was missing
  5100. * rhide error and warning output corrected
  5101. Revision 1.55 1998/08/14 18:18:44 peter
  5102. + dynamic set contruction
  5103. * smallsets are now working (always longint size)
  5104. Revision 1.54 1998/08/13 11:00:10 peter
  5105. * fixed procedure<>procedure construct
  5106. Revision 1.53 1998/08/12 19:39:28 peter
  5107. * fixed some crashes
  5108. Revision 1.52 1998/08/10 14:50:08 peter
  5109. + localswitches, moduleswitches, globalswitches splitting
  5110. Revision 1.51 1998/08/10 10:18:29 peter
  5111. + Compiler,Comphook unit which are the new interface units to the
  5112. compiler
  5113. Revision 1.50 1998/08/08 21:51:39 peter
  5114. * small crash prevent is firstassignment
  5115. Revision 1.49 1998/07/30 16:07:08 florian
  5116. * try ... expect <statement> end; works now
  5117. Revision 1.48 1998/07/30 13:30:35 florian
  5118. * final implemenation of exception support, maybe it needs
  5119. some fixes :)
  5120. Revision 1.47 1998/07/30 11:18:17 florian
  5121. + first implementation of try ... except on .. do end;
  5122. * limitiation of 65535 bytes parameters for cdecl removed
  5123. Revision 1.46 1998/07/28 21:52:52 florian
  5124. + implementation of raise and try..finally
  5125. + some misc. exception stuff
  5126. Revision 1.45 1998/07/26 21:58:59 florian
  5127. + better support for switch $H
  5128. + index access to ansi strings added
  5129. + assigment of data (records/arrays) containing ansi strings
  5130. Revision 1.44 1998/07/24 22:16:59 florian
  5131. * internal error 10 together with array access fixed. I hope
  5132. that's the final fix.
  5133. Revision 1.43 1998/07/20 18:40:14 florian
  5134. * handling of ansi string constants should now work
  5135. Revision 1.42 1998/07/20 10:23:01 florian
  5136. * better ansi string assignement
  5137. Revision 1.41 1998/07/18 22:54:27 florian
  5138. * some ansi/wide/longstring support fixed:
  5139. o parameter passing
  5140. o returning as result from functions
  5141. Revision 1.40 1998/07/18 17:11:09 florian
  5142. + ansi string constants fixed
  5143. + switch $H partial implemented
  5144. Revision 1.39 1998/07/14 21:46:47 peter
  5145. * updated messages file
  5146. Revision 1.38 1998/07/14 14:46:50 peter
  5147. * released NEWINPUT
  5148. Revision 1.37 1998/07/07 12:31:44 peter
  5149. * fixed string:= which allowed almost any type
  5150. Revision 1.36 1998/07/07 11:20:00 peter
  5151. + NEWINPUT for a better inputfile and scanner object
  5152. Revision 1.35 1998/06/25 14:04:19 peter
  5153. + internal inc/dec
  5154. Revision 1.34 1998/06/25 08:48:14 florian
  5155. * first version of rtti support
  5156. Revision 1.33 1998/06/16 08:56:24 peter
  5157. + targetcpu
  5158. * cleaner pmodules for newppu
  5159. Revision 1.32 1998/06/14 18:23:57 peter
  5160. * fixed xor bug (from mailinglist)
  5161. Revision 1.31 1998/06/13 00:10:09 peter
  5162. * working browser and newppu
  5163. * some small fixes against crashes which occured in bp7 (but not in
  5164. fpc?!)
  5165. Revision 1.30 1998/06/12 10:32:28 pierre
  5166. * column problem hopefully solved
  5167. + C vars declaration changed
  5168. Revision 1.29 1998/06/09 16:01:44 pierre
  5169. + added procedure directive parsing for procvars
  5170. (accepted are popstack cdecl and pascal)
  5171. + added C vars with the following syntax
  5172. var C calias 'true_c_name';(can be followed by external)
  5173. reason is that you must add the Cprefix
  5174. which is target dependent
  5175. Revision 1.28 1998/06/05 14:37:29 pierre
  5176. * fixes for inline for operators
  5177. * inline procedure more correctly restricted
  5178. Revision 1.27 1998/06/05 00:01:06 florian
  5179. * bugs with assigning related objects and passing objects by reference
  5180. to a procedure
  5181. Revision 1.26 1998/06/04 09:55:39 pierre
  5182. * demangled name of procsym reworked to become independant
  5183. of the mangling scheme
  5184. Revision 1.25 1998/06/03 22:48:57 peter
  5185. + wordbool,longbool
  5186. * rename bis,von -> high,low
  5187. * moved some systemunit loading/creating to psystem.pas
  5188. Revision 1.24 1998/06/02 17:03:01 pierre
  5189. * with node corrected for objects
  5190. * small bugs for SUPPORT_MMX fixed
  5191. Revision 1.23 1998/06/01 16:50:20 peter
  5192. + boolean -> ord conversion
  5193. * fixed ord -> boolean conversion
  5194. Revision 1.22 1998/05/28 17:26:49 peter
  5195. * fixed -R switch, it didn't work after my previous akt/init patch
  5196. * fixed bugs 110,130,136
  5197. Revision 1.21 1998/05/25 17:11:41 pierre
  5198. * firstpasscount bug fixed
  5199. now all is already set correctly the first time
  5200. under EXTDEBUG try -gp to skip all other firstpasses
  5201. it works !!
  5202. * small bug fixes
  5203. - for smallsets with -dTESTSMALLSET
  5204. - some warnings removed (by correcting code !)
  5205. Revision 1.20 1998/05/23 01:21:17 peter
  5206. + aktasmmode, aktoptprocessor, aktoutputformat
  5207. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  5208. + $LIBNAME to set the library name where the unit will be put in
  5209. * splitted cgi386 a bit (codeseg to large for bp7)
  5210. * nasm, tasm works again. nasm moved to ag386nsm.pas
  5211. Revision 1.19 1998/05/20 09:42:34 pierre
  5212. + UseTokenInfo now default
  5213. * unit in interface uses and implementation uses gives error now
  5214. * only one error for unknown symbol (uses lastsymknown boolean)
  5215. the problem came from the label code !
  5216. + first inlined procedures and function work
  5217. (warning there might be allowed cases were the result is still wrong !!)
  5218. * UseBrower updated gives a global list of all position of all used symbols
  5219. with switch -gb
  5220. Revision 1.18 1998/05/11 13:07:55 peter
  5221. + $ifdef NEWPPU for the new ppuformat
  5222. + $define GDB not longer required
  5223. * removed all warnings and stripped some log comments
  5224. * no findfirst/findnext anymore to remove smartlink *.o files
  5225. Revision 1.17 1998/05/06 08:38:43 pierre
  5226. * better position info with UseTokenInfo
  5227. UseTokenInfo greatly simplified
  5228. + added check for changed tree after first time firstpass
  5229. (if we could remove all the cases were it happen
  5230. we could skip all firstpass if firstpasscount > 1)
  5231. Only with ExtDebug
  5232. Revision 1.16 1998/05/01 16:38:45 florian
  5233. * handling of private and protected fixed
  5234. + change_keywords_to_tp implemented to remove
  5235. keywords which aren't supported by tp
  5236. * break and continue are now symbols of the system unit
  5237. + widestring, longstring and ansistring type released
  5238. Revision 1.15 1998/05/01 09:01:23 florian
  5239. + correct semantics of private and protected
  5240. * small fix in variable scope:
  5241. a id can be used in a parameter list of a method, even it is used in
  5242. an anchestor class as field id
  5243. Revision 1.14 1998/04/30 15:59:41 pierre
  5244. * GDB works again better :
  5245. correct type info in one pass
  5246. + UseTokenInfo for better source position
  5247. * fixed one remaining bug in scanner for line counts
  5248. * several little fixes
  5249. Revision 1.13 1998/04/29 10:33:56 pierre
  5250. + added some code for ansistring (not complete nor working yet)
  5251. * corrected operator overloading
  5252. * corrected nasm output
  5253. + started inline procedures
  5254. + added starstarn : use ** for exponentiation (^ gave problems)
  5255. + started UseTokenInfo cond to get accurate positions
  5256. Revision 1.12 1998/04/22 21:06:50 florian
  5257. * last fixes before the release:
  5258. - veryyyy slow firstcall fixed
  5259. Revision 1.11 1998/04/21 10:16:48 peter
  5260. * patches from strasbourg
  5261. * objects is not used anymore in the fpc compiled version
  5262. Revision 1.10 1998/04/14 23:27:03 florian
  5263. + exclude/include with constant second parameter added
  5264. Revision 1.9 1998/04/13 21:15:42 florian
  5265. * error handling of pass_1 and cgi386 fixed
  5266. * the following bugs fixed: 0117, 0118, 0119 and 0129, 0122 was already
  5267. fixed, verified
  5268. Revision 1.8 1998/04/13 08:42:52 florian
  5269. * call by reference and call by value open arrays fixed
  5270. Revision 1.7 1998/04/12 22:39:44 florian
  5271. * problem with read access to properties solved
  5272. * correct handling of hidding methods via virtual (COM)
  5273. * correct result type of constructor calls (COM), the resulttype
  5274. depends now on the type of the class reference
  5275. Revision 1.6 1998/04/09 22:16:34 florian
  5276. * problem with previous REGALLOC solved
  5277. * improved property support
  5278. Revision 1.5 1998/04/08 16:58:04 pierre
  5279. * several bugfixes
  5280. ADD ADC and AND are also sign extended
  5281. nasm output OK (program still crashes at end
  5282. and creates wrong assembler files !!)
  5283. procsym types sym in tdef removed !!
  5284. Revision 1.4 1998/04/07 22:45:04 florian
  5285. * bug0092, bug0115 and bug0121 fixed
  5286. + packed object/class/array
  5287. }