gengtype.c 149 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389
  1. /* Process source files and output type information.
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifdef HOST_GENERATOR_FILE
  16. #include "config.h"
  17. #define GENERATOR_FILE 1
  18. #else
  19. #include "bconfig.h"
  20. #endif
  21. #include "system.h"
  22. #include "errors.h" /* for fatal */
  23. #include "getopt.h"
  24. #include "version.h" /* for version_string & pkgversion_string. */
  25. #include "hashtab.h"
  26. #include "xregex.h"
  27. #include "obstack.h"
  28. #include "gengtype.h"
  29. #include "filenames.h"
  30. /* Data types, macros, etc. used only in this file. */
  31. /* The list of output files. */
  32. outf_p output_files;
  33. /* The output header file that is included into pretty much every
  34. source file. */
  35. outf_p header_file;
  36. /* The name of the file containing the list of input files. */
  37. static char *inputlist;
  38. /* The plugin input files and their number; in that case only
  39. a single file is produced. */
  40. static input_file **plugin_files;
  41. static size_t nb_plugin_files;
  42. /* The generated plugin output file and name. */
  43. static outf_p plugin_output;
  44. static char *plugin_output_filename;
  45. /* Our source directory and its length. */
  46. const char *srcdir;
  47. size_t srcdir_len;
  48. /* Variables used for reading and writing the state. */
  49. const char *read_state_filename;
  50. const char *write_state_filename;
  51. /* Variables to help debugging. */
  52. int do_dump;
  53. int do_debug;
  54. /* Level for verbose messages. */
  55. int verbosity_level;
  56. /* We have a type count and use it to set the state_number of newly
  57. allocated types to some unique negative number. */
  58. static int type_count;
  59. /* The backup directory should be in the same file system as the
  60. generated files, otherwise the rename(2) system call would fail.
  61. If NULL, no backup is made when overwriting a generated file. */
  62. static const char* backup_dir; /* (-B) program option. */
  63. static outf_p create_file (const char *, const char *);
  64. static const char *get_file_basename (const input_file *);
  65. static const char *get_file_realbasename (const input_file *);
  66. static int get_prefix_langdir_index (const char *);
  67. static const char *get_file_langdir (const input_file *);
  68. static void dump_pair (int indent, pair_p p);
  69. static void dump_type (int indent, type_p p);
  70. static void dump_type_list (int indent, type_p p);
  71. /* Nonzero iff an error has occurred. */
  72. bool hit_error = false;
  73. static void gen_rtx_next (void);
  74. static void write_rtx_next (void);
  75. static void open_base_files (void);
  76. static void close_output_files (void);
  77. /* Report an error at POS, printing MSG. */
  78. void
  79. error_at_line (const struct fileloc *pos, const char *msg, ...)
  80. {
  81. va_list ap;
  82. gcc_assert (pos != NULL && pos->file != NULL);
  83. va_start (ap, msg);
  84. fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
  85. vfprintf (stderr, msg, ap);
  86. fputc ('\n', stderr);
  87. hit_error = true;
  88. va_end (ap);
  89. }
  90. /* Locate the ultimate base class of struct S. */
  91. static const_type_p
  92. get_ultimate_base_class (const_type_p s)
  93. {
  94. while (s->u.s.base_class)
  95. s = s->u.s.base_class;
  96. return s;
  97. }
  98. static type_p
  99. get_ultimate_base_class (type_p s)
  100. {
  101. while (s->u.s.base_class)
  102. s = s->u.s.base_class;
  103. return s;
  104. }
  105. /* Input file handling. */
  106. /* Table of all input files. */
  107. const input_file **gt_files;
  108. size_t num_gt_files;
  109. /* A number of places use the name of this "gengtype.c" file for a
  110. location for things that we can't rely on the source to define.
  111. Make sure we can still use pointer comparison on filenames. */
  112. input_file* this_file;
  113. /* The "system.h" file is likewise specially useful. */
  114. input_file* system_h_file;
  115. /* Vector of per-language directories. */
  116. const char **lang_dir_names;
  117. size_t num_lang_dirs;
  118. /* An array of output files suitable for definitions. There is one
  119. BASE_FILES entry for each language. */
  120. static outf_p *base_files;
  121. #if ENABLE_CHECKING
  122. /* Utility debugging function, printing the various type counts within
  123. a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
  124. void
  125. dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
  126. {
  127. int nb_types = 0, nb_scalar = 0, nb_string = 0;
  128. int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
  129. int nb_lang_struct = 0;
  130. int nb_user_struct = 0, nb_undefined = 0;
  131. type_p p = NULL;
  132. for (p = t; p; p = p->next)
  133. {
  134. nb_types++;
  135. switch (p->kind)
  136. {
  137. case TYPE_UNDEFINED:
  138. nb_undefined++;
  139. case TYPE_SCALAR:
  140. nb_scalar++;
  141. break;
  142. case TYPE_STRING:
  143. nb_string++;
  144. break;
  145. case TYPE_STRUCT:
  146. nb_struct++;
  147. break;
  148. case TYPE_USER_STRUCT:
  149. nb_user_struct++;
  150. break;
  151. case TYPE_UNION:
  152. nb_union++;
  153. break;
  154. case TYPE_POINTER:
  155. nb_pointer++;
  156. break;
  157. case TYPE_ARRAY:
  158. nb_array++;
  159. break;
  160. case TYPE_LANG_STRUCT:
  161. nb_lang_struct++;
  162. break;
  163. case TYPE_NONE:
  164. gcc_unreachable ();
  165. }
  166. }
  167. fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
  168. lbasename (fil), lin, msg, nb_types);
  169. if (nb_scalar > 0 || nb_string > 0)
  170. fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
  171. if (nb_struct > 0 || nb_union > 0)
  172. fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
  173. if (nb_pointer > 0 || nb_array > 0)
  174. fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
  175. if (nb_lang_struct > 0)
  176. fprintf (stderr, "@@%%@@ %d lang_structs\n", nb_lang_struct);
  177. if (nb_user_struct > 0)
  178. fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
  179. if (nb_undefined > 0)
  180. fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
  181. fprintf (stderr, "\n");
  182. }
  183. #endif /* ENABLE_CHECKING */
  184. /* Scan the input file, LIST, and determine how much space we need to
  185. store strings in. Also, count the number of language directories
  186. and files. The numbers returned are overestimates as they does not
  187. consider repeated files. */
  188. static size_t
  189. measure_input_list (FILE *list)
  190. {
  191. size_t n = 0;
  192. int c;
  193. bool atbol = true;
  194. num_lang_dirs = 0;
  195. num_gt_files = plugin_files ? nb_plugin_files : 0;
  196. while ((c = getc (list)) != EOF)
  197. {
  198. n++;
  199. if (atbol)
  200. {
  201. if (c == '[')
  202. num_lang_dirs++;
  203. else
  204. {
  205. /* Add space for a lang_bitmap before the input file name. */
  206. n += sizeof (lang_bitmap);
  207. num_gt_files++;
  208. }
  209. atbol = false;
  210. }
  211. if (c == '\n')
  212. atbol = true;
  213. }
  214. rewind (list);
  215. return n;
  216. }
  217. /* Read one input line from LIST to HEREP (which is updated). A
  218. pointer to the string is returned via LINEP. If it was a language
  219. subdirectory in square brackets, strip off the square brackets and
  220. return true. Otherwise, leave space before the string for a
  221. lang_bitmap, and return false. At EOF, returns false, does not
  222. touch *HEREP, and sets *LINEP to NULL. POS is used for
  223. diagnostics. */
  224. static bool
  225. read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
  226. {
  227. char *here = *herep;
  228. char *line;
  229. int c = getc (list);
  230. /* Read over whitespace. */
  231. while (c == '\n' || c == ' ')
  232. c = getc (list);
  233. if (c == EOF)
  234. {
  235. *linep = 0;
  236. return false;
  237. }
  238. else if (c == '[')
  239. {
  240. /* No space for a lang_bitmap is necessary. Discard the '['. */
  241. c = getc (list);
  242. line = here;
  243. while (c != ']' && c != '\n' && c != EOF)
  244. {
  245. *here++ = c;
  246. c = getc (list);
  247. }
  248. *here++ = '\0';
  249. if (c == ']')
  250. {
  251. c = getc (list); /* eat what should be a newline */
  252. if (c != '\n' && c != EOF)
  253. error_at_line (pos, "junk on line after language tag [%s]", line);
  254. }
  255. else
  256. error_at_line (pos, "missing close bracket for language tag [%s",
  257. line);
  258. *herep = here;
  259. *linep = line;
  260. return true;
  261. }
  262. else
  263. {
  264. /* Leave space for a lang_bitmap. */
  265. memset (here, 0, sizeof (lang_bitmap));
  266. here += sizeof (lang_bitmap);
  267. line = here;
  268. do
  269. {
  270. *here++ = c;
  271. c = getc (list);
  272. }
  273. while (c != EOF && c != '\n');
  274. *here++ = '\0';
  275. *herep = here;
  276. *linep = line;
  277. return false;
  278. }
  279. }
  280. /* Read the list of input files from LIST and compute all of the
  281. relevant tables. There is one file per line of the list. At
  282. first, all the files on the list are language-generic, but
  283. eventually a line will appear which is the name of a language
  284. subdirectory in square brackets, like this: [cp]. All subsequent
  285. files are specific to that language, until another language
  286. subdirectory tag appears. Files can appear more than once, if
  287. they apply to more than one language. */
  288. static void
  289. read_input_list (const char *listname)
  290. {
  291. FILE *list = fopen (listname, "r");
  292. if (!list)
  293. fatal ("cannot open %s: %s", listname, xstrerror (errno));
  294. else
  295. {
  296. struct fileloc epos;
  297. size_t bufsz = measure_input_list (list);
  298. char *buf = XNEWVEC (char, bufsz);
  299. char *here = buf;
  300. char *committed = buf;
  301. char *limit = buf + bufsz;
  302. char *line;
  303. bool is_language;
  304. size_t langno = 0;
  305. size_t nfiles = 0;
  306. lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
  307. epos.file = input_file_by_name (listname);
  308. epos.line = 0;
  309. lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
  310. gt_files = XNEWVEC (const input_file *, num_gt_files);
  311. for (;;)
  312. {
  313. next_line:
  314. epos.line++;
  315. committed = here;
  316. is_language = read_input_line (list, &here, &line, &epos);
  317. gcc_assert (here <= limit);
  318. if (line == 0)
  319. break;
  320. else if (is_language)
  321. {
  322. size_t i;
  323. gcc_assert (langno <= num_lang_dirs);
  324. for (i = 0; i < langno; i++)
  325. if (strcmp (lang_dir_names[i], line) == 0)
  326. {
  327. error_at_line (&epos, "duplicate language tag [%s]",
  328. line);
  329. curlangs = 1 << i;
  330. here = committed;
  331. goto next_line;
  332. }
  333. curlangs = 1 << langno;
  334. lang_dir_names[langno++] = line;
  335. }
  336. else
  337. {
  338. size_t i;
  339. input_file *inpf = input_file_by_name (line);
  340. gcc_assert (nfiles <= num_gt_files);
  341. for (i = 0; i < nfiles; i++)
  342. /* Since the input_file-s are uniquely hash-consed, we
  343. can just compare pointers! */
  344. if (gt_files[i] == inpf)
  345. {
  346. /* Throw away the string we just read, and add the
  347. current language to the existing string's bitmap. */
  348. lang_bitmap bmap = get_lang_bitmap (inpf);
  349. if (bmap & curlangs)
  350. error_at_line (&epos,
  351. "file %s specified more than once "
  352. "for language %s", line,
  353. langno ==
  354. 0 ? "(all)" : lang_dir_names[langno -
  355. 1]);
  356. bmap |= curlangs;
  357. set_lang_bitmap (inpf, bmap);
  358. here = committed;
  359. goto next_line;
  360. }
  361. set_lang_bitmap (inpf, curlangs);
  362. gt_files[nfiles++] = inpf;
  363. }
  364. }
  365. /* Update the global counts now that we know accurately how many
  366. things there are. (We do not bother resizing the arrays down.) */
  367. num_lang_dirs = langno;
  368. /* Add the plugin files if provided. */
  369. if (plugin_files)
  370. {
  371. size_t i;
  372. for (i = 0; i < nb_plugin_files; i++)
  373. gt_files[nfiles++] = plugin_files[i];
  374. }
  375. num_gt_files = nfiles;
  376. }
  377. /* Sanity check: any file that resides in a language subdirectory
  378. (e.g. 'cp') ought to belong to the corresponding language.
  379. ??? Still true if for instance ObjC++ is enabled and C++ isn't?
  380. (Can you even do that? Should you be allowed to?) */
  381. {
  382. size_t f;
  383. for (f = 0; f < num_gt_files; f++)
  384. {
  385. lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
  386. const char *basename = get_file_basename (gt_files[f]);
  387. const char *slashpos = strchr (basename, '/');
  388. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  389. const char *slashpos2 = strchr (basename, '\\');
  390. if (!slashpos || (slashpos2 && slashpos2 < slashpos))
  391. slashpos = slashpos2;
  392. #endif
  393. if (slashpos)
  394. {
  395. size_t l;
  396. for (l = 0; l < num_lang_dirs; l++)
  397. if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
  398. && memcmp (basename, lang_dir_names[l],
  399. strlen (lang_dir_names[l])) == 0)
  400. {
  401. if (!(bitmap & (1 << l)))
  402. error ("%s is in language directory '%s' but is not "
  403. "tagged for that language",
  404. basename, lang_dir_names[l]);
  405. break;
  406. }
  407. }
  408. }
  409. }
  410. if (ferror (list))
  411. fatal ("error reading %s: %s", listname, xstrerror (errno));
  412. fclose (list);
  413. }
  414. /* The one and only TYPE_STRING. */
  415. struct type string_type = {
  416. TYPE_STRING, 0, 0, 0, GC_USED, {0}
  417. };
  418. /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
  419. set early in main. */
  420. struct type scalar_nonchar = {
  421. TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
  422. };
  423. struct type scalar_char = {
  424. TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
  425. };
  426. /* Lists of various things. */
  427. pair_p typedefs = NULL;
  428. type_p structures = NULL;
  429. pair_p variables = NULL;
  430. static type_p adjust_field_tree_exp (type_p t, options_p opt);
  431. static type_p adjust_field_rtx_def (type_p t, options_p opt);
  432. /* Define S as a typedef to T at POS. */
  433. void
  434. do_typedef (const char *s, type_p t, struct fileloc *pos)
  435. {
  436. pair_p p;
  437. /* temporary kludge - gengtype doesn't handle conditionals or
  438. macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
  439. is coming from this file (main() sets them up with safe dummy
  440. definitions). */
  441. if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
  442. return;
  443. for (p = typedefs; p != NULL; p = p->next)
  444. if (strcmp (p->name, s) == 0)
  445. {
  446. if (p->type != t && strcmp (s, "result_type") != 0)
  447. {
  448. error_at_line (pos, "type `%s' previously defined", s);
  449. error_at_line (&p->line, "previously defined here");
  450. }
  451. return;
  452. }
  453. p = XNEW (struct pair);
  454. p->next = typedefs;
  455. p->name = s;
  456. p->type = t;
  457. p->line = *pos;
  458. p->opt = NULL;
  459. typedefs = p;
  460. }
  461. /* Define S as a typename of a scalar. Cannot be used to define
  462. typedefs of 'char'. Note: is also used for pointer-to-function
  463. typedefs (which are therefore not treated as pointers). */
  464. void
  465. do_scalar_typedef (const char *s, struct fileloc *pos)
  466. {
  467. do_typedef (s, &scalar_nonchar, pos);
  468. }
  469. /* Similar to strtok_r. */
  470. static char *
  471. strtoken (char *str, const char *delim, char **next)
  472. {
  473. char *p;
  474. if (str == NULL)
  475. str = *next;
  476. /* Skip the leading delimiters. */
  477. str += strspn (str, delim);
  478. if (*str == '\0')
  479. /* This is an empty token. */
  480. return NULL;
  481. /* The current token. */
  482. p = str;
  483. /* Find the next delimiter. */
  484. str += strcspn (str, delim);
  485. if (*str == '\0')
  486. /* This is the last token. */
  487. *next = str;
  488. else
  489. {
  490. /* Terminate the current token. */
  491. *str = '\0';
  492. /* Advance to the next token. */
  493. *next = str + 1;
  494. }
  495. return p;
  496. }
  497. /* Define TYPE_NAME to be a user defined type at location POS. */
  498. type_p
  499. create_user_defined_type (const char *type_name, struct fileloc *pos)
  500. {
  501. type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
  502. /* We might have already seen an incomplete decl of the given type,
  503. in which case we won't have yet seen a GTY((user)), and the type will
  504. only have kind "TYPE_STRUCT". Mark it as a user struct. */
  505. ty->kind = TYPE_USER_STRUCT;
  506. ty->u.s.line = *pos;
  507. ty->u.s.bitmap = get_lang_bitmap (pos->file);
  508. do_typedef (type_name, ty, pos);
  509. /* If TYPE_NAME specifies a template, create references to the types
  510. in the template by pretending that each type is a field of TY.
  511. This is needed to make sure that the types referenced by the
  512. template are marked as used. */
  513. char *str = xstrdup (type_name);
  514. char *open_bracket = strchr (str, '<');
  515. if (open_bracket)
  516. {
  517. /* We only accept simple template declarations (see
  518. require_template_declaration), so we only need to parse a
  519. comma-separated list of strings, implicitly assumed to
  520. be type names, potentially with "*" characters. */
  521. char *arg = open_bracket + 1;
  522. /* Workaround -Wmaybe-uninitialized false positive during
  523. profiledbootstrap by initializing it. */
  524. char *next = NULL;
  525. char *type_id = strtoken (arg, ",>", &next);
  526. pair_p fields = 0;
  527. while (type_id)
  528. {
  529. /* Create a new field for every type found inside the template
  530. parameter list. */
  531. /* Support a single trailing "*" character. */
  532. const char *star = strchr (type_id, '*');
  533. int is_ptr = (star != NULL);
  534. size_t offset_to_star = star - type_id;
  535. if (is_ptr)
  536. offset_to_star = star - type_id;
  537. if (strstr (type_id, "char*"))
  538. {
  539. type_id = strtoken (0, ",>", &next);
  540. continue;
  541. }
  542. char *field_name = xstrdup (type_id);
  543. type_p arg_type;
  544. if (is_ptr)
  545. {
  546. /* Strip off the first '*' character (and any subsequent text). */
  547. *(field_name + offset_to_star) = '\0';
  548. arg_type = find_structure (field_name, TYPE_STRUCT);
  549. arg_type = create_pointer (arg_type);
  550. }
  551. else
  552. arg_type = resolve_typedef (field_name, pos);
  553. fields = create_field_at (fields, arg_type, field_name, 0, pos);
  554. type_id = strtoken (0, ",>", &next);
  555. }
  556. /* Associate the field list to TY. */
  557. ty->u.s.fields = fields;
  558. }
  559. free (str);
  560. return ty;
  561. }
  562. /* Given a typedef name S, return its associated type. Return NULL if
  563. S is not a registered type name. */
  564. static type_p
  565. type_for_name (const char *s)
  566. {
  567. pair_p p;
  568. /* Special-case support for types within a "gcc::" namespace. Rather
  569. than fully-supporting namespaces, simply strip off the "gcc::" prefix
  570. where present. This allows us to have GTY roots of this form:
  571. extern GTY(()) gcc::some_type *some_ptr;
  572. where the autogenerated functions will refer to simply "some_type",
  573. where they can be resolved into their namespace. */
  574. if (0 == strncmp (s, "gcc::", 5))
  575. s += 5;
  576. for (p = typedefs; p != NULL; p = p->next)
  577. if (strcmp (p->name, s) == 0)
  578. return p->type;
  579. return NULL;
  580. }
  581. /* Create an undefined type with name S and location POS. Return the
  582. newly created type. */
  583. static type_p
  584. create_undefined_type (const char *s, struct fileloc *pos)
  585. {
  586. type_p ty = find_structure (s, TYPE_UNDEFINED);
  587. ty->u.s.line = *pos;
  588. ty->u.s.bitmap = get_lang_bitmap (pos->file);
  589. do_typedef (s, ty, pos);
  590. return ty;
  591. }
  592. /* Return the type previously defined for S. Use POS to report errors. */
  593. type_p
  594. resolve_typedef (const char *s, struct fileloc *pos)
  595. {
  596. bool is_template_instance = (strchr (s, '<') != NULL);
  597. type_p p = type_for_name (s);
  598. /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
  599. type for regular type identifiers. If the type identifier S is a
  600. template instantiation, however, we treat it as a user defined
  601. type.
  602. FIXME, this is actually a limitation in gengtype. Supporting
  603. template types and their instances would require keeping separate
  604. track of the basic types definition and its instances. This
  605. essentially forces all template classes in GC to be marked
  606. GTY((user)). */
  607. if (!p)
  608. p = (is_template_instance)
  609. ? create_user_defined_type (s, pos)
  610. : create_undefined_type (s, pos);
  611. return p;
  612. }
  613. /* Add SUBCLASS to head of linked list of BASE's subclasses. */
  614. void add_subclass (type_p base, type_p subclass)
  615. {
  616. gcc_assert (union_or_struct_p (base));
  617. gcc_assert (union_or_struct_p (subclass));
  618. subclass->u.s.next_sibling_class = base->u.s.first_subclass;
  619. base->u.s.first_subclass = subclass;
  620. }
  621. /* Create and return a new structure with tag NAME at POS with fields
  622. FIELDS and options O. The KIND of structure must be one of
  623. TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
  624. type_p
  625. new_structure (const char *name, enum typekind kind, struct fileloc *pos,
  626. pair_p fields, options_p o, type_p base_class)
  627. {
  628. type_p si;
  629. type_p s = NULL;
  630. lang_bitmap bitmap = get_lang_bitmap (pos->file);
  631. bool isunion = (kind == TYPE_UNION);
  632. gcc_assert (union_or_struct_p (kind));
  633. for (si = structures; si != NULL; si = si->next)
  634. if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
  635. {
  636. type_p ls = NULL;
  637. if (si->kind == TYPE_LANG_STRUCT)
  638. {
  639. ls = si;
  640. for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
  641. if (si->u.s.bitmap == bitmap)
  642. s = si;
  643. }
  644. else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
  645. {
  646. ls = si;
  647. type_count++;
  648. si = XCNEW (struct type);
  649. memcpy (si, ls, sizeof (struct type));
  650. ls->kind = TYPE_LANG_STRUCT;
  651. ls->u.s.lang_struct = si;
  652. ls->u.s.fields = NULL;
  653. si->next = NULL;
  654. si->state_number = -type_count;
  655. si->pointer_to = NULL;
  656. si->u.s.lang_struct = ls;
  657. }
  658. else
  659. s = si;
  660. if (ls != NULL && s == NULL)
  661. {
  662. type_count++;
  663. s = XCNEW (struct type);
  664. s->state_number = -type_count;
  665. s->next = ls->u.s.lang_struct;
  666. ls->u.s.lang_struct = s;
  667. s->u.s.lang_struct = ls;
  668. }
  669. break;
  670. }
  671. if (s == NULL)
  672. {
  673. type_count++;
  674. s = XCNEW (struct type);
  675. s->state_number = -type_count;
  676. s->next = structures;
  677. structures = s;
  678. }
  679. if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
  680. {
  681. error_at_line (pos, "duplicate definition of '%s %s'",
  682. isunion ? "union" : "struct", s->u.s.tag);
  683. error_at_line (&s->u.s.line, "previous definition here");
  684. }
  685. s->kind = kind;
  686. s->u.s.tag = name;
  687. s->u.s.line = *pos;
  688. s->u.s.fields = fields;
  689. s->u.s.opt = o;
  690. s->u.s.bitmap = bitmap;
  691. if (s->u.s.lang_struct)
  692. s->u.s.lang_struct->u.s.bitmap |= bitmap;
  693. s->u.s.base_class = base_class;
  694. if (base_class)
  695. add_subclass (base_class, s);
  696. return s;
  697. }
  698. /* Return the previously-defined structure or union with tag NAME,
  699. or a new empty structure or union if none was defined previously.
  700. The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
  701. TYPE_USER_STRUCT. */
  702. type_p
  703. find_structure (const char *name, enum typekind kind)
  704. {
  705. type_p s;
  706. bool isunion = (kind == TYPE_UNION);
  707. gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
  708. for (s = structures; s != NULL; s = s->next)
  709. if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
  710. return s;
  711. type_count++;
  712. s = XCNEW (struct type);
  713. s->next = structures;
  714. s->state_number = -type_count;
  715. structures = s;
  716. s->kind = kind;
  717. s->u.s.tag = name;
  718. structures = s;
  719. return s;
  720. }
  721. /* Return a scalar type with name NAME. */
  722. type_p
  723. create_scalar_type (const char *name)
  724. {
  725. if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
  726. return &scalar_char;
  727. else
  728. return &scalar_nonchar;
  729. }
  730. /* Return a pointer to T. */
  731. type_p
  732. create_pointer (type_p t)
  733. {
  734. if (!t->pointer_to)
  735. {
  736. type_p r = XCNEW (struct type);
  737. type_count++;
  738. r->state_number = -type_count;
  739. r->kind = TYPE_POINTER;
  740. r->u.p = t;
  741. t->pointer_to = r;
  742. }
  743. return t->pointer_to;
  744. }
  745. /* Return an array of length LEN. */
  746. type_p
  747. create_array (type_p t, const char *len)
  748. {
  749. type_p v;
  750. type_count++;
  751. v = XCNEW (struct type);
  752. v->kind = TYPE_ARRAY;
  753. v->state_number = -type_count;
  754. v->u.a.p = t;
  755. v->u.a.len = len;
  756. return v;
  757. }
  758. /* Return a string options structure with name NAME and info INFO.
  759. NEXT is the next option in the chain. */
  760. options_p
  761. create_string_option (options_p next, const char *name, const char *info)
  762. {
  763. options_p o = XNEW (struct options);
  764. o->kind = OPTION_STRING;
  765. o->next = next;
  766. o->name = name;
  767. o->info.string = info;
  768. return o;
  769. }
  770. /* Create a type options structure with name NAME and info INFO. NEXT
  771. is the next option in the chain. */
  772. options_p
  773. create_type_option (options_p next, const char* name, type_p info)
  774. {
  775. options_p o = XNEW (struct options);
  776. o->next = next;
  777. o->name = name;
  778. o->kind = OPTION_TYPE;
  779. o->info.type = info;
  780. return o;
  781. }
  782. /* Create a nested pointer options structure with name NAME and info
  783. INFO. NEXT is the next option in the chain. */
  784. options_p
  785. create_nested_option (options_p next, const char* name,
  786. struct nested_ptr_data* info)
  787. {
  788. options_p o;
  789. o = XNEW (struct options);
  790. o->next = next;
  791. o->name = name;
  792. o->kind = OPTION_NESTED;
  793. o->info.nested = info;
  794. return o;
  795. }
  796. /* Return an options structure for a "nested_ptr" option. */
  797. options_p
  798. create_nested_ptr_option (options_p next, type_p t,
  799. const char *to, const char *from)
  800. {
  801. struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
  802. d->type = adjust_field_type (t, 0);
  803. d->convert_to = to;
  804. d->convert_from = from;
  805. return create_nested_option (next, "nested_ptr", d);
  806. }
  807. /* Add a variable named S of type T with options O defined at POS,
  808. to `variables'. */
  809. void
  810. note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
  811. {
  812. pair_p n;
  813. n = XNEW (struct pair);
  814. n->name = s;
  815. n->type = t;
  816. n->line = *pos;
  817. n->opt = o;
  818. n->next = variables;
  819. variables = n;
  820. }
  821. /* Most-general structure field creator. */
  822. static pair_p
  823. create_field_all (pair_p next, type_p type, const char *name, options_p opt,
  824. const input_file *inpf, int line)
  825. {
  826. pair_p field;
  827. field = XNEW (struct pair);
  828. field->next = next;
  829. field->type = type;
  830. field->name = name;
  831. field->opt = opt;
  832. field->line.file = inpf;
  833. field->line.line = line;
  834. return field;
  835. }
  836. /* Create a field that came from the source code we are scanning,
  837. i.e. we have a 'struct fileloc', and possibly options; also,
  838. adjust_field_type should be called. */
  839. pair_p
  840. create_field_at (pair_p next, type_p type, const char *name, options_p opt,
  841. struct fileloc *pos)
  842. {
  843. return create_field_all (next, adjust_field_type (type, opt),
  844. name, opt, pos->file, pos->line);
  845. }
  846. /* Create a fake field with the given type and name. NEXT is the next
  847. field in the chain. */
  848. #define create_field(next,type,name) \
  849. create_field_all (next,type,name, 0, this_file, __LINE__)
  850. /* Like create_field, but the field is only valid when condition COND
  851. is true. */
  852. static pair_p
  853. create_optional_field_ (pair_p next, type_p type, const char *name,
  854. const char *cond, int line)
  855. {
  856. static int id = 1;
  857. pair_p union_fields;
  858. type_p union_type;
  859. /* Create a fake union type with a single nameless field of type TYPE.
  860. The field has a tag of "1". This allows us to make the presence
  861. of a field of type TYPE depend on some boolean "desc" being true. */
  862. union_fields = create_field (NULL, type, "");
  863. union_fields->opt =
  864. create_string_option (union_fields->opt, "dot", "");
  865. union_fields->opt =
  866. create_string_option (union_fields->opt, "tag", "1");
  867. union_type =
  868. new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
  869. &lexer_line, union_fields, NULL, NULL);
  870. /* Create the field and give it the new fake union type. Add a "desc"
  871. tag that specifies the condition under which the field is valid. */
  872. return create_field_all (next, union_type, name,
  873. create_string_option (0, "desc", cond),
  874. this_file, line);
  875. }
  876. #define create_optional_field(next,type,name,cond) \
  877. create_optional_field_(next,type,name,cond,__LINE__)
  878. /* Reverse a linked list of 'struct pair's in place. */
  879. pair_p
  880. nreverse_pairs (pair_p list)
  881. {
  882. pair_p prev = 0, p, next;
  883. for (p = list; p; p = next)
  884. {
  885. next = p->next;
  886. p->next = prev;
  887. prev = p;
  888. }
  889. return prev;
  890. }
  891. /* We don't care how long a CONST_DOUBLE is. */
  892. #define CONST_DOUBLE_FORMAT "ww"
  893. /* We don't want to see codes that are only for generator files. */
  894. #undef GENERATOR_FILE
  895. enum rtx_code
  896. {
  897. #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
  898. #include "rtl.def"
  899. #undef DEF_RTL_EXPR
  900. NUM_RTX_CODE
  901. };
  902. static const char *const rtx_name[NUM_RTX_CODE] = {
  903. #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
  904. #include "rtl.def"
  905. #undef DEF_RTL_EXPR
  906. };
  907. static const char *const rtx_format[NUM_RTX_CODE] = {
  908. #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
  909. #include "rtl.def"
  910. #undef DEF_RTL_EXPR
  911. };
  912. static int rtx_next_new[NUM_RTX_CODE];
  913. /* We also need codes and names for insn notes (not register notes).
  914. Note that we do *not* bias the note values here. */
  915. enum insn_note
  916. {
  917. #define DEF_INSN_NOTE(NAME) NAME,
  918. #include "insn-notes.def"
  919. #undef DEF_INSN_NOTE
  920. NOTE_INSN_MAX
  921. };
  922. /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
  923. default field for line number notes. */
  924. static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
  925. #define DEF_INSN_NOTE(NAME) #NAME,
  926. #include "insn-notes.def"
  927. #undef DEF_INSN_NOTE
  928. };
  929. #undef CONST_DOUBLE_FORMAT
  930. #define GENERATOR_FILE
  931. /* Generate the contents of the rtx_next array. This really doesn't belong
  932. in gengtype at all, but it's needed for adjust_field_rtx_def. */
  933. static void
  934. gen_rtx_next (void)
  935. {
  936. int i;
  937. for (i = 0; i < NUM_RTX_CODE; i++)
  938. {
  939. int k;
  940. rtx_next_new[i] = -1;
  941. if (strncmp (rtx_format[i], "uu", 2) == 0)
  942. rtx_next_new[i] = 1;
  943. else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
  944. rtx_next_new[i] = 1;
  945. else
  946. for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
  947. if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
  948. rtx_next_new[i] = k;
  949. }
  950. }
  951. /* Write out the contents of the rtx_next array. */
  952. static void
  953. write_rtx_next (void)
  954. {
  955. outf_p f = get_output_file_with_visibility (NULL);
  956. int i;
  957. if (!f)
  958. return;
  959. oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
  960. oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
  961. for (i = 0; i < NUM_RTX_CODE; i++)
  962. if (rtx_next_new[i] == -1)
  963. oprintf (f, " 0,\n");
  964. else
  965. oprintf (f,
  966. " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
  967. oprintf (f, "};\n");
  968. }
  969. /* Handle `special("rtx_def")'. This is a special case for field
  970. `fld' of struct rtx_def, which is an array of unions whose values
  971. are based in a complex way on the type of RTL. */
  972. static type_p
  973. adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
  974. {
  975. pair_p flds = NULL;
  976. options_p nodot;
  977. int i;
  978. type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
  979. type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
  980. if (t->kind != TYPE_UNION)
  981. {
  982. error_at_line (&lexer_line,
  983. "special `rtx_def' must be applied to a union");
  984. return &string_type;
  985. }
  986. nodot = create_string_option (NULL, "dot", "");
  987. rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
  988. rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
  989. tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
  990. mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
  991. reg_attrs_tp =
  992. create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
  993. basic_block_tp =
  994. create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
  995. constant_tp =
  996. create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
  997. scalar_tp = &scalar_nonchar; /* rtunion int */
  998. {
  999. pair_p note_flds = NULL;
  1000. int c;
  1001. for (c = 0; c <= NOTE_INSN_MAX; c++)
  1002. {
  1003. switch (c)
  1004. {
  1005. case NOTE_INSN_MAX:
  1006. case NOTE_INSN_DELETED_LABEL:
  1007. case NOTE_INSN_DELETED_DEBUG_LABEL:
  1008. note_flds = create_field (note_flds, &string_type, "rt_str");
  1009. break;
  1010. case NOTE_INSN_BLOCK_BEG:
  1011. case NOTE_INSN_BLOCK_END:
  1012. note_flds = create_field (note_flds, tree_tp, "rt_tree");
  1013. break;
  1014. case NOTE_INSN_VAR_LOCATION:
  1015. case NOTE_INSN_CALL_ARG_LOCATION:
  1016. note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
  1017. break;
  1018. default:
  1019. note_flds = create_field (note_flds, scalar_tp, "rt_int");
  1020. break;
  1021. }
  1022. /* NOTE_INSN_MAX is used as the default field for line
  1023. number notes. */
  1024. if (c == NOTE_INSN_MAX)
  1025. note_flds->opt =
  1026. create_string_option (nodot, "default", "");
  1027. else
  1028. note_flds->opt =
  1029. create_string_option (nodot, "tag", note_insn_name[c]);
  1030. }
  1031. note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
  1032. &lexer_line, note_flds, NULL, NULL);
  1033. }
  1034. /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
  1035. {
  1036. pair_p sym_flds;
  1037. sym_flds = create_field (NULL, tree_tp, "rt_tree");
  1038. sym_flds->opt = create_string_option (nodot, "default", "");
  1039. sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
  1040. sym_flds->opt = create_string_option (nodot, "tag", "1");
  1041. symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
  1042. &lexer_line, sym_flds, NULL, NULL);
  1043. }
  1044. for (i = 0; i < NUM_RTX_CODE; i++)
  1045. {
  1046. pair_p subfields = NULL;
  1047. size_t aindex, nmindex;
  1048. const char *sname;
  1049. type_p substruct;
  1050. char *ftag;
  1051. for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
  1052. {
  1053. type_p t;
  1054. const char *subname;
  1055. switch (rtx_format[i][aindex])
  1056. {
  1057. case '*':
  1058. case 'i':
  1059. case 'n':
  1060. case 'w':
  1061. t = scalar_tp;
  1062. subname = "rt_int";
  1063. break;
  1064. case '0':
  1065. if (i == MEM && aindex == 1)
  1066. t = mem_attrs_tp, subname = "rt_mem";
  1067. else if (i == JUMP_INSN && aindex == 7)
  1068. t = rtx_tp, subname = "rt_rtx";
  1069. else if (i == CODE_LABEL && aindex == 4)
  1070. t = scalar_tp, subname = "rt_int";
  1071. else if (i == CODE_LABEL && aindex == 3)
  1072. t = rtx_tp, subname = "rt_rtx";
  1073. else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
  1074. t = rtx_tp, subname = "rt_rtx";
  1075. else if (i == NOTE && aindex == 3)
  1076. t = note_union_tp, subname = "";
  1077. else if (i == NOTE && aindex == 4)
  1078. t = scalar_tp, subname = "rt_int";
  1079. else if (i == NOTE && aindex >= 6)
  1080. t = scalar_tp, subname = "rt_int";
  1081. else if (i == ADDR_DIFF_VEC && aindex == 4)
  1082. t = scalar_tp, subname = "rt_int";
  1083. else if (i == VALUE && aindex == 0)
  1084. t = scalar_tp, subname = "rt_int";
  1085. else if (i == DEBUG_EXPR && aindex == 0)
  1086. t = tree_tp, subname = "rt_tree";
  1087. else if (i == REG && aindex == 1)
  1088. t = reg_attrs_tp, subname = "rt_reg";
  1089. else if (i == SYMBOL_REF && aindex == 1)
  1090. t = symbol_union_tp, subname = "";
  1091. else if (i == JUMP_TABLE_DATA && aindex >= 4)
  1092. t = scalar_tp, subname = "rt_int";
  1093. else if (i == BARRIER && aindex >= 2)
  1094. t = scalar_tp, subname = "rt_int";
  1095. else if (i == ENTRY_VALUE && aindex == 0)
  1096. t = rtx_tp, subname = "rt_rtx";
  1097. else
  1098. {
  1099. error_at_line
  1100. (&lexer_line,
  1101. "rtx type `%s' has `0' in position %lu, can't handle",
  1102. rtx_name[i], (unsigned long) aindex);
  1103. t = &string_type;
  1104. subname = "rt_int";
  1105. }
  1106. break;
  1107. case 's':
  1108. case 'S':
  1109. case 'T':
  1110. t = &string_type;
  1111. subname = "rt_str";
  1112. break;
  1113. case 'e':
  1114. case 'u':
  1115. t = rtx_tp;
  1116. subname = "rt_rtx";
  1117. break;
  1118. case 'E':
  1119. case 'V':
  1120. t = rtvec_tp;
  1121. subname = "rt_rtvec";
  1122. break;
  1123. case 't':
  1124. t = tree_tp;
  1125. subname = "rt_tree";
  1126. break;
  1127. case 'B':
  1128. t = basic_block_tp;
  1129. subname = "rt_bb";
  1130. break;
  1131. default:
  1132. error_at_line
  1133. (&lexer_line,
  1134. "rtx type `%s' has `%c' in position %lu, can't handle",
  1135. rtx_name[i], rtx_format[i][aindex],
  1136. (unsigned long) aindex);
  1137. t = &string_type;
  1138. subname = "rt_int";
  1139. break;
  1140. }
  1141. subfields = create_field (subfields, t,
  1142. xasprintf (".fld[%lu].%s",
  1143. (unsigned long) aindex,
  1144. subname));
  1145. subfields->opt = nodot;
  1146. if (t == note_union_tp)
  1147. subfields->opt =
  1148. create_string_option (subfields->opt, "desc",
  1149. "NOTE_KIND (&%0)");
  1150. if (t == symbol_union_tp)
  1151. subfields->opt =
  1152. create_string_option (subfields->opt, "desc",
  1153. "CONSTANT_POOL_ADDRESS_P (&%0)");
  1154. }
  1155. if (i == SYMBOL_REF)
  1156. {
  1157. /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
  1158. holds. */
  1159. type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
  1160. subfields
  1161. = create_optional_field (subfields, field_tp, "block_sym",
  1162. "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
  1163. }
  1164. sname = xasprintf ("rtx_def_%s", rtx_name[i]);
  1165. substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
  1166. NULL, NULL);
  1167. ftag = xstrdup (rtx_name[i]);
  1168. for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
  1169. ftag[nmindex] = TOUPPER (ftag[nmindex]);
  1170. flds = create_field (flds, substruct, "");
  1171. flds->opt = create_string_option (nodot, "tag", ftag);
  1172. }
  1173. return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
  1174. nodot, NULL);
  1175. }
  1176. /* Handle `special("tree_exp")'. This is a special case for
  1177. field `operands' of struct tree_exp, which although it claims to contain
  1178. pointers to trees, actually sometimes contains pointers to RTL too.
  1179. Passed T, the old type of the field, and OPT its options. Returns
  1180. a new type for the field. */
  1181. static type_p
  1182. adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
  1183. {
  1184. pair_p flds;
  1185. options_p nodot;
  1186. if (t->kind != TYPE_ARRAY)
  1187. {
  1188. error_at_line (&lexer_line,
  1189. "special `tree_exp' must be applied to an array");
  1190. return &string_type;
  1191. }
  1192. nodot = create_string_option (NULL, "dot", "");
  1193. flds = create_field (NULL, t, "");
  1194. flds->opt = create_string_option (nodot, "length",
  1195. "TREE_OPERAND_LENGTH ((tree) &%0)");
  1196. flds->opt = create_string_option (flds->opt, "default", "");
  1197. return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
  1198. nodot, NULL);
  1199. }
  1200. /* Perform any special processing on a type T, about to become the type
  1201. of a field. Return the appropriate type for the field.
  1202. At present:
  1203. - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
  1204. - Similarly for arrays of pointer-to-char;
  1205. - Converts structures for which a parameter is provided to
  1206. TYPE_PARAM_STRUCT;
  1207. - Handles "special" options.
  1208. */
  1209. type_p
  1210. adjust_field_type (type_p t, options_p opt)
  1211. {
  1212. int length_p = 0;
  1213. const int pointer_p = t->kind == TYPE_POINTER;
  1214. for (; opt; opt = opt->next)
  1215. if (strcmp (opt->name, "length") == 0)
  1216. {
  1217. if (length_p)
  1218. error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
  1219. if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
  1220. {
  1221. error_at_line (&lexer_line,
  1222. "option `%s' may not be applied to "
  1223. "arrays of atomic types", opt->name);
  1224. }
  1225. length_p = 1;
  1226. }
  1227. else if (strcmp (opt->name, "special") == 0
  1228. && opt->kind == OPTION_STRING)
  1229. {
  1230. const char *special_name = opt->info.string;
  1231. if (strcmp (special_name, "tree_exp") == 0)
  1232. t = adjust_field_tree_exp (t, opt);
  1233. else if (strcmp (special_name, "rtx_def") == 0)
  1234. t = adjust_field_rtx_def (t, opt);
  1235. else
  1236. error_at_line (&lexer_line, "unknown special `%s'", special_name);
  1237. }
  1238. if (!length_p
  1239. && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
  1240. return &string_type;
  1241. if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
  1242. && t->u.a.p->u.p->kind == TYPE_SCALAR
  1243. && t->u.a.p->u.p->u.scalar_is_char)
  1244. return create_array (&string_type, t->u.a.len);
  1245. return t;
  1246. }
  1247. static void set_gc_used_type (type_p, enum gc_used_enum, bool = false);
  1248. static void set_gc_used (pair_p);
  1249. /* Handle OPT for set_gc_used_type. */
  1250. static void
  1251. process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
  1252. int *length, int *skip, type_p *nested_ptr)
  1253. {
  1254. options_p o;
  1255. for (o = opt; o; o = o->next)
  1256. if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
  1257. && o->kind == OPTION_TYPE)
  1258. set_gc_used_type (o->info.type,
  1259. GC_POINTED_TO);
  1260. else if (strcmp (o->name, "maybe_undef") == 0)
  1261. *maybe_undef = 1;
  1262. else if (strcmp (o->name, "length") == 0)
  1263. *length = 1;
  1264. else if (strcmp (o->name, "skip") == 0)
  1265. *skip = 1;
  1266. else if (strcmp (o->name, "nested_ptr") == 0
  1267. && o->kind == OPTION_NESTED)
  1268. *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
  1269. }
  1270. /* Set the gc_used field of T to LEVEL, and handle the types it references.
  1271. If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
  1272. are set to GC_UNUSED. Otherwise, an error is emitted for
  1273. TYPE_UNDEFINED types. This is used to support user-defined
  1274. template types with non-type arguments.
  1275. For instance, when we parse a template type with enum arguments
  1276. (e.g. MyType<AnotherType, EnumValue>), the parser created two
  1277. artificial fields for 'MyType', one for 'AnotherType', the other
  1278. one for 'EnumValue'.
  1279. At the time that we parse this type we don't know that 'EnumValue'
  1280. is really an enum value, so the parser creates a TYPE_UNDEFINED
  1281. type for it. Since 'EnumValue' is never resolved to a known
  1282. structure, it will stay with TYPE_UNDEFINED.
  1283. Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
  1284. 'EnumValue'. Generating marking code for it would cause
  1285. compilation failures since the marking routines assumes that
  1286. 'EnumValue' is a type. */
  1287. static void
  1288. set_gc_used_type (type_p t, enum gc_used_enum level,
  1289. bool allow_undefined_types)
  1290. {
  1291. if (t->gc_used >= level)
  1292. return;
  1293. t->gc_used = level;
  1294. switch (t->kind)
  1295. {
  1296. case TYPE_STRUCT:
  1297. case TYPE_UNION:
  1298. case TYPE_USER_STRUCT:
  1299. {
  1300. pair_p f;
  1301. int dummy;
  1302. type_p dummy2;
  1303. bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
  1304. process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy,
  1305. &dummy2);
  1306. if (t->u.s.base_class)
  1307. set_gc_used_type (t->u.s.base_class, level, allow_undefined_types);
  1308. /* Anything pointing to a base class might actually be pointing
  1309. to a subclass. */
  1310. for (type_p subclass = t->u.s.first_subclass; subclass;
  1311. subclass = subclass->u.s.next_sibling_class)
  1312. set_gc_used_type (subclass, level, allow_undefined_types);
  1313. FOR_ALL_INHERITED_FIELDS(t, f)
  1314. {
  1315. int maybe_undef = 0;
  1316. int length = 0;
  1317. int skip = 0;
  1318. type_p nested_ptr = NULL;
  1319. process_gc_options (f->opt, level, &maybe_undef, &length, &skip,
  1320. &nested_ptr);
  1321. if (nested_ptr && f->type->kind == TYPE_POINTER)
  1322. set_gc_used_type (nested_ptr, GC_POINTED_TO);
  1323. else if (length && f->type->kind == TYPE_POINTER)
  1324. set_gc_used_type (f->type->u.p, GC_USED);
  1325. else if (maybe_undef && f->type->kind == TYPE_POINTER)
  1326. set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO);
  1327. else if (skip)
  1328. ; /* target type is not used through this field */
  1329. else
  1330. set_gc_used_type (f->type, GC_USED, allow_undefined_field_types);
  1331. }
  1332. break;
  1333. }
  1334. case TYPE_UNDEFINED:
  1335. if (level > GC_UNUSED)
  1336. {
  1337. if (!allow_undefined_types)
  1338. error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
  1339. t->gc_used = GC_UNUSED;
  1340. }
  1341. break;
  1342. case TYPE_POINTER:
  1343. set_gc_used_type (t->u.p, GC_POINTED_TO);
  1344. break;
  1345. case TYPE_ARRAY:
  1346. set_gc_used_type (t->u.a.p, GC_USED);
  1347. break;
  1348. case TYPE_LANG_STRUCT:
  1349. for (t = t->u.s.lang_struct; t; t = t->next)
  1350. set_gc_used_type (t, level);
  1351. break;
  1352. default:
  1353. break;
  1354. }
  1355. }
  1356. /* Set the gc_used fields of all the types pointed to by VARIABLES. */
  1357. static void
  1358. set_gc_used (pair_p variables)
  1359. {
  1360. int nbvars = 0;
  1361. pair_p p;
  1362. for (p = variables; p; p = p->next)
  1363. {
  1364. set_gc_used_type (p->type, GC_USED);
  1365. nbvars++;
  1366. };
  1367. if (verbosity_level >= 2)
  1368. printf ("%s used %d GTY-ed variables\n", progname, nbvars);
  1369. }
  1370. /* File mapping routines. For each input file, there is one output .c file
  1371. (but some output files have many input files), and there is one .h file
  1372. for the whole build. */
  1373. /* Output file handling. */
  1374. /* Create and return an outf_p for a new file for NAME, to be called
  1375. ONAME. */
  1376. static outf_p
  1377. create_file (const char *name, const char *oname)
  1378. {
  1379. static const char *const hdr[] = {
  1380. " Copyright (C) 2004-2015 Free Software Foundation, Inc.\n",
  1381. "\n",
  1382. "This file is part of GCC.\n",
  1383. "\n",
  1384. "GCC is free software; you can redistribute it and/or modify it under\n",
  1385. "the terms of the GNU General Public License as published by the Free\n",
  1386. "Software Foundation; either version 3, or (at your option) any later\n",
  1387. "version.\n",
  1388. "\n",
  1389. "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
  1390. "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
  1391. "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
  1392. "for more details.\n",
  1393. "\n",
  1394. "You should have received a copy of the GNU General Public License\n",
  1395. "along with GCC; see the file COPYING3. If not see\n",
  1396. "<http://www.gnu.org/licenses/>. */\n",
  1397. "\n",
  1398. "/* This file is machine generated. Do not edit. */\n"
  1399. };
  1400. outf_p f;
  1401. size_t i;
  1402. gcc_assert (name != NULL);
  1403. gcc_assert (oname != NULL);
  1404. f = XCNEW (struct outf);
  1405. f->next = output_files;
  1406. f->name = oname;
  1407. output_files = f;
  1408. oprintf (f, "/* Type information for %s.\n", name);
  1409. for (i = 0; i < ARRAY_SIZE (hdr); i++)
  1410. oprintf (f, "%s", hdr[i]);
  1411. return f;
  1412. }
  1413. /* Print, like fprintf, to O.
  1414. N.B. You might think this could be implemented more efficiently
  1415. with vsnprintf(). Unfortunately, there are C libraries that
  1416. provide that function but without the C99 semantics for its return
  1417. value, making it impossible to know how much space is required. */
  1418. void
  1419. oprintf (outf_p o, const char *format, ...)
  1420. {
  1421. char *s;
  1422. size_t slength;
  1423. va_list ap;
  1424. /* In plugin mode, the O could be a NULL pointer, so avoid crashing
  1425. in that case. */
  1426. if (!o)
  1427. return;
  1428. va_start (ap, format);
  1429. slength = vasprintf (&s, format, ap);
  1430. if (s == NULL || (int) slength < 0)
  1431. fatal ("out of memory");
  1432. va_end (ap);
  1433. if (o->bufused + slength > o->buflength)
  1434. {
  1435. size_t new_len = o->buflength;
  1436. if (new_len == 0)
  1437. new_len = 1024;
  1438. do
  1439. {
  1440. new_len *= 2;
  1441. }
  1442. while (o->bufused + slength >= new_len);
  1443. o->buf = XRESIZEVEC (char, o->buf, new_len);
  1444. o->buflength = new_len;
  1445. }
  1446. memcpy (o->buf + o->bufused, s, slength);
  1447. o->bufused += slength;
  1448. free (s);
  1449. }
  1450. /* Open the global header file and the language-specific header files. */
  1451. static void
  1452. open_base_files (void)
  1453. {
  1454. size_t i;
  1455. if (nb_plugin_files > 0 && plugin_files)
  1456. return;
  1457. header_file = create_file ("GCC", "gtype-desc.h");
  1458. base_files = XNEWVEC (outf_p, num_lang_dirs);
  1459. for (i = 0; i < num_lang_dirs; i++)
  1460. base_files[i] = create_file (lang_dir_names[i],
  1461. xasprintf ("gtype-%s.h", lang_dir_names[i]));
  1462. /* gtype-desc.c is a little special, so we create it here. */
  1463. {
  1464. /* The order of files here matters very much. */
  1465. static const char *const ifiles[] = {
  1466. "config.h", "system.h", "coretypes.h", "tm.h", "insn-codes.h",
  1467. "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
  1468. "hash-set.h", "machmode.h", "vec.h", "double-int.h", "input.h",
  1469. "alias.h", "symtab.h", "options.h",
  1470. "wide-int.h", "inchash.h",
  1471. "tree.h", "fold-const.h", "rtl.h",
  1472. "machmode.h", "tm.h", "hard-reg-set.h", "input.h", "predict.h",
  1473. "function.h", "insn-config.h", "flags.h", "statistics.h",
  1474. "real.h", "fixed-value.h", "tree.h", "expmed.h", "dojump.h",
  1475. "explow.h", "calls.h", "emit-rtl.h", "varasm.h", "stmt.h",
  1476. "expr.h", "alloc-pool.h",
  1477. "basic-block.h", "cselib.h", "insn-addr.h",
  1478. "optabs.h", "libfuncs.h", "debug.h", "ggc.h",
  1479. "ggc.h", "dominance.h", "cfg.h", "basic-block.h",
  1480. "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
  1481. "gimple-expr.h", "is-a.h",
  1482. "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
  1483. "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
  1484. "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
  1485. "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
  1486. "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
  1487. "except.h", "output.h", "cfgloop.h", "target.h", "lto-streamer.h",
  1488. "target-globals.h", "ipa-ref.h", "cgraph.h", "symbol-summary.h",
  1489. "ipa-prop.h", "ipa-inline.h", "dwarf2out.h", "omp-low.h", NULL
  1490. };
  1491. const char *const *ifp;
  1492. outf_p gtype_desc_c;
  1493. gtype_desc_c = create_file ("GCC", "gtype-desc.c");
  1494. for (ifp = ifiles; *ifp; ifp++)
  1495. oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
  1496. /* Make sure we handle "cfun" specially. */
  1497. oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
  1498. oprintf (gtype_desc_c, "#undef cfun\n");
  1499. oprintf (gtype_desc_c,
  1500. "\n"
  1501. "/* Types with a \"gcc::\" namespace have it stripped\n"
  1502. " during gengtype parsing. Provide a \"using\" directive\n"
  1503. " to ensure that the fully-qualified types are found. */\n"
  1504. "using namespace gcc;\n");
  1505. }
  1506. }
  1507. /* For INPF an input file, return the real basename of INPF, with all
  1508. the directory components skipped. */
  1509. static const char *
  1510. get_file_realbasename (const input_file *inpf)
  1511. {
  1512. return lbasename (get_input_file_name (inpf));
  1513. }
  1514. /* For INPF a filename, return the relative path to INPF from
  1515. $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
  1516. const char *
  1517. get_file_srcdir_relative_path (const input_file *inpf)
  1518. {
  1519. const char *f = get_input_file_name (inpf);
  1520. if (strlen (f) > srcdir_len
  1521. && IS_DIR_SEPARATOR (f[srcdir_len])
  1522. && strncmp (f, srcdir, srcdir_len) == 0)
  1523. return f + srcdir_len + 1;
  1524. else
  1525. return NULL;
  1526. }
  1527. /* For INPF an input_file, return the relative path to INPF from
  1528. $(srcdir) if the latter is a prefix in INPF, or the real basename
  1529. of INPF otherwise. */
  1530. static const char *
  1531. get_file_basename (const input_file *inpf)
  1532. {
  1533. const char *srcdir_path = get_file_srcdir_relative_path (inpf);
  1534. return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
  1535. }
  1536. /* For F a filename, return the lang_dir_names relative index of the language
  1537. directory that is a prefix in F, if any, -1 otherwise. */
  1538. static int
  1539. get_prefix_langdir_index (const char *f)
  1540. {
  1541. size_t f_len = strlen (f);
  1542. size_t lang_index;
  1543. for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
  1544. {
  1545. const char *langdir = lang_dir_names[lang_index];
  1546. size_t langdir_len = strlen (langdir);
  1547. if (f_len > langdir_len
  1548. && IS_DIR_SEPARATOR (f[langdir_len])
  1549. && memcmp (f, langdir, langdir_len) == 0)
  1550. return lang_index;
  1551. }
  1552. return -1;
  1553. }
  1554. /* For INPF an input file, return the name of language directory where
  1555. F is located, if any, NULL otherwise. */
  1556. static const char *
  1557. get_file_langdir (const input_file *inpf)
  1558. {
  1559. /* Get the relative path to INPF from $(srcdir) and find the
  1560. language by comparing the prefix with language directory names.
  1561. If INPF is not even srcdir relative, no point in looking
  1562. further. */
  1563. int lang_index;
  1564. const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
  1565. const char *r;
  1566. if (!srcdir_relative_path)
  1567. return NULL;
  1568. lang_index = get_prefix_langdir_index (srcdir_relative_path);
  1569. if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
  1570. r = "c-family";
  1571. else if (lang_index >= 0)
  1572. r = lang_dir_names[lang_index];
  1573. else
  1574. r = NULL;
  1575. return r;
  1576. }
  1577. /* The gt- output file name for INPF. */
  1578. static const char *
  1579. get_file_gtfilename (const input_file *inpf)
  1580. {
  1581. /* Cook up an initial version of the gt- file name from the file real
  1582. basename and the language name, if any. */
  1583. const char *basename = get_file_realbasename (inpf);
  1584. const char *langdir = get_file_langdir (inpf);
  1585. char *result =
  1586. (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
  1587. : xasprintf ("gt-%s", basename));
  1588. /* Then replace all non alphanumerics characters by '-' and change the
  1589. extension to ".h". We expect the input filename extension was at least
  1590. one character long. */
  1591. char *s = result;
  1592. for (; *s != '.'; s++)
  1593. if (!ISALNUM (*s) && *s != '-')
  1594. *s = '-';
  1595. memcpy (s, ".h", sizeof (".h"));
  1596. return result;
  1597. }
  1598. /* Each input_file has its associated output file outf_p. The
  1599. association is computed by the function
  1600. get_output_file_with_visibility. The associated file is cached
  1601. inside input_file in its inpoutf field, so is really computed only
  1602. once. Associated output file paths (i.e. output_name-s) are
  1603. computed by a rule based regexp machinery, using the files_rules
  1604. array of struct file_rule_st. A for_name is also computed, giving
  1605. the source file name for which the output_file is generated; it is
  1606. often the last component of the input_file path. */
  1607. /*
  1608. Regexpr machinery to compute the output_name and for_name-s of each
  1609. input_file. We have a sequence of file rules which gives the POSIX
  1610. extended regular expression to match an input file path, and two
  1611. transformed strings for the corresponding output_name and the
  1612. corresponding for_name. The transformed string contain dollars: $0
  1613. is replaced by the entire match, $1 is replaced by the substring
  1614. matching the first parenthesis in the regexp, etc. And $$ is replaced
  1615. by a single verbatim dollar. The rule order is important. The
  1616. general case is last, and the particular cases should come before.
  1617. An action routine can, when needed, update the out_name & for_name
  1618. and/or return the appropriate output file. It is invoked only when a
  1619. rule is triggered. When a rule is triggered, the output_name and
  1620. for_name are computed using their transform string in while $$, $0,
  1621. $1, ... are suitably replaced. If there is an action, it is called.
  1622. In some few cases, the action can directly return the outf_p, but
  1623. usually it just updates the output_name and for_name so should free
  1624. them before replacing them. The get_output_file_with_visibility
  1625. function creates an outf_p only once per each output_name, so it
  1626. scans the output_files list for previously seen output file names.
  1627. */
  1628. /* Signature of actions in file rules. */
  1629. typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
  1630. struct file_rule_st {
  1631. const char* frul_srcexpr; /* Source string for regexp. */
  1632. int frul_rflags; /* Flags passed to regcomp, usually
  1633. * REG_EXTENDED. */
  1634. regex_t* frul_re; /* Compiled regular expression
  1635. obtained by regcomp. */
  1636. const char* frul_tr_out; /* Transformation string for making
  1637. * the output_name, with $1 ... $9 for
  1638. * subpatterns and $0 for the whole
  1639. * matched filename. */
  1640. const char* frul_tr_for; /* Tranformation string for making the
  1641. for_name. */
  1642. frul_actionrout_t* frul_action; /* The action, if non null, is
  1643. * called once the rule matches, on
  1644. * the transformed out_name &
  1645. * for_name. It could change them
  1646. * and/or give the output file. */
  1647. };
  1648. /* File rule action handling *.h files. */
  1649. static outf_p header_dot_h_frul (input_file*, char**, char**);
  1650. /* File rule action handling *.c files. */
  1651. static outf_p source_dot_c_frul (input_file*, char**, char**);
  1652. #define NULL_REGEX (regex_t*)0
  1653. /* The prefix in our regexp-s matching the directory. */
  1654. #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
  1655. #define NULL_FRULACT (frul_actionrout_t*)0
  1656. /* The array of our rules governing file name generation. Rules order
  1657. matters, so change with extreme care! */
  1658. struct file_rule_st files_rules[] = {
  1659. /* The general rule assumes that files in subdirectories belong to a
  1660. particular front-end, and files not in subdirectories are shared.
  1661. The following rules deal with exceptions - files that are in
  1662. subdirectories and yet are shared, and files that are top-level,
  1663. but are not shared. */
  1664. /* the c-family/ source directory is special. */
  1665. { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
  1666. REG_EXTENDED, NULL_REGEX,
  1667. "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
  1668. { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
  1669. REG_EXTENDED, NULL_REGEX,
  1670. "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
  1671. /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
  1672. { DIR_PREFIX_REGEX "c/c-lang\\.h$",
  1673. REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
  1674. { DIR_PREFIX_REGEX "c/c-tree\\.h$",
  1675. REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
  1676. /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
  1677. { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
  1678. REG_EXTENDED, NULL_REGEX,
  1679. "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
  1680. /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
  1681. { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
  1682. REG_EXTENDED, NULL_REGEX,
  1683. "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
  1684. /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
  1685. { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
  1686. REG_EXTENDED, NULL_REGEX,
  1687. "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
  1688. /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
  1689. { DIR_PREFIX_REGEX "cp/parser\\.h$",
  1690. REG_EXTENDED, NULL_REGEX,
  1691. "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
  1692. /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
  1693. { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
  1694. REG_EXTENDED, NULL_REGEX,
  1695. "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
  1696. /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
  1697. { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
  1698. REG_EXTENDED, NULL_REGEX,
  1699. "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
  1700. /* General cases. For header *.h and source *.c or *.cc files, we
  1701. * need special actions to handle the language. */
  1702. /* Source *.c files are using get_file_gtfilename to compute their
  1703. output_name and get_file_basename to compute their for_name
  1704. through the source_dot_c_frul action. */
  1705. { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
  1706. REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
  1707. /* Source *.cc files are using get_file_gtfilename to compute their
  1708. output_name and get_file_basename to compute their for_name
  1709. through the source_dot_c_frul action. */
  1710. { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
  1711. REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_c_frul},
  1712. /* Common header files get "gtype-desc.c" as their output_name,
  1713. * while language specific header files are handled specially. So
  1714. * we need the header_dot_h_frul action. */
  1715. { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
  1716. REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
  1717. { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
  1718. REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
  1719. /* Mandatory null last entry signaling end of rules. */
  1720. {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
  1721. };
  1722. /* Special file rules action for handling *.h header files. It gives
  1723. "gtype-desc.c" for common headers and corresponding output
  1724. files for language-specific header files. */
  1725. static outf_p
  1726. header_dot_h_frul (input_file* inpf, char**poutname,
  1727. char**pforname ATTRIBUTE_UNUSED)
  1728. {
  1729. const char *basename = 0;
  1730. int lang_index = 0;
  1731. DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
  1732. (void*) inpf, get_input_file_name (inpf),
  1733. *poutname, *pforname);
  1734. basename = get_file_basename (inpf);
  1735. lang_index = get_prefix_langdir_index (basename);
  1736. DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
  1737. if (lang_index >= 0)
  1738. {
  1739. /* The header is language specific. Given output_name &
  1740. for_name remains unchanged. The base_files array gives the
  1741. outf_p. */
  1742. DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
  1743. (void*) base_files[lang_index],
  1744. (base_files[lang_index])->name);
  1745. return base_files[lang_index];
  1746. }
  1747. else
  1748. {
  1749. /* The header is common to all front-end languages. So
  1750. output_name is "gtype-desc.c" file. The calling function
  1751. get_output_file_with_visibility will find its outf_p. */
  1752. free (*poutname);
  1753. *poutname = xstrdup ("gtype-desc.c");
  1754. DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
  1755. get_input_file_name (inpf));
  1756. return NULL;
  1757. }
  1758. }
  1759. /* Special file rules action for handling *.c source files using
  1760. * get_file_gtfilename to compute their output_name and
  1761. * get_file_basename to compute their for_name. The output_name is
  1762. * gt-<LANG>-<BASE>.h for language specific source files, and
  1763. * gt-<BASE>.h for common source files. */
  1764. static outf_p
  1765. source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
  1766. {
  1767. char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
  1768. char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
  1769. DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
  1770. (void*) inpf, get_input_file_name (inpf),
  1771. *poutname, *pforname);
  1772. DBGPRINTF ("newoutname %s", newoutname);
  1773. DBGPRINTF ("newbasename %s", newbasename);
  1774. free (*poutname);
  1775. free (*pforname);
  1776. *poutname = newoutname;
  1777. *pforname = newbasename;
  1778. return NULL;
  1779. }
  1780. /* Utility function for get_output_file_with_visibility which returns
  1781. * a malloc-ed substituted string using TRS on matching of the FILNAM
  1782. * file name, using the PMATCH array. */
  1783. static char*
  1784. matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
  1785. const char *trs)
  1786. {
  1787. struct obstack str_obstack;
  1788. char *str = NULL;
  1789. char *rawstr = NULL;
  1790. const char *pt = NULL;
  1791. DBGPRINTF ("filnam %s", filnam);
  1792. obstack_init (&str_obstack);
  1793. for (pt = trs; *pt; pt++) {
  1794. char c = *pt;
  1795. if (c == '$')
  1796. {
  1797. if (pt[1] == '$')
  1798. {
  1799. /* A double dollar $$ is substituted by a single verbatim
  1800. dollar, but who really uses dollar signs in file
  1801. paths? */
  1802. obstack_1grow (&str_obstack, '$');
  1803. }
  1804. else if (ISDIGIT (pt[1]))
  1805. {
  1806. /* Handle $0 $1 ... $9 by appropriate substitution. */
  1807. int dolnum = pt[1] - '0';
  1808. int so = pmatch[dolnum].rm_so;
  1809. int eo = pmatch[dolnum].rm_eo;
  1810. DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
  1811. if (so>=0 && eo>=so)
  1812. obstack_grow (&str_obstack, filnam + so, eo - so);
  1813. }
  1814. else
  1815. {
  1816. /* This can happen only when files_rules is buggy! */
  1817. gcc_unreachable ();
  1818. }
  1819. /* Always skip the character after the dollar. */
  1820. pt++;
  1821. }
  1822. else
  1823. obstack_1grow (&str_obstack, c);
  1824. }
  1825. obstack_1grow (&str_obstack, '\0');
  1826. rawstr = XOBFINISH (&str_obstack, char *);
  1827. str = xstrdup (rawstr);
  1828. obstack_free (&str_obstack, NULL);
  1829. DBGPRINTF ("matched replacement %s", str);
  1830. rawstr = NULL;
  1831. return str;
  1832. }
  1833. /* An output file, suitable for definitions, that can see declarations
  1834. made in INPF and is linked into every language that uses INPF.
  1835. Since the result is cached inside INPF, that argument cannot be
  1836. declared constant, but is "almost" constant. */
  1837. outf_p
  1838. get_output_file_with_visibility (input_file *inpf)
  1839. {
  1840. outf_p r;
  1841. char *for_name = NULL;
  1842. char *output_name = NULL;
  1843. const char* inpfname;
  1844. /* This can happen when we need a file with visibility on a
  1845. structure that we've never seen. We have to just hope that it's
  1846. globally visible. */
  1847. if (inpf == NULL)
  1848. inpf = system_h_file;
  1849. /* The result is cached in INPF, so return it if already known. */
  1850. if (inpf->inpoutf)
  1851. return inpf->inpoutf;
  1852. /* In plugin mode, return NULL unless the input_file is one of the
  1853. plugin_files. */
  1854. if (plugin_files)
  1855. {
  1856. size_t i;
  1857. for (i = 0; i < nb_plugin_files; i++)
  1858. if (inpf == plugin_files[i])
  1859. {
  1860. inpf->inpoutf = plugin_output;
  1861. return plugin_output;
  1862. }
  1863. return NULL;
  1864. }
  1865. inpfname = get_input_file_name (inpf);
  1866. /* Try each rule in sequence in files_rules until one is triggered. */
  1867. {
  1868. int rulix = 0;
  1869. DBGPRINTF ("passing input file @ %p named %s through the files_rules",
  1870. (void*) inpf, inpfname);
  1871. for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
  1872. {
  1873. DBGPRINTF ("rulix#%d srcexpr %s",
  1874. rulix, files_rules[rulix].frul_srcexpr);
  1875. if (!files_rules[rulix].frul_re)
  1876. {
  1877. /* Compile the regexpr lazily. */
  1878. int err = 0;
  1879. files_rules[rulix].frul_re = XCNEW (regex_t);
  1880. err = regcomp (files_rules[rulix].frul_re,
  1881. files_rules[rulix].frul_srcexpr,
  1882. files_rules[rulix].frul_rflags);
  1883. if (err)
  1884. {
  1885. /* The regular expression compilation fails only when
  1886. file_rules is buggy. */
  1887. gcc_unreachable ();
  1888. }
  1889. }
  1890. output_name = NULL;
  1891. for_name = NULL;
  1892. /* Match the regexpr and trigger the rule if matched. */
  1893. {
  1894. /* We have exactly ten pmatch-s, one for each $0, $1, $2,
  1895. $3, ... $9. */
  1896. regmatch_t pmatch[10];
  1897. memset (pmatch, 0, sizeof (pmatch));
  1898. if (!regexec (files_rules[rulix].frul_re,
  1899. inpfname, 10, pmatch, 0))
  1900. {
  1901. DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
  1902. (void*) inpf, inpfname, rulix,
  1903. files_rules[rulix].frul_srcexpr);
  1904. for_name =
  1905. matching_file_name_substitute (inpfname, pmatch,
  1906. files_rules[rulix].frul_tr_for);
  1907. DBGPRINTF ("for_name %s", for_name);
  1908. output_name =
  1909. matching_file_name_substitute (inpfname, pmatch,
  1910. files_rules[rulix].frul_tr_out);
  1911. DBGPRINTF ("output_name %s", output_name);
  1912. if (files_rules[rulix].frul_action)
  1913. {
  1914. /* Invoke our action routine. */
  1915. outf_p of = NULL;
  1916. DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
  1917. rulix, output_name, for_name);
  1918. of =
  1919. (files_rules[rulix].frul_action) (inpf,
  1920. &output_name, &for_name);
  1921. DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
  1922. rulix, (void*)of, output_name, for_name);
  1923. /* If the action routine returned something, give it back
  1924. immediately and cache it in inpf. */
  1925. if (of)
  1926. {
  1927. inpf->inpoutf = of;
  1928. return of;
  1929. }
  1930. }
  1931. /* The rule matched, and had no action, or that action did
  1932. not return any output file but could have changed the
  1933. output_name or for_name. We break out of the loop on the
  1934. files_rules. */
  1935. break;
  1936. }
  1937. else
  1938. {
  1939. /* The regexpr did not match. */
  1940. DBGPRINTF ("rulix#%d did not match %s pattern %s",
  1941. rulix, inpfname, files_rules[rulix].frul_srcexpr);
  1942. continue;
  1943. }
  1944. }
  1945. }
  1946. }
  1947. if (!output_name || !for_name)
  1948. {
  1949. /* This should not be possible, and could only happen if the
  1950. files_rules is incomplete or buggy. */
  1951. fatal ("failed to compute output name for %s", inpfname);
  1952. }
  1953. /* Look through to see if we've ever seen this output filename
  1954. before. If found, cache the result in inpf. */
  1955. for (r = output_files; r; r = r->next)
  1956. if (filename_cmp (r->name, output_name) == 0)
  1957. {
  1958. inpf->inpoutf = r;
  1959. DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
  1960. output_name, for_name);
  1961. return r;
  1962. }
  1963. /* If not found, create it, and cache it in inpf. */
  1964. r = create_file (for_name, output_name);
  1965. gcc_assert (r && r->name);
  1966. DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
  1967. output_name, for_name);
  1968. inpf->inpoutf = r;
  1969. return r;
  1970. }
  1971. /* The name of an output file, suitable for definitions, that can see
  1972. declarations made in INPF and is linked into every language that
  1973. uses INPF. */
  1974. const char *
  1975. get_output_file_name (input_file* inpf)
  1976. {
  1977. outf_p o = get_output_file_with_visibility (inpf);
  1978. if (o)
  1979. return o->name;
  1980. return NULL;
  1981. }
  1982. /* Check if existing file is equal to the in memory buffer. */
  1983. static bool
  1984. is_file_equal (outf_p of)
  1985. {
  1986. FILE *newfile = fopen (of->name, "r");
  1987. size_t i;
  1988. bool equal;
  1989. if (newfile == NULL)
  1990. return false;
  1991. equal = true;
  1992. for (i = 0; i < of->bufused; i++)
  1993. {
  1994. int ch;
  1995. ch = fgetc (newfile);
  1996. if (ch == EOF || ch != (unsigned char) of->buf[i])
  1997. {
  1998. equal = false;
  1999. break;
  2000. }
  2001. }
  2002. if (equal && EOF != fgetc (newfile))
  2003. equal = false;
  2004. fclose (newfile);
  2005. return equal;
  2006. }
  2007. /* Copy the output to its final destination,
  2008. but don't unnecessarily change modification times. */
  2009. static void
  2010. close_output_files (void)
  2011. {
  2012. int nbwrittenfiles = 0;
  2013. outf_p of;
  2014. for (of = output_files; of; of = of->next)
  2015. {
  2016. if (!is_file_equal (of))
  2017. {
  2018. FILE *newfile = NULL;
  2019. char *backupname = NULL;
  2020. /* Back up the old version of the output file gt-FOO.c as
  2021. BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
  2022. if (backup_dir)
  2023. {
  2024. backupname = concat (backup_dir, "/",
  2025. lbasename (of->name), "~", NULL);
  2026. if (!access (of->name, F_OK) && rename (of->name, backupname))
  2027. fatal ("failed to back up %s as %s: %s",
  2028. of->name, backupname, xstrerror (errno));
  2029. }
  2030. newfile = fopen (of->name, "w");
  2031. if (newfile == NULL)
  2032. fatal ("opening output file %s: %s", of->name, xstrerror (errno));
  2033. if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
  2034. fatal ("writing output file %s: %s", of->name, xstrerror (errno));
  2035. if (fclose (newfile) != 0)
  2036. fatal ("closing output file %s: %s", of->name, xstrerror (errno));
  2037. nbwrittenfiles++;
  2038. if (verbosity_level >= 2 && backupname)
  2039. printf ("%s wrote #%-3d %s backed-up in %s\n",
  2040. progname, nbwrittenfiles, of->name, backupname);
  2041. else if (verbosity_level >= 1)
  2042. printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
  2043. free (backupname);
  2044. }
  2045. else
  2046. {
  2047. /* output file remains unchanged. */
  2048. if (verbosity_level >= 2)
  2049. printf ("%s keep %s\n", progname, of->name);
  2050. }
  2051. free (of->buf);
  2052. of->buf = NULL;
  2053. of->bufused = of->buflength = 0;
  2054. }
  2055. if (verbosity_level >= 1)
  2056. printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
  2057. }
  2058. struct flist
  2059. {
  2060. struct flist *next;
  2061. int started_p;
  2062. const input_file* file;
  2063. outf_p f;
  2064. };
  2065. struct walk_type_data;
  2066. /* For scalars and strings, given the item in 'val'.
  2067. For structures, given a pointer to the item in 'val'.
  2068. For misc. pointers, given the item in 'val'.
  2069. */
  2070. typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
  2071. typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
  2072. /* Parameters for write_types. */
  2073. struct write_types_data
  2074. {
  2075. const char *prefix;
  2076. const char *param_prefix;
  2077. const char *subfield_marker_routine;
  2078. const char *marker_routine;
  2079. const char *reorder_note_routine;
  2080. const char *comment;
  2081. int skip_hooks; /* skip hook generation if non zero */
  2082. enum write_types_kinds kind;
  2083. };
  2084. static void output_escaped_param (struct walk_type_data *d,
  2085. const char *, const char *);
  2086. static void output_mangled_typename (outf_p, const_type_p);
  2087. static void walk_type (type_p t, struct walk_type_data *d);
  2088. static void write_func_for_structure (type_p orig_s, type_p s,
  2089. const struct write_types_data *wtd);
  2090. static void write_types_process_field
  2091. (type_p f, const struct walk_type_data *d);
  2092. static void write_types (outf_p output_header,
  2093. type_p structures,
  2094. const struct write_types_data *wtd);
  2095. static void write_types_local_process_field
  2096. (type_p f, const struct walk_type_data *d);
  2097. static void write_local_func_for_structure (const_type_p orig_s, type_p s);
  2098. static void write_local (outf_p output_header,
  2099. type_p structures);
  2100. static int contains_scalar_p (type_p t);
  2101. static void put_mangled_filename (outf_p, const input_file *);
  2102. static void finish_root_table (struct flist *flp, const char *pfx,
  2103. const char *tname, const char *lastname,
  2104. const char *name);
  2105. static void write_root (outf_p, pair_p, type_p, const char *, int,
  2106. struct fileloc *, bool);
  2107. static void write_array (outf_p f, pair_p v,
  2108. const struct write_types_data *wtd);
  2109. static void write_roots (pair_p, bool);
  2110. /* Parameters for walk_type. */
  2111. struct walk_type_data
  2112. {
  2113. process_field_fn process_field;
  2114. const void *cookie;
  2115. outf_p of;
  2116. options_p opt;
  2117. const char *val;
  2118. const char *prev_val[4];
  2119. int indent;
  2120. int counter;
  2121. const struct fileloc *line;
  2122. lang_bitmap bitmap;
  2123. int used_length;
  2124. type_p orig_s;
  2125. const char *reorder_fn;
  2126. bool needs_cast_p;
  2127. bool fn_wants_lvalue;
  2128. bool in_record_p;
  2129. int loopcounter;
  2130. bool in_ptr_field;
  2131. bool have_this_obj;
  2132. };
  2133. /* Given a string TYPE_NAME, representing a C++ typename, return a valid
  2134. pre-processor identifier to use in a #define directive. This replaces
  2135. special characters used in C++ identifiers like '>', '<' and ':' with
  2136. '_'.
  2137. If no C++ special characters are found in TYPE_NAME, return
  2138. TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
  2139. characters replaced with '_'. In this case, the caller is
  2140. responsible for freeing the allocated string. */
  2141. static const char *
  2142. filter_type_name (const char *type_name)
  2143. {
  2144. if (strchr (type_name, '<') || strchr (type_name, ':'))
  2145. {
  2146. size_t i;
  2147. char *s = xstrdup (type_name);
  2148. for (i = 0; i < strlen (s); i++)
  2149. if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ','
  2150. || s[i] == '*')
  2151. s[i] = '_';
  2152. return s;
  2153. }
  2154. else
  2155. return type_name;
  2156. }
  2157. /* Print a mangled name representing T to OF. */
  2158. static void
  2159. output_mangled_typename (outf_p of, const_type_p t)
  2160. {
  2161. if (t == NULL)
  2162. oprintf (of, "Z");
  2163. else
  2164. switch (t->kind)
  2165. {
  2166. case TYPE_NONE:
  2167. case TYPE_UNDEFINED:
  2168. gcc_unreachable ();
  2169. break;
  2170. case TYPE_POINTER:
  2171. oprintf (of, "P");
  2172. output_mangled_typename (of, t->u.p);
  2173. break;
  2174. case TYPE_SCALAR:
  2175. oprintf (of, "I");
  2176. break;
  2177. case TYPE_STRING:
  2178. oprintf (of, "S");
  2179. break;
  2180. case TYPE_STRUCT:
  2181. case TYPE_UNION:
  2182. case TYPE_LANG_STRUCT:
  2183. case TYPE_USER_STRUCT:
  2184. {
  2185. /* For references to classes within an inheritance hierarchy,
  2186. only ever reference the ultimate base class, since only
  2187. it will have gt_ functions. */
  2188. t = get_ultimate_base_class (t);
  2189. const char *id_for_tag = filter_type_name (t->u.s.tag);
  2190. oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
  2191. id_for_tag);
  2192. if (id_for_tag != t->u.s.tag)
  2193. free (CONST_CAST (char *, id_for_tag));
  2194. }
  2195. break;
  2196. case TYPE_ARRAY:
  2197. gcc_unreachable ();
  2198. }
  2199. }
  2200. /* Print PARAM to D->OF processing escapes. D->VAL references the
  2201. current object, D->PREV_VAL the object containing the current
  2202. object, ONAME is the name of the option and D->LINE is used to
  2203. print error messages. */
  2204. static void
  2205. output_escaped_param (struct walk_type_data *d, const char *param,
  2206. const char *oname)
  2207. {
  2208. const char *p;
  2209. for (p = param; *p; p++)
  2210. if (*p != '%')
  2211. oprintf (d->of, "%c", *p);
  2212. else
  2213. switch (*++p)
  2214. {
  2215. case 'h':
  2216. oprintf (d->of, "(%s)", d->prev_val[2]);
  2217. break;
  2218. case '0':
  2219. oprintf (d->of, "(%s)", d->prev_val[0]);
  2220. break;
  2221. case '1':
  2222. oprintf (d->of, "(%s)", d->prev_val[1]);
  2223. break;
  2224. case 'a':
  2225. {
  2226. const char *pp = d->val + strlen (d->val);
  2227. while (pp[-1] == ']')
  2228. while (*pp != '[')
  2229. pp--;
  2230. oprintf (d->of, "%s", pp);
  2231. }
  2232. break;
  2233. default:
  2234. error_at_line (d->line, "`%s' option contains bad escape %c%c",
  2235. oname, '%', *p);
  2236. }
  2237. }
  2238. const char *
  2239. get_string_option (options_p opt, const char *key)
  2240. {
  2241. for (; opt; opt = opt->next)
  2242. if (strcmp (opt->name, key) == 0)
  2243. return opt->info.string;
  2244. return NULL;
  2245. }
  2246. /* Machinery for avoiding duplicate tags within switch statements. */
  2247. struct seen_tag
  2248. {
  2249. const char *tag;
  2250. struct seen_tag *next;
  2251. };
  2252. int
  2253. already_seen_tag (struct seen_tag *seen_tags, const char *tag)
  2254. {
  2255. /* Linear search, so O(n^2), but n is currently small. */
  2256. while (seen_tags)
  2257. {
  2258. if (!strcmp (seen_tags->tag, tag))
  2259. return 1;
  2260. seen_tags = seen_tags->next;
  2261. }
  2262. /* Not yet seen this tag. */
  2263. return 0;
  2264. }
  2265. void
  2266. mark_tag_as_seen (struct seen_tag **seen_tags, const char *tag)
  2267. {
  2268. /* Add to front of linked list. */
  2269. struct seen_tag *new_node = XCNEW (struct seen_tag);
  2270. new_node->tag = tag;
  2271. new_node->next = *seen_tags;
  2272. *seen_tags = new_node;
  2273. }
  2274. static void
  2275. walk_subclasses (type_p base, struct walk_type_data *d,
  2276. struct seen_tag **seen_tags)
  2277. {
  2278. for (type_p sub = base->u.s.first_subclass; sub != NULL;
  2279. sub = sub->u.s.next_sibling_class)
  2280. {
  2281. const char *type_tag = get_string_option (sub->u.s.opt, "tag");
  2282. if (type_tag && !already_seen_tag (*seen_tags, type_tag))
  2283. {
  2284. mark_tag_as_seen (seen_tags, type_tag);
  2285. oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
  2286. d->indent += 2;
  2287. oprintf (d->of, "%*s{\n", d->indent, "");
  2288. d->indent += 2;
  2289. oprintf (d->of, "%*s%s *sub = static_cast <%s *> (x);\n",
  2290. d->indent, "", sub->u.s.tag, sub->u.s.tag);
  2291. const char *old_val = d->val;
  2292. d->val = "(*sub)";
  2293. walk_type (sub, d);
  2294. d->val = old_val;
  2295. d->indent -= 2;
  2296. oprintf (d->of, "%*s}\n", d->indent, "");
  2297. oprintf (d->of, "%*sbreak;\n", d->indent, "");
  2298. d->indent -= 2;
  2299. }
  2300. walk_subclasses (sub, d, seen_tags);
  2301. }
  2302. }
  2303. /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
  2304. which is of type T. Write code to D->OF to constrain execution (at
  2305. the point that D->PROCESS_FIELD is called) to the appropriate
  2306. cases. Call D->PROCESS_FIELD on subobjects before calling it on
  2307. pointers to those objects. D->PREV_VAL lists the objects
  2308. containing the current object, D->OPT is a list of options to
  2309. apply, D->INDENT is the current indentation level, D->LINE is used
  2310. to print error messages, D->BITMAP indicates which languages to
  2311. print the structure for. */
  2312. static void
  2313. walk_type (type_p t, struct walk_type_data *d)
  2314. {
  2315. const char *length = NULL;
  2316. const char *desc = NULL;
  2317. const char *type_tag = NULL;
  2318. int maybe_undef_p = 0;
  2319. int atomic_p = 0;
  2320. options_p oo;
  2321. const struct nested_ptr_data *nested_ptr_d = NULL;
  2322. d->needs_cast_p = false;
  2323. for (oo = d->opt; oo; oo = oo->next)
  2324. if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
  2325. length = oo->info.string;
  2326. else if (strcmp (oo->name, "maybe_undef") == 0)
  2327. maybe_undef_p = 1;
  2328. else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
  2329. desc = oo->info.string;
  2330. else if (strcmp (oo->name, "mark_hook") == 0)
  2331. ;
  2332. else if (strcmp (oo->name, "nested_ptr") == 0
  2333. && oo->kind == OPTION_NESTED)
  2334. nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
  2335. else if (strcmp (oo->name, "dot") == 0)
  2336. ;
  2337. else if (strcmp (oo->name, "tag") == 0)
  2338. type_tag = oo->info.string;
  2339. else if (strcmp (oo->name, "special") == 0)
  2340. ;
  2341. else if (strcmp (oo->name, "skip") == 0)
  2342. ;
  2343. else if (strcmp (oo->name, "atomic") == 0)
  2344. atomic_p = 1;
  2345. else if (strcmp (oo->name, "default") == 0)
  2346. ;
  2347. else if (strcmp (oo->name, "chain_next") == 0)
  2348. ;
  2349. else if (strcmp (oo->name, "chain_prev") == 0)
  2350. ;
  2351. else if (strcmp (oo->name, "chain_circular") == 0)
  2352. ;
  2353. else if (strcmp (oo->name, "reorder") == 0)
  2354. ;
  2355. else if (strcmp (oo->name, "variable_size") == 0)
  2356. ;
  2357. else if (strcmp (oo->name, "for_user") == 0)
  2358. ;
  2359. else
  2360. error_at_line (d->line, "unknown option `%s'\n", oo->name);
  2361. if (d->used_length)
  2362. length = NULL;
  2363. if (maybe_undef_p
  2364. && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
  2365. {
  2366. error_at_line (d->line,
  2367. "field `%s' has invalid option `maybe_undef_p'\n",
  2368. d->val);
  2369. return;
  2370. }
  2371. if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
  2372. {
  2373. error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
  2374. return;
  2375. }
  2376. switch (t->kind)
  2377. {
  2378. case TYPE_SCALAR:
  2379. case TYPE_STRING:
  2380. d->process_field (t, d);
  2381. break;
  2382. case TYPE_POINTER:
  2383. {
  2384. d->in_ptr_field = true;
  2385. if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
  2386. {
  2387. oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
  2388. break;
  2389. }
  2390. /* If a pointer type is marked as "atomic", we process the
  2391. field itself, but we don't walk the data that they point to.
  2392. There are two main cases where we walk types: to mark
  2393. pointers that are reachable, and to relocate pointers when
  2394. writing a PCH file. In both cases, an atomic pointer is
  2395. itself marked or relocated, but the memory that it points
  2396. to is left untouched. In the case of PCH, that memory will
  2397. be read/written unchanged to the PCH file. */
  2398. if (atomic_p)
  2399. {
  2400. oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
  2401. d->indent += 2;
  2402. d->process_field (t, d);
  2403. d->indent -= 2;
  2404. oprintf (d->of, "%*s}\n", d->indent, "");
  2405. break;
  2406. }
  2407. if (!length)
  2408. {
  2409. if (!union_or_struct_p (t->u.p))
  2410. {
  2411. error_at_line (d->line,
  2412. "field `%s' is pointer to unimplemented type",
  2413. d->val);
  2414. break;
  2415. }
  2416. if (nested_ptr_d)
  2417. {
  2418. const char *oldprevval2 = d->prev_val[2];
  2419. if (!union_or_struct_p (nested_ptr_d->type))
  2420. {
  2421. error_at_line (d->line,
  2422. "field `%s' has invalid "
  2423. "option `nested_ptr'\n", d->val);
  2424. return;
  2425. }
  2426. d->prev_val[2] = d->val;
  2427. oprintf (d->of, "%*s{\n", d->indent, "");
  2428. d->indent += 2;
  2429. d->val = xasprintf ("x%d", d->counter++);
  2430. oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
  2431. (nested_ptr_d->type->kind == TYPE_UNION
  2432. ? "union" : "struct"),
  2433. nested_ptr_d->type->u.s.tag,
  2434. d->fn_wants_lvalue ? "" : "const ", d->val);
  2435. oprintf (d->of, "%*s", d->indent + 2, "");
  2436. output_escaped_param (d, nested_ptr_d->convert_from,
  2437. "nested_ptr");
  2438. oprintf (d->of, ";\n");
  2439. d->process_field (nested_ptr_d->type, d);
  2440. if (d->fn_wants_lvalue)
  2441. {
  2442. oprintf (d->of, "%*s%s = ", d->indent, "",
  2443. d->prev_val[2]);
  2444. d->prev_val[2] = d->val;
  2445. output_escaped_param (d, nested_ptr_d->convert_to,
  2446. "nested_ptr");
  2447. oprintf (d->of, ";\n");
  2448. }
  2449. d->indent -= 2;
  2450. oprintf (d->of, "%*s}\n", d->indent, "");
  2451. d->val = d->prev_val[2];
  2452. d->prev_val[2] = oldprevval2;
  2453. }
  2454. else
  2455. d->process_field (t->u.p, d);
  2456. }
  2457. else
  2458. {
  2459. int loopcounter = d->loopcounter;
  2460. const char *oldval = d->val;
  2461. const char *oldprevval3 = d->prev_val[3];
  2462. char *newval;
  2463. oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
  2464. d->indent += 2;
  2465. oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
  2466. oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
  2467. "", loopcounter, loopcounter);
  2468. if (!d->in_record_p)
  2469. output_escaped_param (d, length, "length");
  2470. else
  2471. oprintf (d->of, "l%d", loopcounter);
  2472. if (d->have_this_obj)
  2473. /* Try to unswitch loops (see PR53880). */
  2474. oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
  2475. oprintf (d->of, "); i%d++) {\n", loopcounter);
  2476. d->indent += 2;
  2477. d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
  2478. d->used_length = 1;
  2479. d->prev_val[3] = oldval;
  2480. walk_type (t->u.p, d);
  2481. free (newval);
  2482. d->val = oldval;
  2483. d->prev_val[3] = oldprevval3;
  2484. d->used_length = 0;
  2485. d->indent -= 2;
  2486. oprintf (d->of, "%*s}\n", d->indent, "");
  2487. d->process_field (t, d);
  2488. d->indent -= 2;
  2489. oprintf (d->of, "%*s}\n", d->indent, "");
  2490. }
  2491. d->in_ptr_field = false;
  2492. }
  2493. break;
  2494. case TYPE_ARRAY:
  2495. {
  2496. int loopcounter;
  2497. const char *oldval = d->val;
  2498. char *newval;
  2499. /* If it's an array of scalars, we optimize by not generating
  2500. any code. */
  2501. if (t->u.a.p->kind == TYPE_SCALAR)
  2502. break;
  2503. if (length)
  2504. loopcounter = d->loopcounter;
  2505. else
  2506. loopcounter = d->counter++;
  2507. /* When walking an array, compute the length and store it in a
  2508. local variable before walking the array elements, instead of
  2509. recomputing the length expression each time through the loop.
  2510. This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
  2511. where the length is stored in the first array element,
  2512. because otherwise that operand can get overwritten on the
  2513. first iteration. */
  2514. oprintf (d->of, "%*s{\n", d->indent, "");
  2515. d->indent += 2;
  2516. oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
  2517. if (!d->in_record_p || !length)
  2518. {
  2519. oprintf (d->of, "%*ssize_t l%d = (size_t)(",
  2520. d->indent, "", loopcounter);
  2521. if (length)
  2522. output_escaped_param (d, length, "length");
  2523. else
  2524. oprintf (d->of, "%s", t->u.a.len);
  2525. oprintf (d->of, ");\n");
  2526. }
  2527. oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
  2528. d->indent, "",
  2529. loopcounter, loopcounter, loopcounter, loopcounter);
  2530. d->indent += 2;
  2531. d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
  2532. d->used_length = 1;
  2533. walk_type (t->u.a.p, d);
  2534. free (newval);
  2535. d->used_length = 0;
  2536. d->val = oldval;
  2537. d->indent -= 2;
  2538. oprintf (d->of, "%*s}\n", d->indent, "");
  2539. d->indent -= 2;
  2540. oprintf (d->of, "%*s}\n", d->indent, "");
  2541. }
  2542. break;
  2543. case TYPE_STRUCT:
  2544. case TYPE_UNION:
  2545. {
  2546. pair_p f;
  2547. const char *oldval = d->val;
  2548. const char *oldprevval1 = d->prev_val[1];
  2549. const char *oldprevval2 = d->prev_val[2];
  2550. const char *struct_mark_hook = NULL;
  2551. const int union_p = t->kind == TYPE_UNION;
  2552. int seen_default_p = 0;
  2553. options_p o;
  2554. int lengths_seen = 0;
  2555. int endcounter;
  2556. bool any_length_seen = false;
  2557. if (!t->u.s.line.file)
  2558. error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
  2559. if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
  2560. {
  2561. error_at_line (d->line,
  2562. "structure `%s' defined for mismatching languages",
  2563. t->u.s.tag);
  2564. error_at_line (&t->u.s.line, "one structure defined here");
  2565. }
  2566. /* Some things may also be defined in the structure's options. */
  2567. for (o = t->u.s.opt; o; o = o->next)
  2568. if (!desc && strcmp (o->name, "desc") == 0
  2569. && o->kind == OPTION_STRING)
  2570. desc = o->info.string;
  2571. else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
  2572. && o->kind == OPTION_STRING)
  2573. struct_mark_hook = o->info.string;
  2574. if (struct_mark_hook)
  2575. oprintf (d->of, "%*s%s (&%s);\n",
  2576. d->indent, "", struct_mark_hook, oldval);
  2577. d->prev_val[2] = oldval;
  2578. d->prev_val[1] = oldprevval2;
  2579. if (union_p)
  2580. {
  2581. if (desc == NULL)
  2582. {
  2583. error_at_line (d->line,
  2584. "missing `desc' option for union `%s'",
  2585. t->u.s.tag);
  2586. desc = "1";
  2587. }
  2588. oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
  2589. output_escaped_param (d, desc, "desc");
  2590. oprintf (d->of, "))\n");
  2591. d->indent += 2;
  2592. oprintf (d->of, "%*s{\n", d->indent, "");
  2593. }
  2594. else if (desc)
  2595. {
  2596. /* We have a "desc" option on a struct, signifying the
  2597. base class within a GC-managed inheritance hierarchy.
  2598. The current code specialcases the base class, then walks
  2599. into subclasses, recursing into this routine to handle them.
  2600. This organization requires the base class to have a case in
  2601. the switch statement, and hence a tag value is mandatory
  2602. for the base class. This restriction could be removed, but
  2603. it would require some restructing of this code. */
  2604. if (!type_tag)
  2605. {
  2606. error_at_line (d->line,
  2607. "missing `tag' option for type `%s'",
  2608. t->u.s.tag);
  2609. }
  2610. oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
  2611. output_escaped_param (d, desc, "desc");
  2612. oprintf (d->of, "))\n");
  2613. d->indent += 2;
  2614. oprintf (d->of, "%*s{\n", d->indent, "");
  2615. oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
  2616. d->indent += 2;
  2617. }
  2618. FOR_ALL_INHERITED_FIELDS (t, f)
  2619. {
  2620. options_p oo;
  2621. int skip_p = 0;
  2622. const char *fieldlength = NULL;
  2623. d->reorder_fn = NULL;
  2624. for (oo = f->opt; oo; oo = oo->next)
  2625. if (strcmp (oo->name, "skip") == 0)
  2626. skip_p = 1;
  2627. else if (strcmp (oo->name, "length") == 0
  2628. && oo->kind == OPTION_STRING)
  2629. fieldlength = oo->info.string;
  2630. if (skip_p)
  2631. continue;
  2632. if (fieldlength)
  2633. {
  2634. lengths_seen++;
  2635. d->counter++;
  2636. if (!union_p)
  2637. {
  2638. if (!any_length_seen)
  2639. {
  2640. oprintf (d->of, "%*s{\n", d->indent, "");
  2641. d->indent += 2;
  2642. }
  2643. any_length_seen = true;
  2644. oprintf (d->of, "%*ssize_t l%d = (size_t)(",
  2645. d->indent, "", d->counter - 1);
  2646. output_escaped_param (d, fieldlength, "length");
  2647. oprintf (d->of, ");\n");
  2648. }
  2649. }
  2650. }
  2651. endcounter = d->counter;
  2652. FOR_ALL_INHERITED_FIELDS (t, f)
  2653. {
  2654. options_p oo;
  2655. const char *dot = ".";
  2656. const char *tagid = NULL;
  2657. int skip_p = 0;
  2658. int default_p = 0;
  2659. const char *fieldlength = NULL;
  2660. char *newval;
  2661. d->reorder_fn = NULL;
  2662. for (oo = f->opt; oo; oo = oo->next)
  2663. if (strcmp (oo->name, "dot") == 0
  2664. && oo->kind == OPTION_STRING)
  2665. dot = oo->info.string;
  2666. else if (strcmp (oo->name, "tag") == 0
  2667. && oo->kind == OPTION_STRING)
  2668. tagid = oo->info.string;
  2669. else if (strcmp (oo->name, "skip") == 0)
  2670. skip_p = 1;
  2671. else if (strcmp (oo->name, "default") == 0)
  2672. default_p = 1;
  2673. else if (strcmp (oo->name, "reorder") == 0
  2674. && oo->kind == OPTION_STRING)
  2675. d->reorder_fn = oo->info.string;
  2676. else if (strcmp (oo->name, "length") == 0
  2677. && oo->kind == OPTION_STRING)
  2678. fieldlength = oo->info.string;
  2679. if (skip_p)
  2680. continue;
  2681. if (union_p && tagid)
  2682. {
  2683. oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
  2684. d->indent += 2;
  2685. }
  2686. else if (union_p && default_p)
  2687. {
  2688. oprintf (d->of, "%*sdefault:\n", d->indent, "");
  2689. d->indent += 2;
  2690. seen_default_p = 1;
  2691. }
  2692. else if (!union_p && (default_p || tagid))
  2693. error_at_line (d->line,
  2694. "can't use `%s' outside a union on field `%s'",
  2695. default_p ? "default" : "tag", f->name);
  2696. else if (union_p && !(default_p || tagid)
  2697. && f->type->kind == TYPE_SCALAR)
  2698. {
  2699. fprintf (stderr,
  2700. "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
  2701. get_input_file_name (d->line->file), d->line->line,
  2702. f->name);
  2703. continue;
  2704. }
  2705. else if (union_p && !(default_p || tagid))
  2706. error_at_line (d->line,
  2707. "field `%s' is missing `tag' or `default' option",
  2708. f->name);
  2709. if (fieldlength)
  2710. {
  2711. d->loopcounter = endcounter - lengths_seen--;
  2712. }
  2713. d->line = &f->line;
  2714. d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
  2715. d->opt = f->opt;
  2716. d->used_length = false;
  2717. d->in_record_p = !union_p;
  2718. walk_type (f->type, d);
  2719. d->in_record_p = false;
  2720. free (newval);
  2721. if (union_p)
  2722. {
  2723. oprintf (d->of, "%*sbreak;\n", d->indent, "");
  2724. d->indent -= 2;
  2725. }
  2726. }
  2727. d->reorder_fn = NULL;
  2728. d->val = oldval;
  2729. d->prev_val[1] = oldprevval1;
  2730. d->prev_val[2] = oldprevval2;
  2731. if (union_p && !seen_default_p)
  2732. {
  2733. oprintf (d->of, "%*sdefault:\n", d->indent, "");
  2734. oprintf (d->of, "%*s break;\n", d->indent, "");
  2735. }
  2736. if (desc && !union_p)
  2737. {
  2738. oprintf (d->of, "%*sbreak;\n", d->indent, "");
  2739. d->indent -= 2;
  2740. }
  2741. if (union_p)
  2742. {
  2743. oprintf (d->of, "%*s}\n", d->indent, "");
  2744. d->indent -= 2;
  2745. }
  2746. else if (desc)
  2747. {
  2748. /* Add cases to handle subclasses. */
  2749. struct seen_tag *tags = NULL;
  2750. walk_subclasses (t, d, &tags);
  2751. /* Ensure that if someone forgets a "tag" option that we don't
  2752. silent fail to traverse that subclass's fields. */
  2753. if (!seen_default_p)
  2754. {
  2755. oprintf (d->of, "%*s/* Unrecognized tag value. */\n",
  2756. d->indent, "");
  2757. oprintf (d->of, "%*sdefault: gcc_unreachable (); \n",
  2758. d->indent, "");
  2759. }
  2760. /* End of the switch statement */
  2761. oprintf (d->of, "%*s}\n", d->indent, "");
  2762. d->indent -= 2;
  2763. }
  2764. if (any_length_seen)
  2765. {
  2766. d->indent -= 2;
  2767. oprintf (d->of, "%*s}\n", d->indent, "");
  2768. }
  2769. }
  2770. break;
  2771. case TYPE_LANG_STRUCT:
  2772. {
  2773. type_p nt;
  2774. for (nt = t->u.s.lang_struct; nt; nt = nt->next)
  2775. if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
  2776. break;
  2777. if (nt == NULL)
  2778. error_at_line (d->line, "structure `%s' differs between languages",
  2779. t->u.s.tag);
  2780. else
  2781. walk_type (nt, d);
  2782. }
  2783. break;
  2784. case TYPE_USER_STRUCT:
  2785. d->process_field (t, d);
  2786. break;
  2787. case TYPE_NONE:
  2788. case TYPE_UNDEFINED:
  2789. gcc_unreachable ();
  2790. }
  2791. }
  2792. /* process_field routine for marking routines. */
  2793. static void
  2794. write_types_process_field (type_p f, const struct walk_type_data *d)
  2795. {
  2796. const struct write_types_data *wtd;
  2797. const char *cast = d->needs_cast_p ? "(void *)" : "";
  2798. wtd = (const struct write_types_data *) d->cookie;
  2799. switch (f->kind)
  2800. {
  2801. case TYPE_NONE:
  2802. case TYPE_UNDEFINED:
  2803. gcc_unreachable ();
  2804. case TYPE_POINTER:
  2805. oprintf (d->of, "%*s%s (%s%s", d->indent, "",
  2806. wtd->subfield_marker_routine, cast, d->val);
  2807. if (wtd->param_prefix)
  2808. {
  2809. if (f->u.p->kind == TYPE_SCALAR)
  2810. /* The current type is a pointer to a scalar (so not
  2811. considered like a pointer to instances of user defined
  2812. types) and we are seeing it; it means we must be even
  2813. more careful about the second argument of the
  2814. SUBFIELD_MARKER_ROUTINE call. That argument must
  2815. always be the instance of the type for which
  2816. write_func_for_structure was called - this really is
  2817. what the function SUBFIELD_MARKER_ROUTINE expects.
  2818. That is, it must be an instance of the ORIG_S type
  2819. parameter of write_func_for_structure. The convention
  2820. is that that argument must be "x" in that case (as set
  2821. by write_func_for_structure). The problem is, we can't
  2822. count on d->prev_val[3] to be always set to "x" in that
  2823. case. Sometimes walk_type can set it to something else
  2824. (to e.g cooperate with write_array when called from
  2825. write_roots). So let's set it to "x" here then. */
  2826. oprintf (d->of, ", x");
  2827. else
  2828. oprintf (d->of, ", %s", d->prev_val[3]);
  2829. if (d->orig_s)
  2830. {
  2831. oprintf (d->of, ", gt_%s_", wtd->param_prefix);
  2832. output_mangled_typename (d->of, d->orig_s);
  2833. }
  2834. else
  2835. oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
  2836. }
  2837. oprintf (d->of, ");\n");
  2838. if (d->reorder_fn && wtd->reorder_note_routine)
  2839. oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
  2840. wtd->reorder_note_routine, cast, d->val,
  2841. d->prev_val[3], d->reorder_fn);
  2842. break;
  2843. case TYPE_STRING:
  2844. case TYPE_STRUCT:
  2845. case TYPE_UNION:
  2846. case TYPE_LANG_STRUCT:
  2847. case TYPE_USER_STRUCT:
  2848. if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
  2849. {
  2850. /* If F is a user-defined type and the field is not a
  2851. pointer to the type, then we should not generate the
  2852. standard pointer-marking code. All we need to do is call
  2853. the user-provided marking function to process the fields
  2854. of F. */
  2855. oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
  2856. d->val);
  2857. }
  2858. else
  2859. {
  2860. oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
  2861. output_mangled_typename (d->of, f);
  2862. oprintf (d->of, " (%s%s);\n", cast, d->val);
  2863. if (d->reorder_fn && wtd->reorder_note_routine)
  2864. oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
  2865. wtd->reorder_note_routine, cast, d->val, cast, d->val,
  2866. d->reorder_fn);
  2867. }
  2868. break;
  2869. case TYPE_SCALAR:
  2870. break;
  2871. case TYPE_ARRAY:
  2872. gcc_unreachable ();
  2873. }
  2874. }
  2875. /* Return an output file that is suitable for definitions which can
  2876. reference struct S */
  2877. static outf_p
  2878. get_output_file_for_structure (const_type_p s)
  2879. {
  2880. const input_file *fn;
  2881. gcc_assert (union_or_struct_p (s));
  2882. fn = s->u.s.line.file;
  2883. /* The call to get_output_file_with_visibility may update fn by
  2884. caching its result inside, so we need the CONST_CAST. */
  2885. return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
  2886. }
  2887. /* Returns the specifier keyword for a string or union type S, empty string
  2888. otherwise. */
  2889. static const char *
  2890. get_type_specifier (const type_p s)
  2891. {
  2892. if (s->kind == TYPE_STRUCT)
  2893. return "struct ";
  2894. else if (s->kind == TYPE_LANG_STRUCT)
  2895. return get_type_specifier (s->u.s.lang_struct);
  2896. else if (s->kind == TYPE_UNION)
  2897. return "union ";
  2898. return "";
  2899. }
  2900. /* Emits a declaration for type TY (assumed to be a union or a
  2901. structure) on stream OUT. */
  2902. static void
  2903. write_type_decl (outf_p out, type_p ty)
  2904. {
  2905. if (union_or_struct_p (ty))
  2906. oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
  2907. else if (ty->kind == TYPE_SCALAR)
  2908. {
  2909. if (ty->u.scalar_is_char)
  2910. oprintf (out, "const char");
  2911. else
  2912. oprintf (out, "void");
  2913. }
  2914. else if (ty->kind == TYPE_POINTER)
  2915. {
  2916. write_type_decl (out, ty->u.p);
  2917. oprintf (out, " *");
  2918. }
  2919. else if (ty->kind == TYPE_ARRAY)
  2920. {
  2921. write_type_decl (out, ty->u.a.p);
  2922. oprintf (out, " *");
  2923. }
  2924. else if (ty->kind == TYPE_STRING)
  2925. {
  2926. oprintf (out, "const char *");
  2927. }
  2928. else
  2929. gcc_unreachable ();
  2930. }
  2931. /* Write on OF the name of the marker function for structure S. PREFIX
  2932. is the prefix to use (to distinguish ggc from pch markers). */
  2933. static void
  2934. write_marker_function_name (outf_p of, type_p s, const char *prefix)
  2935. {
  2936. if (union_or_struct_p (s))
  2937. {
  2938. const char *id_for_tag = filter_type_name (s->u.s.tag);
  2939. oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
  2940. if (id_for_tag != s->u.s.tag)
  2941. free (CONST_CAST (char *, id_for_tag));
  2942. }
  2943. else
  2944. gcc_unreachable ();
  2945. }
  2946. /* Write on OF a user-callable routine to act as an entry point for
  2947. the marking routine for S, generated by write_func_for_structure.
  2948. WTD distinguishes between ggc and pch markers. */
  2949. static void
  2950. write_user_func_for_structure_ptr (outf_p of, type_p s, const write_types_data *wtd)
  2951. {
  2952. gcc_assert (union_or_struct_p (s));
  2953. type_p alias_of = NULL;
  2954. for (options_p opt = s->u.s.opt; opt; opt = opt->next)
  2955. if (strcmp (opt->name, "ptr_alias") == 0)
  2956. {
  2957. /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
  2958. we do not generate marking code for ORIG_S here. Instead, a
  2959. forwarder #define in gtype-desc.h will cause every call to its
  2960. marker to call the target of this alias.
  2961. However, we still want to create a user entry code for the
  2962. aliased type. So, if ALIAS_OF is set, we only generate the
  2963. user-callable marker function. */
  2964. alias_of = opt->info.type;
  2965. break;
  2966. }
  2967. DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s->u.s.tag,
  2968. wtd->prefix);
  2969. /* Only write the function once. */
  2970. if (s->u.s.wrote_user_func_for_ptr[wtd->kind])
  2971. return;
  2972. s->u.s.wrote_user_func_for_ptr[wtd->kind] = true;
  2973. oprintf (of, "\nvoid\n");
  2974. oprintf (of, "gt_%sx (", wtd->prefix);
  2975. write_type_decl (of, s);
  2976. oprintf (of, " *& x)\n");
  2977. oprintf (of, "{\n");
  2978. oprintf (of, " if (x)\n ");
  2979. write_marker_function_name (of,
  2980. alias_of ? alias_of : get_ultimate_base_class (s),
  2981. wtd->prefix);
  2982. oprintf (of, " ((void *) x);\n");
  2983. oprintf (of, "}\n");
  2984. }
  2985. /* Write a function to mark all the fields of type S on OF. PREFIX
  2986. and D are as in write_user_marking_functions. */
  2987. static void
  2988. write_user_func_for_structure_body (type_p s, const char *prefix,
  2989. struct walk_type_data *d)
  2990. {
  2991. oprintf (d->of, "\nvoid\n");
  2992. oprintf (d->of, "gt_%sx (", prefix);
  2993. write_type_decl (d->of, s);
  2994. oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
  2995. oprintf (d->of, "{\n");
  2996. oprintf (d->of, " ");
  2997. write_type_decl (d->of, s);
  2998. oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
  2999. d->val = "(*x)";
  3000. d->indent = 2;
  3001. walk_type (s, d);
  3002. oprintf (d->of, "}\n");
  3003. }
  3004. /* Emit the user-callable functions needed to mark all the types used
  3005. by the user structure S. PREFIX is the prefix to use to
  3006. distinguish ggc and pch markers. D contains data needed to pass to
  3007. walk_type when traversing the fields of a type.
  3008. For every type T referenced by S, two routines are generated: one
  3009. that takes 'T *', marks the pointer and calls the second routine,
  3010. which just marks the fields of T. */
  3011. static void
  3012. write_user_marking_functions (type_p s,
  3013. const write_types_data *w,
  3014. struct walk_type_data *d)
  3015. {
  3016. gcc_assert (s->kind == TYPE_USER_STRUCT);
  3017. for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
  3018. {
  3019. type_p fld_type = fld->type;
  3020. if (fld_type->kind == TYPE_POINTER)
  3021. {
  3022. type_p pointed_to_type = fld_type->u.p;
  3023. if (union_or_struct_p (pointed_to_type))
  3024. write_user_func_for_structure_ptr (d->of, pointed_to_type, w);
  3025. }
  3026. else if (union_or_struct_p (fld_type))
  3027. write_user_func_for_structure_body (fld_type, w->prefix, d);
  3028. }
  3029. }
  3030. /* For S, a structure that's part of ORIG_S write out a routine that:
  3031. - Takes a parameter, a void * but actually of type *S
  3032. - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
  3033. field of S or its substructures and (in some cases) things
  3034. that are pointed to by S. */
  3035. static void
  3036. write_func_for_structure (type_p orig_s, type_p s,
  3037. const struct write_types_data *wtd)
  3038. {
  3039. const char *chain_next = NULL;
  3040. const char *chain_prev = NULL;
  3041. const char *chain_circular = NULL;
  3042. const char *mark_hook_name = NULL;
  3043. options_p opt;
  3044. struct walk_type_data d;
  3045. if (s->u.s.base_class)
  3046. {
  3047. /* Verify that the base class has a "desc", since otherwise
  3048. the traversal hooks there won't attempt to visit fields of
  3049. subclasses such as this one. */
  3050. const_type_p ubc = get_ultimate_base_class (s);
  3051. if ((!opts_have (ubc->u.s.opt, "user")
  3052. && !opts_have (ubc->u.s.opt, "desc")))
  3053. error_at_line (&s->u.s.line,
  3054. ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
  3055. ", but '%s' lacks a discriminator 'desc' option"),
  3056. s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
  3057. /* Don't write fns for subclasses, only for the ultimate base class
  3058. within an inheritance hierarchy. */
  3059. return;
  3060. }
  3061. memset (&d, 0, sizeof (d));
  3062. d.of = get_output_file_for_structure (s);
  3063. bool for_user = false;
  3064. for (opt = s->u.s.opt; opt; opt = opt->next)
  3065. if (strcmp (opt->name, "chain_next") == 0
  3066. && opt->kind == OPTION_STRING)
  3067. chain_next = opt->info.string;
  3068. else if (strcmp (opt->name, "chain_prev") == 0
  3069. && opt->kind == OPTION_STRING)
  3070. chain_prev = opt->info.string;
  3071. else if (strcmp (opt->name, "chain_circular") == 0
  3072. && opt->kind == OPTION_STRING)
  3073. chain_circular = opt->info.string;
  3074. else if (strcmp (opt->name, "mark_hook") == 0
  3075. && opt->kind == OPTION_STRING)
  3076. mark_hook_name = opt->info.string;
  3077. else if (strcmp (opt->name, "for_user") == 0)
  3078. for_user = true;
  3079. if (chain_prev != NULL && chain_next == NULL)
  3080. error_at_line (&s->u.s.line, "chain_prev without chain_next");
  3081. if (chain_circular != NULL && chain_next != NULL)
  3082. error_at_line (&s->u.s.line, "chain_circular with chain_next");
  3083. if (chain_circular != NULL)
  3084. chain_next = chain_circular;
  3085. d.process_field = write_types_process_field;
  3086. d.cookie = wtd;
  3087. d.orig_s = orig_s;
  3088. d.opt = s->u.s.opt;
  3089. d.line = &s->u.s.line;
  3090. d.bitmap = s->u.s.bitmap;
  3091. d.prev_val[0] = "*x";
  3092. d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
  3093. d.prev_val[3] = "x";
  3094. d.val = "(*x)";
  3095. d.have_this_obj = false;
  3096. oprintf (d.of, "\n");
  3097. oprintf (d.of, "void\n");
  3098. write_marker_function_name (d.of, orig_s, wtd->prefix);
  3099. oprintf (d.of, " (void *x_p)\n");
  3100. oprintf (d.of, "{\n ");
  3101. write_type_decl (d.of, s);
  3102. oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
  3103. write_type_decl (d.of, s);
  3104. oprintf (d.of, " *)x_p;\n");
  3105. if (chain_next != NULL)
  3106. {
  3107. /* TYPE_USER_STRUCTs should not occur here. These structures
  3108. are completely handled by user code. */
  3109. gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
  3110. oprintf (d.of, " ");
  3111. write_type_decl (d.of, s);
  3112. oprintf (d.of, " * xlimit = x;\n");
  3113. }
  3114. if (chain_next == NULL)
  3115. {
  3116. oprintf (d.of, " if (%s (x", wtd->marker_routine);
  3117. if (wtd->param_prefix)
  3118. {
  3119. oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
  3120. output_mangled_typename (d.of, orig_s);
  3121. }
  3122. oprintf (d.of, "))\n");
  3123. }
  3124. else
  3125. {
  3126. if (chain_circular != NULL)
  3127. oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
  3128. else
  3129. oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
  3130. if (wtd->param_prefix)
  3131. {
  3132. oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
  3133. output_mangled_typename (d.of, orig_s);
  3134. }
  3135. oprintf (d.of, "))\n");
  3136. if (chain_circular != NULL)
  3137. oprintf (d.of, " return;\n do\n");
  3138. if (mark_hook_name && !wtd->skip_hooks)
  3139. {
  3140. oprintf (d.of, " {\n");
  3141. oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
  3142. }
  3143. oprintf (d.of, " xlimit = (");
  3144. d.prev_val[2] = "*xlimit";
  3145. output_escaped_param (&d, chain_next, "chain_next");
  3146. oprintf (d.of, ");\n");
  3147. if (mark_hook_name && !wtd->skip_hooks)
  3148. oprintf (d.of, " }\n");
  3149. if (chain_prev != NULL)
  3150. {
  3151. oprintf (d.of, " if (x != xlimit)\n");
  3152. oprintf (d.of, " for (;;)\n");
  3153. oprintf (d.of, " {\n");
  3154. oprintf (d.of, " %s %s * const xprev = (",
  3155. s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
  3156. d.prev_val[2] = "*x";
  3157. output_escaped_param (&d, chain_prev, "chain_prev");
  3158. oprintf (d.of, ");\n");
  3159. oprintf (d.of, " if (xprev == NULL) break;\n");
  3160. oprintf (d.of, " x = xprev;\n");
  3161. oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
  3162. if (wtd->param_prefix)
  3163. {
  3164. oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
  3165. output_mangled_typename (d.of, orig_s);
  3166. }
  3167. oprintf (d.of, ");\n");
  3168. oprintf (d.of, " }\n");
  3169. }
  3170. if (chain_circular != NULL)
  3171. {
  3172. oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
  3173. if (wtd->param_prefix)
  3174. {
  3175. oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
  3176. output_mangled_typename (d.of, orig_s);
  3177. }
  3178. oprintf (d.of, "));\n");
  3179. if (mark_hook_name && !wtd->skip_hooks)
  3180. oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
  3181. oprintf (d.of, " do\n");
  3182. }
  3183. else
  3184. oprintf (d.of, " while (x != xlimit)\n");
  3185. }
  3186. oprintf (d.of, " {\n");
  3187. if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
  3188. {
  3189. oprintf (d.of, " %s (x);\n", mark_hook_name);
  3190. }
  3191. d.prev_val[2] = "*x";
  3192. d.indent = 6;
  3193. if (orig_s->kind != TYPE_USER_STRUCT)
  3194. walk_type (s, &d);
  3195. else
  3196. {
  3197. /* User structures have no fields to walk. Simply generate a call
  3198. to the user-provided structure marker. */
  3199. oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
  3200. }
  3201. if (chain_next != NULL)
  3202. {
  3203. oprintf (d.of, " x = (");
  3204. output_escaped_param (&d, chain_next, "chain_next");
  3205. oprintf (d.of, ");\n");
  3206. }
  3207. oprintf (d.of, " }\n");
  3208. if (chain_circular != NULL)
  3209. oprintf (d.of, " while (x != xlimit);\n");
  3210. oprintf (d.of, "}\n");
  3211. if (orig_s->kind == TYPE_USER_STRUCT)
  3212. write_user_marking_functions (orig_s, wtd, &d);
  3213. if (for_user)
  3214. {
  3215. write_user_func_for_structure_body (orig_s, wtd->prefix, &d);
  3216. write_user_func_for_structure_ptr (d.of, orig_s, wtd);
  3217. }
  3218. }
  3219. /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
  3220. static void
  3221. write_types (outf_p output_header, type_p structures,
  3222. const struct write_types_data *wtd)
  3223. {
  3224. int nbfun = 0; /* Count the emitted functions. */
  3225. type_p s;
  3226. oprintf (output_header, "\n/* %s*/\n", wtd->comment);
  3227. /* We first emit the macros and the declarations. Functions' code is
  3228. emitted afterwards. This is needed in plugin mode. */
  3229. oprintf (output_header, "/* Macros and declarations. */\n");
  3230. for (s = structures; s; s = s->next)
  3231. /* Do not emit handlers for derived classes; we only ever deal with
  3232. the ultimate base class within an inheritance hierarchy. */
  3233. if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
  3234. && !s->u.s.base_class)
  3235. {
  3236. options_p opt;
  3237. if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
  3238. continue;
  3239. const char *s_id_for_tag = filter_type_name (s->u.s.tag);
  3240. oprintf (output_header, "#define gt_%s_", wtd->prefix);
  3241. output_mangled_typename (output_header, s);
  3242. oprintf (output_header, "(X) do { \\\n");
  3243. oprintf (output_header,
  3244. " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
  3245. s_id_for_tag);
  3246. oprintf (output_header, " } while (0)\n");
  3247. for (opt = s->u.s.opt; opt; opt = opt->next)
  3248. if (strcmp (opt->name, "ptr_alias") == 0
  3249. && opt->kind == OPTION_TYPE)
  3250. {
  3251. const_type_p const t = (const_type_p) opt->info.type;
  3252. if (t->kind == TYPE_STRUCT
  3253. || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
  3254. {
  3255. const char *t_id_for_tag = filter_type_name (t->u.s.tag);
  3256. oprintf (output_header,
  3257. "#define gt_%sx_%s gt_%sx_%s\n",
  3258. wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
  3259. if (t_id_for_tag != t->u.s.tag)
  3260. free (CONST_CAST (char *, t_id_for_tag));
  3261. }
  3262. else
  3263. error_at_line (&s->u.s.line,
  3264. "structure alias is not a structure");
  3265. break;
  3266. }
  3267. if (opt)
  3268. continue;
  3269. /* Declare the marker procedure only once. */
  3270. oprintf (output_header,
  3271. "extern void gt_%sx_%s (void *);\n",
  3272. wtd->prefix, s_id_for_tag);
  3273. if (s_id_for_tag != s->u.s.tag)
  3274. free (CONST_CAST (char *, s_id_for_tag));
  3275. if (s->u.s.line.file == NULL)
  3276. {
  3277. fprintf (stderr, "warning: structure `%s' used but not defined\n",
  3278. s->u.s.tag);
  3279. continue;
  3280. }
  3281. }
  3282. /* At last we emit the functions code. */
  3283. oprintf (output_header, "\n/* functions code */\n");
  3284. for (s = structures; s; s = s->next)
  3285. if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
  3286. {
  3287. options_p opt;
  3288. if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
  3289. continue;
  3290. for (opt = s->u.s.opt; opt; opt = opt->next)
  3291. if (strcmp (opt->name, "ptr_alias") == 0)
  3292. break;
  3293. if (opt)
  3294. continue;
  3295. if (s->kind == TYPE_LANG_STRUCT)
  3296. {
  3297. type_p ss;
  3298. for (ss = s->u.s.lang_struct; ss; ss = ss->next)
  3299. {
  3300. nbfun++;
  3301. DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
  3302. nbfun, (void*) ss, ss->u.s.tag);
  3303. write_func_for_structure (s, ss, wtd);
  3304. }
  3305. }
  3306. else
  3307. {
  3308. nbfun++;
  3309. DBGPRINTF ("writing func #%d struct s @ %p '%s'",
  3310. nbfun, (void*) s, s->u.s.tag);
  3311. write_func_for_structure (s, s, wtd);
  3312. }
  3313. }
  3314. else
  3315. {
  3316. /* Structure s is not possibly pointed to, so can be ignored. */
  3317. DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
  3318. (void*)s, s->u.s.tag,
  3319. (int) s->gc_used);
  3320. }
  3321. if (verbosity_level >= 2)
  3322. printf ("%s emitted %d routines for %s\n",
  3323. progname, nbfun, wtd->comment);
  3324. }
  3325. static const struct write_types_data ggc_wtd = {
  3326. "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
  3327. "GC marker procedures. ",
  3328. FALSE, WTK_GGC
  3329. };
  3330. static const struct write_types_data pch_wtd = {
  3331. "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
  3332. "gt_pch_note_reorder",
  3333. "PCH type-walking procedures. ",
  3334. TRUE, WTK_PCH
  3335. };
  3336. /* Write out the local pointer-walking routines. */
  3337. /* process_field routine for local pointer-walking for user-callable
  3338. routines. The difference between this and
  3339. write_types_local_process_field is that, in this case, we do not
  3340. need to check whether the given pointer matches the address of the
  3341. parent structure. This check was already generated by the call
  3342. to gt_pch_nx in the main gt_pch_p_*() function that is calling
  3343. this code. */
  3344. static void
  3345. write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
  3346. {
  3347. switch (f->kind)
  3348. {
  3349. case TYPE_POINTER:
  3350. case TYPE_STRUCT:
  3351. case TYPE_UNION:
  3352. case TYPE_LANG_STRUCT:
  3353. case TYPE_STRING:
  3354. oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
  3355. break;
  3356. case TYPE_USER_STRUCT:
  3357. if (d->in_ptr_field)
  3358. oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
  3359. else
  3360. oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
  3361. d->indent, "", d->val);
  3362. break;
  3363. case TYPE_SCALAR:
  3364. break;
  3365. case TYPE_ARRAY:
  3366. case TYPE_NONE:
  3367. case TYPE_UNDEFINED:
  3368. gcc_unreachable ();
  3369. }
  3370. }
  3371. /* Write a function to PCH walk all the fields of type S on OF.
  3372. D contains data needed by walk_type to recurse into the fields of S. */
  3373. static void
  3374. write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
  3375. {
  3376. oprintf (d->of, "\nvoid\n");
  3377. oprintf (d->of, "gt_pch_nx (");
  3378. write_type_decl (d->of, s);
  3379. oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
  3380. "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
  3381. "\tATTRIBUTE_UNUSED void *cookie)\n");
  3382. oprintf (d->of, "{\n");
  3383. d->val = "(*x)";
  3384. d->indent = 2;
  3385. d->process_field = write_types_local_user_process_field;
  3386. walk_type (s, d);
  3387. oprintf (d->of, "}\n");
  3388. }
  3389. /* Emit the user-callable functions needed to mark all the types used
  3390. by the user structure S. PREFIX is the prefix to use to
  3391. distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
  3392. chain_next option defined. D contains data needed to pass to
  3393. walk_type when traversing the fields of a type.
  3394. For every type T referenced by S, two routines are generated: one
  3395. that takes 'T *', marks the pointer and calls the second routine,
  3396. which just marks the fields of T. */
  3397. static void
  3398. write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
  3399. {
  3400. gcc_assert (s->kind == TYPE_USER_STRUCT);
  3401. for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
  3402. {
  3403. type_p fld_type = fld->type;
  3404. if (union_or_struct_p (fld_type))
  3405. write_pch_user_walking_for_structure_body (fld_type, d);
  3406. }
  3407. }
  3408. /* process_field routine for local pointer-walking. */
  3409. static void
  3410. write_types_local_process_field (type_p f, const struct walk_type_data *d)
  3411. {
  3412. gcc_assert (d->have_this_obj);
  3413. switch (f->kind)
  3414. {
  3415. case TYPE_POINTER:
  3416. case TYPE_STRUCT:
  3417. case TYPE_UNION:
  3418. case TYPE_LANG_STRUCT:
  3419. case TYPE_STRING:
  3420. oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
  3421. d->prev_val[3]);
  3422. oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
  3423. break;
  3424. case TYPE_USER_STRUCT:
  3425. oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
  3426. d->prev_val[3]);
  3427. if (d->in_ptr_field)
  3428. oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
  3429. else
  3430. oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
  3431. d->indent, "", d->val);
  3432. break;
  3433. case TYPE_SCALAR:
  3434. break;
  3435. case TYPE_ARRAY:
  3436. case TYPE_NONE:
  3437. case TYPE_UNDEFINED:
  3438. gcc_unreachable ();
  3439. }
  3440. }
  3441. /* For S, a structure that's part of ORIG_S, and using parameters
  3442. PARAM, write out a routine that:
  3443. - Is of type gt_note_pointers
  3444. - Calls PROCESS_FIELD on each field of S or its substructures.
  3445. */
  3446. static void
  3447. write_local_func_for_structure (const_type_p orig_s, type_p s)
  3448. {
  3449. struct walk_type_data d;
  3450. /* Don't write fns for subclasses, only for the ultimate base class
  3451. within an inheritance hierarchy. */
  3452. if (s->u.s.base_class)
  3453. return;
  3454. memset (&d, 0, sizeof (d));
  3455. d.of = get_output_file_for_structure (s);
  3456. d.process_field = write_types_local_process_field;
  3457. d.opt = s->u.s.opt;
  3458. d.line = &s->u.s.line;
  3459. d.bitmap = s->u.s.bitmap;
  3460. d.prev_val[0] = d.prev_val[2] = "*x";
  3461. d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
  3462. d.prev_val[3] = "x";
  3463. d.val = "(*x)";
  3464. d.fn_wants_lvalue = true;
  3465. oprintf (d.of, "\n");
  3466. oprintf (d.of, "void\n");
  3467. oprintf (d.of, "gt_pch_p_");
  3468. output_mangled_typename (d.of, orig_s);
  3469. oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
  3470. "\tvoid *x_p,\n"
  3471. "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
  3472. "\tATTRIBUTE_UNUSED void *cookie)\n");
  3473. oprintf (d.of, "{\n");
  3474. oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
  3475. s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
  3476. s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
  3477. d.indent = 2;
  3478. d.have_this_obj = true;
  3479. if (s->kind != TYPE_USER_STRUCT)
  3480. walk_type (s, &d);
  3481. else
  3482. {
  3483. /* User structures have no fields to walk. Simply generate a
  3484. call to the user-provided PCH walker. */
  3485. oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
  3486. d.prev_val[3]);
  3487. oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
  3488. d.indent, "", d.val);
  3489. }
  3490. oprintf (d.of, "}\n");
  3491. /* Write user-callable entry points for the PCH walking routines. */
  3492. if (orig_s->kind == TYPE_USER_STRUCT)
  3493. write_pch_user_walking_functions (s, &d);
  3494. for (options_p o = s->u.s.opt; o; o = o->next)
  3495. if (strcmp (o->name, "for_user") == 0)
  3496. {
  3497. write_pch_user_walking_for_structure_body (s, &d);
  3498. break;
  3499. }
  3500. }
  3501. /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
  3502. static void
  3503. write_local (outf_p output_header, type_p structures)
  3504. {
  3505. type_p s;
  3506. if (!output_header)
  3507. return;
  3508. oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
  3509. for (s = structures; s; s = s->next)
  3510. if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
  3511. {
  3512. options_p opt;
  3513. if (s->u.s.line.file == NULL)
  3514. continue;
  3515. for (opt = s->u.s.opt; opt; opt = opt->next)
  3516. if (strcmp (opt->name, "ptr_alias") == 0
  3517. && opt->kind == OPTION_TYPE)
  3518. {
  3519. const_type_p const t = (const_type_p) opt->info.type;
  3520. if (t->kind == TYPE_STRUCT
  3521. || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
  3522. {
  3523. oprintf (output_header, "#define gt_pch_p_");
  3524. output_mangled_typename (output_header, s);
  3525. oprintf (output_header, " gt_pch_p_");
  3526. output_mangled_typename (output_header, t);
  3527. oprintf (output_header, "\n");
  3528. }
  3529. else
  3530. error_at_line (&s->u.s.line,
  3531. "structure alias is not a structure");
  3532. break;
  3533. }
  3534. if (opt)
  3535. continue;
  3536. /* Declare the marker procedure only once. */
  3537. oprintf (output_header, "extern void gt_pch_p_");
  3538. output_mangled_typename (output_header, s);
  3539. oprintf (output_header,
  3540. "\n (void *, void *, gt_pointer_operator, void *);\n");
  3541. if (s->kind == TYPE_LANG_STRUCT)
  3542. {
  3543. type_p ss;
  3544. for (ss = s->u.s.lang_struct; ss; ss = ss->next)
  3545. write_local_func_for_structure (s, ss);
  3546. }
  3547. else
  3548. write_local_func_for_structure (s, s);
  3549. }
  3550. }
  3551. /* Nonzero if S is a type for which typed GC allocators should be output. */
  3552. #define USED_BY_TYPED_GC_P(s) \
  3553. ((s->kind == TYPE_POINTER \
  3554. && (s->u.p->gc_used == GC_POINTED_TO \
  3555. || s->u.p->gc_used == GC_USED)) \
  3556. || (union_or_struct_p (s) \
  3557. && ((s)->gc_used == GC_POINTED_TO \
  3558. || ((s)->gc_used == GC_MAYBE_POINTED_TO \
  3559. && s->u.s.line.file != NULL) \
  3560. || ((s)->gc_used == GC_USED \
  3561. && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
  3562. || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
  3563. /* Might T contain any non-pointer elements? */
  3564. static int
  3565. contains_scalar_p (type_p t)
  3566. {
  3567. switch (t->kind)
  3568. {
  3569. case TYPE_STRING:
  3570. case TYPE_POINTER:
  3571. return 0;
  3572. case TYPE_ARRAY:
  3573. return contains_scalar_p (t->u.a.p);
  3574. case TYPE_USER_STRUCT:
  3575. /* User-marked structures will typically contain pointers. */
  3576. return 0;
  3577. default:
  3578. /* Could also check for structures that have no non-pointer
  3579. fields, but there aren't enough of those to worry about. */
  3580. return 1;
  3581. }
  3582. }
  3583. /* Mangle INPF and print it to F. */
  3584. static void
  3585. put_mangled_filename (outf_p f, const input_file *inpf)
  3586. {
  3587. /* The call to get_output_file_name may indirectly update fn since
  3588. get_output_file_with_visibility caches its result inside, so we
  3589. need the CONST_CAST. */
  3590. const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
  3591. if (!f || !name)
  3592. return;
  3593. for (; *name != 0; name++)
  3594. if (ISALNUM (*name))
  3595. oprintf (f, "%c", *name);
  3596. else
  3597. oprintf (f, "%c", '_');
  3598. }
  3599. /* Finish off the currently-created root tables in FLP. PFX, TNAME,
  3600. LASTNAME, and NAME are all strings to insert in various places in
  3601. the resulting code. */
  3602. static void
  3603. finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
  3604. const char *tname, const char *name)
  3605. {
  3606. struct flist *fli2;
  3607. for (fli2 = flp; fli2; fli2 = fli2->next)
  3608. if (fli2->started_p)
  3609. {
  3610. oprintf (fli2->f, " %s\n", lastname);
  3611. oprintf (fli2->f, "};\n\n");
  3612. }
  3613. for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
  3614. if (fli2->started_p)
  3615. {
  3616. lang_bitmap bitmap = get_lang_bitmap (fli2->file);
  3617. int fnum;
  3618. for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
  3619. if (bitmap & 1)
  3620. {
  3621. oprintf (base_files[fnum],
  3622. "extern const struct %s gt_%s_", tname, pfx);
  3623. put_mangled_filename (base_files[fnum], fli2->file);
  3624. oprintf (base_files[fnum], "[];\n");
  3625. }
  3626. }
  3627. {
  3628. size_t fnum;
  3629. for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
  3630. oprintf (base_files[fnum],
  3631. "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
  3632. }
  3633. for (fli2 = flp; fli2; fli2 = fli2->next)
  3634. if (fli2->started_p)
  3635. {
  3636. lang_bitmap bitmap = get_lang_bitmap (fli2->file);
  3637. int fnum;
  3638. fli2->started_p = 0;
  3639. for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
  3640. if (bitmap & 1)
  3641. {
  3642. oprintf (base_files[fnum], " gt_%s_", pfx);
  3643. put_mangled_filename (base_files[fnum], fli2->file);
  3644. oprintf (base_files[fnum], ",\n");
  3645. }
  3646. }
  3647. {
  3648. size_t fnum;
  3649. for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
  3650. {
  3651. oprintf (base_files[fnum], " NULL\n");
  3652. oprintf (base_files[fnum], "};\n");
  3653. }
  3654. }
  3655. }
  3656. /* Finish off the created gt_clear_caches_file_c functions. */
  3657. static void
  3658. finish_cache_funcs (flist *flp)
  3659. {
  3660. struct flist *fli2;
  3661. for (fli2 = flp; fli2; fli2 = fli2->next)
  3662. if (fli2->started_p)
  3663. {
  3664. oprintf (fli2->f, "}\n\n");
  3665. }
  3666. for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
  3667. if (fli2->started_p)
  3668. {
  3669. lang_bitmap bitmap = get_lang_bitmap (fli2->file);
  3670. int fnum;
  3671. for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
  3672. if (bitmap & 1)
  3673. {
  3674. oprintf (base_files[fnum], "extern void gt_clear_caches_");
  3675. put_mangled_filename (base_files[fnum], fli2->file);
  3676. oprintf (base_files[fnum], " ();\n");
  3677. }
  3678. }
  3679. for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
  3680. oprintf (base_files[fnum], "void\ngt_clear_caches ()\n{\n");
  3681. for (fli2 = flp; fli2; fli2 = fli2->next)
  3682. if (fli2->started_p)
  3683. {
  3684. lang_bitmap bitmap = get_lang_bitmap (fli2->file);
  3685. int fnum;
  3686. fli2->started_p = 0;
  3687. for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
  3688. if (bitmap & 1)
  3689. {
  3690. oprintf (base_files[fnum], " gt_clear_caches_");
  3691. put_mangled_filename (base_files[fnum], fli2->file);
  3692. oprintf (base_files[fnum], " ();\n");
  3693. }
  3694. }
  3695. for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
  3696. {
  3697. oprintf (base_files[fnum], "}\n");
  3698. }
  3699. }
  3700. /* Write the first three fields (pointer, count and stride) for
  3701. root NAME to F. V and LINE are as for write_root.
  3702. Return true if the entry could be written; return false on error. */
  3703. static bool
  3704. start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
  3705. {
  3706. type_p ap;
  3707. if (!v)
  3708. {
  3709. error_at_line (line, "`%s' is too complex to be a root", name);
  3710. return false;
  3711. }
  3712. oprintf (f, " {\n");
  3713. oprintf (f, " &%s,\n", name);
  3714. oprintf (f, " 1");
  3715. for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
  3716. if (ap->u.a.len[0])
  3717. oprintf (f, " * (%s)", ap->u.a.len);
  3718. else if (ap == v->type)
  3719. oprintf (f, " * ARRAY_SIZE (%s)", v->name);
  3720. oprintf (f, ",\n");
  3721. oprintf (f, " sizeof (%s", v->name);
  3722. for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
  3723. oprintf (f, "[0]");
  3724. oprintf (f, "),\n");
  3725. return true;
  3726. }
  3727. /* A subroutine of write_root for writing the roots for field FIELD_NAME,
  3728. which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
  3729. of the caller. */
  3730. static void
  3731. write_field_root (outf_p f, pair_p v, type_p type, const char *name,
  3732. int has_length, struct fileloc *line,
  3733. bool emit_pch, type_p field_type, const char *field_name)
  3734. {
  3735. struct pair newv;
  3736. /* If the field reference is relative to V, rather than to some
  3737. subcomponent of V, we can mark any subarrays with a single stride.
  3738. We're effectively treating the field as a global variable in its
  3739. own right. */
  3740. if (v && type == v->type)
  3741. {
  3742. newv = *v;
  3743. newv.type = field_type;
  3744. newv.name = ACONCAT ((v->name, ".", field_name, NULL));
  3745. v = &newv;
  3746. }
  3747. /* Otherwise, any arrays nested in the structure are too complex to
  3748. handle. */
  3749. else if (field_type->kind == TYPE_ARRAY)
  3750. v = NULL;
  3751. write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
  3752. has_length, line, emit_pch);
  3753. }
  3754. /* Write out to F the table entry and any marker routines needed to
  3755. mark NAME as TYPE. V can be one of three values:
  3756. - null, if NAME is too complex to represent using a single
  3757. count and stride. In this case, it is an error for NAME to
  3758. contain any gc-ed data.
  3759. - the outermost array that contains NAME, if NAME is part of an array.
  3760. - the C variable that contains NAME, if NAME is not part of an array.
  3761. LINE is the line of the C source that declares the root variable.
  3762. HAS_LENGTH is nonzero iff V was a variable-length array. */
  3763. static void
  3764. write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
  3765. struct fileloc *line, bool emit_pch)
  3766. {
  3767. switch (type->kind)
  3768. {
  3769. case TYPE_STRUCT:
  3770. {
  3771. pair_p fld;
  3772. for (fld = type->u.s.fields; fld; fld = fld->next)
  3773. {
  3774. int skip_p = 0;
  3775. const char *desc = NULL;
  3776. options_p o;
  3777. for (o = fld->opt; o; o = o->next)
  3778. if (strcmp (o->name, "skip") == 0)
  3779. skip_p = 1;
  3780. else if (strcmp (o->name, "desc") == 0
  3781. && o->kind == OPTION_STRING)
  3782. desc = o->info.string;
  3783. else
  3784. error_at_line (line,
  3785. "field `%s' of global `%s' has unknown option `%s'",
  3786. fld->name, name, o->name);
  3787. if (skip_p)
  3788. continue;
  3789. else if (desc && fld->type->kind == TYPE_UNION)
  3790. {
  3791. pair_p validf = NULL;
  3792. pair_p ufld;
  3793. for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
  3794. {
  3795. const char *tag = NULL;
  3796. options_p oo;
  3797. for (oo = ufld->opt; oo; oo = oo->next)
  3798. if (strcmp (oo->name, "tag") == 0
  3799. && oo->kind == OPTION_STRING)
  3800. tag = oo->info.string;
  3801. if (tag == NULL || strcmp (tag, desc) != 0)
  3802. continue;
  3803. if (validf != NULL)
  3804. error_at_line (line,
  3805. "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
  3806. name, fld->name, validf->name,
  3807. name, fld->name, ufld->name, tag);
  3808. validf = ufld;
  3809. }
  3810. if (validf != NULL)
  3811. write_field_root (f, v, type, name, 0, line, emit_pch,
  3812. validf->type,
  3813. ACONCAT ((fld->name, ".",
  3814. validf->name, NULL)));
  3815. }
  3816. else if (desc)
  3817. error_at_line (line,
  3818. "global `%s.%s' has `desc' option but is not union",
  3819. name, fld->name);
  3820. else
  3821. write_field_root (f, v, type, name, 0, line, emit_pch, fld->type,
  3822. fld->name);
  3823. }
  3824. }
  3825. break;
  3826. case TYPE_ARRAY:
  3827. {
  3828. char *newname;
  3829. newname = xasprintf ("%s[0]", name);
  3830. write_root (f, v, type->u.a.p, newname, has_length, line, emit_pch);
  3831. free (newname);
  3832. }
  3833. break;
  3834. case TYPE_USER_STRUCT:
  3835. error_at_line (line, "`%s' must be a pointer type, because it is "
  3836. "a GC root and its type is marked with GTY((user))",
  3837. v->name);
  3838. break;
  3839. case TYPE_POINTER:
  3840. {
  3841. const_type_p tp;
  3842. if (!start_root_entry (f, v, name, line))
  3843. return;
  3844. tp = type->u.p;
  3845. if (!has_length && union_or_struct_p (tp))
  3846. {
  3847. tp = get_ultimate_base_class (tp);
  3848. const char *id_for_tag = filter_type_name (tp->u.s.tag);
  3849. oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
  3850. if (emit_pch)
  3851. oprintf (f, " &gt_pch_nx_%s", id_for_tag);
  3852. else
  3853. oprintf (f, " NULL");
  3854. if (id_for_tag != tp->u.s.tag)
  3855. free (CONST_CAST (char *, id_for_tag));
  3856. }
  3857. else if (has_length
  3858. && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
  3859. {
  3860. oprintf (f, " &gt_ggc_ma_%s,\n", name);
  3861. if (emit_pch)
  3862. oprintf (f, " &gt_pch_na_%s", name);
  3863. else
  3864. oprintf (f, " NULL");
  3865. }
  3866. else
  3867. {
  3868. error_at_line (line,
  3869. "global `%s' is pointer to unimplemented type",
  3870. name);
  3871. }
  3872. oprintf (f, "\n },\n");
  3873. }
  3874. break;
  3875. case TYPE_STRING:
  3876. {
  3877. if (!start_root_entry (f, v, name, line))
  3878. return;
  3879. oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
  3880. oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
  3881. oprintf (f, " },\n");
  3882. }
  3883. break;
  3884. case TYPE_SCALAR:
  3885. break;
  3886. case TYPE_NONE:
  3887. case TYPE_UNDEFINED:
  3888. case TYPE_UNION:
  3889. case TYPE_LANG_STRUCT:
  3890. error_at_line (line, "global `%s' is unimplemented type", name);
  3891. }
  3892. }
  3893. /* This generates a routine to walk an array. */
  3894. static void
  3895. write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
  3896. {
  3897. struct walk_type_data d;
  3898. char *prevval3;
  3899. memset (&d, 0, sizeof (d));
  3900. d.of = f;
  3901. d.cookie = wtd;
  3902. d.indent = 2;
  3903. d.line = &v->line;
  3904. d.opt = v->opt;
  3905. d.bitmap = get_lang_bitmap (v->line.file);
  3906. d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
  3907. if (wtd->param_prefix)
  3908. {
  3909. oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
  3910. oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
  3911. oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
  3912. wtd->param_prefix, v->name);
  3913. oprintf (d.of,
  3914. " ATTRIBUTE_UNUSED void *x_p,\n"
  3915. " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
  3916. " ATTRIBUTE_UNUSED void * cookie)\n");
  3917. oprintf (d.of, "{\n");
  3918. d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
  3919. d.process_field = write_types_local_process_field;
  3920. d.have_this_obj = true;
  3921. walk_type (v->type, &d);
  3922. oprintf (f, "}\n\n");
  3923. }
  3924. d.opt = v->opt;
  3925. oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
  3926. oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
  3927. wtd->prefix, v->name);
  3928. oprintf (f, "{\n");
  3929. d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
  3930. d.process_field = write_types_process_field;
  3931. d.have_this_obj = false;
  3932. walk_type (v->type, &d);
  3933. free (prevval3);
  3934. oprintf (f, "}\n\n");
  3935. }
  3936. /* Output a table describing the locations and types of VARIABLES. */
  3937. static void
  3938. write_roots (pair_p variables, bool emit_pch)
  3939. {
  3940. pair_p v;
  3941. struct flist *flp = NULL;
  3942. for (v = variables; v; v = v->next)
  3943. {
  3944. outf_p f =
  3945. get_output_file_with_visibility (CONST_CAST (input_file*,
  3946. v->line.file));
  3947. struct flist *fli;
  3948. const char *length = NULL;
  3949. int deletable_p = 0;
  3950. options_p o;
  3951. for (o = v->opt; o; o = o->next)
  3952. if (strcmp (o->name, "length") == 0
  3953. && o->kind == OPTION_STRING)
  3954. length = o->info.string;
  3955. else if (strcmp (o->name, "deletable") == 0)
  3956. deletable_p = 1;
  3957. else if (strcmp (o->name, "cache") == 0)
  3958. ;
  3959. else
  3960. error_at_line (&v->line,
  3961. "global `%s' has unknown option `%s'",
  3962. v->name, o->name);
  3963. for (fli = flp; fli; fli = fli->next)
  3964. if (fli->f == f && f)
  3965. break;
  3966. if (fli == NULL)
  3967. {
  3968. fli = XNEW (struct flist);
  3969. fli->f = f;
  3970. fli->next = flp;
  3971. fli->started_p = 0;
  3972. fli->file = v->line.file;
  3973. gcc_assert (fli->file);
  3974. flp = fli;
  3975. oprintf (f, "\n/* GC roots. */\n\n");
  3976. }
  3977. if (!deletable_p
  3978. && length
  3979. && v->type->kind == TYPE_POINTER
  3980. && (v->type->u.p->kind == TYPE_POINTER
  3981. || v->type->u.p->kind == TYPE_STRUCT))
  3982. {
  3983. write_array (f, v, &ggc_wtd);
  3984. write_array (f, v, &pch_wtd);
  3985. }
  3986. }
  3987. for (v = variables; v; v = v->next)
  3988. {
  3989. outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
  3990. v->line.file));
  3991. struct flist *fli;
  3992. int skip_p = 0;
  3993. int length_p = 0;
  3994. options_p o;
  3995. for (o = v->opt; o; o = o->next)
  3996. if (strcmp (o->name, "length") == 0)
  3997. length_p = 1;
  3998. else if (strcmp (o->name, "deletable") == 0)
  3999. skip_p = 1;
  4000. if (skip_p)
  4001. continue;
  4002. for (fli = flp; fli; fli = fli->next)
  4003. if (fli->f == f)
  4004. break;
  4005. if (!fli->started_p)
  4006. {
  4007. fli->started_p = 1;
  4008. oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
  4009. put_mangled_filename (f, v->line.file);
  4010. oprintf (f, "[] = {\n");
  4011. }
  4012. write_root (f, v, v->type, v->name, length_p, &v->line, emit_pch);
  4013. }
  4014. finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
  4015. "gt_ggc_rtab");
  4016. for (v = variables; v; v = v->next)
  4017. {
  4018. outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
  4019. v->line.file));
  4020. struct flist *fli;
  4021. int skip_p = 1;
  4022. options_p o;
  4023. for (o = v->opt; o; o = o->next)
  4024. if (strcmp (o->name, "deletable") == 0)
  4025. skip_p = 0;
  4026. if (skip_p)
  4027. continue;
  4028. for (fli = flp; fli; fli = fli->next)
  4029. if (fli->f == f)
  4030. break;
  4031. if (!fli->started_p)
  4032. {
  4033. fli->started_p = 1;
  4034. oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
  4035. put_mangled_filename (f, v->line.file);
  4036. oprintf (f, "[] = {\n");
  4037. }
  4038. oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
  4039. v->name, v->name);
  4040. }
  4041. finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
  4042. "gt_ggc_deletable_rtab");
  4043. for (v = variables; v; v = v->next)
  4044. {
  4045. outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
  4046. v->line.file));
  4047. struct flist *fli;
  4048. bool cache = false;
  4049. options_p o;
  4050. for (o = v->opt; o; o = o->next)
  4051. if (strcmp (o->name, "cache") == 0)
  4052. cache = true;
  4053. if (!cache)
  4054. continue;
  4055. for (fli = flp; fli; fli = fli->next)
  4056. if (fli->f == f)
  4057. break;
  4058. if (!fli->started_p)
  4059. {
  4060. fli->started_p = 1;
  4061. oprintf (f, "void\ngt_clear_caches_");
  4062. put_mangled_filename (f, v->line.file);
  4063. oprintf (f, " ()\n{\n");
  4064. }
  4065. oprintf (f, " gt_cleare_cache (%s);\n", v->name);
  4066. }
  4067. finish_cache_funcs (flp);
  4068. if (!emit_pch)
  4069. return;
  4070. for (v = variables; v; v = v->next)
  4071. {
  4072. outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
  4073. v->line.file));
  4074. struct flist *fli;
  4075. int skip_p = 0;
  4076. options_p o;
  4077. for (o = v->opt; o; o = o->next)
  4078. if (strcmp (o->name, "deletable") == 0)
  4079. {
  4080. skip_p = 1;
  4081. break;
  4082. }
  4083. if (skip_p)
  4084. continue;
  4085. if (!contains_scalar_p (v->type))
  4086. continue;
  4087. for (fli = flp; fli; fli = fli->next)
  4088. if (fli->f == f)
  4089. break;
  4090. if (!fli->started_p)
  4091. {
  4092. fli->started_p = 1;
  4093. oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
  4094. put_mangled_filename (f, v->line.file);
  4095. oprintf (f, "[] = {\n");
  4096. }
  4097. oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
  4098. v->name, v->name);
  4099. }
  4100. finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
  4101. "gt_pch_scalar_rtab");
  4102. }
  4103. /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
  4104. guaranteee for somewhat increased readability. If name conflicts do happen,
  4105. this funcion will have to be adjusted to be more like
  4106. output_mangled_typename. */
  4107. static void
  4108. output_typename (outf_p of, const_type_p t)
  4109. {
  4110. switch (t->kind)
  4111. {
  4112. case TYPE_STRING:
  4113. oprintf (of, "str");
  4114. break;
  4115. case TYPE_SCALAR:
  4116. oprintf (of, "scalar");
  4117. break;
  4118. case TYPE_POINTER:
  4119. output_typename (of, t->u.p);
  4120. break;
  4121. case TYPE_STRUCT:
  4122. case TYPE_USER_STRUCT:
  4123. case TYPE_UNION:
  4124. case TYPE_LANG_STRUCT:
  4125. oprintf (of, "%s", t->u.s.tag);
  4126. break;
  4127. case TYPE_NONE:
  4128. case TYPE_UNDEFINED:
  4129. case TYPE_ARRAY:
  4130. gcc_unreachable ();
  4131. }
  4132. }
  4133. #define INDENT 2
  4134. /* Dumps the value of typekind KIND. */
  4135. static void
  4136. dump_typekind (int indent, enum typekind kind)
  4137. {
  4138. printf ("%*ckind = ", indent, ' ');
  4139. switch (kind)
  4140. {
  4141. case TYPE_SCALAR:
  4142. printf ("TYPE_SCALAR");
  4143. break;
  4144. case TYPE_STRING:
  4145. printf ("TYPE_STRING");
  4146. break;
  4147. case TYPE_STRUCT:
  4148. printf ("TYPE_STRUCT");
  4149. break;
  4150. case TYPE_UNDEFINED:
  4151. printf ("TYPE_UNDEFINED");
  4152. break;
  4153. case TYPE_USER_STRUCT:
  4154. printf ("TYPE_USER_STRUCT");
  4155. break;
  4156. case TYPE_UNION:
  4157. printf ("TYPE_UNION");
  4158. break;
  4159. case TYPE_POINTER:
  4160. printf ("TYPE_POINTER");
  4161. break;
  4162. case TYPE_ARRAY:
  4163. printf ("TYPE_ARRAY");
  4164. break;
  4165. case TYPE_LANG_STRUCT:
  4166. printf ("TYPE_LANG_STRUCT");
  4167. break;
  4168. default:
  4169. gcc_unreachable ();
  4170. }
  4171. printf ("\n");
  4172. }
  4173. /* Dumps the value of GC_USED flag. */
  4174. static void
  4175. dump_gc_used (int indent, enum gc_used_enum gc_used)
  4176. {
  4177. printf ("%*cgc_used = ", indent, ' ');
  4178. switch (gc_used)
  4179. {
  4180. case GC_UNUSED:
  4181. printf ("GC_UNUSED");
  4182. break;
  4183. case GC_USED:
  4184. printf ("GC_USED");
  4185. break;
  4186. case GC_MAYBE_POINTED_TO:
  4187. printf ("GC_MAYBE_POINTED_TO");
  4188. break;
  4189. case GC_POINTED_TO:
  4190. printf ("GC_POINTED_TO");
  4191. break;
  4192. default:
  4193. gcc_unreachable ();
  4194. }
  4195. printf ("\n");
  4196. }
  4197. /* Dumps the type options OPT. */
  4198. static void
  4199. dump_options (int indent, options_p opt)
  4200. {
  4201. options_p o;
  4202. printf ("%*coptions = ", indent, ' ');
  4203. o = opt;
  4204. while (o)
  4205. {
  4206. switch (o->kind)
  4207. {
  4208. case OPTION_STRING:
  4209. printf ("%s:string %s ", o->name, o->info.string);
  4210. break;
  4211. case OPTION_TYPE:
  4212. printf ("%s:type ", o->name);
  4213. dump_type (indent+1, o->info.type);
  4214. break;
  4215. case OPTION_NESTED:
  4216. printf ("%s:nested ", o->name);
  4217. break;
  4218. case OPTION_NONE:
  4219. gcc_unreachable ();
  4220. }
  4221. o = o->next;
  4222. }
  4223. printf ("\n");
  4224. }
  4225. /* Dumps the source file location in LINE. */
  4226. static void
  4227. dump_fileloc (int indent, struct fileloc line)
  4228. {
  4229. printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
  4230. get_input_file_name (line.file),
  4231. line.line);
  4232. }
  4233. /* Recursively dumps the struct, union, or a language-specific
  4234. struct T. */
  4235. static void
  4236. dump_type_u_s (int indent, type_p t)
  4237. {
  4238. pair_p fields;
  4239. gcc_assert (union_or_struct_p (t));
  4240. printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
  4241. dump_fileloc (indent, t->u.s.line);
  4242. printf ("%*cu.s.fields =\n", indent, ' ');
  4243. fields = t->u.s.fields;
  4244. while (fields)
  4245. {
  4246. dump_pair (indent + INDENT, fields);
  4247. fields = fields->next;
  4248. }
  4249. printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
  4250. dump_options (indent, t->u.s.opt);
  4251. printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
  4252. if (t->kind == TYPE_LANG_STRUCT)
  4253. {
  4254. printf ("%*cu.s.lang_struct:\n", indent, ' ');
  4255. dump_type_list (indent + INDENT, t->u.s.lang_struct);
  4256. }
  4257. }
  4258. /* Recursively dumps the array T. */
  4259. static void
  4260. dump_type_u_a (int indent, type_p t)
  4261. {
  4262. gcc_assert (t->kind == TYPE_ARRAY);
  4263. printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
  4264. dump_type_list (indent + INDENT, t->u.a.p);
  4265. }
  4266. /* Recursively dumps the type list T. */
  4267. static void
  4268. dump_type_list (int indent, type_p t)
  4269. {
  4270. type_p p = t;
  4271. while (p)
  4272. {
  4273. dump_type (indent, p);
  4274. p = p->next;
  4275. }
  4276. }
  4277. static htab_t seen_types;
  4278. /* Recursively dumps the type T if it was not dumped previously. */
  4279. static void
  4280. dump_type (int indent, type_p t)
  4281. {
  4282. PTR *slot;
  4283. if (seen_types == NULL)
  4284. seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
  4285. printf ("%*cType at %p: ", indent, ' ', (void *) t);
  4286. slot = htab_find_slot (seen_types, t, INSERT);
  4287. if (*slot != NULL)
  4288. {
  4289. printf ("already seen.\n");
  4290. return;
  4291. }
  4292. *slot = t;
  4293. printf ("\n");
  4294. dump_typekind (indent, t->kind);
  4295. printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
  4296. (void *) t->pointer_to);
  4297. dump_gc_used (indent + INDENT, t->gc_used);
  4298. switch (t->kind)
  4299. {
  4300. case TYPE_SCALAR:
  4301. printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
  4302. t->u.scalar_is_char ? "true" : "false");
  4303. break;
  4304. case TYPE_STRING:
  4305. break;
  4306. case TYPE_STRUCT:
  4307. case TYPE_UNION:
  4308. case TYPE_LANG_STRUCT:
  4309. case TYPE_USER_STRUCT:
  4310. dump_type_u_s (indent + INDENT, t);
  4311. break;
  4312. case TYPE_POINTER:
  4313. printf ("%*cp:\n", indent + INDENT, ' ');
  4314. dump_type (indent + INDENT, t->u.p);
  4315. break;
  4316. case TYPE_ARRAY:
  4317. dump_type_u_a (indent + INDENT, t);
  4318. break;
  4319. default:
  4320. gcc_unreachable ();
  4321. }
  4322. printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
  4323. }
  4324. /* Dumps the pair P. */
  4325. static void
  4326. dump_pair (int indent, pair_p p)
  4327. {
  4328. printf ("%*cpair: name = %s\n", indent, ' ', p->name);
  4329. dump_type (indent, p->type);
  4330. dump_fileloc (indent, p->line);
  4331. dump_options (indent, p->opt);
  4332. printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
  4333. }
  4334. /* Dumps the list of pairs PP. */
  4335. static void
  4336. dump_pair_list (const char *name, pair_p pp)
  4337. {
  4338. pair_p p;
  4339. printf ("%s:\n", name);
  4340. for (p = pp; p != NULL; p = p->next)
  4341. dump_pair (0, p);
  4342. printf ("End of %s\n\n", name);
  4343. }
  4344. /* Dumps the STRUCTURES. */
  4345. static void
  4346. dump_structures (const char *name, type_p structures)
  4347. {
  4348. printf ("%s:\n", name);
  4349. dump_type_list (0, structures);
  4350. printf ("End of %s\n\n", name);
  4351. }
  4352. /* Dumps the internal structures of gengtype. This is useful to debug
  4353. gengtype itself, or to understand what it does, e.g. for plugin
  4354. developers. */
  4355. static void
  4356. dump_everything (void)
  4357. {
  4358. dump_pair_list ("typedefs", typedefs);
  4359. dump_structures ("structures", structures);
  4360. dump_pair_list ("variables", variables);
  4361. /* Allocated with the first call to dump_type. */
  4362. htab_delete (seen_types);
  4363. }
  4364. /* Option specification for getopt_long. */
  4365. static const struct option gengtype_long_options[] = {
  4366. {"help", no_argument, NULL, 'h'},
  4367. {"version", no_argument, NULL, 'V'},
  4368. {"verbose", no_argument, NULL, 'v'},
  4369. {"dump", no_argument, NULL, 'd'},
  4370. {"debug", no_argument, NULL, 'D'},
  4371. {"plugin", required_argument, NULL, 'P'},
  4372. {"srcdir", required_argument, NULL, 'S'},
  4373. {"backupdir", required_argument, NULL, 'B'},
  4374. {"inputs", required_argument, NULL, 'I'},
  4375. {"read-state", required_argument, NULL, 'r'},
  4376. {"write-state", required_argument, NULL, 'w'},
  4377. /* Terminating NULL placeholder. */
  4378. {NULL, no_argument, NULL, 0},
  4379. };
  4380. static void
  4381. print_usage (void)
  4382. {
  4383. printf ("Usage: %s\n", progname);
  4384. printf ("\t -h | --help " " \t# Give this help.\n");
  4385. printf ("\t -D | --debug "
  4386. " \t# Give debug output to debug %s itself.\n", progname);
  4387. printf ("\t -V | --version " " \t# Give version information.\n");
  4388. printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
  4389. printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
  4390. printf ("\t -P | --plugin <output-file> <plugin-src> ... "
  4391. " \t# Generate for plugin.\n");
  4392. printf ("\t -S | --srcdir <GCC-directory> "
  4393. " \t# Specify the GCC source directory.\n");
  4394. printf ("\t -B | --backupdir <directory> "
  4395. " \t# Specify the backup directory for updated files.\n");
  4396. printf ("\t -I | --inputs <input-list> "
  4397. " \t# Specify the file with source files list.\n");
  4398. printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
  4399. printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
  4400. }
  4401. static void
  4402. print_version (void)
  4403. {
  4404. printf ("%s %s%s\n", progname, pkgversion_string, version_string);
  4405. printf ("Report bugs: %s\n", bug_report_url);
  4406. }
  4407. /* Parse the program options using getopt_long... */
  4408. static void
  4409. parse_program_options (int argc, char **argv)
  4410. {
  4411. int opt = -1;
  4412. while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
  4413. gengtype_long_options, NULL)) >= 0)
  4414. {
  4415. switch (opt)
  4416. {
  4417. case 'h': /* --help */
  4418. print_usage ();
  4419. break;
  4420. case 'V': /* --version */
  4421. print_version ();
  4422. break;
  4423. case 'd': /* --dump */
  4424. do_dump = 1;
  4425. break;
  4426. case 'D': /* --debug */
  4427. do_debug = 1;
  4428. break;
  4429. case 'v': /* --verbose */
  4430. verbosity_level++;
  4431. break;
  4432. case 'P': /* --plugin */
  4433. if (optarg)
  4434. plugin_output_filename = optarg;
  4435. else
  4436. fatal ("missing plugin output file name");
  4437. break;
  4438. case 'S': /* --srcdir */
  4439. if (optarg)
  4440. srcdir = optarg;
  4441. else
  4442. fatal ("missing source directory");
  4443. srcdir_len = strlen (srcdir);
  4444. break;
  4445. case 'B': /* --backupdir */
  4446. if (optarg)
  4447. backup_dir = optarg;
  4448. else
  4449. fatal ("missing backup directory");
  4450. break;
  4451. case 'I': /* --inputs */
  4452. if (optarg)
  4453. inputlist = optarg;
  4454. else
  4455. fatal ("missing input list");
  4456. break;
  4457. case 'r': /* --read-state */
  4458. if (optarg)
  4459. read_state_filename = optarg;
  4460. else
  4461. fatal ("missing read state file");
  4462. DBGPRINTF ("read state %s\n", optarg);
  4463. break;
  4464. case 'w': /* --write-state */
  4465. DBGPRINTF ("write state %s\n", optarg);
  4466. if (optarg)
  4467. write_state_filename = optarg;
  4468. else
  4469. fatal ("missing write state file");
  4470. break;
  4471. default:
  4472. fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
  4473. print_usage ();
  4474. fatal ("unexpected flag");
  4475. }
  4476. };
  4477. if (plugin_output_filename)
  4478. {
  4479. /* In plugin mode we require some input files. */
  4480. int i = 0;
  4481. if (optind >= argc)
  4482. fatal ("no source files given in plugin mode");
  4483. nb_plugin_files = argc - optind;
  4484. plugin_files = XNEWVEC (input_file*, nb_plugin_files);
  4485. for (i = 0; i < (int) nb_plugin_files; i++)
  4486. {
  4487. char *name = argv[i + optind];
  4488. plugin_files[i] = input_file_by_name (name);
  4489. }
  4490. }
  4491. }
  4492. /******* Manage input files. ******/
  4493. /* Hash table of unique input file names. */
  4494. static htab_t input_file_htab;
  4495. /* Find or allocate a new input_file by hash-consing it. */
  4496. input_file*
  4497. input_file_by_name (const char* name)
  4498. {
  4499. PTR* slot;
  4500. input_file* f = NULL;
  4501. int namlen = 0;
  4502. if (!name)
  4503. return NULL;
  4504. namlen = strlen (name);
  4505. f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
  4506. f->inpbitmap = 0;
  4507. f->inpoutf = NULL;
  4508. f->inpisplugin = false;
  4509. strcpy (f->inpname, name);
  4510. slot = htab_find_slot (input_file_htab, f, INSERT);
  4511. gcc_assert (slot != NULL);
  4512. if (*slot)
  4513. {
  4514. /* Already known input file. */
  4515. free (f);
  4516. return (input_file*)(*slot);
  4517. }
  4518. /* New input file. */
  4519. *slot = f;
  4520. return f;
  4521. }
  4522. /* Hash table support routines for input_file-s. */
  4523. static hashval_t
  4524. htab_hash_inputfile (const void *p)
  4525. {
  4526. const input_file *inpf = (const input_file *) p;
  4527. gcc_assert (inpf);
  4528. return htab_hash_string (get_input_file_name (inpf));
  4529. }
  4530. static int
  4531. htab_eq_inputfile (const void *x, const void *y)
  4532. {
  4533. const input_file *inpfx = (const input_file *) x;
  4534. const input_file *inpfy = (const input_file *) y;
  4535. gcc_assert (inpfx != NULL && inpfy != NULL);
  4536. return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
  4537. }
  4538. int
  4539. main (int argc, char **argv)
  4540. {
  4541. size_t i;
  4542. static struct fileloc pos = { NULL, 0 };
  4543. outf_p output_header;
  4544. /* Mandatory common initializations. */
  4545. progname = "gengtype"; /* For fatal and messages. */
  4546. /* Create the hash-table used to hash-cons input files. */
  4547. input_file_htab =
  4548. htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
  4549. /* Initialize our special input files. */
  4550. this_file = input_file_by_name (__FILE__);
  4551. system_h_file = input_file_by_name ("system.h");
  4552. /* Set the scalar_is_char union number for predefined scalar types. */
  4553. scalar_nonchar.u.scalar_is_char = FALSE;
  4554. scalar_char.u.scalar_is_char = TRUE;
  4555. parse_program_options (argc, argv);
  4556. #if ENABLE_CHECKING
  4557. if (do_debug)
  4558. {
  4559. time_t now = (time_t) 0;
  4560. time (&now);
  4561. DBGPRINTF ("gengtype started pid %d at %s",
  4562. (int) getpid (), ctime (&now));
  4563. }
  4564. #endif /* ENABLE_CHECKING */
  4565. /* Parse the input list and the input files. */
  4566. DBGPRINTF ("inputlist %s", inputlist);
  4567. if (read_state_filename)
  4568. {
  4569. if (inputlist)
  4570. fatal ("input list %s cannot be given with a read state file %s",
  4571. inputlist, read_state_filename);
  4572. read_state (read_state_filename);
  4573. DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
  4574. }
  4575. else if (inputlist)
  4576. {
  4577. /* These types are set up with #define or else outside of where
  4578. we can see them. We should initialize them before calling
  4579. read_input_list. */
  4580. #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
  4581. Call;} while (0)
  4582. POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
  4583. POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
  4584. POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
  4585. POS_HERE (do_scalar_typedef ("double_int", &pos));
  4586. POS_HERE (do_scalar_typedef ("offset_int", &pos));
  4587. POS_HERE (do_scalar_typedef ("widest_int", &pos));
  4588. POS_HERE (do_scalar_typedef ("int64_t", &pos));
  4589. POS_HERE (do_scalar_typedef ("uint64_t", &pos));
  4590. POS_HERE (do_scalar_typedef ("uint8", &pos));
  4591. POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
  4592. POS_HERE (do_scalar_typedef ("jword", &pos));
  4593. POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
  4594. POS_HERE (do_scalar_typedef ("void", &pos));
  4595. POS_HERE (do_scalar_typedef ("machine_mode", &pos));
  4596. POS_HERE (do_typedef ("PTR",
  4597. create_pointer (resolve_typedef ("void", &pos)),
  4598. &pos));
  4599. #undef POS_HERE
  4600. read_input_list (inputlist);
  4601. for (i = 0; i < num_gt_files; i++)
  4602. {
  4603. parse_file (get_input_file_name (gt_files[i]));
  4604. DBGPRINTF ("parsed file #%d %s",
  4605. (int) i, get_input_file_name (gt_files[i]));
  4606. }
  4607. if (verbosity_level >= 1)
  4608. printf ("%s parsed %d files with %d GTY types\n",
  4609. progname, (int) num_gt_files, type_count);
  4610. DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
  4611. }
  4612. else
  4613. fatal ("either an input list or a read state file should be given");
  4614. if (hit_error)
  4615. return 1;
  4616. if (plugin_output_filename)
  4617. {
  4618. size_t ix = 0;
  4619. /* In plugin mode, we should have read a state file, and have
  4620. given at least one plugin file. */
  4621. if (!read_state_filename)
  4622. fatal ("No read state given in plugin mode for %s",
  4623. plugin_output_filename);
  4624. if (nb_plugin_files == 0 || !plugin_files)
  4625. fatal ("No plugin files given in plugin mode for %s",
  4626. plugin_output_filename);
  4627. /* Parse our plugin files and augment the state. */
  4628. for (ix = 0; ix < nb_plugin_files; ix++)
  4629. {
  4630. input_file* pluginput = plugin_files [ix];
  4631. pluginput->inpisplugin = true;
  4632. parse_file (get_input_file_name (pluginput));
  4633. }
  4634. if (hit_error)
  4635. return 1;
  4636. plugin_output = create_file ("GCC", plugin_output_filename);
  4637. DBGPRINTF ("created plugin_output %p named %s",
  4638. (void *) plugin_output, plugin_output->name);
  4639. }
  4640. else
  4641. { /* No plugin files, we are in normal mode. */
  4642. if (!srcdir)
  4643. fatal ("gengtype needs a source directory in normal mode");
  4644. }
  4645. if (hit_error)
  4646. return 1;
  4647. gen_rtx_next ();
  4648. set_gc_used (variables);
  4649. for (type_p t = structures; t; t = t->next)
  4650. {
  4651. bool for_user = false;
  4652. for (options_p o = t->u.s.opt; o; o = o->next)
  4653. if (strcmp (o->name, "for_user") == 0)
  4654. {
  4655. for_user = true;
  4656. break;
  4657. }
  4658. if (for_user)
  4659. set_gc_used_type (t, GC_POINTED_TO);
  4660. }
  4661. /* The state at this point is read from the state input file or by
  4662. parsing source files and optionally augmented by parsing plugin
  4663. source files. Write it now. */
  4664. if (write_state_filename)
  4665. {
  4666. DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
  4667. if (hit_error)
  4668. fatal ("didn't write state file %s after errors",
  4669. write_state_filename);
  4670. DBGPRINTF ("before write_state %s", write_state_filename);
  4671. write_state (write_state_filename);
  4672. if (do_dump)
  4673. dump_everything ();
  4674. /* After having written the state file we return immediately to
  4675. avoid generating any output file. */
  4676. if (hit_error)
  4677. return 1;
  4678. else
  4679. return 0;
  4680. }
  4681. open_base_files ();
  4682. output_header = plugin_output ? plugin_output : header_file;
  4683. DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
  4684. structures);
  4685. write_types (output_header, structures, &ggc_wtd);
  4686. if (plugin_files == NULL)
  4687. {
  4688. DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
  4689. structures);
  4690. write_types (header_file, structures, &pch_wtd);
  4691. write_local (header_file, structures);
  4692. }
  4693. write_roots (variables, plugin_files == NULL);
  4694. write_rtx_next ();
  4695. close_output_files ();
  4696. if (do_dump)
  4697. dump_everything ();
  4698. /* Don't bother about free-ing any input or plugin file, etc. */
  4699. if (hit_error)
  4700. return 1;
  4701. return 0;
  4702. }