nasm-pp.c 168 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317
  1. /* -*- mode: c; c-file-style: "bsd" -*- */
  2. /* preproc.c macro preprocessor for the Netwide Assembler
  3. *
  4. * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
  5. * Julian Hall. All rights reserved. The software is
  6. * redistributable under the licence given in the file "Licence"
  7. * distributed in the NASM archive.
  8. *
  9. * initial version 18/iii/97 by Simon Tatham
  10. */
  11. /* Typical flow of text through preproc
  12. *
  13. * pp_getline gets tokenised lines, either
  14. *
  15. * from a macro expansion
  16. *
  17. * or
  18. * {
  19. * read_line gets raw text from stdmacpos, or predef, or current input file
  20. * tokenise converts to tokens
  21. * }
  22. *
  23. * expand_mmac_params is used to expand %1 etc., unless a macro is being
  24. * defined or a false conditional is being processed
  25. * (%0, %1, %+1, %-1, %%foo
  26. *
  27. * do_directive checks for directives
  28. *
  29. * expand_smacro is used to expand single line macros
  30. *
  31. * expand_mmacro is used to expand multi-line macros
  32. *
  33. * detoken is used to convert the line back to text
  34. */
  35. #include <util.h>
  36. #include <libyasm-stdint.h>
  37. #include <libyasm/coretype.h>
  38. #include <libyasm/intnum.h>
  39. #include <libyasm/expr.h>
  40. #include <libyasm/file.h>
  41. #include <stdarg.h>
  42. #include <ctype.h>
  43. #include <limits.h>
  44. #include "nasm.h"
  45. #include "nasmlib.h"
  46. #include "nasm-pp.h"
  47. typedef struct SMacro SMacro;
  48. typedef struct MMacro MMacro;
  49. typedef struct Context Context;
  50. typedef struct Token Token;
  51. typedef struct Blocks Blocks;
  52. typedef struct Line Line;
  53. typedef struct Include Include;
  54. typedef struct Cond Cond;
  55. /*
  56. * Store the definition of a single-line macro.
  57. */
  58. struct SMacro
  59. {
  60. SMacro *next;
  61. char *name;
  62. int level;
  63. int casesense;
  64. int nparam;
  65. int in_progress;
  66. Token *expansion;
  67. };
  68. /*
  69. * Store the definition of a multi-line macro. This is also used to
  70. * store the interiors of `%rep...%endrep' blocks, which are
  71. * effectively self-re-invoking multi-line macros which simply
  72. * don't have a name or bother to appear in the hash tables. %rep
  73. * blocks are signified by having a NULL `name' field.
  74. *
  75. * In a MMacro describing a `%rep' block, the `in_progress' field
  76. * isn't merely boolean, but gives the number of repeats left to
  77. * run.
  78. *
  79. * The `next' field is used for storing MMacros in hash tables; the
  80. * `next_active' field is for stacking them on istk entries.
  81. *
  82. * When a MMacro is being expanded, `params', `iline', `nparam',
  83. * `paramlen', `rotate' and `unique' are local to the invocation.
  84. */
  85. struct MMacro
  86. {
  87. MMacro *next;
  88. char *name;
  89. int casesense;
  90. long nparam_min, nparam_max;
  91. int plus; /* is the last parameter greedy? */
  92. int nolist; /* is this macro listing-inhibited? */
  93. int in_progress;
  94. Token *dlist; /* All defaults as one list */
  95. Token **defaults; /* Parameter default pointers */
  96. int ndefs; /* number of default parameters */
  97. Line *expansion;
  98. MMacro *next_active;
  99. MMacro *rep_nest; /* used for nesting %rep */
  100. Token **params; /* actual parameters */
  101. Token *iline; /* invocation line */
  102. long nparam, rotate, *paramlen;
  103. unsigned long unique;
  104. int lineno; /* Current line number on expansion */
  105. };
  106. /*
  107. * The context stack is composed of a linked list of these.
  108. */
  109. struct Context
  110. {
  111. Context *next;
  112. SMacro *localmac;
  113. char *name;
  114. unsigned long number;
  115. };
  116. /*
  117. * This is the internal form which we break input lines up into.
  118. * Typically stored in linked lists.
  119. *
  120. * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
  121. * necessarily used as-is, but is intended to denote the number of
  122. * the substituted parameter. So in the definition
  123. *
  124. * %define a(x,y) ( (x) & ~(y) )
  125. *
  126. * the token representing `x' will have its type changed to
  127. * TOK_SMAC_PARAM, but the one representing `y' will be
  128. * TOK_SMAC_PARAM+1.
  129. *
  130. * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
  131. * which doesn't need quotes around it. Used in the pre-include
  132. * mechanism as an alternative to trying to find a sensible type of
  133. * quote to use on the filename we were passed.
  134. */
  135. struct Token
  136. {
  137. Token *next;
  138. char *text;
  139. SMacro *mac; /* associated macro for TOK_SMAC_END */
  140. int type;
  141. };
  142. enum
  143. {
  144. TOK_WHITESPACE = 1, TOK_COMMENT, TOK_ID, TOK_PREPROC_ID, TOK_STRING,
  145. TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
  146. TOK_INTERNAL_STRING
  147. };
  148. /*
  149. * Multi-line macro definitions are stored as a linked list of
  150. * these, which is essentially a container to allow several linked
  151. * lists of Tokens.
  152. *
  153. * Note that in this module, linked lists are treated as stacks
  154. * wherever possible. For this reason, Lines are _pushed_ on to the
  155. * `expansion' field in MMacro structures, so that the linked list,
  156. * if walked, would give the macro lines in reverse order; this
  157. * means that we can walk the list when expanding a macro, and thus
  158. * push the lines on to the `expansion' field in _istk_ in reverse
  159. * order (so that when popped back off they are in the right
  160. * order). It may seem cockeyed, and it relies on my design having
  161. * an even number of steps in, but it works...
  162. *
  163. * Some of these structures, rather than being actual lines, are
  164. * markers delimiting the end of the expansion of a given macro.
  165. * This is for use in the cycle-tracking and %rep-handling code.
  166. * Such structures have `finishes' non-NULL, and `first' NULL. All
  167. * others have `finishes' NULL, but `first' may still be NULL if
  168. * the line is blank.
  169. */
  170. struct Line
  171. {
  172. Line *next;
  173. MMacro *finishes;
  174. Token *first;
  175. };
  176. /*
  177. * To handle an arbitrary level of file inclusion, we maintain a
  178. * stack (ie linked list) of these things.
  179. */
  180. struct Include
  181. {
  182. Include *next;
  183. FILE *fp;
  184. Cond *conds;
  185. Line *expansion;
  186. char *fname;
  187. int lineno, lineinc;
  188. MMacro *mstk; /* stack of active macros/reps */
  189. };
  190. /*
  191. * Conditional assembly: we maintain a separate stack of these for
  192. * each level of file inclusion. (The only reason we keep the
  193. * stacks separate is to ensure that a stray `%endif' in a file
  194. * included from within the true branch of a `%if' won't terminate
  195. * it and cause confusion: instead, rightly, it'll cause an error.)
  196. */
  197. struct Cond
  198. {
  199. Cond *next;
  200. int state;
  201. };
  202. enum
  203. {
  204. /*
  205. * These states are for use just after %if or %elif: IF_TRUE
  206. * means the condition has evaluated to truth so we are
  207. * currently emitting, whereas IF_FALSE means we are not
  208. * currently emitting but will start doing so if a %else comes
  209. * up. In these states, all directives are admissible: %elif,
  210. * %else and %endif. (And of course %if.)
  211. */
  212. COND_IF_TRUE, COND_IF_FALSE,
  213. /*
  214. * These states come up after a %else: ELSE_TRUE means we're
  215. * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
  216. * any %elif or %else will cause an error.
  217. */
  218. COND_ELSE_TRUE, COND_ELSE_FALSE,
  219. /*
  220. * This state means that we're not emitting now, and also that
  221. * nothing until %endif will be emitted at all. It's for use in
  222. * two circumstances: (i) when we've had our moment of emission
  223. * and have now started seeing %elifs, and (ii) when the
  224. * condition construct in question is contained within a
  225. * non-emitting branch of a larger condition construct.
  226. */
  227. COND_NEVER
  228. };
  229. #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
  230. /*
  231. * These defines are used as the possible return values for do_directive
  232. */
  233. #define NO_DIRECTIVE_FOUND 0
  234. #define DIRECTIVE_FOUND 1
  235. /*
  236. * Condition codes. Note that we use c_ prefix not C_ because C_ is
  237. * used in nasm.h for the "real" condition codes. At _this_ level,
  238. * we treat CXZ and ECXZ as condition codes, albeit non-invertible
  239. * ones, so we need a different enum...
  240. */
  241. static const char *conditions[] = {
  242. "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
  243. "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
  244. "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
  245. };
  246. enum
  247. {
  248. c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
  249. c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
  250. c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_S, c_Z
  251. };
  252. static int inverse_ccs[] = {
  253. c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
  254. c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
  255. c_Z, c_NO, c_NP, c_PO, c_PE, c_NS, c_NZ
  256. };
  257. /*
  258. * Directive names.
  259. */
  260. static const char *directives[] = {
  261. "%arg",
  262. "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
  263. "%elifid", "%elifidn", "%elifidni", "%elifmacro", "%elifnctx", "%elifndef",
  264. "%elifnid", "%elifnidn", "%elifnidni", "%elifnmacro", "%elifnnum", "%elifnstr",
  265. "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
  266. "%endrep", "%endscope", "%error", "%exitrep", "%iassign", "%idefine", "%if",
  267. "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifmacro", "%ifnctx",
  268. "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnmacro", "%ifnnum",
  269. "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include",
  270. "%ixdefine", "%line",
  271. "%local",
  272. "%macro", "%pop", "%push", "%rep", "%repl", "%rotate",
  273. "%scope", "%stacksize",
  274. "%strlen", "%substr", "%undef", "%xdefine"
  275. };
  276. enum
  277. {
  278. PP_ARG,
  279. PP_ASSIGN, PP_CLEAR, PP_DEFINE, PP_ELIF, PP_ELIFCTX, PP_ELIFDEF,
  280. PP_ELIFID, PP_ELIFIDN, PP_ELIFIDNI, PP_ELIFMACRO, PP_ELIFNCTX, PP_ELIFNDEF,
  281. PP_ELIFNID, PP_ELIFNIDN, PP_ELIFNIDNI, PP_ELIFNMACRO, PP_ELIFNNUM, PP_ELIFNSTR,
  282. PP_ELIFNUM, PP_ELIFSTR, PP_ELSE, PP_ENDIF, PP_ENDM, PP_ENDMACRO,
  283. PP_ENDREP, PP_ENDSCOPE, PP_ERROR, PP_EXITREP, PP_IASSIGN, PP_IDEFINE, PP_IF,
  284. PP_IFCTX, PP_IFDEF, PP_IFID, PP_IFIDN, PP_IFIDNI, PP_IFMACRO, PP_IFNCTX,
  285. PP_IFNDEF, PP_IFNID, PP_IFNIDN, PP_IFNIDNI, PP_IFNMACRO, PP_IFNNUM,
  286. PP_IFNSTR, PP_IFNUM, PP_IFSTR, PP_IMACRO, PP_INCLUDE,
  287. PP_IXDEFINE, PP_LINE,
  288. PP_LOCAL,
  289. PP_MACRO, PP_POP, PP_PUSH, PP_REP, PP_REPL, PP_ROTATE,
  290. PP_SCOPE, PP_STACKSIZE,
  291. PP_STRLEN, PP_SUBSTR, PP_UNDEF, PP_XDEFINE
  292. };
  293. /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
  294. static int is_condition(int arg)
  295. {
  296. return ((arg >= PP_ELIF) && (arg <= PP_ENDIF)) ||
  297. ((arg >= PP_IF) && (arg <= PP_IFSTR));
  298. }
  299. /* For TASM compatibility we need to be able to recognise TASM compatible
  300. * conditional compilation directives. Using the NASM pre-processor does
  301. * not work, so we look for them specifically from the following list and
  302. * then jam in the equivalent NASM directive into the input stream.
  303. */
  304. #ifndef MAX
  305. # define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
  306. #endif
  307. enum
  308. {
  309. TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
  310. TM_IFNDEF, TM_INCLUDE, TM_LOCAL,
  311. TM_REPT, TM_IRP, TM_MACRO,
  312. TM_STRUC, TM_SEGMENT
  313. };
  314. static const char *tasm_directives[] = {
  315. "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
  316. "ifndef", "include", "local"
  317. };
  318. static int StackSize = 4;
  319. static const char *StackPointer = "ebp";
  320. static int ArgOffset = 8;
  321. static int LocalOffset = 4;
  322. static int Level = 0;
  323. static Context *cstk;
  324. static Include *istk;
  325. static FILE *first_fp = NULL;
  326. static efunc _error; /* Pointer to client-provided error reporting function */
  327. static evalfunc evaluate;
  328. static int pass; /* HACK: pass 0 = generate dependencies only */
  329. static unsigned long unique; /* unique identifier numbers */
  330. static Line *builtindef = NULL;
  331. static Line *stddef = NULL;
  332. static Line *predef = NULL;
  333. static int first_line = 1;
  334. static ListGen *list;
  335. /*
  336. * The number of hash values we use for the macro lookup tables.
  337. * FIXME: We should *really* be able to configure this at run time,
  338. * or even have the hash table automatically expanding when necessary.
  339. */
  340. #define NHASH 4096
  341. /*
  342. * The current set of multi-line macros we have defined.
  343. */
  344. static MMacro *mmacros[NHASH];
  345. /*
  346. * The current set of single-line macros we have defined.
  347. */
  348. static SMacro *smacros[NHASH];
  349. /*
  350. * The multi-line macro we are currently defining, or the %rep
  351. * block we are currently reading, if any.
  352. */
  353. static MMacro *defining;
  354. /*
  355. * The number of macro parameters to allocate space for at a time.
  356. */
  357. #define PARAM_DELTA 16
  358. /*
  359. * Macros to make NASM ignore some TASM directives before the first include
  360. * directive.
  361. */
  362. static const char *tasm_compat_macros[] =
  363. {
  364. "%idefine IDEAL",
  365. "%idefine JUMPS",
  366. "%idefine END",
  367. "%idefine P8086 CPU 8086",
  368. "%idefine P186 CPU 186",
  369. "%idefine P286 CPU 286",
  370. "%idefine P286N CPU 286",
  371. "%idefine P286P CPU 286 Priv",
  372. "%idefine P386 CPU 386",
  373. "%idefine P386N CPU 386",
  374. "%idefine P386P CPU 386 Priv",
  375. "%idefine P486 CPU 486",
  376. "%idefine P586 CPU 586",
  377. "%idefine .8086 CPU 8086",
  378. "%idefine .186 CPU 186",
  379. "%idefine .286 CPU 286",
  380. "%idefine .286C CPU 286",
  381. "%idefine .286P CPU 286",
  382. "%idefine .386 CPU 386",
  383. "%idefine .386C CPU 386",
  384. "%idefine .386P CPU 386",
  385. "%idefine .486 CPU 486",
  386. "%idefine .486C CPU 486",
  387. "%idefine .486P CPU 486",
  388. "%idefine .586 CPU 586",
  389. "%idefine .586C CPU 586",
  390. "%idefine .586P CPU 586",
  391. "",
  392. "%imacro TITLE 1",
  393. "%endm",
  394. "%imacro NAME 1",
  395. "%endm",
  396. "",
  397. "%imacro EXTRN 1-*.nolist",
  398. "%rep %0",
  399. "[extern %1]",
  400. "%rotate 1",
  401. "%endrep",
  402. "%endmacro",
  403. "",
  404. "%imacro PUBLIC 1-*.nolist",
  405. "%rep %0",
  406. "[global %1]",
  407. "%rotate 1",
  408. "%endrep",
  409. "%endmacro",
  410. "",
  411. "; this is not needed",
  412. "%idefine PTR",
  413. NULL
  414. };
  415. static int nested_mac_count, nested_rep_count;
  416. /*
  417. * Tokens are allocated in blocks to improve speed
  418. */
  419. #define TOKEN_BLOCKSIZE 4096
  420. static Token *freeTokens = NULL;
  421. struct Blocks {
  422. Blocks *next;
  423. void *chunk;
  424. };
  425. static Blocks blocks = { NULL, NULL };
  426. /*
  427. * Forward declarations.
  428. */
  429. static Token *expand_mmac_params(Token * tline);
  430. static Token *expand_smacro(Token * tline);
  431. static Token *expand_id(Token * tline);
  432. static Context *get_ctx(char *name, int all_contexts);
  433. static void make_tok_num(Token * tok, yasm_intnum *val);
  434. static void error(int severity, const char *fmt, ...);
  435. static void *new_Block(size_t size);
  436. static void delete_Blocks(void);
  437. static Token *new_Token(Token * next, int type, const char *text,
  438. size_t txtlen);
  439. static Token *delete_Token(Token * t);
  440. static Token *tokenise(char *line);
  441. /*
  442. * Macros for safe checking of token pointers, avoid *(NULL)
  443. */
  444. #define tok_type_(x,t) ((x) && (x)->type == (t))
  445. #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
  446. #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
  447. #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
  448. /* Handle TASM specific directives, which do not contain a % in
  449. * front of them. We do it here because I could not find any other
  450. * place to do it for the moment, and it is a hack (ideally it would
  451. * be nice to be able to use the NASM pre-processor to do it).
  452. */
  453. typedef struct TMEndItem {
  454. int type;
  455. void *data;
  456. struct TMEndItem *next;
  457. } TMEndItem;
  458. static TMEndItem *EndmStack = NULL, *EndsStack = NULL;
  459. char **TMParameters;
  460. struct TStrucField {
  461. char *name;
  462. char *type;
  463. struct TStrucField *next;
  464. };
  465. struct TStruc {
  466. char *name;
  467. struct TStrucField *fields, *lastField;
  468. struct TStruc *next;
  469. };
  470. static struct TStruc *TStrucs = NULL;
  471. static int inTstruc = 0;
  472. struct TSegmentAssume {
  473. char *segreg;
  474. char *segment;
  475. };
  476. struct TSegmentAssume *TAssumes;
  477. const char *tasm_get_segment_register(const char *segment)
  478. {
  479. struct TSegmentAssume *assume;
  480. if (!TAssumes)
  481. return NULL;
  482. for (assume = TAssumes; assume->segreg; assume++) {
  483. if (!strcmp(assume->segment, segment))
  484. break;
  485. }
  486. return assume->segreg;
  487. }
  488. static char *
  489. check_tasm_directive(char *line)
  490. {
  491. int i, j, k, m;
  492. size_t len, len2;
  493. char *p, *oldline, oldchar, *q, oldchar2;
  494. TMEndItem *end;
  495. p = line;
  496. /* Skip whitespace */
  497. while (isspace(*p) && *p != 0 && *p != ';')
  498. p++;
  499. /* Ignore nasm directives */
  500. if (*p == '%')
  501. return line;
  502. /* Binary search for the directive name */
  503. len = 0;
  504. while (!isspace(p[len]) && p[len] != 0 && p[len] != ';')
  505. len++;
  506. if (!len)
  507. return line;
  508. oldchar = p[len];
  509. p[len] = 0;
  510. i = -1;
  511. j = elements(tasm_directives);
  512. while (j - i > 1)
  513. {
  514. k = (j + i) / 2;
  515. m = nasm_stricmp(p, tasm_directives[k]);
  516. if (m == 0)
  517. {
  518. /* We have found a directive, so jam a % in front of it
  519. * so that NASM will then recognise it as one if it's own.
  520. */
  521. p[len] = oldchar;
  522. len = strlen(p);
  523. oldline = line;
  524. if (k == TM_IFDIFI)
  525. {
  526. /* NASM does not recognise IFDIFI, so we convert it to
  527. * %ifdef BOGUS. This is not used in NASM comaptible
  528. * code, but does need to parse for the TASM macro
  529. * package.
  530. */
  531. line = nasm_malloc(13);
  532. strcpy(line, "%ifdef BOGUS");
  533. }
  534. else if (k == TM_INCLUDE)
  535. {
  536. /* add double quotes around file name */
  537. p += 7 + 1;
  538. while (isspace(*p) && *p)
  539. p++;
  540. len = strlen(p);
  541. line = nasm_malloc(1 + 7 + 1 + 1 + len + 1 + 1);
  542. sprintf(line, "%%include \"%s\"", p);
  543. }
  544. else
  545. {
  546. line = nasm_malloc(len + 2);
  547. line[0] = '%';
  548. memcpy(line + 1, p, len + 1);
  549. }
  550. nasm_free(oldline);
  551. return line;
  552. }
  553. else if (m < 0)
  554. {
  555. j = k;
  556. }
  557. else
  558. i = k;
  559. }
  560. /* Not a simple directive */
  561. if (!nasm_stricmp(p, "endm")) {
  562. /* handle end of endm directive */
  563. char **parameter;
  564. end = EndmStack;
  565. /* undef parameters */
  566. if (!end) {
  567. error(ERR_FATAL, "ENDM: not in an endm context");
  568. return line;
  569. }
  570. EndmStack = EndmStack->next;
  571. nasm_free(line);
  572. switch (end->type) {
  573. case TM_MACRO:
  574. len = 0;
  575. for (parameter = end->data; *parameter; parameter++)
  576. len += 6 + 1 + strlen(*parameter) + 1;
  577. len += 5 + 1;
  578. line = nasm_malloc(len);
  579. p = line;
  580. for (parameter = end->data; *parameter; parameter++) {
  581. p += sprintf(p, "%%undef %s\n", *parameter);
  582. nasm_free(*parameter);
  583. }
  584. nasm_free(end->data);
  585. nasm_free(end);
  586. sprintf(p, "%%endm");
  587. return line;
  588. case TM_REPT:
  589. nasm_free(end);
  590. return nasm_strdup("%endrep");
  591. case TM_IRP: {
  592. char **data;
  593. const char *irp_format =
  594. "%%undef %s\n"
  595. "%%rotate 1\n"
  596. "%%endrep\n"
  597. "%%endm\n"
  598. "irp %s\n"
  599. "%%undef irp";
  600. data = end->data;
  601. line = nasm_malloc(strlen(irp_format) - 4 + strlen(data[0])
  602. + strlen(data[1]));
  603. sprintf(line, irp_format, data[0], data[1]);
  604. nasm_free(data[0]);
  605. nasm_free(data[1]);
  606. nasm_free(data);
  607. return line;
  608. }
  609. default:
  610. error(ERR_FATAL, "ENDM: bogus endm context type %d\n",end->type);
  611. return NULL;
  612. }
  613. } else if (!nasm_stricmp(p, "end")) {
  614. nasm_free(line);
  615. return nasm_strdup("");
  616. } else if (!nasm_stricmp(p, "rept")) {
  617. /* handle repeat directive */
  618. end = nasm_malloc(sizeof(*end));
  619. end->type = TM_REPT;
  620. end->next = EndmStack;
  621. EndmStack = end;
  622. memcpy(p, "%rep", 4);
  623. p[len] = oldchar;
  624. return line;
  625. } else if (!nasm_stricmp(p, "locals")) {
  626. tasm_locals = 1;
  627. nasm_free(line);
  628. return nasm_strdup("");
  629. }
  630. if (!oldchar)
  631. return line;
  632. /* handle two-words directives */
  633. q = p + len + 1;
  634. /* Skip whitespaces */
  635. while (isspace(*q) && *q)
  636. q++;
  637. len2 = 0;
  638. while (!isspace(q[len2]) && q[len2]!=',' && q[len2] != 0)
  639. len2++;
  640. oldchar2 = q[len2];
  641. q[len2] = '\0';
  642. if (!nasm_stricmp(p, "irp")) {
  643. /* handle indefinite repeat directive */
  644. const char *irp_format =
  645. "%%imacro irp 0-*\n"
  646. "%%rep %%0\n"
  647. "%%define %s %%1\n";
  648. char **data;
  649. data = malloc(2*sizeof(char*));
  650. oldline = line;
  651. line = nasm_malloc(strlen(irp_format) - 2 + len2 + 1);
  652. sprintf(line,irp_format,q);
  653. data[0] = nasm_strdup(q);
  654. if (!oldchar2)
  655. error(ERR_FATAL, "%s: expected <values>", q + len2);
  656. p = strchr(q + len2 + 1, '<');
  657. if (!p)
  658. error(ERR_FATAL, "%s: expected <values>", q + len2);
  659. p++;
  660. q = strchr(p, '>');
  661. data[1] = nasm_strndup(p, q - p);
  662. end = nasm_malloc(sizeof(*end));
  663. end->type = TM_IRP;
  664. end->next = EndmStack;
  665. end->data = data;
  666. EndmStack = end;
  667. nasm_free(oldline);
  668. return line;
  669. } else if (!nasm_stricmp(q, "macro")) {
  670. char *name = p;
  671. /* handle MACRO */
  672. /* count parameters */
  673. j = 1;
  674. i = 0;
  675. TMParameters = nasm_malloc(j*sizeof(*TMParameters));
  676. len = 0;
  677. p = q + len2 + 1;
  678. /* Skip whitespaces */
  679. while (isspace(*p) && *p)
  680. p++;
  681. while (*p) {
  682. /* Get parameter name */
  683. for (q = p; !isspace(*q) && *q != ',' && *q; q++);
  684. len2 = q-p;
  685. if (len2 == 0)
  686. error(ERR_FATAL, "'%s': expected parameter name", p);
  687. TMParameters[i] = nasm_malloc(len2 + 1);
  688. memcpy(TMParameters[i], p, len2);
  689. TMParameters[i][len2] = '\0';
  690. len += len2;
  691. i++;
  692. if (i + 1 > j) {
  693. j *= 2;
  694. TMParameters = nasm_realloc(TMParameters,
  695. j*sizeof(*TMParameters));
  696. }
  697. if (i == 1000)
  698. error(ERR_FATAL, "too many parameters for macro %s", name);
  699. p = q;
  700. while (isspace(*p) && *p)
  701. p++;
  702. if (!*p)
  703. break;
  704. if (*p != ',')
  705. error(ERR_FATAL, "expected comma");
  706. p++;
  707. while (isspace(*p) && *p)
  708. p++;
  709. }
  710. TMParameters[i] = NULL;
  711. TMParameters = nasm_realloc(TMParameters,
  712. (i+1)*sizeof(*TMParameters));
  713. len += 1 + 6 + 1 + strlen(name) + 1 + 3; /* macro definition */
  714. len += i * (1 + 9 + 1 + 1 + 1 + 3 + 2); /* macro parameter definition */
  715. oldline = line;
  716. p = line = nasm_malloc(len + 1);
  717. p += sprintf(p, "%%imacro %s 0-*", name);
  718. nasm_free(oldline);
  719. for (j = 0; TMParameters[j]; j++) {
  720. p += sprintf(p, "\n%%idefine %s %%{%-u}", TMParameters[j], j + 1);
  721. }
  722. end = nasm_malloc(sizeof(*end));
  723. end->type = TM_MACRO;
  724. end->next = EndmStack;
  725. end->data = TMParameters;
  726. EndmStack = end;
  727. return line;
  728. } else if (!nasm_stricmp(q, "proc")) {
  729. /* handle PROC */
  730. oldline = line;
  731. line = nasm_malloc(2 + len + 1);
  732. sprintf(line, "..%s",p);
  733. nasm_free(oldline);
  734. return line;
  735. } else if (!nasm_stricmp(q, "struc")) {
  736. /* handle struc */
  737. struct TStruc *struc;
  738. if (inTstruc) {
  739. error(ERR_FATAL, "STRUC: already in a struc context");
  740. return line;
  741. }
  742. oldline = line;
  743. line = nasm_malloc(5 + 1 + len + 1);
  744. sprintf(line, "struc %s", p);
  745. struc = malloc(sizeof(*struc));
  746. struc->name = nasm_strdup(p);
  747. struc->fields = NULL;
  748. struc->lastField = NULL;
  749. struc->next = TStrucs;
  750. TStrucs = struc;
  751. inTstruc = 1;
  752. nasm_free(oldline);
  753. end = nasm_malloc(sizeof(*end));
  754. end->type = TM_STRUC;
  755. end->next = EndsStack;
  756. EndsStack = end;
  757. return line;
  758. } else if (!nasm_stricmp(q, "segment")) {
  759. /* handle SEGMENT */
  760. oldline = line;
  761. line = nasm_strdup(oldchar2?q+len2+1:"");
  762. if (tasm_segment) {
  763. error(ERR_FATAL, "SEGMENT: already in a segment context");
  764. return line;
  765. }
  766. tasm_segment = nasm_strdup(p);
  767. nasm_free(oldline);
  768. end = nasm_malloc(sizeof(*end));
  769. end->type = TM_SEGMENT;
  770. end->next = EndsStack;
  771. EndsStack = end;
  772. return line;
  773. } else if (!nasm_stricmp(p, "ends") || !nasm_stricmp(q, "ends")) {
  774. /* handle end of ends directive */
  775. end = EndsStack;
  776. /* undef parameters */
  777. if (!end) {
  778. error(ERR_FATAL, "ENDS: not in an ends context");
  779. return line;
  780. }
  781. EndsStack = EndsStack->next;
  782. nasm_free(line);
  783. switch (end->type) {
  784. case TM_STRUC:
  785. inTstruc = 0;
  786. return nasm_strdup("endstruc");
  787. case TM_SEGMENT:
  788. /* XXX: yes, we leak memory here, but that permits labels
  789. * to avoid strduping... */
  790. tasm_segment = NULL;
  791. return nasm_strdup("");
  792. default:
  793. error(ERR_FATAL, "ENDS: bogus ends context type %d",end->type);
  794. return NULL;
  795. }
  796. } else if (!nasm_stricmp(p, "endp") || !nasm_stricmp(q, "endp")) {
  797. nasm_free(line);
  798. return nasm_strdup("");
  799. } else if (!nasm_stricmp(p, "assume")) {
  800. struct TSegmentAssume *assume;
  801. /* handle ASSUME */
  802. if (!TAssumes) {
  803. TAssumes = nasm_malloc(sizeof(*TAssumes));
  804. TAssumes[0].segreg = NULL;
  805. }
  806. i = 0;
  807. q[len2] = oldchar2;
  808. /* Skip whitespaces */
  809. while (isspace(*q) && *q)
  810. q++;
  811. while (*q && *q != ';') {
  812. p = q;
  813. for (; *q && *q != ';' && *q != ':' && !isspace(*q); q++);
  814. if (!*q || *q == ';')
  815. break;
  816. /* segment register name */
  817. for (assume = TAssumes; assume->segreg; assume++)
  818. if (strlen(assume->segreg) == (size_t)(q-p) &&
  819. !yasm__strncasecmp(assume->segreg, p, q-p))
  820. break;
  821. if (!assume->segreg) {
  822. i = assume - TAssumes + 1;
  823. TAssumes = nasm_realloc(TAssumes, (i+1)*sizeof(*TAssumes));
  824. assume = TAssumes + i - 1;
  825. assume->segreg = nasm_strndup(p, q-p);
  826. assume[1].segreg = NULL;
  827. }
  828. for (; *q && *q != ';' && *q != ':' && isspace(*q); q++);
  829. if (*q != ':')
  830. error(ERR_FATAL, "expected `:' instead of `%c'", *q);
  831. for (q++; *q && isspace(*q); q++);
  832. /* segment name */
  833. p = q;
  834. for (; *q && *q != ';' && *q != ',' && !isspace(*q); q++);
  835. assume->segment = nasm_strndup(p, q-p);
  836. for (; *q && isspace(*q); q++);
  837. if (*q && *q != ';' && *q != ',')
  838. error(ERR_FATAL, "expected `,' instead of `%c'", *q);
  839. if (*q && *q != ';')
  840. q++;
  841. for (; *q && isspace(*q); q++);
  842. }
  843. TAssumes[i].segreg = NULL;
  844. TAssumes = nasm_realloc(TAssumes, (i+1)*sizeof(*TAssumes));
  845. nasm_free(line);
  846. return nasm_strdup("");
  847. } else if (inTstruc) {
  848. struct TStrucField *field;
  849. /* TODO: handle unnamed data */
  850. field = nasm_malloc(sizeof(*field));
  851. field->name = nasm_strdup(p);
  852. /* TODO: type struc ! */
  853. field->type = nasm_strdup(q);
  854. field->next = NULL;
  855. if (!TStrucs->fields)
  856. TStrucs->fields = field;
  857. else if (TStrucs->lastField)
  858. TStrucs->lastField->next = field;
  859. TStrucs->lastField = field;
  860. if (!oldchar2) {
  861. error(ERR_FATAL, "Expected struc field initializer after %s %s", p, q);
  862. return line;
  863. }
  864. oldline = line;
  865. line = nasm_malloc(1 + len + 1 + len2 + 1 + strlen(q+len2+1) + 1);
  866. sprintf(line, ".%s %s %s", p, q, q+len2+1);
  867. nasm_free(oldline);
  868. return line;
  869. }
  870. {
  871. struct TStruc *struc;
  872. for (struc = TStrucs; struc; struc = struc->next) {
  873. if (!yasm__strcasecmp(q, struc->name)) {
  874. char *r = q + len2 + 1, *s, *t, tasm_param[6];
  875. struct TStrucField *field = struc->fields;
  876. int size, n;
  877. if (!oldchar2) {
  878. error(ERR_FATAL, "Expected struc field initializer after %s %s", p, q);
  879. return line;
  880. }
  881. r = strchr(r, '<');
  882. if (!r) {
  883. error(ERR_FATAL, "Expected < for struc field initializer in %s %s %s", p, q, r);
  884. return line;
  885. }
  886. t = strchr(r + 1, '>');
  887. if (!t) {
  888. error(ERR_FATAL, "Expected > for struc field initializer in %s %s %s", p, q, r);
  889. return line;
  890. }
  891. *t = 0;
  892. oldline = line;
  893. size = len + len2 + 128;
  894. line = nasm_malloc(size);
  895. if (defining)
  896. for (n=0;TMParameters[n];n++)
  897. if (!strcmp(TMParameters[n],p)) {
  898. sprintf(tasm_param,"%%{%d}",n+1);
  899. p = tasm_param;
  900. break;
  901. }
  902. n = sprintf(line, "%s: istruc %s\n", p, q);
  903. /* use initialisers */
  904. while ((s = strchr(r + 1, ','))) {
  905. if (!field) {
  906. error(ERR_FATAL, "Too many initializers in structure %s %s", p, q);
  907. return oldline;
  908. }
  909. *s = 0;
  910. m = strlen(p) + 1 + strlen(field->name)*2 + 8 +
  911. strlen(field->type) + 1 + strlen(r+1) + 2;
  912. size += m;
  913. line = nasm_realloc(line, size);
  914. sprintf(line + n, "%s.%s: at .%s, %s %s\n",
  915. p, field->name, field->name, field->type, r + 1);
  916. n += m-1;
  917. r = s;
  918. field = field->next;
  919. }
  920. /* complete with last initializer and '?' */
  921. while(field) {
  922. m = strlen(p) + 1 + strlen(field->name)*2 + 8 +
  923. strlen(field->type) + 1 + (r ? strlen(r+1) : 1) + 2;
  924. size += m;
  925. line = nasm_realloc(line, size);
  926. sprintf(line + n, "%s.%s: at .%s, %s %s\n", p, field->name,
  927. field->name, field->type, r ? r + 1: "?");
  928. n += m-1;
  929. r = NULL;
  930. field = field->next;
  931. }
  932. line = nasm_realloc(line, n + 5);
  933. sprintf(line + n, "iend");
  934. nasm_free(oldline);
  935. return line;
  936. }
  937. }
  938. }
  939. q[len2] = oldchar2;
  940. p[len] = oldchar;
  941. return line;
  942. }
  943. static Token * tasm_join_tokens(Token *tline)
  944. {
  945. Token *t, *prev, *next;
  946. for (prev = NULL, t = tline; t; prev = t, t = next) {
  947. next = t->next;
  948. if (t->type == TOK_OTHER && !strcmp(t->text,"&")) {
  949. if (!prev)
  950. error(ERR_FATAL, "no token before &");
  951. else if (!next)
  952. error(ERR_FATAL, "no token after &");
  953. else if (prev->type != next->type)
  954. error(ERR_FATAL, "can't handle different types of token around &");
  955. else if (!prev->text || !next->text)
  956. error(ERR_FATAL, "can't handle empty token around &");
  957. else {
  958. int lenp = strlen(prev->text);
  959. int lenn = strlen(next->text);
  960. prev->text = nasm_realloc(prev->text, lenp + lenn + 1);
  961. strncpy(prev->text + lenp, next->text, lenn + 1);
  962. (void) delete_Token(t);
  963. prev->next = delete_Token(next);
  964. t = prev;
  965. next = t->next;
  966. }
  967. }
  968. }
  969. return tline;
  970. }
  971. /*
  972. * The pre-preprocessing stage... This function translates line
  973. * number indications as they emerge from GNU cpp (`# lineno "file"
  974. * flags') into NASM preprocessor line number indications (`%line
  975. * lineno file').
  976. */
  977. static char *
  978. prepreproc(char *line)
  979. {
  980. int lineno;
  981. size_t fnlen;
  982. char *fname, *oldline;
  983. char *c, *d, *ret;
  984. Line *l, **lp;
  985. if (line[0] == '#' && line[1] == ' ')
  986. {
  987. oldline = line;
  988. fname = oldline + 2;
  989. lineno = atoi(fname);
  990. fname += strspn(fname, "0123456789 ");
  991. if (*fname == '"')
  992. fname++;
  993. fnlen = strcspn(fname, "\"");
  994. line = nasm_malloc(20 + fnlen);
  995. sprintf(line, "%%line %d %.*s", lineno, (int)fnlen, fname);
  996. nasm_free(oldline);
  997. }
  998. if (tasm_compatible_mode)
  999. line = check_tasm_directive(line);
  1000. if (!(c = strchr(line, '\n')))
  1001. return line;
  1002. /* Turn multiline macros into several lines */
  1003. *c = '\0';
  1004. ret = nasm_strdup(line);
  1005. lp = &istk->expansion;
  1006. do {
  1007. d = strchr(c+1, '\n');
  1008. if (d)
  1009. *d = '\0';
  1010. l = malloc(sizeof(*l));
  1011. l -> first = tokenise(c+1);
  1012. l -> finishes = NULL;
  1013. l -> next = *lp;
  1014. *lp = l;
  1015. c = d;
  1016. lp = &l -> next;
  1017. } while (c);
  1018. nasm_free(line);
  1019. return ret;
  1020. }
  1021. /*
  1022. * The hash function for macro lookups. Note that due to some
  1023. * macros having case-insensitive names, the hash function must be
  1024. * invariant under case changes. We implement this by applying a
  1025. * perfectly normal hash function to the uppercase of the string.
  1026. */
  1027. static int
  1028. hash(char *s)
  1029. {
  1030. unsigned int h = 0;
  1031. unsigned int i = 0;
  1032. /*
  1033. * Powers of three, mod 31.
  1034. */
  1035. static const int multipliers[] = {
  1036. 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
  1037. 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
  1038. };
  1039. while (*s)
  1040. {
  1041. h += multipliers[i] * (unsigned char) (toupper(*s));
  1042. s++;
  1043. if (++i >= elements(multipliers))
  1044. i = 0;
  1045. }
  1046. h %= NHASH;
  1047. return h;
  1048. }
  1049. /*
  1050. * Free a linked list of tokens.
  1051. */
  1052. static void
  1053. free_tlist(Token * list_)
  1054. {
  1055. while (list_)
  1056. {
  1057. list_ = delete_Token(list_);
  1058. }
  1059. }
  1060. /*
  1061. * Free a linked list of lines.
  1062. */
  1063. static void
  1064. free_llist(Line * list_)
  1065. {
  1066. Line *l;
  1067. while (list_)
  1068. {
  1069. l = list_;
  1070. list_ = list_->next;
  1071. free_tlist(l->first);
  1072. nasm_free(l);
  1073. }
  1074. }
  1075. /*
  1076. * Free an MMacro
  1077. */
  1078. static void
  1079. free_mmacro(MMacro * m)
  1080. {
  1081. nasm_free(m->name);
  1082. free_tlist(m->dlist);
  1083. nasm_free(m->defaults);
  1084. free_llist(m->expansion);
  1085. nasm_free(m);
  1086. }
  1087. /*
  1088. * Pop the context stack.
  1089. */
  1090. static void
  1091. ctx_pop(void)
  1092. {
  1093. Context *c = cstk;
  1094. SMacro *smac, *s;
  1095. cstk = cstk->next;
  1096. smac = c->localmac;
  1097. while (smac)
  1098. {
  1099. s = smac;
  1100. smac = smac->next;
  1101. nasm_free(s->name);
  1102. free_tlist(s->expansion);
  1103. nasm_free(s);
  1104. }
  1105. nasm_free(c->name);
  1106. nasm_free(c);
  1107. }
  1108. #define BUF_DELTA 512
  1109. /*
  1110. * Read a line from the top file in istk, handling multiple CR/LFs
  1111. * at the end of the line read, and handling spurious ^Zs. Will
  1112. * return lines from the standard macro set if this has not already
  1113. * been done.
  1114. */
  1115. static char *
  1116. read_line(void)
  1117. {
  1118. char *buffer, *p, *q;
  1119. int bufsize, continued_count;
  1120. bufsize = BUF_DELTA;
  1121. buffer = nasm_malloc(BUF_DELTA);
  1122. p = buffer;
  1123. continued_count = 0;
  1124. while (1)
  1125. {
  1126. q = fgets(p, bufsize - (int)(p - buffer), istk->fp);
  1127. if (!q)
  1128. break;
  1129. p += strlen(p);
  1130. if (p > buffer && p[-1] == '\n')
  1131. {
  1132. /* Convert backslash-CRLF line continuation sequences into
  1133. nothing at all (for DOS and Windows) */
  1134. if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
  1135. p -= 3;
  1136. *p = 0;
  1137. continued_count++;
  1138. }
  1139. /* Also convert backslash-LF line continuation sequences into
  1140. nothing at all (for Unix) */
  1141. else if (((p - 1) > buffer) && (p[-2] == '\\')) {
  1142. p -= 2;
  1143. *p = 0;
  1144. continued_count++;
  1145. }
  1146. else {
  1147. break;
  1148. }
  1149. }
  1150. if (p - buffer > bufsize - 10)
  1151. {
  1152. long offset = (long)(p - buffer);
  1153. bufsize += BUF_DELTA;
  1154. buffer = nasm_realloc(buffer, (size_t)bufsize);
  1155. p = buffer + offset; /* prevent stale-pointer problems */
  1156. }
  1157. }
  1158. if (!q && p == buffer)
  1159. {
  1160. nasm_free(buffer);
  1161. return NULL;
  1162. }
  1163. nasm_src_set_linnum(nasm_src_get_linnum() + istk->lineinc + (continued_count * istk->lineinc));
  1164. /*
  1165. * Play safe: remove CRs as well as LFs, if any of either are
  1166. * present at the end of the line.
  1167. */
  1168. while (--p >= buffer && (*p == '\n' || *p == '\r'))
  1169. *p = '\0';
  1170. /*
  1171. * Handle spurious ^Z, which may be inserted into source files
  1172. * by some file transfer utilities.
  1173. */
  1174. buffer[strcspn(buffer, "\032")] = '\0';
  1175. list->line(LIST_READ, buffer);
  1176. return buffer;
  1177. }
  1178. /*
  1179. * Tokenise a line of text. This is a very simple process since we
  1180. * don't need to parse the value out of e.g. numeric tokens: we
  1181. * simply split one string into many.
  1182. */
  1183. static Token *
  1184. tokenise(char *line)
  1185. {
  1186. char *p = line;
  1187. int type;
  1188. Token *list_ = NULL;
  1189. Token *t, **tail = &list_;
  1190. while (*line)
  1191. {
  1192. p = line;
  1193. if (*p == '%')
  1194. {
  1195. p++;
  1196. if ( isdigit(*p) ||
  1197. ((*p == '-' || *p == '+') && isdigit(p[1])) ||
  1198. ((*p == '+') && (isspace(p[1]) || !p[1])))
  1199. {
  1200. do
  1201. {
  1202. p++;
  1203. }
  1204. while (isdigit(*p));
  1205. type = TOK_PREPROC_ID;
  1206. }
  1207. else if (*p == '{')
  1208. {
  1209. p++;
  1210. while (*p && *p != '}')
  1211. {
  1212. p[-1] = *p;
  1213. p++;
  1214. }
  1215. p[-1] = '\0';
  1216. if (*p)
  1217. p++;
  1218. type = TOK_PREPROC_ID;
  1219. }
  1220. else if (isidchar(*p) ||
  1221. ((*p == '!' || *p == '%' || *p == '$') &&
  1222. isidchar(p[1])))
  1223. {
  1224. do
  1225. {
  1226. p++;
  1227. }
  1228. while (isidchar(*p));
  1229. type = TOK_PREPROC_ID;
  1230. }
  1231. else
  1232. {
  1233. type = TOK_OTHER;
  1234. if (*p == '%')
  1235. p++;
  1236. }
  1237. }
  1238. else if (isidstart(*p) || (*p == '$' && isidstart(p[1])))
  1239. {
  1240. type = TOK_ID;
  1241. p++;
  1242. while (*p && isidchar(*p))
  1243. p++;
  1244. }
  1245. else if (*p == '\'' || *p == '"')
  1246. {
  1247. /*
  1248. * A string token.
  1249. */
  1250. char c = *p;
  1251. p++;
  1252. type = TOK_STRING;
  1253. while (*p && *p != c)
  1254. p++;
  1255. if (*p)
  1256. {
  1257. p++;
  1258. }
  1259. else
  1260. {
  1261. error(ERR_WARNING, "unterminated string");
  1262. type = -1;
  1263. }
  1264. }
  1265. else if (isnumstart(*p))
  1266. {
  1267. /*
  1268. * A number token.
  1269. */
  1270. type = TOK_NUMBER;
  1271. p++;
  1272. while (*p && isnumchar(*p))
  1273. p++;
  1274. }
  1275. else if (isspace(*p))
  1276. {
  1277. type = TOK_WHITESPACE;
  1278. p++;
  1279. while (*p && isspace(*p))
  1280. p++;
  1281. /*
  1282. * Whitespace just before end-of-line is discarded by
  1283. * pretending it's a comment; whitespace just before a
  1284. * comment gets lumped into the comment.
  1285. */
  1286. if (!*p || *p == ';')
  1287. {
  1288. type = TOK_COMMENT;
  1289. while (*p)
  1290. p++;
  1291. }
  1292. }
  1293. else if (*p == ';')
  1294. {
  1295. type = TOK_COMMENT;
  1296. while (*p)
  1297. p++;
  1298. }
  1299. else
  1300. {
  1301. /*
  1302. * Anything else is an operator of some kind. We check
  1303. * for all the double-character operators (>>, <<, //,
  1304. * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
  1305. * else is a single-character operator.
  1306. */
  1307. type = TOK_OTHER;
  1308. if ((p[0] == '>' && p[1] == '>') ||
  1309. (p[0] == '<' && p[1] == '<') ||
  1310. (p[0] == '/' && p[1] == '/') ||
  1311. (p[0] == '<' && p[1] == '=') ||
  1312. (p[0] == '>' && p[1] == '=') ||
  1313. (p[0] == '=' && p[1] == '=') ||
  1314. (p[0] == '!' && p[1] == '=') ||
  1315. (p[0] == '<' && p[1] == '>') ||
  1316. (p[0] == '&' && p[1] == '&') ||
  1317. (p[0] == '|' && p[1] == '|') ||
  1318. (p[0] == '^' && p[1] == '^'))
  1319. {
  1320. p++;
  1321. }
  1322. p++;
  1323. }
  1324. /* Handle unterminated string */
  1325. if (type == -1)
  1326. {
  1327. *tail = t = new_Token(NULL, TOK_STRING, line, (size_t)(p-line)+1);
  1328. t->text[p-line] = *line;
  1329. tail = &t->next;
  1330. }
  1331. else if (type != TOK_COMMENT)
  1332. {
  1333. *tail = t = new_Token(NULL, type, line, (size_t)(p - line));
  1334. tail = &t->next;
  1335. }
  1336. line = p;
  1337. }
  1338. return list_;
  1339. }
  1340. /*
  1341. * this function allocates a new managed block of memory and
  1342. * returns a pointer to the block. The managed blocks are
  1343. * deleted only all at once by the delete_Blocks function.
  1344. */
  1345. static void *
  1346. new_Block(size_t size)
  1347. {
  1348. Blocks *b = &blocks;
  1349. /* first, get to the end of the linked list */
  1350. while (b->next)
  1351. b = b->next;
  1352. /* now allocate the requested chunk */
  1353. b->chunk = nasm_malloc(size);
  1354. /* now allocate a new block for the next request */
  1355. b->next = nasm_malloc(sizeof(Blocks));
  1356. /* and initialize the contents of the new block */
  1357. b->next->next = NULL;
  1358. b->next->chunk = NULL;
  1359. return b->chunk;
  1360. }
  1361. /*
  1362. * this function deletes all managed blocks of memory
  1363. */
  1364. static void
  1365. delete_Blocks(void)
  1366. {
  1367. Blocks *a,*b = &blocks;
  1368. /*
  1369. * keep in mind that the first block, pointed to by blocks
  1370. * is a static and not dynamically allocated, so we don't
  1371. * free it.
  1372. */
  1373. while (b)
  1374. {
  1375. if (b->chunk)
  1376. nasm_free(b->chunk);
  1377. a = b;
  1378. b = b->next;
  1379. if (a != &blocks)
  1380. nasm_free(a);
  1381. }
  1382. }
  1383. /*
  1384. * this function creates a new Token and passes a pointer to it
  1385. * back to the caller. It sets the type and text elements, and
  1386. * also the mac and next elements to NULL.
  1387. */
  1388. static Token *
  1389. new_Token(Token * next, int type, const char *text, size_t txtlen)
  1390. {
  1391. Token *t;
  1392. int i;
  1393. if (freeTokens == NULL)
  1394. {
  1395. freeTokens = (Token *)new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
  1396. for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
  1397. freeTokens[i].next = &freeTokens[i + 1];
  1398. freeTokens[i].next = NULL;
  1399. }
  1400. t = freeTokens;
  1401. freeTokens = t->next;
  1402. t->next = next;
  1403. t->mac = NULL;
  1404. t->type = type;
  1405. if (type == TOK_WHITESPACE || text == NULL)
  1406. {
  1407. t->text = NULL;
  1408. }
  1409. else
  1410. {
  1411. if (txtlen == 0)
  1412. txtlen = strlen(text);
  1413. t->text = nasm_malloc(1 + txtlen);
  1414. strncpy(t->text, text, txtlen);
  1415. t->text[txtlen] = '\0';
  1416. }
  1417. return t;
  1418. }
  1419. static Token *
  1420. delete_Token(Token * t)
  1421. {
  1422. Token *next = t->next;
  1423. nasm_free(t->text);
  1424. t->next = freeTokens;
  1425. freeTokens = t;
  1426. return next;
  1427. }
  1428. /*
  1429. * Convert a line of tokens back into text.
  1430. * If expand_locals is not zero, identifiers of the form "%$*xxx"
  1431. * will be transformed into ..@ctxnum.xxx
  1432. */
  1433. static char *
  1434. detoken(Token * tlist, int expand_locals)
  1435. {
  1436. Token *t;
  1437. size_t len;
  1438. char *line, *p;
  1439. len = 0;
  1440. for (t = tlist; t; t = t->next)
  1441. {
  1442. if (t->type == TOK_PREPROC_ID && t->text[1] == '!')
  1443. {
  1444. char *p2 = getenv(t->text + 2);
  1445. nasm_free(t->text);
  1446. if (p2)
  1447. t->text = nasm_strdup(p2);
  1448. else
  1449. t->text = NULL;
  1450. }
  1451. /* Expand local macros here and not during preprocessing */
  1452. if (expand_locals &&
  1453. t->type == TOK_PREPROC_ID && t->text &&
  1454. t->text[0] == '%' && t->text[1] == '$')
  1455. {
  1456. Context *ctx = get_ctx(t->text, FALSE);
  1457. if (ctx)
  1458. {
  1459. char buffer[40];
  1460. char *p2, *q = t->text + 2;
  1461. q += strspn(q, "$");
  1462. sprintf(buffer, "..@%lu.", ctx->number);
  1463. p2 = nasm_strcat(buffer, q);
  1464. nasm_free(t->text);
  1465. t->text = p2;
  1466. }
  1467. }
  1468. if (t->type == TOK_WHITESPACE)
  1469. {
  1470. len++;
  1471. }
  1472. else if (t->text)
  1473. {
  1474. len += strlen(t->text);
  1475. }
  1476. }
  1477. p = line = nasm_malloc(len + 1);
  1478. for (t = tlist; t; t = t->next)
  1479. {
  1480. if (t->type == TOK_WHITESPACE)
  1481. {
  1482. *p = ' ';
  1483. p++;
  1484. *p = '\0';
  1485. }
  1486. else if (t->text)
  1487. {
  1488. strcpy(p, t->text);
  1489. p += strlen(p);
  1490. }
  1491. }
  1492. *p = '\0';
  1493. return line;
  1494. }
  1495. /*
  1496. * A scanner, suitable for use by the expression evaluator, which
  1497. * operates on a line of Tokens. Expects a pointer to a pointer to
  1498. * the first token in the line to be passed in as its private_data
  1499. * field.
  1500. */
  1501. static int
  1502. ppscan(void *private_data, struct tokenval *tokval)
  1503. {
  1504. Token **tlineptr = private_data;
  1505. Token *tline;
  1506. do
  1507. {
  1508. tline = *tlineptr;
  1509. *tlineptr = tline ? tline->next : NULL;
  1510. }
  1511. while (tline && (tline->type == TOK_WHITESPACE ||
  1512. tline->type == TOK_COMMENT));
  1513. if (!tline)
  1514. return tokval->t_type = TOKEN_EOS;
  1515. if (tline->text[0] == '$' && !tline->text[1])
  1516. return tokval->t_type = TOKEN_HERE;
  1517. if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
  1518. return tokval->t_type = TOKEN_BASE;
  1519. if (tline->type == TOK_ID)
  1520. {
  1521. tokval->t_charptr = tline->text;
  1522. if (tline->text[0] == '$')
  1523. {
  1524. tokval->t_charptr++;
  1525. return tokval->t_type = TOKEN_ID;
  1526. }
  1527. /*
  1528. * This is the only special case we actually need to worry
  1529. * about in this restricted context.
  1530. */
  1531. if (!nasm_stricmp(tline->text, "seg"))
  1532. return tokval->t_type = TOKEN_SEG;
  1533. return tokval->t_type = TOKEN_ID;
  1534. }
  1535. if (tline->type == TOK_NUMBER)
  1536. {
  1537. int rn_error;
  1538. tokval->t_integer = nasm_readnum(tline->text, &rn_error);
  1539. if (rn_error)
  1540. return tokval->t_type = TOKEN_ERRNUM;
  1541. tokval->t_charptr = NULL;
  1542. return tokval->t_type = TOKEN_NUM;
  1543. }
  1544. if (tline->type == TOK_STRING)
  1545. {
  1546. int rn_warn;
  1547. char q, *r;
  1548. size_t l;
  1549. r = tline->text;
  1550. q = *r++;
  1551. l = strlen(r);
  1552. if (l == 0 || r[l - 1] != q)
  1553. return tokval->t_type = TOKEN_ERRNUM;
  1554. tokval->t_integer = nasm_readstrnum(r, l - 1, &rn_warn);
  1555. if (rn_warn)
  1556. error(ERR_WARNING | ERR_PASS1, "character constant too long");
  1557. tokval->t_charptr = NULL;
  1558. return tokval->t_type = TOKEN_NUM;
  1559. }
  1560. if (tline->type == TOK_OTHER)
  1561. {
  1562. if (!strcmp(tline->text, "<<"))
  1563. return tokval->t_type = TOKEN_SHL;
  1564. if (!strcmp(tline->text, ">>"))
  1565. return tokval->t_type = TOKEN_SHR;
  1566. if (!strcmp(tline->text, "//"))
  1567. return tokval->t_type = TOKEN_SDIV;
  1568. if (!strcmp(tline->text, "%%"))
  1569. return tokval->t_type = TOKEN_SMOD;
  1570. if (!strcmp(tline->text, "=="))
  1571. return tokval->t_type = TOKEN_EQ;
  1572. if (!strcmp(tline->text, "<>"))
  1573. return tokval->t_type = TOKEN_NE;
  1574. if (!strcmp(tline->text, "!="))
  1575. return tokval->t_type = TOKEN_NE;
  1576. if (!strcmp(tline->text, "<="))
  1577. return tokval->t_type = TOKEN_LE;
  1578. if (!strcmp(tline->text, ">="))
  1579. return tokval->t_type = TOKEN_GE;
  1580. if (!strcmp(tline->text, "&&"))
  1581. return tokval->t_type = TOKEN_DBL_AND;
  1582. if (!strcmp(tline->text, "^^"))
  1583. return tokval->t_type = TOKEN_DBL_XOR;
  1584. if (!strcmp(tline->text, "||"))
  1585. return tokval->t_type = TOKEN_DBL_OR;
  1586. }
  1587. /*
  1588. * We have no other options: just return the first character of
  1589. * the token text.
  1590. */
  1591. return tokval->t_type = tline->text[0];
  1592. }
  1593. /*
  1594. * Compare a string to the name of an existing macro; this is a
  1595. * simple wrapper which calls either strcmp or nasm_stricmp
  1596. * depending on the value of the `casesense' parameter.
  1597. */
  1598. static int
  1599. mstrcmp(char *p, char *q, int casesense)
  1600. {
  1601. return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
  1602. }
  1603. /*
  1604. * Return the Context structure associated with a %$ token. Return
  1605. * NULL, having _already_ reported an error condition, if the
  1606. * context stack isn't deep enough for the supplied number of $
  1607. * signs.
  1608. * If all_contexts == TRUE, contexts that enclose current are
  1609. * also scanned for such smacro, until it is found; if not -
  1610. * only the context that directly results from the number of $'s
  1611. * in variable's name.
  1612. */
  1613. static Context *
  1614. get_ctx(char *name, int all_contexts)
  1615. {
  1616. Context *ctx;
  1617. SMacro *m;
  1618. size_t i;
  1619. if (!name || name[0] != '%' || name[1] != '$')
  1620. return NULL;
  1621. if (!cstk)
  1622. {
  1623. error(ERR_NONFATAL, "`%s': context stack is empty", name);
  1624. return NULL;
  1625. }
  1626. for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--)
  1627. {
  1628. ctx = ctx->next;
  1629. /* i--; Lino - 02/25/02 */
  1630. }
  1631. if (!ctx)
  1632. {
  1633. error(ERR_NONFATAL, "`%s': context stack is only"
  1634. " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
  1635. return NULL;
  1636. }
  1637. if (!all_contexts)
  1638. return ctx;
  1639. do
  1640. {
  1641. /* Search for this smacro in found context */
  1642. m = ctx->localmac;
  1643. while (m)
  1644. {
  1645. if (!mstrcmp(m->name, name, m->casesense))
  1646. return ctx;
  1647. m = m->next;
  1648. }
  1649. ctx = ctx->next;
  1650. }
  1651. while (ctx);
  1652. return NULL;
  1653. }
  1654. /*
  1655. * Open an include file. This routine must always return a valid
  1656. * file pointer if it returns - it's responsible for throwing an
  1657. * ERR_FATAL and bombing out completely if not. It should also try
  1658. * the include path one by one until it finds the file or reaches
  1659. * the end of the path.
  1660. */
  1661. static FILE *
  1662. inc_fopen(char *file, char **newname)
  1663. {
  1664. FILE *fp;
  1665. char *combine = NULL, *c;
  1666. char *pb, *p1, *p2, *file2 = NULL;
  1667. /* Try to expand all %ENVVAR% in filename. Warn, and leave %string%
  1668. * intact, if ENVVAR is not set in the environment.
  1669. */
  1670. pb = file;
  1671. p1 = pb;
  1672. for (;;) {
  1673. char *env;
  1674. while (*p1 != '\0' && *p1 != '%')
  1675. p1++;
  1676. if (*p1 == '\0')
  1677. break;
  1678. p2 = p1+1;
  1679. while (*p2 != '\0' && *p2 != '%')
  1680. p2++;
  1681. if (*p2 == '\0')
  1682. break;
  1683. /* Okay, we have a %...%, with p1 pointing to the first %, and p2
  1684. * pointing to the second %.
  1685. */
  1686. *p2 = '\0';
  1687. env = getenv(p1+1);
  1688. if (!env) {
  1689. /* warn, restore %, and continue looking */
  1690. error(ERR_WARNING, "environment variable `%s' does not exist",
  1691. p1+1);
  1692. *p2 = '%';
  1693. p1 = p2+1;
  1694. continue;
  1695. }
  1696. /* need to expand */
  1697. if (!file2) {
  1698. file2 = nasm_malloc(strlen(file)+strlen(env)+1);
  1699. file2[0] = '\0';
  1700. } else
  1701. file2 = nasm_realloc(file2, strlen(file2)+strlen(env)+1);
  1702. *p1 = '\0';
  1703. strcat(file2, pb);
  1704. strcat(file2, env);
  1705. pb = p2+1;
  1706. p1 = pb;
  1707. }
  1708. /* add tail end; string is long enough that we don't need to realloc */
  1709. if (file2)
  1710. strcat(file2, pb);
  1711. fp = yasm_fopen_include(file2 ? file2 : file, nasm_src_get_fname(), "r",
  1712. &combine);
  1713. if (!fp && tasm_compatible_mode)
  1714. {
  1715. char *thefile = file2 ? file2 : file;
  1716. /* try a few case combinations */
  1717. do {
  1718. for (c = thefile; *c; c++)
  1719. *c = toupper(*c);
  1720. fp = yasm_fopen_include(thefile, nasm_src_get_fname(), "r", &combine);
  1721. if (fp) break;
  1722. *thefile = tolower(*thefile);
  1723. fp = yasm_fopen_include(thefile, nasm_src_get_fname(), "r", &combine);
  1724. if (fp) break;
  1725. for (c = thefile; *c; c++)
  1726. *c = tolower(*c);
  1727. fp = yasm_fopen_include(thefile, nasm_src_get_fname(), "r", &combine);
  1728. if (fp) break;
  1729. *thefile = toupper(*thefile);
  1730. fp = yasm_fopen_include(thefile, nasm_src_get_fname(), "r", &combine);
  1731. if (fp) break;
  1732. } while (0);
  1733. }
  1734. if (!fp)
  1735. error(ERR_FATAL, "unable to open include file `%s'",
  1736. file2 ? file2 : file);
  1737. nasm_preproc_add_dep(combine);
  1738. if (file2)
  1739. nasm_free(file2);
  1740. *newname = combine;
  1741. return fp;
  1742. }
  1743. /*
  1744. * Determine if we should warn on defining a single-line macro of
  1745. * name `name', with `nparam' parameters. If nparam is 0 or -1, will
  1746. * return TRUE if _any_ single-line macro of that name is defined.
  1747. * Otherwise, will return TRUE if a single-line macro with either
  1748. * `nparam' or no parameters is defined.
  1749. *
  1750. * If a macro with precisely the right number of parameters is
  1751. * defined, or nparam is -1, the address of the definition structure
  1752. * will be returned in `defn'; otherwise NULL will be returned. If `defn'
  1753. * is NULL, no action will be taken regarding its contents, and no
  1754. * error will occur.
  1755. *
  1756. * Note that this is also called with nparam zero to resolve
  1757. * `ifdef'.
  1758. *
  1759. * If you already know which context macro belongs to, you can pass
  1760. * the context pointer as first parameter; if you won't but name begins
  1761. * with %$ the context will be automatically computed. If all_contexts
  1762. * is true, macro will be searched in outer contexts as well.
  1763. */
  1764. static int
  1765. smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
  1766. int nocase)
  1767. {
  1768. SMacro *m;
  1769. int highest_level = -1;
  1770. if (ctx)
  1771. m = ctx->localmac;
  1772. else if (name[0] == '%' && name[1] == '$')
  1773. {
  1774. if (cstk)
  1775. ctx = get_ctx(name, FALSE);
  1776. if (!ctx)
  1777. return FALSE; /* got to return _something_ */
  1778. m = ctx->localmac;
  1779. }
  1780. else
  1781. m = smacros[hash(name)];
  1782. while (m)
  1783. {
  1784. if (!mstrcmp(m->name, name, m->casesense && nocase) &&
  1785. (nparam <= 0 || m->nparam == 0 || nparam == m->nparam) && (highest_level < 0 || m->level > highest_level))
  1786. {
  1787. highest_level = m->level;
  1788. if (defn)
  1789. {
  1790. if (nparam == m->nparam || nparam == -1)
  1791. *defn = m;
  1792. else
  1793. *defn = NULL;
  1794. }
  1795. }
  1796. m = m->next;
  1797. }
  1798. return highest_level >= 0;
  1799. }
  1800. /*
  1801. * Count and mark off the parameters in a multi-line macro call.
  1802. * This is called both from within the multi-line macro expansion
  1803. * code, and also to mark off the default parameters when provided
  1804. * in a %macro definition line.
  1805. */
  1806. static void
  1807. count_mmac_params(Token * t, int *nparam, Token *** params)
  1808. {
  1809. int paramsize, brace;
  1810. *nparam = paramsize = 0;
  1811. *params = NULL;
  1812. while (t)
  1813. {
  1814. if (*nparam+1 >= paramsize)
  1815. {
  1816. paramsize += PARAM_DELTA;
  1817. *params = nasm_realloc(*params, sizeof(**params) * paramsize);
  1818. }
  1819. skip_white_(t);
  1820. brace = FALSE;
  1821. if (tok_is_(t, "{"))
  1822. brace = TRUE;
  1823. (*params)[(*nparam)++] = t;
  1824. while (tok_isnt_(t, brace ? "}" : ","))
  1825. t = t->next;
  1826. if (t)
  1827. { /* got a comma/brace */
  1828. t = t->next;
  1829. if (brace)
  1830. {
  1831. /*
  1832. * Now we've found the closing brace, look further
  1833. * for the comma.
  1834. */
  1835. skip_white_(t);
  1836. if (tok_isnt_(t, ","))
  1837. {
  1838. error(ERR_NONFATAL,
  1839. "braces do not enclose all of macro parameter");
  1840. while (tok_isnt_(t, ","))
  1841. t = t->next;
  1842. }
  1843. if (t)
  1844. t = t->next; /* eat the comma */
  1845. }
  1846. }
  1847. }
  1848. }
  1849. /*
  1850. * Determine whether one of the various `if' conditions is true or
  1851. * not.
  1852. *
  1853. * We must free the tline we get passed.
  1854. */
  1855. static int
  1856. if_condition(Token * tline, int i)
  1857. {
  1858. int j, casesense;
  1859. Token *t, *tt, **tptr, *origline;
  1860. struct tokenval tokval;
  1861. yasm_expr *evalresult;
  1862. yasm_intnum *intn;
  1863. origline = tline;
  1864. switch (i)
  1865. {
  1866. case PP_IFCTX:
  1867. case PP_ELIFCTX:
  1868. case PP_IFNCTX:
  1869. case PP_ELIFNCTX:
  1870. j = FALSE; /* have we matched yet? */
  1871. while (cstk && tline)
  1872. {
  1873. skip_white_(tline);
  1874. if (!tline || tline->type != TOK_ID)
  1875. {
  1876. error(ERR_NONFATAL,
  1877. "`%s' expects context identifiers",
  1878. directives[i]);
  1879. free_tlist(origline);
  1880. return -1;
  1881. }
  1882. if (!nasm_stricmp(tline->text, cstk->name))
  1883. j = TRUE;
  1884. tline = tline->next;
  1885. }
  1886. if (i == PP_IFNCTX || i == PP_ELIFNCTX)
  1887. j = !j;
  1888. free_tlist(origline);
  1889. return j;
  1890. case PP_IFDEF:
  1891. case PP_ELIFDEF:
  1892. case PP_IFNDEF:
  1893. case PP_ELIFNDEF:
  1894. j = FALSE; /* have we matched yet? */
  1895. while (tline)
  1896. {
  1897. skip_white_(tline);
  1898. if (!tline || (tline->type != TOK_ID &&
  1899. (tline->type != TOK_PREPROC_ID ||
  1900. tline->text[1] != '$')))
  1901. {
  1902. error(ERR_NONFATAL,
  1903. "`%s' expects macro identifiers",
  1904. directives[i]);
  1905. free_tlist(origline);
  1906. return -1;
  1907. }
  1908. if (smacro_defined(NULL, tline->text, 0, NULL, 1))
  1909. j = TRUE;
  1910. tline = tline->next;
  1911. }
  1912. if (i == PP_IFNDEF || i == PP_ELIFNDEF)
  1913. j = !j;
  1914. free_tlist(origline);
  1915. return j;
  1916. case PP_IFIDN:
  1917. case PP_ELIFIDN:
  1918. case PP_IFNIDN:
  1919. case PP_ELIFNIDN:
  1920. case PP_IFIDNI:
  1921. case PP_ELIFIDNI:
  1922. case PP_IFNIDNI:
  1923. case PP_ELIFNIDNI:
  1924. tline = expand_smacro(tline);
  1925. t = tt = tline;
  1926. while (tok_isnt_(tt, ","))
  1927. tt = tt->next;
  1928. if (!tt)
  1929. {
  1930. error(ERR_NONFATAL,
  1931. "`%s' expects two comma-separated arguments",
  1932. directives[i]);
  1933. free_tlist(tline);
  1934. return -1;
  1935. }
  1936. tt = tt->next;
  1937. casesense = (i == PP_IFIDN || i == PP_ELIFIDN ||
  1938. i == PP_IFNIDN || i == PP_ELIFNIDN);
  1939. j = TRUE; /* assume equality unless proved not */
  1940. while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt)
  1941. {
  1942. if (tt->type == TOK_OTHER && !strcmp(tt->text, ","))
  1943. {
  1944. error(ERR_NONFATAL, "`%s': more than one comma on line",
  1945. directives[i]);
  1946. free_tlist(tline);
  1947. return -1;
  1948. }
  1949. if (t->type == TOK_WHITESPACE)
  1950. {
  1951. t = t->next;
  1952. continue;
  1953. }
  1954. if (tt->type == TOK_WHITESPACE)
  1955. {
  1956. tt = tt->next;
  1957. continue;
  1958. }
  1959. if (tt->type != t->type)
  1960. {
  1961. j = FALSE; /* found mismatching tokens */
  1962. break;
  1963. }
  1964. /* Unify surrounding quotes for strings */
  1965. if (t->type == TOK_STRING)
  1966. {
  1967. tt->text[0] = t->text[0];
  1968. tt->text[strlen(tt->text) - 1] = t->text[0];
  1969. }
  1970. if (mstrcmp(tt->text, t->text, casesense) != 0)
  1971. {
  1972. j = FALSE; /* found mismatching tokens */
  1973. break;
  1974. }
  1975. t = t->next;
  1976. tt = tt->next;
  1977. }
  1978. if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
  1979. j = FALSE; /* trailing gunk on one end or other */
  1980. if (i == PP_IFNIDN || i == PP_ELIFNIDN ||
  1981. i == PP_IFNIDNI || i == PP_ELIFNIDNI)
  1982. j = !j;
  1983. free_tlist(tline);
  1984. return j;
  1985. case PP_IFMACRO:
  1986. case PP_ELIFMACRO:
  1987. case PP_IFNMACRO:
  1988. case PP_ELIFNMACRO:
  1989. {
  1990. int found = 0;
  1991. MMacro searching, *mmac;
  1992. tline = tline->next;
  1993. skip_white_(tline);
  1994. tline = expand_id(tline);
  1995. if (!tok_type_(tline, TOK_ID))
  1996. {
  1997. error(ERR_NONFATAL,
  1998. "`%s' expects a macro name",
  1999. directives[i]);
  2000. return -1;
  2001. }
  2002. searching.name = nasm_strdup(tline->text);
  2003. searching.casesense = (i == PP_MACRO);
  2004. searching.plus = FALSE;
  2005. searching.nolist = FALSE;
  2006. searching.in_progress = FALSE;
  2007. searching.rep_nest = NULL;
  2008. searching.nparam_min = 0;
  2009. searching.nparam_max = INT_MAX;
  2010. tline = expand_smacro(tline->next);
  2011. skip_white_(tline);
  2012. if (!tline)
  2013. {
  2014. } else if (!tok_type_(tline, TOK_NUMBER))
  2015. {
  2016. error(ERR_NONFATAL,
  2017. "`%s' expects a parameter count or nothing",
  2018. directives[i]);
  2019. }
  2020. else
  2021. {
  2022. intn = nasm_readnum(tline->text, &j);
  2023. searching.nparam_min = searching.nparam_max =
  2024. yasm_intnum_get_int(intn);
  2025. yasm_intnum_destroy(intn);
  2026. if (j)
  2027. error(ERR_NONFATAL,
  2028. "unable to parse parameter count `%s'",
  2029. tline->text);
  2030. }
  2031. if (tline && tok_is_(tline->next, "-"))
  2032. {
  2033. tline = tline->next->next;
  2034. if (tok_is_(tline, "*"))
  2035. searching.nparam_max = INT_MAX;
  2036. else if (!tok_type_(tline, TOK_NUMBER))
  2037. error(ERR_NONFATAL,
  2038. "`%s' expects a parameter count after `-'",
  2039. directives[i]);
  2040. else
  2041. {
  2042. intn = nasm_readnum(tline->text, &j);
  2043. searching.nparam_max = yasm_intnum_get_int(intn);
  2044. yasm_intnum_destroy(intn);
  2045. if (j)
  2046. error(ERR_NONFATAL,
  2047. "unable to parse parameter count `%s'",
  2048. tline->text);
  2049. if (searching.nparam_min > searching.nparam_max)
  2050. error(ERR_NONFATAL,
  2051. "minimum parameter count exceeds maximum");
  2052. }
  2053. }
  2054. if (tline && tok_is_(tline->next, "+"))
  2055. {
  2056. tline = tline->next;
  2057. searching.plus = TRUE;
  2058. }
  2059. mmac = mmacros[hash(searching.name)];
  2060. while (mmac)
  2061. {
  2062. if (!strcmp(mmac->name, searching.name) &&
  2063. (mmac->nparam_min <= searching.nparam_max
  2064. || searching.plus)
  2065. && (searching.nparam_min <= mmac->nparam_max
  2066. || mmac->plus))
  2067. {
  2068. found = TRUE;
  2069. break;
  2070. }
  2071. mmac = mmac->next;
  2072. }
  2073. nasm_free(searching.name);
  2074. free_tlist(origline);
  2075. if (i == PP_IFNMACRO || i == PP_ELIFNMACRO)
  2076. found = !found;
  2077. return found;
  2078. }
  2079. case PP_IFID:
  2080. case PP_ELIFID:
  2081. case PP_IFNID:
  2082. case PP_ELIFNID:
  2083. case PP_IFNUM:
  2084. case PP_ELIFNUM:
  2085. case PP_IFNNUM:
  2086. case PP_ELIFNNUM:
  2087. case PP_IFSTR:
  2088. case PP_ELIFSTR:
  2089. case PP_IFNSTR:
  2090. case PP_ELIFNSTR:
  2091. tline = expand_smacro(tline);
  2092. t = tline;
  2093. while (tok_type_(t, TOK_WHITESPACE))
  2094. t = t->next;
  2095. j = FALSE; /* placate optimiser */
  2096. if (t)
  2097. switch (i)
  2098. {
  2099. case PP_IFID:
  2100. case PP_ELIFID:
  2101. case PP_IFNID:
  2102. case PP_ELIFNID:
  2103. j = (t->type == TOK_ID);
  2104. break;
  2105. case PP_IFNUM:
  2106. case PP_ELIFNUM:
  2107. case PP_IFNNUM:
  2108. case PP_ELIFNNUM:
  2109. j = (t->type == TOK_NUMBER);
  2110. break;
  2111. case PP_IFSTR:
  2112. case PP_ELIFSTR:
  2113. case PP_IFNSTR:
  2114. case PP_ELIFNSTR:
  2115. j = (t->type == TOK_STRING);
  2116. break;
  2117. }
  2118. if (i == PP_IFNID || i == PP_ELIFNID ||
  2119. i == PP_IFNNUM || i == PP_ELIFNNUM ||
  2120. i == PP_IFNSTR || i == PP_ELIFNSTR)
  2121. j = !j;
  2122. free_tlist(tline);
  2123. return j;
  2124. case PP_IF:
  2125. case PP_ELIF:
  2126. t = tline = expand_smacro(tline);
  2127. tptr = &t;
  2128. tokval.t_type = TOKEN_INVALID;
  2129. evalresult = evaluate(ppscan, tptr, &tokval, pass | CRITICAL,
  2130. error);
  2131. free_tlist(tline);
  2132. if (!evalresult)
  2133. return -1;
  2134. if (tokval.t_type)
  2135. error(ERR_WARNING,
  2136. "trailing garbage after expression ignored");
  2137. intn = yasm_expr_get_intnum(&evalresult, 0);
  2138. if (!intn)
  2139. {
  2140. error(ERR_NONFATAL,
  2141. "non-constant value given to `%s'", directives[i]);
  2142. yasm_expr_destroy(evalresult);
  2143. return -1;
  2144. }
  2145. j = !yasm_intnum_is_zero(intn);
  2146. yasm_expr_destroy(evalresult);
  2147. return j;
  2148. default:
  2149. error(ERR_FATAL,
  2150. "preprocessor directive `%s' not yet implemented",
  2151. directives[i]);
  2152. free_tlist(origline);
  2153. return -1; /* yeah, right */
  2154. }
  2155. }
  2156. /*
  2157. * Expand macros in a string. Used in %error and %include directives.
  2158. * First tokenise the string, apply "expand_smacro" and then de-tokenise back.
  2159. * The returned variable should ALWAYS be freed after usage.
  2160. */
  2161. static void
  2162. expand_macros_in_string(char **p)
  2163. {
  2164. Token *line = tokenise(*p);
  2165. line = expand_smacro(line);
  2166. *p = detoken(line, FALSE);
  2167. }
  2168. /**
  2169. * find and process preprocessor directive in passed line
  2170. * Find out if a line contains a preprocessor directive, and deal
  2171. * with it if so.
  2172. *
  2173. * If a directive _is_ found, it is the responsibility of this routine
  2174. * (and not the caller) to free_tlist() the line.
  2175. *
  2176. * @param tline a pointer to the current tokeninzed line linked list
  2177. * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
  2178. *
  2179. */
  2180. static int
  2181. do_directive(Token * tline)
  2182. {
  2183. int i, j, k, m, nparam, nolist;
  2184. int offset;
  2185. char *p, *mname, *newname;
  2186. Include *inc;
  2187. Context *ctx;
  2188. Cond *cond;
  2189. SMacro *smac, **smhead;
  2190. MMacro *mmac;
  2191. Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
  2192. Line *l;
  2193. struct tokenval tokval;
  2194. yasm_expr *evalresult;
  2195. MMacro *tmp_defining; /* Used when manipulating rep_nest */
  2196. yasm_intnum *intn;
  2197. origline = tline;
  2198. skip_white_(tline);
  2199. if (!tok_type_(tline, TOK_PREPROC_ID) ||
  2200. (tline->text[1] == '%' || tline->text[1] == '$'
  2201. || tline->text[1] == '!'))
  2202. return NO_DIRECTIVE_FOUND;
  2203. i = -1;
  2204. j = elements(directives);
  2205. while (j - i > 1)
  2206. {
  2207. k = (j + i) / 2;
  2208. m = nasm_stricmp(tline->text, directives[k]);
  2209. if (m == 0) {
  2210. if (tasm_compatible_mode) {
  2211. i = k;
  2212. j = -2;
  2213. } else if (k != PP_ARG && k != PP_LOCAL && k != PP_STACKSIZE) {
  2214. i = k;
  2215. j = -2;
  2216. }
  2217. break;
  2218. }
  2219. else if (m < 0) {
  2220. j = k;
  2221. }
  2222. else
  2223. i = k;
  2224. }
  2225. /*
  2226. * If we're in a non-emitting branch of a condition construct,
  2227. * or walking to the end of an already terminated %rep block,
  2228. * we should ignore all directives except for condition
  2229. * directives.
  2230. */
  2231. if (((istk->conds && !emitting(istk->conds->state)) ||
  2232. (istk->mstk && !istk->mstk->in_progress)) &&
  2233. !is_condition(i))
  2234. {
  2235. return NO_DIRECTIVE_FOUND;
  2236. }
  2237. /*
  2238. * If we're defining a macro or reading a %rep block, we should
  2239. * ignore all directives except for %macro/%imacro (which
  2240. * generate an error), %endm/%endmacro, and (only if we're in a
  2241. * %rep block) %endrep. If we're in a %rep block, another %rep
  2242. * causes an error, so should be let through.
  2243. */
  2244. if (defining && i != PP_MACRO && i != PP_IMACRO &&
  2245. i != PP_ENDMACRO && i != PP_ENDM &&
  2246. (defining->name || (i != PP_ENDREP && i != PP_REP)))
  2247. {
  2248. return NO_DIRECTIVE_FOUND;
  2249. }
  2250. if (defining) {
  2251. if (i == PP_MACRO || i == PP_IMACRO) {
  2252. nested_mac_count++;
  2253. return NO_DIRECTIVE_FOUND;
  2254. } else if (nested_mac_count > 0) {
  2255. if (i == PP_ENDMACRO) {
  2256. nested_mac_count--;
  2257. return NO_DIRECTIVE_FOUND;
  2258. }
  2259. }
  2260. if (!defining->name) {
  2261. if (i == PP_REP) {
  2262. nested_rep_count++;
  2263. return NO_DIRECTIVE_FOUND;
  2264. } else if (nested_rep_count > 0) {
  2265. if (i == PP_ENDREP) {
  2266. nested_rep_count--;
  2267. return NO_DIRECTIVE_FOUND;
  2268. }
  2269. }
  2270. }
  2271. }
  2272. if (j != -2)
  2273. {
  2274. error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
  2275. tline->text);
  2276. return NO_DIRECTIVE_FOUND; /* didn't get it */
  2277. }
  2278. switch (i)
  2279. {
  2280. case PP_STACKSIZE:
  2281. /* Directive to tell NASM what the default stack size is. The
  2282. * default is for a 16-bit stack, and this can be overriden with
  2283. * %stacksize large.
  2284. * the following form:
  2285. *
  2286. * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
  2287. */
  2288. tline = tline->next;
  2289. if (tline && tline->type == TOK_WHITESPACE)
  2290. tline = tline->next;
  2291. if (!tline || tline->type != TOK_ID)
  2292. {
  2293. error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
  2294. free_tlist(origline);
  2295. return DIRECTIVE_FOUND;
  2296. }
  2297. if (nasm_stricmp(tline->text, "flat") == 0)
  2298. {
  2299. /* All subsequent ARG directives are for a 32-bit stack */
  2300. StackSize = 4;
  2301. StackPointer = "ebp";
  2302. ArgOffset = 8;
  2303. LocalOffset = 4;
  2304. }
  2305. else if (nasm_stricmp(tline->text, "large") == 0)
  2306. {
  2307. /* All subsequent ARG directives are for a 16-bit stack,
  2308. * far function call.
  2309. */
  2310. StackSize = 2;
  2311. StackPointer = "bp";
  2312. ArgOffset = 4;
  2313. LocalOffset = 2;
  2314. }
  2315. else if (nasm_stricmp(tline->text, "small") == 0)
  2316. {
  2317. /* All subsequent ARG directives are for a 16-bit stack,
  2318. * far function call. We don't support near functions.
  2319. */
  2320. StackSize = 2;
  2321. StackPointer = "bp";
  2322. ArgOffset = 6;
  2323. LocalOffset = 2;
  2324. }
  2325. else
  2326. {
  2327. error(ERR_NONFATAL, "`%%stacksize' invalid size type");
  2328. free_tlist(origline);
  2329. return DIRECTIVE_FOUND;
  2330. }
  2331. free_tlist(origline);
  2332. return DIRECTIVE_FOUND;
  2333. case PP_ARG:
  2334. /* TASM like ARG directive to define arguments to functions, in
  2335. * the following form:
  2336. *
  2337. * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
  2338. */
  2339. offset = ArgOffset;
  2340. do
  2341. {
  2342. char *arg, directive[256];
  2343. int size = StackSize;
  2344. /* Find the argument name */
  2345. tline = tline->next;
  2346. if (tline && tline->type == TOK_WHITESPACE)
  2347. tline = tline->next;
  2348. if (!tline || tline->type != TOK_ID)
  2349. {
  2350. error(ERR_NONFATAL, "`%%arg' missing argument parameter");
  2351. free_tlist(origline);
  2352. return DIRECTIVE_FOUND;
  2353. }
  2354. arg = tline->text;
  2355. /* Find the argument size type */
  2356. tline = tline->next;
  2357. if (!tline || tline->type != TOK_OTHER
  2358. || tline->text[0] != ':')
  2359. {
  2360. error(ERR_NONFATAL,
  2361. "Syntax error processing `%%arg' directive");
  2362. free_tlist(origline);
  2363. return DIRECTIVE_FOUND;
  2364. }
  2365. tline = tline->next;
  2366. if (!tline || tline->type != TOK_ID)
  2367. {
  2368. error(ERR_NONFATAL,
  2369. "`%%arg' missing size type parameter");
  2370. free_tlist(origline);
  2371. return DIRECTIVE_FOUND;
  2372. }
  2373. /* Allow macro expansion of type parameter */
  2374. tt = tokenise(tline->text);
  2375. tt = expand_smacro(tt);
  2376. if (nasm_stricmp(tt->text, "byte") == 0)
  2377. {
  2378. size = MAX(StackSize, 1);
  2379. }
  2380. else if (nasm_stricmp(tt->text, "word") == 0)
  2381. {
  2382. size = MAX(StackSize, 2);
  2383. }
  2384. else if (nasm_stricmp(tt->text, "dword") == 0)
  2385. {
  2386. size = MAX(StackSize, 4);
  2387. }
  2388. else if (nasm_stricmp(tt->text, "qword") == 0)
  2389. {
  2390. size = MAX(StackSize, 8);
  2391. }
  2392. else if (nasm_stricmp(tt->text, "tword") == 0)
  2393. {
  2394. size = MAX(StackSize, 10);
  2395. }
  2396. else
  2397. {
  2398. error(ERR_NONFATAL,
  2399. "Invalid size type for `%%arg' missing directive");
  2400. free_tlist(tt);
  2401. free_tlist(origline);
  2402. return DIRECTIVE_FOUND;
  2403. }
  2404. free_tlist(tt);
  2405. /* Now define the macro for the argument */
  2406. sprintf(directive, "%%define %s (%s+%d)", arg, StackPointer,
  2407. offset);
  2408. do_directive(tokenise(directive));
  2409. offset += size;
  2410. /* Move to the next argument in the list */
  2411. tline = tline->next;
  2412. if (tline && tline->type == TOK_WHITESPACE)
  2413. tline = tline->next;
  2414. }
  2415. while (tline && tline->type == TOK_OTHER
  2416. && tline->text[0] == ',');
  2417. free_tlist(origline);
  2418. return DIRECTIVE_FOUND;
  2419. case PP_LOCAL:
  2420. /* TASM like LOCAL directive to define local variables for a
  2421. * function, in the following form:
  2422. *
  2423. * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
  2424. *
  2425. * The '= LocalSize' at the end is ignored by NASM, but is
  2426. * required by TASM to define the local parameter size (and used
  2427. * by the TASM macro package).
  2428. */
  2429. offset = LocalOffset;
  2430. do
  2431. {
  2432. char *local, directive[256];
  2433. int size = StackSize;
  2434. /* Find the argument name */
  2435. tline = tline->next;
  2436. if (tline && tline->type == TOK_WHITESPACE)
  2437. tline = tline->next;
  2438. if (!tline || tline->type != TOK_ID)
  2439. {
  2440. error(ERR_NONFATAL,
  2441. "`%%local' missing argument parameter");
  2442. free_tlist(origline);
  2443. return DIRECTIVE_FOUND;
  2444. }
  2445. local = tline->text;
  2446. /* Find the argument size type */
  2447. tline = tline->next;
  2448. if (!tline || tline->type != TOK_OTHER
  2449. || tline->text[0] != ':')
  2450. {
  2451. error(ERR_NONFATAL,
  2452. "Syntax error processing `%%local' directive");
  2453. free_tlist(origline);
  2454. return DIRECTIVE_FOUND;
  2455. }
  2456. tline = tline->next;
  2457. if (!tline || tline->type != TOK_ID)
  2458. {
  2459. error(ERR_NONFATAL,
  2460. "`%%local' missing size type parameter");
  2461. free_tlist(origline);
  2462. return DIRECTIVE_FOUND;
  2463. }
  2464. /* Allow macro expansion of type parameter */
  2465. tt = tokenise(tline->text);
  2466. tt = expand_smacro(tt);
  2467. if (nasm_stricmp(tt->text, "byte") == 0)
  2468. {
  2469. size = MAX(StackSize, 1);
  2470. }
  2471. else if (nasm_stricmp(tt->text, "word") == 0)
  2472. {
  2473. size = MAX(StackSize, 2);
  2474. }
  2475. else if (nasm_stricmp(tt->text, "dword") == 0)
  2476. {
  2477. size = MAX(StackSize, 4);
  2478. }
  2479. else if (nasm_stricmp(tt->text, "qword") == 0)
  2480. {
  2481. size = MAX(StackSize, 8);
  2482. }
  2483. else if (nasm_stricmp(tt->text, "tword") == 0)
  2484. {
  2485. size = MAX(StackSize, 10);
  2486. }
  2487. else
  2488. {
  2489. error(ERR_NONFATAL,
  2490. "Invalid size type for `%%local' missing directive");
  2491. free_tlist(tt);
  2492. free_tlist(origline);
  2493. return DIRECTIVE_FOUND;
  2494. }
  2495. free_tlist(tt);
  2496. /* Now define the macro for the argument */
  2497. sprintf(directive, "%%define %s (%s-%d)", local, StackPointer,
  2498. offset);
  2499. do_directive(tokenise(directive));
  2500. offset += size;
  2501. /* Now define the assign to setup the enter_c macro correctly */
  2502. sprintf(directive, "%%assign %%$localsize %%$localsize+%d",
  2503. size);
  2504. do_directive(tokenise(directive));
  2505. /* Move to the next argument in the list */
  2506. tline = tline->next;
  2507. if (tline && tline->type == TOK_WHITESPACE)
  2508. tline = tline->next;
  2509. }
  2510. while (tline && tline->type == TOK_OTHER
  2511. && tline->text[0] == ',');
  2512. free_tlist(origline);
  2513. return DIRECTIVE_FOUND;
  2514. case PP_CLEAR:
  2515. if (tline->next)
  2516. error(ERR_WARNING,
  2517. "trailing garbage after `%%clear' ignored");
  2518. for (j = 0; j < NHASH; j++)
  2519. {
  2520. while (mmacros[j])
  2521. {
  2522. MMacro *m2 = mmacros[j];
  2523. mmacros[j] = m2->next;
  2524. free_mmacro(m2);
  2525. }
  2526. while (smacros[j])
  2527. {
  2528. SMacro *s = smacros[j];
  2529. smacros[j] = smacros[j]->next;
  2530. nasm_free(s->name);
  2531. free_tlist(s->expansion);
  2532. nasm_free(s);
  2533. }
  2534. }
  2535. free_tlist(origline);
  2536. return DIRECTIVE_FOUND;
  2537. case PP_INCLUDE:
  2538. tline = tline->next;
  2539. skip_white_(tline);
  2540. if (!tline || (tline->type != TOK_STRING &&
  2541. tline->type != TOK_INTERNAL_STRING))
  2542. {
  2543. error(ERR_NONFATAL, "`%%include' expects a file name");
  2544. free_tlist(origline);
  2545. return DIRECTIVE_FOUND; /* but we did _something_ */
  2546. }
  2547. if (tline->next)
  2548. error(ERR_WARNING,
  2549. "trailing garbage after `%%include' ignored");
  2550. if (tline->type != TOK_INTERNAL_STRING)
  2551. {
  2552. p = tline->text + 1; /* point past the quote to the name */
  2553. p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
  2554. }
  2555. else
  2556. p = tline->text; /* internal_string is easier */
  2557. expand_macros_in_string(&p);
  2558. inc = nasm_malloc(sizeof(Include));
  2559. inc->next = istk;
  2560. inc->conds = NULL;
  2561. inc->fp = inc_fopen(p, &newname);
  2562. inc->fname = nasm_src_set_fname(newname);
  2563. inc->lineno = nasm_src_set_linnum(0);
  2564. inc->lineinc = 1;
  2565. inc->expansion = NULL;
  2566. inc->mstk = NULL;
  2567. istk = inc;
  2568. list->uplevel(LIST_INCLUDE);
  2569. free_tlist(origline);
  2570. return DIRECTIVE_FOUND;
  2571. case PP_PUSH:
  2572. tline = tline->next;
  2573. skip_white_(tline);
  2574. tline = expand_id(tline);
  2575. if (!tok_type_(tline, TOK_ID))
  2576. {
  2577. error(ERR_NONFATAL, "`%%push' expects a context identifier");
  2578. free_tlist(origline);
  2579. return DIRECTIVE_FOUND; /* but we did _something_ */
  2580. }
  2581. if (tline->next)
  2582. error(ERR_WARNING, "trailing garbage after `%%push' ignored");
  2583. ctx = nasm_malloc(sizeof(Context));
  2584. ctx->next = cstk;
  2585. ctx->localmac = NULL;
  2586. ctx->name = nasm_strdup(tline->text);
  2587. ctx->number = unique++;
  2588. cstk = ctx;
  2589. free_tlist(origline);
  2590. break;
  2591. case PP_REPL:
  2592. tline = tline->next;
  2593. skip_white_(tline);
  2594. tline = expand_id(tline);
  2595. if (!tok_type_(tline, TOK_ID))
  2596. {
  2597. error(ERR_NONFATAL, "`%%repl' expects a context identifier");
  2598. free_tlist(origline);
  2599. return DIRECTIVE_FOUND; /* but we did _something_ */
  2600. }
  2601. if (tline->next)
  2602. error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
  2603. if (!cstk)
  2604. error(ERR_NONFATAL, "`%%repl': context stack is empty");
  2605. else
  2606. {
  2607. nasm_free(cstk->name);
  2608. cstk->name = nasm_strdup(tline->text);
  2609. }
  2610. free_tlist(origline);
  2611. break;
  2612. case PP_POP:
  2613. if (tline->next)
  2614. error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
  2615. if (!cstk)
  2616. error(ERR_NONFATAL,
  2617. "`%%pop': context stack is already empty");
  2618. else
  2619. ctx_pop();
  2620. free_tlist(origline);
  2621. break;
  2622. case PP_SCOPE:
  2623. if (tline->next)
  2624. error(ERR_WARNING, "trailing garbage after `%%scope' ignored");
  2625. Level++;
  2626. free_tlist(origline);
  2627. break;
  2628. case PP_ENDSCOPE:
  2629. if (tline->next)
  2630. error(ERR_WARNING, "trailing garbage after `%%endscope' ignored");
  2631. if (!Level)
  2632. error(ERR_NONFATAL,
  2633. "`%%endscope': already popped all levels");
  2634. else
  2635. {
  2636. for (k = 0; k < NHASH; k++)
  2637. {
  2638. SMacro **smlast = &smacros[k];
  2639. smac = smacros[k];
  2640. while (smac)
  2641. {
  2642. if (smac->level < Level)
  2643. {
  2644. smlast = &smac->next;
  2645. smac = smac->next;
  2646. }
  2647. else
  2648. {
  2649. *smlast = smac->next;
  2650. nasm_free(smac->name);
  2651. free_tlist(smac->expansion);
  2652. nasm_free(smac);
  2653. smac = *smlast;
  2654. }
  2655. }
  2656. }
  2657. for (ctx = cstk; ctx; ctx = ctx->next)
  2658. {
  2659. SMacro **smlast = &ctx->localmac;
  2660. smac = ctx->localmac;
  2661. while (smac)
  2662. {
  2663. if (smac->level < Level)
  2664. {
  2665. smlast = &smac->next;
  2666. smac = smac->next;
  2667. }
  2668. else
  2669. {
  2670. *smlast = smac->next;
  2671. nasm_free(smac->name);
  2672. free_tlist(smac->expansion);
  2673. nasm_free(smac);
  2674. smac = *smlast;
  2675. }
  2676. }
  2677. }
  2678. Level--;
  2679. }
  2680. free_tlist(origline);
  2681. break;
  2682. case PP_ERROR:
  2683. tline->next = expand_smacro(tline->next);
  2684. tline = tline->next;
  2685. skip_white_(tline);
  2686. if (tok_type_(tline, TOK_STRING))
  2687. {
  2688. p = tline->text + 1; /* point past the quote to the name */
  2689. p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
  2690. expand_macros_in_string(&p);
  2691. error(ERR_NONFATAL, "%s", p);
  2692. nasm_free(p);
  2693. }
  2694. else
  2695. {
  2696. p = detoken(tline, FALSE);
  2697. error(ERR_WARNING, "%s", p);
  2698. nasm_free(p);
  2699. }
  2700. free_tlist(origline);
  2701. break;
  2702. case PP_IF:
  2703. case PP_IFCTX:
  2704. case PP_IFDEF:
  2705. case PP_IFID:
  2706. case PP_IFIDN:
  2707. case PP_IFIDNI:
  2708. case PP_IFMACRO:
  2709. case PP_IFNCTX:
  2710. case PP_IFNDEF:
  2711. case PP_IFNID:
  2712. case PP_IFNIDN:
  2713. case PP_IFNIDNI:
  2714. case PP_IFNMACRO:
  2715. case PP_IFNNUM:
  2716. case PP_IFNSTR:
  2717. case PP_IFNUM:
  2718. case PP_IFSTR:
  2719. if (istk->conds && !emitting(istk->conds->state))
  2720. j = COND_NEVER;
  2721. else
  2722. {
  2723. j = if_condition(tline->next, i);
  2724. tline->next = NULL; /* it got freed */
  2725. j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
  2726. }
  2727. free_tlist(origline);
  2728. cond = nasm_malloc(sizeof(Cond));
  2729. cond->next = istk->conds;
  2730. cond->state = j;
  2731. istk->conds = cond;
  2732. return DIRECTIVE_FOUND;
  2733. case PP_ELIF:
  2734. case PP_ELIFCTX:
  2735. case PP_ELIFDEF:
  2736. case PP_ELIFID:
  2737. case PP_ELIFIDN:
  2738. case PP_ELIFIDNI:
  2739. case PP_ELIFMACRO:
  2740. case PP_ELIFNCTX:
  2741. case PP_ELIFNDEF:
  2742. case PP_ELIFNID:
  2743. case PP_ELIFNIDN:
  2744. case PP_ELIFNIDNI:
  2745. case PP_ELIFNMACRO:
  2746. case PP_ELIFNNUM:
  2747. case PP_ELIFNSTR:
  2748. case PP_ELIFNUM:
  2749. case PP_ELIFSTR:
  2750. if (!istk->conds)
  2751. error(ERR_FATAL, "`%s': no matching `%%if'", directives[i]);
  2752. if (emitting(istk->conds->state)
  2753. || istk->conds->state == COND_NEVER)
  2754. istk->conds->state = COND_NEVER;
  2755. else
  2756. {
  2757. /*
  2758. * IMPORTANT: In the case of %if, we will already have
  2759. * called expand_mmac_params(); however, if we're
  2760. * processing an %elif we must have been in a
  2761. * non-emitting mode, which would have inhibited
  2762. * the normal invocation of expand_mmac_params(). Therefore,
  2763. * we have to do it explicitly here.
  2764. */
  2765. j = if_condition(expand_mmac_params(tline->next), i);
  2766. tline->next = NULL; /* it got freed */
  2767. istk->conds->state =
  2768. j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
  2769. }
  2770. free_tlist(origline);
  2771. return DIRECTIVE_FOUND;
  2772. case PP_ELSE:
  2773. if (tline->next)
  2774. error(ERR_WARNING, "trailing garbage after `%%else' ignored");
  2775. if (!istk->conds)
  2776. error(ERR_FATAL, "`%%else': no matching `%%if'");
  2777. if (emitting(istk->conds->state)
  2778. || istk->conds->state == COND_NEVER)
  2779. istk->conds->state = COND_ELSE_FALSE;
  2780. else
  2781. istk->conds->state = COND_ELSE_TRUE;
  2782. free_tlist(origline);
  2783. return DIRECTIVE_FOUND;
  2784. case PP_ENDIF:
  2785. if (tline->next)
  2786. error(ERR_WARNING,
  2787. "trailing garbage after `%%endif' ignored");
  2788. if (!istk->conds)
  2789. error(ERR_FATAL, "`%%endif': no matching `%%if'");
  2790. cond = istk->conds;
  2791. istk->conds = cond->next;
  2792. nasm_free(cond);
  2793. free_tlist(origline);
  2794. return DIRECTIVE_FOUND;
  2795. case PP_MACRO:
  2796. case PP_IMACRO:
  2797. if (defining)
  2798. error(ERR_FATAL,
  2799. "`%%%smacro': already defining a macro",
  2800. (i == PP_IMACRO ? "i" : ""));
  2801. tline = tline->next;
  2802. skip_white_(tline);
  2803. tline = expand_id(tline);
  2804. if (!tok_type_(tline, TOK_ID))
  2805. {
  2806. error(ERR_NONFATAL,
  2807. "`%%%smacro' expects a macro name",
  2808. (i == PP_IMACRO ? "i" : ""));
  2809. return DIRECTIVE_FOUND;
  2810. }
  2811. defining = nasm_malloc(sizeof(MMacro));
  2812. defining->name = nasm_strdup(tline->text);
  2813. defining->casesense = (i == PP_MACRO);
  2814. defining->plus = FALSE;
  2815. defining->nolist = FALSE;
  2816. defining->in_progress = FALSE;
  2817. defining->rep_nest = NULL;
  2818. tline = expand_smacro(tline->next);
  2819. skip_white_(tline);
  2820. if (!tok_type_(tline, TOK_NUMBER))
  2821. {
  2822. error(ERR_NONFATAL,
  2823. "`%%%smacro' expects a parameter count",
  2824. (i == PP_IMACRO ? "i" : ""));
  2825. defining->nparam_min = defining->nparam_max = 0;
  2826. }
  2827. else
  2828. {
  2829. intn = nasm_readnum(tline->text, &j);
  2830. defining->nparam_min = defining->nparam_max =
  2831. yasm_intnum_get_int(intn);
  2832. yasm_intnum_destroy(intn);
  2833. if (j)
  2834. error(ERR_NONFATAL,
  2835. "unable to parse parameter count `%s'",
  2836. tline->text);
  2837. }
  2838. if (tline && tok_is_(tline->next, "-"))
  2839. {
  2840. tline = tline->next->next;
  2841. if (tok_is_(tline, "*"))
  2842. defining->nparam_max = INT_MAX;
  2843. else if (!tok_type_(tline, TOK_NUMBER))
  2844. error(ERR_NONFATAL,
  2845. "`%%%smacro' expects a parameter count after `-'",
  2846. (i == PP_IMACRO ? "i" : ""));
  2847. else
  2848. {
  2849. intn = nasm_readnum(tline->text, &j);
  2850. defining->nparam_max = yasm_intnum_get_int(intn);
  2851. yasm_intnum_destroy(intn);
  2852. if (j)
  2853. error(ERR_NONFATAL,
  2854. "unable to parse parameter count `%s'",
  2855. tline->text);
  2856. if (defining->nparam_min > defining->nparam_max)
  2857. error(ERR_NONFATAL,
  2858. "minimum parameter count exceeds maximum");
  2859. }
  2860. }
  2861. if (tline && tok_is_(tline->next, "+"))
  2862. {
  2863. tline = tline->next;
  2864. defining->plus = TRUE;
  2865. }
  2866. if (tline && tok_type_(tline->next, TOK_ID) &&
  2867. !nasm_stricmp(tline->next->text, ".nolist"))
  2868. {
  2869. tline = tline->next;
  2870. defining->nolist = TRUE;
  2871. }
  2872. mmac = mmacros[hash(defining->name)];
  2873. while (mmac)
  2874. {
  2875. if (!strcmp(mmac->name, defining->name) &&
  2876. (mmac->nparam_min <= defining->nparam_max
  2877. || defining->plus)
  2878. && (defining->nparam_min <= mmac->nparam_max
  2879. || mmac->plus))
  2880. {
  2881. error(ERR_WARNING,
  2882. "redefining multi-line macro `%s'",
  2883. defining->name);
  2884. break;
  2885. }
  2886. mmac = mmac->next;
  2887. }
  2888. /*
  2889. * Handle default parameters.
  2890. */
  2891. if (tline && tline->next)
  2892. {
  2893. defining->dlist = tline->next;
  2894. tline->next = NULL;
  2895. count_mmac_params(defining->dlist, &defining->ndefs,
  2896. &defining->defaults);
  2897. }
  2898. else
  2899. {
  2900. defining->dlist = NULL;
  2901. defining->defaults = NULL;
  2902. }
  2903. defining->expansion = NULL;
  2904. free_tlist(origline);
  2905. return DIRECTIVE_FOUND;
  2906. case PP_ENDM:
  2907. case PP_ENDMACRO:
  2908. if (!defining)
  2909. {
  2910. error(ERR_NONFATAL, "`%s': not defining a macro",
  2911. tline->text);
  2912. return DIRECTIVE_FOUND;
  2913. }
  2914. k = hash(defining->name);
  2915. defining->next = mmacros[k];
  2916. mmacros[k] = defining;
  2917. defining = NULL;
  2918. free_tlist(origline);
  2919. return DIRECTIVE_FOUND;
  2920. case PP_ROTATE:
  2921. if (tline->next && tline->next->type == TOK_WHITESPACE)
  2922. tline = tline->next;
  2923. if (tline->next == NULL)
  2924. {
  2925. free_tlist(origline);
  2926. error(ERR_NONFATAL, "`%%rotate' missing rotate count");
  2927. return DIRECTIVE_FOUND;
  2928. }
  2929. t = expand_smacro(tline->next);
  2930. tline->next = NULL;
  2931. free_tlist(origline);
  2932. tline = t;
  2933. tptr = &t;
  2934. tokval.t_type = TOKEN_INVALID;
  2935. evalresult = evaluate(ppscan, tptr, &tokval, pass, error);
  2936. free_tlist(tline);
  2937. if (!evalresult)
  2938. return DIRECTIVE_FOUND;
  2939. if (tokval.t_type)
  2940. error(ERR_WARNING,
  2941. "trailing garbage after expression ignored");
  2942. intn = yasm_expr_get_intnum(&evalresult, 0);
  2943. if (!intn)
  2944. {
  2945. error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
  2946. yasm_expr_destroy(evalresult);
  2947. return DIRECTIVE_FOUND;
  2948. }
  2949. mmac = istk->mstk;
  2950. while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
  2951. mmac = mmac->next_active;
  2952. if (!mmac)
  2953. {
  2954. error(ERR_NONFATAL,
  2955. "`%%rotate' invoked outside a macro call");
  2956. }
  2957. else if (mmac->nparam == 0)
  2958. {
  2959. error(ERR_NONFATAL,
  2960. "`%%rotate' invoked within macro without parameters");
  2961. }
  2962. else
  2963. {
  2964. mmac->rotate = mmac->rotate + yasm_intnum_get_int(intn);
  2965. if (mmac->rotate < 0)
  2966. mmac->rotate =
  2967. mmac->nparam - (-mmac->rotate) % mmac->nparam;
  2968. mmac->rotate %= mmac->nparam;
  2969. }
  2970. yasm_expr_destroy(evalresult);
  2971. return DIRECTIVE_FOUND;
  2972. case PP_REP:
  2973. nolist = FALSE;
  2974. do {
  2975. tline = tline->next;
  2976. } while (tok_type_(tline, TOK_WHITESPACE));
  2977. if (tok_type_(tline, TOK_ID) &&
  2978. nasm_stricmp(tline->text, ".nolist") == 0)
  2979. {
  2980. nolist = TRUE;
  2981. do {
  2982. tline = tline->next;
  2983. } while (tok_type_(tline, TOK_WHITESPACE));
  2984. }
  2985. if (tline)
  2986. {
  2987. t = expand_smacro(tline);
  2988. tptr = &t;
  2989. tokval.t_type = TOKEN_INVALID;
  2990. evalresult = evaluate(ppscan, tptr, &tokval, pass, error);
  2991. if (!evalresult)
  2992. {
  2993. free_tlist(origline);
  2994. return DIRECTIVE_FOUND;
  2995. }
  2996. if (tokval.t_type)
  2997. error(ERR_WARNING,
  2998. "trailing garbage after expression ignored");
  2999. intn = yasm_expr_get_intnum(&evalresult, 0);
  3000. if (!intn)
  3001. {
  3002. error(ERR_NONFATAL, "non-constant value given to `%%rep'");
  3003. yasm_expr_destroy(evalresult);
  3004. return DIRECTIVE_FOUND;
  3005. }
  3006. i = (int)yasm_intnum_get_int(intn) + 1;
  3007. yasm_expr_destroy(evalresult);
  3008. }
  3009. else
  3010. {
  3011. error(ERR_NONFATAL, "`%%rep' expects a repeat count");
  3012. i = 0;
  3013. }
  3014. free_tlist(origline);
  3015. tmp_defining = defining;
  3016. defining = nasm_malloc(sizeof(MMacro));
  3017. defining->name = NULL; /* flags this macro as a %rep block */
  3018. defining->casesense = 0;
  3019. defining->plus = FALSE;
  3020. defining->nolist = nolist;
  3021. defining->in_progress = i;
  3022. defining->nparam_min = defining->nparam_max = 0;
  3023. defining->defaults = NULL;
  3024. defining->dlist = NULL;
  3025. defining->expansion = NULL;
  3026. defining->next_active = istk->mstk;
  3027. defining->rep_nest = tmp_defining;
  3028. return DIRECTIVE_FOUND;
  3029. case PP_ENDREP:
  3030. if (!defining || defining->name)
  3031. {
  3032. error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
  3033. return DIRECTIVE_FOUND;
  3034. }
  3035. /*
  3036. * Now we have a "macro" defined - although it has no name
  3037. * and we won't be entering it in the hash tables - we must
  3038. * push a macro-end marker for it on to istk->expansion.
  3039. * After that, it will take care of propagating itself (a
  3040. * macro-end marker line for a macro which is really a %rep
  3041. * block will cause the macro to be re-expanded, complete
  3042. * with another macro-end marker to ensure the process
  3043. * continues) until the whole expansion is forcibly removed
  3044. * from istk->expansion by a %exitrep.
  3045. */
  3046. l = nasm_malloc(sizeof(Line));
  3047. l->next = istk->expansion;
  3048. l->finishes = defining;
  3049. l->first = NULL;
  3050. istk->expansion = l;
  3051. istk->mstk = defining;
  3052. list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
  3053. tmp_defining = defining;
  3054. defining = defining->rep_nest;
  3055. free_tlist(origline);
  3056. return DIRECTIVE_FOUND;
  3057. case PP_EXITREP:
  3058. /*
  3059. * We must search along istk->expansion until we hit a
  3060. * macro-end marker for a macro with no name. Then we set
  3061. * its `in_progress' flag to 0.
  3062. */
  3063. for (l = istk->expansion; l; l = l->next)
  3064. if (l->finishes && !l->finishes->name)
  3065. break;
  3066. if (l)
  3067. l->finishes->in_progress = 0;
  3068. else
  3069. error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
  3070. free_tlist(origline);
  3071. return DIRECTIVE_FOUND;
  3072. case PP_XDEFINE:
  3073. case PP_IXDEFINE:
  3074. case PP_DEFINE:
  3075. case PP_IDEFINE:
  3076. tline = tline->next;
  3077. skip_white_(tline);
  3078. tline = expand_id(tline);
  3079. if (!tline || (tline->type != TOK_ID &&
  3080. (tline->type != TOK_PREPROC_ID ||
  3081. tline->text[1] != '$')))
  3082. {
  3083. error(ERR_NONFATAL,
  3084. "`%%%s%sdefine' expects a macro identifier",
  3085. ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
  3086. ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
  3087. free_tlist(origline);
  3088. return DIRECTIVE_FOUND;
  3089. }
  3090. ctx = get_ctx(tline->text, FALSE);
  3091. if (!ctx)
  3092. smhead = &smacros[hash(tline->text)];
  3093. else
  3094. smhead = &ctx->localmac;
  3095. mname = tline->text;
  3096. last = tline;
  3097. param_start = tline = tline->next;
  3098. nparam = 0;
  3099. /* Expand the macro definition now for %xdefine and %ixdefine */
  3100. if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
  3101. tline = expand_smacro(tline);
  3102. if (tok_is_(tline, "("))
  3103. {
  3104. /*
  3105. * This macro has parameters.
  3106. */
  3107. tline = tline->next;
  3108. while (1)
  3109. {
  3110. skip_white_(tline);
  3111. if (!tline)
  3112. {
  3113. error(ERR_NONFATAL, "parameter identifier expected");
  3114. free_tlist(origline);
  3115. return DIRECTIVE_FOUND;
  3116. }
  3117. if (tline->type != TOK_ID)
  3118. {
  3119. error(ERR_NONFATAL,
  3120. "`%s': parameter identifier expected",
  3121. tline->text);
  3122. free_tlist(origline);
  3123. return DIRECTIVE_FOUND;
  3124. }
  3125. tline->type = TOK_SMAC_PARAM + nparam++;
  3126. tline = tline->next;
  3127. skip_white_(tline);
  3128. if (tok_is_(tline, ","))
  3129. {
  3130. tline = tline->next;
  3131. continue;
  3132. }
  3133. if (!tok_is_(tline, ")"))
  3134. {
  3135. error(ERR_NONFATAL,
  3136. "`)' expected to terminate macro template");
  3137. free_tlist(origline);
  3138. return DIRECTIVE_FOUND;
  3139. }
  3140. break;
  3141. }
  3142. last = tline;
  3143. tline = tline->next;
  3144. }
  3145. if (tok_type_(tline, TOK_WHITESPACE))
  3146. last = tline, tline = tline->next;
  3147. macro_start = NULL;
  3148. last->next = NULL;
  3149. t = tline;
  3150. while (t)
  3151. {
  3152. if (t->type == TOK_ID)
  3153. {
  3154. for (tt = param_start; tt; tt = tt->next)
  3155. if (tt->type >= TOK_SMAC_PARAM &&
  3156. !strcmp(tt->text, t->text))
  3157. t->type = tt->type;
  3158. }
  3159. tt = t->next;
  3160. t->next = macro_start;
  3161. macro_start = t;
  3162. t = tt;
  3163. }
  3164. /*
  3165. * Good. We now have a macro name, a parameter count, and a
  3166. * token list (in reverse order) for an expansion. We ought
  3167. * to be OK just to create an SMacro, store it, and let
  3168. * free_tlist have the rest of the line (which we have
  3169. * carefully re-terminated after chopping off the expansion
  3170. * from the end).
  3171. */
  3172. if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE))
  3173. {
  3174. if (!smac)
  3175. {
  3176. error(ERR_WARNING,
  3177. "single-line macro `%s' defined both with and"
  3178. " without parameters", mname);
  3179. free_tlist(origline);
  3180. free_tlist(macro_start);
  3181. return DIRECTIVE_FOUND;
  3182. }
  3183. else if (smac->level == Level)
  3184. {
  3185. /*
  3186. * We're redefining in the same level, so we have to
  3187. * take over an existing SMacro structure. This means
  3188. * freeing what was already in it.
  3189. */
  3190. nasm_free(smac->name);
  3191. free_tlist(smac->expansion);
  3192. }
  3193. else
  3194. {
  3195. smac = nasm_malloc(sizeof(SMacro));
  3196. smac->next = *smhead;
  3197. *smhead = smac;
  3198. }
  3199. }
  3200. else
  3201. {
  3202. smac = nasm_malloc(sizeof(SMacro));
  3203. smac->next = *smhead;
  3204. *smhead = smac;
  3205. }
  3206. smac->name = nasm_strdup(mname);
  3207. smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
  3208. smac->nparam = nparam;
  3209. smac->level = Level;
  3210. smac->expansion = macro_start;
  3211. smac->in_progress = FALSE;
  3212. free_tlist(origline);
  3213. return DIRECTIVE_FOUND;
  3214. case PP_UNDEF:
  3215. tline = tline->next;
  3216. skip_white_(tline);
  3217. tline = expand_id(tline);
  3218. if (!tline || (tline->type != TOK_ID &&
  3219. (tline->type != TOK_PREPROC_ID ||
  3220. tline->text[1] != '$')))
  3221. {
  3222. error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
  3223. free_tlist(origline);
  3224. return DIRECTIVE_FOUND;
  3225. }
  3226. if (tline->next)
  3227. {
  3228. error(ERR_WARNING,
  3229. "trailing garbage after macro name ignored");
  3230. }
  3231. /* Find the context that symbol belongs to */
  3232. ctx = get_ctx(tline->text, FALSE);
  3233. if (!ctx)
  3234. smhead = &smacros[hash(tline->text)];
  3235. else
  3236. smhead = &ctx->localmac;
  3237. mname = tline->text;
  3238. /*
  3239. * We now have a macro name... go hunt for it.
  3240. */
  3241. while (smacro_defined(ctx, mname, -1, &smac, 1))
  3242. {
  3243. /* Defined, so we need to find its predecessor and nuke it */
  3244. SMacro **s;
  3245. for (s = smhead; *s && *s != smac; s = &(*s)->next);
  3246. if (*s)
  3247. {
  3248. *s = smac->next;
  3249. nasm_free(smac->name);
  3250. free_tlist(smac->expansion);
  3251. nasm_free(smac);
  3252. }
  3253. }
  3254. free_tlist(origline);
  3255. return DIRECTIVE_FOUND;
  3256. case PP_STRLEN:
  3257. tline = tline->next;
  3258. skip_white_(tline);
  3259. tline = expand_id(tline);
  3260. if (!tline || (tline->type != TOK_ID &&
  3261. (tline->type != TOK_PREPROC_ID ||
  3262. tline->text[1] != '$')))
  3263. {
  3264. error(ERR_NONFATAL,
  3265. "`%%strlen' expects a macro identifier as first parameter");
  3266. free_tlist(origline);
  3267. return DIRECTIVE_FOUND;
  3268. }
  3269. ctx = get_ctx(tline->text, FALSE);
  3270. if (!ctx)
  3271. smhead = &smacros[hash(tline->text)];
  3272. else
  3273. smhead = &ctx->localmac;
  3274. mname = tline->text;
  3275. last = tline;
  3276. tline = expand_smacro(tline->next);
  3277. last->next = NULL;
  3278. t = tline;
  3279. while (tok_type_(t, TOK_WHITESPACE))
  3280. t = t->next;
  3281. /* t should now point to the string */
  3282. if (t->type != TOK_STRING)
  3283. {
  3284. error(ERR_NONFATAL,
  3285. "`%%strlen` requires string as second parameter");
  3286. free_tlist(tline);
  3287. free_tlist(origline);
  3288. return DIRECTIVE_FOUND;
  3289. }
  3290. macro_start = nasm_malloc(sizeof(*macro_start));
  3291. macro_start->next = NULL;
  3292. make_tok_num(macro_start,
  3293. yasm_intnum_create_uint((unsigned long)(strlen(t->text) - 2)));
  3294. macro_start->mac = NULL;
  3295. /*
  3296. * We now have a macro name, an implicit parameter count of
  3297. * zero, and a numeric token to use as an expansion. Create
  3298. * and store an SMacro.
  3299. */
  3300. if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN))
  3301. {
  3302. if (!smac)
  3303. error(ERR_WARNING,
  3304. "single-line macro `%s' defined both with and"
  3305. " without parameters", mname);
  3306. else
  3307. {
  3308. /*
  3309. * We're redefining, so we have to take over an
  3310. * existing SMacro structure. This means freeing
  3311. * what was already in it.
  3312. */
  3313. nasm_free(smac->name);
  3314. free_tlist(smac->expansion);
  3315. }
  3316. }
  3317. else
  3318. {
  3319. smac = nasm_malloc(sizeof(SMacro));
  3320. smac->next = *smhead;
  3321. *smhead = smac;
  3322. }
  3323. smac->name = nasm_strdup(mname);
  3324. smac->casesense = (i == PP_STRLEN);
  3325. smac->nparam = 0;
  3326. smac->level = 0;
  3327. smac->expansion = macro_start;
  3328. smac->in_progress = FALSE;
  3329. free_tlist(tline);
  3330. free_tlist(origline);
  3331. return DIRECTIVE_FOUND;
  3332. case PP_SUBSTR:
  3333. tline = tline->next;
  3334. skip_white_(tline);
  3335. tline = expand_id(tline);
  3336. if (!tline || (tline->type != TOK_ID &&
  3337. (tline->type != TOK_PREPROC_ID ||
  3338. tline->text[1] != '$')))
  3339. {
  3340. error(ERR_NONFATAL,
  3341. "`%%substr' expects a macro identifier as first parameter");
  3342. free_tlist(origline);
  3343. return DIRECTIVE_FOUND;
  3344. }
  3345. ctx = get_ctx(tline->text, FALSE);
  3346. if (!ctx)
  3347. smhead = &smacros[hash(tline->text)];
  3348. else
  3349. smhead = &ctx->localmac;
  3350. mname = tline->text;
  3351. last = tline;
  3352. tline = expand_smacro(tline->next);
  3353. last->next = NULL;
  3354. t = tline->next;
  3355. while (tok_type_(t, TOK_WHITESPACE))
  3356. t = t->next;
  3357. /* t should now point to the string */
  3358. if (t->type != TOK_STRING)
  3359. {
  3360. error(ERR_NONFATAL,
  3361. "`%%substr` requires string as second parameter");
  3362. free_tlist(tline);
  3363. free_tlist(origline);
  3364. return DIRECTIVE_FOUND;
  3365. }
  3366. tt = t->next;
  3367. tptr = &tt;
  3368. tokval.t_type = TOKEN_INVALID;
  3369. evalresult = evaluate(ppscan, tptr, &tokval, pass, error);
  3370. if (!evalresult)
  3371. {
  3372. free_tlist(tline);
  3373. free_tlist(origline);
  3374. return DIRECTIVE_FOUND;
  3375. }
  3376. intn = yasm_expr_get_intnum(&evalresult, 0);
  3377. if (!intn)
  3378. {
  3379. error(ERR_NONFATAL, "non-constant value given to `%%substr`");
  3380. free_tlist(tline);
  3381. free_tlist(origline);
  3382. yasm_expr_destroy(evalresult);
  3383. return DIRECTIVE_FOUND;
  3384. }
  3385. macro_start = nasm_malloc(sizeof(*macro_start));
  3386. macro_start->next = NULL;
  3387. macro_start->text = nasm_strdup("'''");
  3388. if (yasm_intnum_sign(intn) == 1
  3389. && yasm_intnum_get_uint(intn) < strlen(t->text) - 1)
  3390. {
  3391. macro_start->text[1] = t->text[yasm_intnum_get_uint(intn)];
  3392. }
  3393. else
  3394. {
  3395. macro_start->text[2] = '\0';
  3396. }
  3397. yasm_expr_destroy(evalresult);
  3398. macro_start->type = TOK_STRING;
  3399. macro_start->mac = NULL;
  3400. /*
  3401. * We now have a macro name, an implicit parameter count of
  3402. * zero, and a numeric token to use as an expansion. Create
  3403. * and store an SMacro.
  3404. */
  3405. if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR))
  3406. {
  3407. if (!smac)
  3408. error(ERR_WARNING,
  3409. "single-line macro `%s' defined both with and"
  3410. " without parameters", mname);
  3411. else
  3412. {
  3413. /*
  3414. * We're redefining, so we have to take over an
  3415. * existing SMacro structure. This means freeing
  3416. * what was already in it.
  3417. */
  3418. nasm_free(smac->name);
  3419. free_tlist(smac->expansion);
  3420. }
  3421. }
  3422. else
  3423. {
  3424. smac = nasm_malloc(sizeof(SMacro));
  3425. smac->next = *smhead;
  3426. *smhead = smac;
  3427. }
  3428. smac->name = nasm_strdup(mname);
  3429. smac->casesense = (i == PP_SUBSTR);
  3430. smac->nparam = 0;
  3431. smac->level = 0;
  3432. smac->expansion = macro_start;
  3433. smac->in_progress = FALSE;
  3434. free_tlist(tline);
  3435. free_tlist(origline);
  3436. return DIRECTIVE_FOUND;
  3437. case PP_ASSIGN:
  3438. case PP_IASSIGN:
  3439. tline = tline->next;
  3440. skip_white_(tline);
  3441. tline = expand_id(tline);
  3442. if (!tline || (tline->type != TOK_ID &&
  3443. (tline->type != TOK_PREPROC_ID ||
  3444. tline->text[1] != '$')))
  3445. {
  3446. error(ERR_NONFATAL,
  3447. "`%%%sassign' expects a macro identifier",
  3448. (i == PP_IASSIGN ? "i" : ""));
  3449. free_tlist(origline);
  3450. return DIRECTIVE_FOUND;
  3451. }
  3452. ctx = get_ctx(tline->text, FALSE);
  3453. if (!ctx)
  3454. smhead = &smacros[hash(tline->text)];
  3455. else
  3456. smhead = &ctx->localmac;
  3457. mname = tline->text;
  3458. last = tline;
  3459. tline = expand_smacro(tline->next);
  3460. last->next = NULL;
  3461. t = tline;
  3462. tptr = &t;
  3463. tokval.t_type = TOKEN_INVALID;
  3464. evalresult = evaluate(ppscan, tptr, &tokval, pass, error);
  3465. free_tlist(tline);
  3466. if (!evalresult)
  3467. {
  3468. free_tlist(origline);
  3469. return DIRECTIVE_FOUND;
  3470. }
  3471. if (tokval.t_type)
  3472. error(ERR_WARNING,
  3473. "trailing garbage after expression ignored");
  3474. intn = yasm_expr_get_intnum(&evalresult, 0);
  3475. if (!intn)
  3476. {
  3477. error(ERR_NONFATAL,
  3478. "non-constant value given to `%%%sassign'",
  3479. (i == PP_IASSIGN ? "i" : ""));
  3480. free_tlist(origline);
  3481. yasm_expr_destroy(evalresult);
  3482. return DIRECTIVE_FOUND;
  3483. }
  3484. macro_start = nasm_malloc(sizeof(*macro_start));
  3485. macro_start->next = NULL;
  3486. make_tok_num(macro_start, yasm_intnum_copy(intn));
  3487. yasm_expr_destroy(evalresult);
  3488. macro_start->mac = NULL;
  3489. /*
  3490. * We now have a macro name, an implicit parameter count of
  3491. * zero, and a numeric token to use as an expansion. Create
  3492. * and store an SMacro.
  3493. */
  3494. if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN))
  3495. {
  3496. if (!smac)
  3497. error(ERR_WARNING,
  3498. "single-line macro `%s' defined both with and"
  3499. " without parameters", mname);
  3500. else
  3501. {
  3502. /*
  3503. * We're redefining, so we have to take over an
  3504. * existing SMacro structure. This means freeing
  3505. * what was already in it.
  3506. */
  3507. nasm_free(smac->name);
  3508. free_tlist(smac->expansion);
  3509. }
  3510. }
  3511. else
  3512. {
  3513. smac = nasm_malloc(sizeof(SMacro));
  3514. smac->next = *smhead;
  3515. *smhead = smac;
  3516. }
  3517. smac->name = nasm_strdup(mname);
  3518. smac->casesense = (i == PP_ASSIGN);
  3519. smac->nparam = 0;
  3520. smac->level = 0;
  3521. smac->expansion = macro_start;
  3522. smac->in_progress = FALSE;
  3523. free_tlist(origline);
  3524. return DIRECTIVE_FOUND;
  3525. case PP_LINE:
  3526. /*
  3527. * Syntax is `%line nnn[+mmm] [filename]'
  3528. */
  3529. tline = tline->next;
  3530. skip_white_(tline);
  3531. if (!tok_type_(tline, TOK_NUMBER))
  3532. {
  3533. error(ERR_NONFATAL, "`%%line' expects line number");
  3534. free_tlist(origline);
  3535. return DIRECTIVE_FOUND;
  3536. }
  3537. intn = nasm_readnum(tline->text, &j);
  3538. k = yasm_intnum_get_int(intn);
  3539. yasm_intnum_destroy(intn);
  3540. m = 1;
  3541. tline = tline->next;
  3542. if (tok_is_(tline, "+"))
  3543. {
  3544. tline = tline->next;
  3545. if (!tok_type_(tline, TOK_NUMBER))
  3546. {
  3547. error(ERR_NONFATAL, "`%%line' expects line increment");
  3548. free_tlist(origline);
  3549. return DIRECTIVE_FOUND;
  3550. }
  3551. intn = nasm_readnum(tline->text, &j);
  3552. m = yasm_intnum_get_int(intn);
  3553. yasm_intnum_destroy(intn);
  3554. tline = tline->next;
  3555. }
  3556. skip_white_(tline);
  3557. nasm_src_set_linnum(k);
  3558. istk->lineinc = m;
  3559. if (tline)
  3560. {
  3561. nasm_free(nasm_src_set_fname(detoken(tline, FALSE)));
  3562. }
  3563. free_tlist(origline);
  3564. return DIRECTIVE_FOUND;
  3565. default:
  3566. error(ERR_FATAL,
  3567. "preprocessor directive `%s' not yet implemented",
  3568. directives[i]);
  3569. break;
  3570. }
  3571. return DIRECTIVE_FOUND;
  3572. }
  3573. /*
  3574. * Ensure that a macro parameter contains a condition code and
  3575. * nothing else. Return the condition code index if so, or -1
  3576. * otherwise.
  3577. */
  3578. static int
  3579. find_cc(Token * t)
  3580. {
  3581. Token *tt;
  3582. int i, j, k, m;
  3583. skip_white_(t);
  3584. if (t->type != TOK_ID)
  3585. return -1;
  3586. tt = t->next;
  3587. skip_white_(tt);
  3588. if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
  3589. return -1;
  3590. i = -1;
  3591. j = elements(conditions);
  3592. while (j - i > 1)
  3593. {
  3594. k = (j + i) / 2;
  3595. m = nasm_stricmp(t->text, conditions[k]);
  3596. if (m == 0)
  3597. {
  3598. i = k;
  3599. j = -2;
  3600. break;
  3601. }
  3602. else if (m < 0)
  3603. {
  3604. j = k;
  3605. }
  3606. else
  3607. i = k;
  3608. }
  3609. if (j != -2)
  3610. return -1;
  3611. return i;
  3612. }
  3613. /*
  3614. * Expand MMacro-local things: parameter references (%0, %n, %+n,
  3615. * %-n) and MMacro-local identifiers (%%foo).
  3616. */
  3617. static Token *
  3618. expand_mmac_params(Token * tline)
  3619. {
  3620. Token *t, *tt, **tail, *thead;
  3621. tail = &thead;
  3622. thead = NULL;
  3623. while (tline)
  3624. {
  3625. if (tline->type == TOK_PREPROC_ID &&
  3626. (((tline->text[1] == '+' || tline->text[1] == '-')
  3627. && tline->text[2]) || tline->text[1] == '%'
  3628. || (tline->text[1] >= '0' && tline->text[1] <= '9')))
  3629. {
  3630. char *text = NULL;
  3631. int type = 0, cc; /* type = 0 to placate optimisers */
  3632. char tmpbuf[30];
  3633. char *second_text = NULL;
  3634. int n, i;
  3635. MMacro *mac;
  3636. t = tline;
  3637. tline = tline->next;
  3638. second_text = strchr(t->text, ':');
  3639. mac = istk->mstk;
  3640. while (mac && !mac->name) /* avoid mistaking %reps for macros */
  3641. mac = mac->next_active;
  3642. if (!mac)
  3643. error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
  3644. else
  3645. {
  3646. if (second_text)
  3647. {
  3648. int end = atoi(second_text+1)-1;
  3649. int is_fst = 1;
  3650. int k;
  3651. n = atoi(t->text + 1)-1;
  3652. if (end < 0)
  3653. end += mac->nparam;
  3654. for (k = n; k <= end; k++)
  3655. {
  3656. if (k >= mac->nparam)
  3657. tt = NULL;
  3658. else
  3659. {
  3660. if (mac->nparam > 1)
  3661. k = (k + mac->rotate) % mac->nparam;
  3662. tt = mac->params[k];
  3663. }
  3664. if (tt)
  3665. {
  3666. if (!is_fst && mac->paramlen[k])
  3667. {
  3668. *tail = new_Token(NULL, TOK_OTHER, ",", 0);
  3669. tail = &(*tail)->next;
  3670. }
  3671. if (mac->paramlen[k])
  3672. is_fst = 0;
  3673. for (i = 0; i < mac->paramlen[k]; i++)
  3674. {
  3675. *tail =
  3676. new_Token(NULL, tt->type, tt->text,
  3677. 0);
  3678. tail = &(*tail)->next;
  3679. tt = tt->next;
  3680. }
  3681. }
  3682. text = NULL; /* we've done it here */
  3683. }
  3684. }
  3685. else
  3686. {
  3687. switch (t->text[1])
  3688. {
  3689. /*
  3690. * We have to make a substitution of one of the
  3691. * forms %1, %-1, %+1, %%foo, %0.
  3692. */
  3693. case '0':
  3694. type = TOK_NUMBER;
  3695. sprintf(tmpbuf, "%ld", mac->nparam);
  3696. text = nasm_strdup(tmpbuf);
  3697. break;
  3698. case '%':
  3699. type = TOK_ID;
  3700. sprintf(tmpbuf, "..@%lu.", mac->unique);
  3701. text = nasm_strcat(tmpbuf, t->text + 2);
  3702. break;
  3703. case '-':
  3704. n = atoi(t->text + 2) - 1;
  3705. if (n >= mac->nparam)
  3706. tt = NULL;
  3707. else
  3708. {
  3709. if (mac->nparam > 1)
  3710. n = (n + mac->rotate) % mac->nparam;
  3711. tt = mac->params[n];
  3712. }
  3713. cc = find_cc(tt);
  3714. if (cc == -1)
  3715. {
  3716. error(ERR_NONFATAL,
  3717. "macro parameter %d is not a condition code",
  3718. n + 1);
  3719. text = NULL;
  3720. }
  3721. else
  3722. {
  3723. type = TOK_ID;
  3724. if (inverse_ccs[cc] == -1)
  3725. {
  3726. error(ERR_NONFATAL,
  3727. "condition code `%s' is not invertible",
  3728. conditions[cc]);
  3729. text = NULL;
  3730. }
  3731. else
  3732. text =
  3733. nasm_strdup(conditions[inverse_ccs
  3734. [cc]]);
  3735. }
  3736. break;
  3737. case '+':
  3738. n = atoi(t->text + 2) - 1;
  3739. if (n >= mac->nparam)
  3740. tt = NULL;
  3741. else
  3742. {
  3743. if (mac->nparam > 1)
  3744. n = (n + mac->rotate) % mac->nparam;
  3745. tt = mac->params[n];
  3746. }
  3747. cc = find_cc(tt);
  3748. if (cc == -1)
  3749. {
  3750. error(ERR_NONFATAL,
  3751. "macro parameter %d is not a condition code",
  3752. n + 1);
  3753. text = NULL;
  3754. }
  3755. else
  3756. {
  3757. type = TOK_ID;
  3758. text = nasm_strdup(conditions[cc]);
  3759. }
  3760. break;
  3761. default:
  3762. n = atoi(t->text + 1) - 1;
  3763. if (n >= mac->nparam)
  3764. tt = NULL;
  3765. else
  3766. {
  3767. if (mac->nparam > 1)
  3768. n = (n + mac->rotate) % mac->nparam;
  3769. tt = mac->params[n];
  3770. }
  3771. if (tt)
  3772. {
  3773. for (i = 0; i < mac->paramlen[n]; i++)
  3774. {
  3775. *tail =
  3776. new_Token(NULL, tt->type, tt->text,
  3777. 0);
  3778. tail = &(*tail)->next;
  3779. tt = tt->next;
  3780. }
  3781. }
  3782. text = NULL; /* we've done it here */
  3783. break;
  3784. }
  3785. }
  3786. }
  3787. if (!text)
  3788. {
  3789. delete_Token(t);
  3790. }
  3791. else
  3792. {
  3793. *tail = t;
  3794. tail = &t->next;
  3795. t->type = type;
  3796. nasm_free(t->text);
  3797. t->text = text;
  3798. t->mac = NULL;
  3799. }
  3800. continue;
  3801. }
  3802. else
  3803. {
  3804. t = *tail = tline;
  3805. tline = tline->next;
  3806. t->mac = NULL;
  3807. tail = &t->next;
  3808. }
  3809. }
  3810. *tail = NULL;
  3811. t = thead;
  3812. for (; t && (tt = t->next) != NULL; t = t->next)
  3813. switch (t->type)
  3814. {
  3815. case TOK_WHITESPACE:
  3816. if (tt->type == TOK_WHITESPACE)
  3817. {
  3818. t->next = delete_Token(tt);
  3819. }
  3820. break;
  3821. case TOK_ID:
  3822. if (tt->type == TOK_ID || tt->type == TOK_NUMBER)
  3823. {
  3824. char *tmp = nasm_strcat(t->text, tt->text);
  3825. nasm_free(t->text);
  3826. t->text = tmp;
  3827. t->next = delete_Token(tt);
  3828. }
  3829. break;
  3830. case TOK_NUMBER:
  3831. if (tt->type == TOK_NUMBER)
  3832. {
  3833. char *tmp = nasm_strcat(t->text, tt->text);
  3834. nasm_free(t->text);
  3835. t->text = tmp;
  3836. t->next = delete_Token(tt);
  3837. }
  3838. break;
  3839. }
  3840. return thead;
  3841. }
  3842. /*
  3843. * Expand all single-line macro calls made in the given line.
  3844. * Return the expanded version of the line. The original is deemed
  3845. * to be destroyed in the process. (In reality we'll just move
  3846. * Tokens from input to output a lot of the time, rather than
  3847. * actually bothering to destroy and replicate.)
  3848. */
  3849. static Token *
  3850. expand_smacro(Token * tline)
  3851. {
  3852. Token *t, *tt, *mstart, **tail, *thead;
  3853. SMacro *head = NULL, *m;
  3854. Token **params;
  3855. int *paramsize;
  3856. int nparam, sparam, brackets, rescan;
  3857. Token *org_tline = tline;
  3858. Context *ctx;
  3859. char *mname;
  3860. /*
  3861. * Trick: we should avoid changing the start token pointer since it can
  3862. * be contained in "next" field of other token. Because of this
  3863. * we allocate a copy of first token and work with it; at the end of
  3864. * routine we copy it back
  3865. */
  3866. if (org_tline)
  3867. {
  3868. tline =
  3869. new_Token(org_tline->next, org_tline->type, org_tline->text,
  3870. 0);
  3871. tline->mac = org_tline->mac;
  3872. nasm_free(org_tline->text);
  3873. org_tline->text = NULL;
  3874. }
  3875. again:
  3876. tail = &thead;
  3877. thead = NULL;
  3878. while (tline)
  3879. { /* main token loop */
  3880. if ((mname = tline->text))
  3881. {
  3882. /* if this token is a local macro, look in local context */
  3883. if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
  3884. ctx = get_ctx(mname, TRUE);
  3885. else
  3886. ctx = NULL;
  3887. if (!ctx)
  3888. head = smacros[hash(mname)];
  3889. else
  3890. head = ctx->localmac;
  3891. /*
  3892. * We've hit an identifier. As in is_mmacro below, we first
  3893. * check whether the identifier is a single-line macro at
  3894. * all, then think about checking for parameters if
  3895. * necessary.
  3896. */
  3897. for (m = head; m; m = m->next)
  3898. if (!mstrcmp(m->name, mname, m->casesense))
  3899. break;
  3900. if (m)
  3901. {
  3902. mstart = tline;
  3903. params = NULL;
  3904. paramsize = NULL;
  3905. if (m->nparam == 0)
  3906. {
  3907. /*
  3908. * Simple case: the macro is parameterless. Discard the
  3909. * one token that the macro call took, and push the
  3910. * expansion back on the to-do stack.
  3911. */
  3912. if (!m->expansion)
  3913. {
  3914. if (!strcmp("__FILE__", m->name))
  3915. {
  3916. long num = 0;
  3917. nasm_src_get(&num, &(tline->text));
  3918. nasm_quote(&(tline->text));
  3919. tline->type = TOK_STRING;
  3920. continue;
  3921. }
  3922. if (!strcmp("__LINE__", m->name))
  3923. {
  3924. nasm_free(tline->text);
  3925. make_tok_num(tline, yasm_intnum_create_int(nasm_src_get_linnum()));
  3926. continue;
  3927. }
  3928. tline = delete_Token(tline);
  3929. continue;
  3930. }
  3931. }
  3932. else
  3933. {
  3934. /*
  3935. * Complicated case: at least one macro with this name
  3936. * exists and takes parameters. We must find the
  3937. * parameters in the call, count them, find the SMacro
  3938. * that corresponds to that form of the macro call, and
  3939. * substitute for the parameters when we expand. What a
  3940. * pain.
  3941. */
  3942. /*tline = tline->next;
  3943. skip_white_(tline);*/
  3944. do {
  3945. t = tline->next;
  3946. while (tok_type_(t, TOK_SMAC_END))
  3947. {
  3948. t->mac->in_progress = FALSE;
  3949. t->text = NULL;
  3950. t = tline->next = delete_Token(t);
  3951. }
  3952. tline = t;
  3953. } while (tok_type_(tline, TOK_WHITESPACE));
  3954. if (!tok_is_(tline, "("))
  3955. {
  3956. /*
  3957. * This macro wasn't called with parameters: ignore
  3958. * the call. (Behaviour borrowed from gnu cpp.)
  3959. */
  3960. tline = mstart;
  3961. m = NULL;
  3962. }
  3963. else
  3964. {
  3965. int paren = 0;
  3966. int white = 0;
  3967. brackets = 0;
  3968. nparam = 0;
  3969. sparam = PARAM_DELTA;
  3970. params = nasm_malloc(sparam * sizeof(Token *));
  3971. params[0] = tline->next;
  3972. paramsize = nasm_malloc(sparam * sizeof(int));
  3973. paramsize[0] = 0;
  3974. while (TRUE)
  3975. { /* parameter loop */
  3976. /*
  3977. * For some unusual expansions
  3978. * which concatenates function call
  3979. */
  3980. t = tline->next;
  3981. while (tok_type_(t, TOK_SMAC_END))
  3982. {
  3983. t->mac->in_progress = FALSE;
  3984. t->text = NULL;
  3985. t = tline->next = delete_Token(t);
  3986. }
  3987. tline = t;
  3988. if (!tline)
  3989. {
  3990. error(ERR_NONFATAL,
  3991. "macro call expects terminating `)'");
  3992. break;
  3993. }
  3994. if (tline->type == TOK_WHITESPACE
  3995. && brackets <= 0)
  3996. {
  3997. if (paramsize[nparam])
  3998. white++;
  3999. else
  4000. params[nparam] = tline->next;
  4001. continue; /* parameter loop */
  4002. }
  4003. if (tline->type == TOK_OTHER
  4004. && tline->text[1] == 0)
  4005. {
  4006. char ch = tline->text[0];
  4007. if (ch == ',' && !paren && brackets <= 0)
  4008. {
  4009. if (++nparam >= sparam)
  4010. {
  4011. sparam += PARAM_DELTA;
  4012. params = nasm_realloc(params,
  4013. sparam * sizeof(Token *));
  4014. paramsize = nasm_realloc(paramsize,
  4015. sparam * sizeof(int));
  4016. }
  4017. params[nparam] = tline->next;
  4018. paramsize[nparam] = 0;
  4019. white = 0;
  4020. continue; /* parameter loop */
  4021. }
  4022. if (ch == '{' &&
  4023. (brackets > 0 || (brackets == 0 &&
  4024. !paramsize[nparam])))
  4025. {
  4026. if (!(brackets++))
  4027. {
  4028. params[nparam] = tline->next;
  4029. continue; /* parameter loop */
  4030. }
  4031. }
  4032. if (ch == '}' && brackets > 0)
  4033. if (--brackets == 0)
  4034. {
  4035. brackets = -1;
  4036. continue; /* parameter loop */
  4037. }
  4038. if (ch == '(' && !brackets)
  4039. paren++;
  4040. if (ch == ')' && brackets <= 0)
  4041. if (--paren < 0)
  4042. break;
  4043. }
  4044. if (brackets < 0)
  4045. {
  4046. brackets = 0;
  4047. error(ERR_NONFATAL, "braces do not "
  4048. "enclose all of macro parameter");
  4049. }
  4050. paramsize[nparam] += white + 1;
  4051. white = 0;
  4052. } /* parameter loop */
  4053. nparam++;
  4054. while (m && (m->nparam != nparam ||
  4055. mstrcmp(m->name, mname,
  4056. m->casesense)))
  4057. m = m->next;
  4058. if (!m)
  4059. error(ERR_WARNING | ERR_WARN_MNP,
  4060. "macro `%s' exists, "
  4061. "but not taking %d parameters",
  4062. mstart->text, nparam);
  4063. }
  4064. }
  4065. if (m && m->in_progress)
  4066. m = NULL;
  4067. if (!m) /* in progess or didn't find '(' or wrong nparam */
  4068. {
  4069. /*
  4070. * Design question: should we handle !tline, which
  4071. * indicates missing ')' here, or expand those
  4072. * macros anyway, which requires the (t) test a few
  4073. * lines down?
  4074. */
  4075. nasm_free(params);
  4076. nasm_free(paramsize);
  4077. tline = mstart;
  4078. }
  4079. else
  4080. {
  4081. /*
  4082. * Expand the macro: we are placed on the last token of the
  4083. * call, so that we can easily split the call from the
  4084. * following tokens. We also start by pushing an SMAC_END
  4085. * token for the cycle removal.
  4086. */
  4087. t = tline;
  4088. if (t)
  4089. {
  4090. tline = t->next;
  4091. t->next = NULL;
  4092. }
  4093. tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
  4094. tt->mac = m;
  4095. m->in_progress = TRUE;
  4096. tline = tt;
  4097. for (t = m->expansion; t; t = t->next)
  4098. {
  4099. if (t->type >= TOK_SMAC_PARAM)
  4100. {
  4101. Token *pcopy = tline, **ptail = &pcopy;
  4102. Token *ttt, *pt;
  4103. int i;
  4104. ttt = params[t->type - TOK_SMAC_PARAM];
  4105. for (i = paramsize[t->type - TOK_SMAC_PARAM];
  4106. --i >= 0;)
  4107. {
  4108. pt = *ptail =
  4109. new_Token(tline, ttt->type, ttt->text,
  4110. 0);
  4111. ptail = &pt->next;
  4112. ttt = ttt->next;
  4113. }
  4114. tline = pcopy;
  4115. }
  4116. else
  4117. {
  4118. tt = new_Token(tline, t->type, t->text, 0);
  4119. tline = tt;
  4120. }
  4121. }
  4122. /*
  4123. * Having done that, get rid of the macro call, and clean
  4124. * up the parameters.
  4125. */
  4126. nasm_free(params);
  4127. nasm_free(paramsize);
  4128. free_tlist(mstart);
  4129. continue; /* main token loop */
  4130. }
  4131. }
  4132. }
  4133. if (tline->type == TOK_SMAC_END)
  4134. {
  4135. tline->mac->in_progress = FALSE;
  4136. tline = delete_Token(tline);
  4137. }
  4138. else
  4139. {
  4140. t = *tail = tline;
  4141. tline = tline->next;
  4142. t->mac = NULL;
  4143. t->next = NULL;
  4144. tail = &t->next;
  4145. }
  4146. }
  4147. /*
  4148. * Now scan the entire line and look for successive TOK_IDs that resulted
  4149. * after expansion (they can't be produced by tokenise()). The successive
  4150. * TOK_IDs should be concatenated.
  4151. * Also we look for %+ tokens and concatenate the tokens before and after
  4152. * them (without white spaces in between).
  4153. */
  4154. t = thead;
  4155. rescan = 0;
  4156. while (t)
  4157. {
  4158. while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
  4159. t = t->next;
  4160. if (!t || !t->next)
  4161. break;
  4162. if (t->next->type == TOK_ID ||
  4163. t->next->type == TOK_PREPROC_ID ||
  4164. t->next->type == TOK_NUMBER)
  4165. {
  4166. char *p = nasm_strcat(t->text, t->next->text);
  4167. nasm_free(t->text);
  4168. t->next = delete_Token(t->next);
  4169. t->text = p;
  4170. rescan = 1;
  4171. }
  4172. else if (t->next->type == TOK_WHITESPACE && t->next->next &&
  4173. t->next->next->type == TOK_PREPROC_ID &&
  4174. strcmp(t->next->next->text, "%+") == 0)
  4175. {
  4176. /* free the next whitespace, the %+ token and next whitespace */
  4177. int i;
  4178. for (i = 1; i <= 3; i++)
  4179. {
  4180. if (!t->next || (i != 2 && t->next->type != TOK_WHITESPACE))
  4181. break;
  4182. t->next = delete_Token(t->next);
  4183. } /* endfor */
  4184. }
  4185. else
  4186. t = t->next;
  4187. }
  4188. /* If we concatenaded something, re-scan the line for macros */
  4189. if (rescan)
  4190. {
  4191. tline = thead;
  4192. goto again;
  4193. }
  4194. if (org_tline)
  4195. {
  4196. if (thead)
  4197. {
  4198. *org_tline = *thead;
  4199. /* since we just gave text to org_line, don't free it */
  4200. thead->text = NULL;
  4201. delete_Token(thead);
  4202. }
  4203. else
  4204. {
  4205. /* the expression expanded to empty line;
  4206. we can't return NULL for some reasons
  4207. we just set the line to a single WHITESPACE token. */
  4208. memset(org_tline, 0, sizeof(*org_tline));
  4209. org_tline->text = NULL;
  4210. org_tline->type = TOK_WHITESPACE;
  4211. }
  4212. thead = org_tline;
  4213. }
  4214. return thead;
  4215. }
  4216. /*
  4217. * Similar to expand_smacro but used exclusively with macro identifiers
  4218. * right before they are fetched in. The reason is that there can be
  4219. * identifiers consisting of several subparts. We consider that if there
  4220. * are more than one element forming the name, user wants a expansion,
  4221. * otherwise it will be left as-is. Example:
  4222. *
  4223. * %define %$abc cde
  4224. *
  4225. * the identifier %$abc will be left as-is so that the handler for %define
  4226. * will suck it and define the corresponding value. Other case:
  4227. *
  4228. * %define _%$abc cde
  4229. *
  4230. * In this case user wants name to be expanded *before* %define starts
  4231. * working, so we'll expand %$abc into something (if it has a value;
  4232. * otherwise it will be left as-is) then concatenate all successive
  4233. * PP_IDs into one.
  4234. */
  4235. static Token *
  4236. expand_id(Token * tline)
  4237. {
  4238. Token *cur, *oldnext = NULL;
  4239. if (!tline || !tline->next)
  4240. return tline;
  4241. cur = tline;
  4242. while (cur->next &&
  4243. (cur->next->type == TOK_ID ||
  4244. cur->next->type == TOK_PREPROC_ID || cur->next->type == TOK_NUMBER))
  4245. cur = cur->next;
  4246. /* If identifier consists of just one token, don't expand */
  4247. if (cur == tline)
  4248. return tline;
  4249. if (cur)
  4250. {
  4251. oldnext = cur->next; /* Detach the tail past identifier */
  4252. cur->next = NULL; /* so that expand_smacro stops here */
  4253. }
  4254. tline = expand_smacro(tline);
  4255. if (cur)
  4256. {
  4257. /* expand_smacro possibly changhed tline; re-scan for EOL */
  4258. cur = tline;
  4259. while (cur && cur->next)
  4260. cur = cur->next;
  4261. if (cur)
  4262. cur->next = oldnext;
  4263. }
  4264. return tline;
  4265. }
  4266. /*
  4267. * Determine whether the given line constitutes a multi-line macro
  4268. * call, and return the MMacro structure called if so. Doesn't have
  4269. * to check for an initial label - that's taken care of in
  4270. * expand_mmacro - but must check numbers of parameters. Guaranteed
  4271. * to be called with tline->type == TOK_ID, so the putative macro
  4272. * name is easy to find.
  4273. */
  4274. static MMacro *
  4275. is_mmacro(Token * tline, Token *** params_array)
  4276. {
  4277. MMacro *head, *m;
  4278. Token **params;
  4279. int nparam;
  4280. head = mmacros[hash(tline->text)];
  4281. /*
  4282. * Efficiency: first we see if any macro exists with the given
  4283. * name. If not, we can return NULL immediately. _Then_ we
  4284. * count the parameters, and then we look further along the
  4285. * list if necessary to find the proper MMacro.
  4286. */
  4287. for (m = head; m; m = m->next)
  4288. if (!mstrcmp(m->name, tline->text, m->casesense))
  4289. break;
  4290. if (!m)
  4291. return NULL;
  4292. /*
  4293. * OK, we have a potential macro. Count and demarcate the
  4294. * parameters.
  4295. */
  4296. count_mmac_params(tline->next, &nparam, &params);
  4297. /*
  4298. * So we know how many parameters we've got. Find the MMacro
  4299. * structure that handles this number.
  4300. */
  4301. while (m)
  4302. {
  4303. if (m->nparam_min <= nparam && (m->plus || nparam <= m->nparam_max))
  4304. {
  4305. /*
  4306. * This one is right. Just check if cycle removal
  4307. * prohibits us using it before we actually celebrate...
  4308. */
  4309. if (m->in_progress)
  4310. {
  4311. #if 0
  4312. error(ERR_NONFATAL,
  4313. "self-reference in multi-line macro `%s'", m->name);
  4314. #endif
  4315. nasm_free(params);
  4316. return NULL;
  4317. }
  4318. /*
  4319. * It's right, and we can use it. Add its default
  4320. * parameters to the end of our list if necessary.
  4321. */
  4322. if (m->defaults && nparam < m->nparam_min + m->ndefs)
  4323. {
  4324. params =
  4325. nasm_realloc(params,
  4326. ((m->nparam_min + m->ndefs + 1) * sizeof(*params)));
  4327. while (nparam < m->nparam_min + m->ndefs)
  4328. {
  4329. params[nparam] = m->defaults[nparam - m->nparam_min];
  4330. nparam++;
  4331. }
  4332. }
  4333. /*
  4334. * If we've gone over the maximum parameter count (and
  4335. * we're in Plus mode), ignore parameters beyond
  4336. * nparam_max.
  4337. */
  4338. if (m->plus && nparam > m->nparam_max)
  4339. nparam = m->nparam_max;
  4340. /*
  4341. * Then terminate the parameter list, and leave.
  4342. */
  4343. if (!params)
  4344. { /* need this special case */
  4345. params = nasm_malloc(sizeof(*params));
  4346. nparam = 0;
  4347. }
  4348. params[nparam] = NULL;
  4349. *params_array = params;
  4350. return m;
  4351. }
  4352. /*
  4353. * This one wasn't right: look for the next one with the
  4354. * same name.
  4355. */
  4356. for (m = m->next; m; m = m->next)
  4357. if (!mstrcmp(m->name, tline->text, m->casesense))
  4358. break;
  4359. }
  4360. /*
  4361. * After all that, we didn't find one with the right number of
  4362. * parameters. Issue a warning, and fail to expand the macro.
  4363. */
  4364. error(ERR_WARNING | ERR_WARN_MNP,
  4365. "macro `%s' exists, but not taking %d parameters",
  4366. tline->text, nparam);
  4367. nasm_free(params);
  4368. return NULL;
  4369. }
  4370. /*
  4371. * Expand the multi-line macro call made by the given line, if
  4372. * there is one to be expanded. If there is, push the expansion on
  4373. * istk->expansion and return 1. Otherwise return 0.
  4374. */
  4375. static int
  4376. expand_mmacro(Token * tline)
  4377. {
  4378. Token *startline = tline;
  4379. Token *label = NULL;
  4380. int dont_prepend = 0;
  4381. Token **params, *t, *tt;
  4382. MMacro *m;
  4383. Line *l, *ll;
  4384. int i, nparam;
  4385. long *paramlen;
  4386. t = tline;
  4387. skip_white_(t);
  4388. /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
  4389. if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
  4390. return 0;
  4391. m = is_mmacro(t, &params);
  4392. if (!m)
  4393. {
  4394. Token *last;
  4395. /*
  4396. * We have an id which isn't a macro call. We'll assume
  4397. * it might be a label; we'll also check to see if a
  4398. * colon follows it. Then, if there's another id after
  4399. * that lot, we'll check it again for macro-hood.
  4400. */
  4401. label = last = t;
  4402. t = t->next;
  4403. if (tok_type_(t, TOK_WHITESPACE))
  4404. last = t, t = t->next;
  4405. if (tok_is_(t, ":"))
  4406. {
  4407. dont_prepend = 1;
  4408. last = t, t = t->next;
  4409. if (tok_type_(t, TOK_WHITESPACE))
  4410. last = t, t = t->next;
  4411. }
  4412. if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
  4413. return 0;
  4414. last->next = NULL;
  4415. tline = t;
  4416. }
  4417. /*
  4418. * Fix up the parameters: this involves stripping leading and
  4419. * trailing whitespace, then stripping braces if they are
  4420. * present.
  4421. */
  4422. for (nparam = 0; params[nparam]; nparam++)
  4423. ;
  4424. paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
  4425. for (i = 0; params[i]; i++)
  4426. {
  4427. int brace = FALSE;
  4428. int comma = (!m->plus || i < nparam - 1);
  4429. t = params[i];
  4430. skip_white_(t);
  4431. if (tok_is_(t, "{"))
  4432. t = t->next, brace = TRUE, comma = FALSE;
  4433. params[i] = t;
  4434. paramlen[i] = 0;
  4435. while (t)
  4436. {
  4437. if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
  4438. break; /* ... because we have hit a comma */
  4439. if (comma && t->type == TOK_WHITESPACE && tok_is_(t->next, ","))
  4440. break; /* ... or a space then a comma */
  4441. if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
  4442. break; /* ... or a brace */
  4443. t = t->next;
  4444. paramlen[i]++;
  4445. }
  4446. }
  4447. /*
  4448. * OK, we have a MMacro structure together with a set of
  4449. * parameters. We must now go through the expansion and push
  4450. * copies of each Line on to istk->expansion. Substitution of
  4451. * parameter tokens and macro-local tokens doesn't get done
  4452. * until the single-line macro substitution process; this is
  4453. * because delaying them allows us to change the semantics
  4454. * later through %rotate.
  4455. *
  4456. * First, push an end marker on to istk->expansion, mark this
  4457. * macro as in progress, and set up its invocation-specific
  4458. * variables.
  4459. */
  4460. ll = nasm_malloc(sizeof(Line));
  4461. ll->next = istk->expansion;
  4462. ll->finishes = m;
  4463. ll->first = NULL;
  4464. istk->expansion = ll;
  4465. m->in_progress = TRUE;
  4466. m->params = params;
  4467. m->iline = tline;
  4468. m->nparam = nparam;
  4469. m->rotate = 0;
  4470. m->paramlen = paramlen;
  4471. m->unique = unique++;
  4472. m->lineno = 0;
  4473. m->next_active = istk->mstk;
  4474. istk->mstk = m;
  4475. for (l = m->expansion; l; l = l->next)
  4476. {
  4477. Token **tail;
  4478. ll = nasm_malloc(sizeof(Line));
  4479. ll->finishes = NULL;
  4480. ll->next = istk->expansion;
  4481. istk->expansion = ll;
  4482. tail = &ll->first;
  4483. for (t = l->first; t; t = t->next)
  4484. {
  4485. Token *x = t;
  4486. if (t->type == TOK_PREPROC_ID &&
  4487. t->text[1] == '0' && t->text[2] == '0')
  4488. {
  4489. dont_prepend = -1;
  4490. x = label;
  4491. if (!x)
  4492. continue;
  4493. }
  4494. tt = *tail = new_Token(NULL, x->type, x->text, 0);
  4495. tail = &tt->next;
  4496. }
  4497. *tail = NULL;
  4498. }
  4499. /*
  4500. * If we had a label, push it on as the first line of
  4501. * the macro expansion.
  4502. */
  4503. if (label)
  4504. {
  4505. if (dont_prepend < 0)
  4506. free_tlist(startline);
  4507. else
  4508. {
  4509. ll = nasm_malloc(sizeof(Line));
  4510. ll->finishes = NULL;
  4511. ll->next = istk->expansion;
  4512. istk->expansion = ll;
  4513. ll->first = startline;
  4514. if (!dont_prepend)
  4515. {
  4516. while (label->next)
  4517. label = label->next;
  4518. label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
  4519. }
  4520. }
  4521. }
  4522. list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
  4523. return 1;
  4524. }
  4525. /*
  4526. * Since preprocessor always operates only on the line that didn't
  4527. * arrive yet, we should always use ERR_OFFBY1. Also since user
  4528. * won't want to see same error twice (preprocessing is done once
  4529. * per pass) we will want to show errors only during pass one.
  4530. */
  4531. static void
  4532. error(int severity, const char *fmt, ...)
  4533. {
  4534. va_list arg;
  4535. char buff[1024];
  4536. /* If we're in a dead branch of IF or something like it, ignore the error */
  4537. if (istk && istk->conds && !emitting(istk->conds->state))
  4538. return;
  4539. va_start(arg, fmt);
  4540. #ifdef HAVE_VSNPRINTF
  4541. vsnprintf(buff, sizeof(buff), fmt, arg);
  4542. #else
  4543. vsprintf(buff, fmt, arg);
  4544. #endif
  4545. va_end(arg);
  4546. if (istk && istk->mstk && istk->mstk->name)
  4547. _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
  4548. istk->mstk->lineno, buff);
  4549. else
  4550. _error(severity | ERR_PASS1, "%s", buff);
  4551. }
  4552. static void
  4553. pp_reset(FILE *f, const char *file, int apass, efunc errfunc, evalfunc eval,
  4554. ListGen * listgen)
  4555. {
  4556. int h;
  4557. first_fp = f;
  4558. _error = errfunc;
  4559. cstk = NULL;
  4560. istk = nasm_malloc(sizeof(Include));
  4561. istk->next = NULL;
  4562. istk->conds = NULL;
  4563. istk->expansion = NULL;
  4564. istk->mstk = NULL;
  4565. istk->fp = f;
  4566. istk->fname = NULL;
  4567. nasm_free(nasm_src_set_fname(nasm_strdup(file)));
  4568. nasm_src_set_linnum(0);
  4569. istk->lineinc = 1;
  4570. defining = NULL;
  4571. nested_mac_count = 0;
  4572. nested_rep_count = 0;
  4573. for (h = 0; h < NHASH; h++)
  4574. {
  4575. mmacros[h] = NULL;
  4576. smacros[h] = NULL;
  4577. }
  4578. unique = 0;
  4579. if (tasm_compatible_mode) {
  4580. pp_extra_stdmac(tasm_compat_macros);
  4581. }
  4582. list = listgen;
  4583. evaluate = eval;
  4584. pass = apass;
  4585. first_line = 1;
  4586. }
  4587. /*
  4588. * Nasty hack: here we push the contents of `predef' on
  4589. * to the top-level expansion stack, since this is the
  4590. * most convenient way to implement the pre-include and
  4591. * pre-define features.
  4592. */
  4593. static void
  4594. poke_predef(Line *predef_lines)
  4595. {
  4596. Line *pd, *l;
  4597. Token *head, **tail, *t;
  4598. for (pd = predef_lines; pd; pd = pd->next)
  4599. {
  4600. head = NULL;
  4601. tail = &head;
  4602. for (t = pd->first; t; t = t->next)
  4603. {
  4604. *tail = new_Token(NULL, t->type, t->text, 0);
  4605. tail = &(*tail)->next;
  4606. }
  4607. l = nasm_malloc(sizeof(Line));
  4608. l->next = istk->expansion;
  4609. l->first = head;
  4610. l->finishes = FALSE;
  4611. istk->expansion = l;
  4612. }
  4613. }
  4614. static char *
  4615. pp_getline(void)
  4616. {
  4617. char *line;
  4618. Token *tline;
  4619. while (1)
  4620. {
  4621. /*
  4622. * Fetch a tokenised line, either from the macro-expansion
  4623. * buffer or from the input file.
  4624. */
  4625. tline = NULL;
  4626. if (first_line)
  4627. {
  4628. /* Reverse order */
  4629. poke_predef(predef);
  4630. poke_predef(stddef);
  4631. poke_predef(builtindef);
  4632. first_line = 0;
  4633. }
  4634. if (!istk)
  4635. return NULL;
  4636. while (istk->expansion && istk->expansion->finishes)
  4637. {
  4638. Line *l = istk->expansion;
  4639. if (!l->finishes->name && l->finishes->in_progress > 1)
  4640. {
  4641. Line *ll;
  4642. /*
  4643. * This is a macro-end marker for a macro with no
  4644. * name, which means it's not really a macro at all
  4645. * but a %rep block, and the `in_progress' field is
  4646. * more than 1, meaning that we still need to
  4647. * repeat. (1 means the natural last repetition; 0
  4648. * means termination by %exitrep.) We have
  4649. * therefore expanded up to the %endrep, and must
  4650. * push the whole block on to the expansion buffer
  4651. * again. We don't bother to remove the macro-end
  4652. * marker: we'd only have to generate another one
  4653. * if we did.
  4654. */
  4655. l->finishes->in_progress--;
  4656. for (l = l->finishes->expansion; l; l = l->next)
  4657. {
  4658. Token *t, *tt, **tail;
  4659. ll = nasm_malloc(sizeof(Line));
  4660. ll->next = istk->expansion;
  4661. ll->finishes = NULL;
  4662. ll->first = NULL;
  4663. tail = &ll->first;
  4664. for (t = l->first; t; t = t->next)
  4665. {
  4666. if (t->text || t->type == TOK_WHITESPACE)
  4667. {
  4668. tt = *tail = new_Token(NULL, t->type, t->text, 0);
  4669. tail = &tt->next;
  4670. }
  4671. }
  4672. istk->expansion = ll;
  4673. }
  4674. }
  4675. else
  4676. {
  4677. /*
  4678. * Check whether a `%rep' was started and not ended
  4679. * within this macro expansion. This can happen and
  4680. * should be detected. It's a fatal error because
  4681. * I'm too confused to work out how to recover
  4682. * sensibly from it.
  4683. */
  4684. if (defining)
  4685. {
  4686. if (defining->name)
  4687. error(ERR_PANIC, "defining with name in expansion");
  4688. else if (istk->mstk->name)
  4689. error(ERR_FATAL, "`%%rep' without `%%endrep' within"
  4690. " expansion of macro `%s'", istk->mstk->name);
  4691. }
  4692. /*
  4693. * FIXME: investigate the relationship at this point between
  4694. * istk->mstk and l->finishes
  4695. */
  4696. {
  4697. MMacro *m = istk->mstk;
  4698. istk->mstk = m->next_active;
  4699. if (m->name)
  4700. {
  4701. /*
  4702. * This was a real macro call, not a %rep, and
  4703. * therefore the parameter information needs to
  4704. * be freed.
  4705. */
  4706. nasm_free(m->params);
  4707. free_tlist(m->iline);
  4708. nasm_free(m->paramlen);
  4709. l->finishes->in_progress = FALSE;
  4710. }
  4711. else
  4712. free_mmacro(m);
  4713. }
  4714. istk->expansion = l->next;
  4715. nasm_free(l);
  4716. list->downlevel(LIST_MACRO);
  4717. }
  4718. }
  4719. while (1)
  4720. { /* until we get a line we can use */
  4721. if (istk->expansion)
  4722. { /* from a macro expansion */
  4723. char *p;
  4724. Line *l = istk->expansion;
  4725. if (istk->mstk)
  4726. istk->mstk->lineno++;
  4727. tline = l->first;
  4728. istk->expansion = l->next;
  4729. nasm_free(l);
  4730. p = detoken(tline, FALSE);
  4731. list->line(LIST_MACRO, p);
  4732. nasm_free(p);
  4733. break;
  4734. }
  4735. line = read_line();
  4736. if (line)
  4737. { /* from the current input file */
  4738. line = prepreproc(line);
  4739. tline = tokenise(line);
  4740. nasm_free(line);
  4741. break;
  4742. }
  4743. /*
  4744. * The current file has ended; work down the istk
  4745. */
  4746. {
  4747. Include *i = istk;
  4748. if (i->fp != first_fp)
  4749. fclose(i->fp);
  4750. if (i->conds)
  4751. error(ERR_FATAL, "expected `%%endif' before end of file");
  4752. /* only set line and file name if there's a next node */
  4753. if (i->next)
  4754. {
  4755. nasm_src_set_linnum(i->lineno);
  4756. nasm_free(nasm_src_set_fname(nasm_strdup(i->fname)));
  4757. }
  4758. istk = i->next;
  4759. list->downlevel(LIST_INCLUDE);
  4760. nasm_free(i);
  4761. if (!istk)
  4762. return NULL;
  4763. if (istk->expansion && istk->expansion->finishes)
  4764. break;
  4765. }
  4766. }
  4767. /*
  4768. * We must expand MMacro parameters and MMacro-local labels
  4769. * _before_ we plunge into directive processing, to cope
  4770. * with things like `%define something %1' such as STRUC
  4771. * uses. Unless we're _defining_ a MMacro, in which case
  4772. * those tokens should be left alone to go into the
  4773. * definition; and unless we're in a non-emitting
  4774. * condition, in which case we don't want to meddle with
  4775. * anything.
  4776. */
  4777. if (!defining && !(istk->conds && !emitting(istk->conds->state)))
  4778. tline = expand_mmac_params(tline);
  4779. /*
  4780. * Check the line to see if it's a preprocessor directive.
  4781. */
  4782. if (do_directive(tline) == DIRECTIVE_FOUND)
  4783. {
  4784. continue;
  4785. }
  4786. else if (defining)
  4787. {
  4788. /*
  4789. * We're defining a multi-line macro. We emit nothing
  4790. * at all, and just
  4791. * shove the tokenised line on to the macro definition.
  4792. */
  4793. Line *l = nasm_malloc(sizeof(Line));
  4794. l->next = defining->expansion;
  4795. l->first = tline;
  4796. l->finishes = FALSE;
  4797. defining->expansion = l;
  4798. continue;
  4799. }
  4800. else if (istk->conds && !emitting(istk->conds->state))
  4801. {
  4802. /*
  4803. * We're in a non-emitting branch of a condition block.
  4804. * Emit nothing at all, not even a blank line: when we
  4805. * emerge from the condition we'll give a line-number
  4806. * directive so we keep our place correctly.
  4807. */
  4808. free_tlist(tline);
  4809. continue;
  4810. }
  4811. else if (istk->mstk && !istk->mstk->in_progress)
  4812. {
  4813. /*
  4814. * We're in a %rep block which has been terminated, so
  4815. * we're walking through to the %endrep without
  4816. * emitting anything. Emit nothing at all, not even a
  4817. * blank line: when we emerge from the %rep block we'll
  4818. * give a line-number directive so we keep our place
  4819. * correctly.
  4820. */
  4821. free_tlist(tline);
  4822. continue;
  4823. }
  4824. else
  4825. {
  4826. tline = expand_smacro(tline);
  4827. if (!expand_mmacro(tline))
  4828. {
  4829. /*
  4830. * De-tokenise the line again, and emit it.
  4831. */
  4832. if (tasm_compatible_mode)
  4833. tline = tasm_join_tokens(tline);
  4834. line = detoken(tline, TRUE);
  4835. free_tlist(tline);
  4836. break;
  4837. }
  4838. else
  4839. {
  4840. continue; /* expand_mmacro calls free_tlist */
  4841. }
  4842. }
  4843. }
  4844. return line;
  4845. }
  4846. static void
  4847. pp_cleanup(int pass_)
  4848. {
  4849. int h;
  4850. if (pass_ == 1)
  4851. {
  4852. if (defining)
  4853. {
  4854. error(ERR_NONFATAL, "end of file while still defining macro `%s'",
  4855. defining->name);
  4856. free_mmacro(defining);
  4857. }
  4858. return;
  4859. }
  4860. while (cstk)
  4861. ctx_pop();
  4862. for (h = 0; h < NHASH; h++)
  4863. {
  4864. while (mmacros[h])
  4865. {
  4866. MMacro *m = mmacros[h];
  4867. mmacros[h] = mmacros[h]->next;
  4868. free_mmacro(m);
  4869. }
  4870. while (smacros[h])
  4871. {
  4872. SMacro *s = smacros[h];
  4873. smacros[h] = smacros[h]->next;
  4874. nasm_free(s->name);
  4875. free_tlist(s->expansion);
  4876. nasm_free(s);
  4877. }
  4878. }
  4879. while (istk)
  4880. {
  4881. Include *i = istk;
  4882. istk = istk->next;
  4883. if (i->fp != first_fp)
  4884. fclose(i->fp);
  4885. nasm_free(i->fname);
  4886. nasm_free(i);
  4887. }
  4888. while (cstk)
  4889. ctx_pop();
  4890. if (pass_ == 0)
  4891. {
  4892. free_llist(builtindef);
  4893. free_llist(stddef);
  4894. free_llist(predef);
  4895. builtindef = NULL;
  4896. stddef = NULL;
  4897. predef = NULL;
  4898. freeTokens = NULL;
  4899. delete_Blocks();
  4900. blocks.next = NULL;
  4901. blocks.chunk = NULL;
  4902. }
  4903. }
  4904. void
  4905. pp_pre_include(const char *fname)
  4906. {
  4907. Token *inc, *space, *name;
  4908. Line *l;
  4909. name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
  4910. space = new_Token(name, TOK_WHITESPACE, NULL, 0);
  4911. inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
  4912. l = nasm_malloc(sizeof(Line));
  4913. l->next = predef;
  4914. l->first = inc;
  4915. l->finishes = FALSE;
  4916. predef = l;
  4917. }
  4918. void
  4919. pp_pre_define(char *definition)
  4920. {
  4921. Token *def, *space;
  4922. Line *l;
  4923. char *equals;
  4924. equals = strchr(definition, '=');
  4925. space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
  4926. def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
  4927. if (equals)
  4928. *equals = ' ';
  4929. space->next = tokenise(definition);
  4930. if (equals)
  4931. *equals = '=';
  4932. l = nasm_malloc(sizeof(Line));
  4933. l->next = predef;
  4934. l->first = def;
  4935. l->finishes = FALSE;
  4936. predef = l;
  4937. }
  4938. void
  4939. pp_pre_undefine(char *definition)
  4940. {
  4941. Token *def, *space;
  4942. Line *l;
  4943. space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
  4944. def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
  4945. space->next = tokenise(definition);
  4946. l = nasm_malloc(sizeof(Line));
  4947. l->next = predef;
  4948. l->first = def;
  4949. l->finishes = FALSE;
  4950. predef = l;
  4951. }
  4952. void
  4953. pp_builtin_define(char *definition)
  4954. {
  4955. Token *def, *space;
  4956. Line *l;
  4957. char *equals;
  4958. equals = strchr(definition, '=');
  4959. space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
  4960. def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
  4961. if (equals)
  4962. *equals = ' ';
  4963. space->next = tokenise(definition);
  4964. if (equals)
  4965. *equals = '=';
  4966. l = nasm_malloc(sizeof(Line));
  4967. l->next = builtindef;
  4968. l->first = def;
  4969. l->finishes = FALSE;
  4970. builtindef = l;
  4971. }
  4972. void
  4973. pp_extra_stdmac(const char **macros)
  4974. {
  4975. const char **lp;
  4976. for (lp=macros; *lp; lp++)
  4977. {
  4978. char *macro;
  4979. Token *t;
  4980. Line *l;
  4981. macro = nasm_strdup(*lp);
  4982. t = tokenise(macro);
  4983. nasm_free(macro);
  4984. l = nasm_malloc(sizeof(Line));
  4985. l->next = stddef;
  4986. l->first = t;
  4987. l->finishes = FALSE;
  4988. stddef = l;
  4989. }
  4990. }
  4991. static void
  4992. make_tok_num(Token * tok, yasm_intnum *val)
  4993. {
  4994. tok->text = yasm_intnum_get_str(val);
  4995. tok->type = TOK_NUMBER;
  4996. yasm_intnum_destroy(val);
  4997. }
  4998. Preproc nasmpp = {
  4999. pp_reset,
  5000. pp_getline,
  5001. pp_cleanup
  5002. };