isl_input.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491
  1. /*
  2. * Copyright 2008-2009 Katholieke Universiteit Leuven
  3. * Copyright 2010 INRIA Saclay
  4. * Copyright 2012-2013 Ecole Normale Superieure
  5. * Copyright 2019 Cerebras Systems
  6. *
  7. * Use of this software is governed by the MIT license
  8. *
  9. * Written by Sven Verdoolaege, K.U.Leuven, Departement
  10. * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  11. * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
  12. * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
  13. * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
  14. * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
  15. */
  16. #include <ctype.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <isl_ctx_private.h>
  20. #include <isl_map_private.h>
  21. #include <isl_id_private.h>
  22. #include <isl/set.h>
  23. #include <isl_seq.h>
  24. #include <isl_stream_private.h>
  25. #include <isl/obj.h>
  26. #include "isl_polynomial_private.h"
  27. #include <isl/union_set.h>
  28. #include <isl/union_map.h>
  29. #include <isl_mat_private.h>
  30. #include <isl_aff_private.h>
  31. #include <isl_vec_private.h>
  32. #include <isl/list.h>
  33. #include <isl_val_private.h>
  34. struct variable {
  35. char *name;
  36. int pos;
  37. struct variable *next;
  38. };
  39. struct vars {
  40. struct isl_ctx *ctx;
  41. int n;
  42. struct variable *v;
  43. };
  44. static struct vars *vars_new(struct isl_ctx *ctx)
  45. {
  46. struct vars *v;
  47. v = isl_alloc_type(ctx, struct vars);
  48. if (!v)
  49. return NULL;
  50. v->ctx = ctx;
  51. v->n = 0;
  52. v->v = NULL;
  53. return v;
  54. }
  55. static void variable_free(struct variable *var)
  56. {
  57. while (var) {
  58. struct variable *next = var->next;
  59. free(var->name);
  60. free(var);
  61. var = next;
  62. }
  63. }
  64. static void vars_free(struct vars *v)
  65. {
  66. if (!v)
  67. return;
  68. variable_free(v->v);
  69. free(v);
  70. }
  71. static void vars_drop(struct vars *v, int n)
  72. {
  73. struct variable *var;
  74. if (!v || !v->v)
  75. return;
  76. v->n -= n;
  77. var = v->v;
  78. while (--n >= 0) {
  79. struct variable *next = var->next;
  80. free(var->name);
  81. free(var);
  82. var = next;
  83. }
  84. v->v = var;
  85. }
  86. static struct variable *variable_new(struct vars *v, const char *name, int len,
  87. int pos)
  88. {
  89. struct variable *var;
  90. var = isl_calloc_type(v->ctx, struct variable);
  91. if (!var)
  92. goto error;
  93. var->name = strdup(name);
  94. var->name[len] = '\0';
  95. var->pos = pos;
  96. var->next = v->v;
  97. return var;
  98. error:
  99. variable_free(v->v);
  100. return NULL;
  101. }
  102. static int vars_pos(struct vars *v, const char *s, int len)
  103. {
  104. int pos;
  105. struct variable *q;
  106. if (len == -1)
  107. len = strlen(s);
  108. for (q = v->v; q; q = q->next) {
  109. if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
  110. break;
  111. }
  112. if (q)
  113. pos = q->pos;
  114. else {
  115. pos = v->n;
  116. v->v = variable_new(v, s, len, v->n);
  117. if (!v->v)
  118. return -1;
  119. v->n++;
  120. }
  121. return pos;
  122. }
  123. static int vars_add_anon(struct vars *v)
  124. {
  125. v->v = variable_new(v, "", 0, v->n);
  126. if (!v->v)
  127. return -1;
  128. v->n++;
  129. return 0;
  130. }
  131. /* Obtain next token, with some preprocessing.
  132. * In particular, evaluate expressions of the form x^y,
  133. * with x and y values.
  134. */
  135. static struct isl_token *next_token(__isl_keep isl_stream *s)
  136. {
  137. struct isl_token *tok, *tok2;
  138. tok = isl_stream_next_token(s);
  139. if (!tok || tok->type != ISL_TOKEN_VALUE)
  140. return tok;
  141. if (!isl_stream_eat_if_available(s, '^'))
  142. return tok;
  143. tok2 = isl_stream_next_token(s);
  144. if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
  145. isl_stream_error(s, tok2, "expecting constant value");
  146. goto error;
  147. }
  148. isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
  149. isl_token_free(tok2);
  150. return tok;
  151. error:
  152. isl_token_free(tok);
  153. isl_token_free(tok2);
  154. return NULL;
  155. }
  156. /* Read an isl_val from "s".
  157. *
  158. * The following token sequences are recognized
  159. *
  160. * "infty" -> infty
  161. * "-" "infty" -> -infty
  162. * "NaN" -> NaN
  163. * n "/" d -> n/d
  164. * v -> v
  165. *
  166. * where n, d and v are integer constants.
  167. */
  168. __isl_give isl_val *isl_stream_read_val(__isl_keep isl_stream *s)
  169. {
  170. struct isl_token *tok = NULL;
  171. struct isl_token *tok2 = NULL;
  172. isl_val *val;
  173. tok = next_token(s);
  174. if (!tok) {
  175. isl_stream_error(s, NULL, "unexpected EOF");
  176. goto error;
  177. }
  178. if (tok->type == ISL_TOKEN_INFTY) {
  179. isl_token_free(tok);
  180. return isl_val_infty(s->ctx);
  181. }
  182. if (tok->type == '-' &&
  183. isl_stream_eat_if_available(s, ISL_TOKEN_INFTY)) {
  184. isl_token_free(tok);
  185. return isl_val_neginfty(s->ctx);
  186. }
  187. if (tok->type == ISL_TOKEN_NAN) {
  188. isl_token_free(tok);
  189. return isl_val_nan(s->ctx);
  190. }
  191. if (tok->type != ISL_TOKEN_VALUE) {
  192. isl_stream_error(s, tok, "expecting value");
  193. goto error;
  194. }
  195. if (isl_stream_eat_if_available(s, '/')) {
  196. tok2 = next_token(s);
  197. if (!tok2) {
  198. isl_stream_error(s, NULL, "unexpected EOF");
  199. goto error;
  200. }
  201. if (tok2->type != ISL_TOKEN_VALUE) {
  202. isl_stream_error(s, tok2, "expecting value");
  203. goto error;
  204. }
  205. val = isl_val_rat_from_isl_int(s->ctx, tok->u.v, tok2->u.v);
  206. val = isl_val_normalize(val);
  207. } else {
  208. val = isl_val_int_from_isl_int(s->ctx, tok->u.v);
  209. }
  210. isl_token_free(tok);
  211. isl_token_free(tok2);
  212. return val;
  213. error:
  214. isl_token_free(tok);
  215. isl_token_free(tok2);
  216. return NULL;
  217. }
  218. /* Read an isl_val from "str".
  219. */
  220. __isl_give isl_val *isl_val_read_from_str(isl_ctx *ctx, const char *str)
  221. {
  222. isl_val *val;
  223. isl_stream *s = isl_stream_new_str(ctx, str);
  224. if (!s)
  225. return NULL;
  226. val = isl_stream_read_val(s);
  227. isl_stream_free(s);
  228. return val;
  229. }
  230. /* Perform an integer division on *f and
  231. * an integer value read from the stream.
  232. */
  233. static isl_stat int_div_by_cst(__isl_keep isl_stream *s, isl_int *f)
  234. {
  235. struct isl_token *tok;
  236. tok = next_token(s);
  237. if (!tok || tok->type != ISL_TOKEN_VALUE) {
  238. isl_stream_error(s, tok, "expecting constant value");
  239. goto error;
  240. }
  241. isl_int_fdiv_q(*f, *f, tok->u.v);
  242. isl_token_free(tok);
  243. return isl_stat_ok;
  244. error:
  245. isl_token_free(tok);
  246. return isl_stat_error;
  247. }
  248. static isl_stat accept_cst_factor(__isl_keep isl_stream *s, isl_int *f)
  249. {
  250. struct isl_token *tok;
  251. tok = next_token(s);
  252. if (!tok || tok->type != ISL_TOKEN_VALUE) {
  253. isl_stream_error(s, tok, "expecting constant value");
  254. goto error;
  255. }
  256. isl_int_mul(*f, *f, tok->u.v);
  257. isl_token_free(tok);
  258. if (isl_stream_eat_if_available(s, '*'))
  259. return accept_cst_factor(s, f);
  260. return isl_stat_ok;
  261. error:
  262. isl_token_free(tok);
  263. return isl_stat_error;
  264. }
  265. /* Given an affine expression aff, return an affine expression
  266. * for aff % d, with d the next token on the stream, which is
  267. * assumed to be a constant.
  268. *
  269. * We introduce an integer division q = [aff/d] and the result
  270. * is set to aff - d q.
  271. */
  272. static __isl_give isl_pw_aff *affine_mod(__isl_keep isl_stream *s,
  273. struct vars *v, __isl_take isl_pw_aff *aff)
  274. {
  275. struct isl_token *tok;
  276. isl_pw_aff *q;
  277. tok = next_token(s);
  278. if (!tok || tok->type != ISL_TOKEN_VALUE) {
  279. isl_stream_error(s, tok, "expecting constant value");
  280. goto error;
  281. }
  282. q = isl_pw_aff_copy(aff);
  283. q = isl_pw_aff_scale_down(q, tok->u.v);
  284. q = isl_pw_aff_floor(q);
  285. q = isl_pw_aff_scale(q, tok->u.v);
  286. aff = isl_pw_aff_sub(aff, q);
  287. isl_token_free(tok);
  288. return aff;
  289. error:
  290. isl_pw_aff_free(aff);
  291. isl_token_free(tok);
  292. return NULL;
  293. }
  294. static __isl_give isl_pw_aff *accept_affine(__isl_keep isl_stream *s,
  295. __isl_take isl_space *space, struct vars *v);
  296. static __isl_give isl_pw_aff_list *accept_affine_list(__isl_keep isl_stream *s,
  297. __isl_take isl_space *space, struct vars *v);
  298. static __isl_give isl_pw_aff *accept_minmax(__isl_keep isl_stream *s,
  299. __isl_take isl_space *space, struct vars *v)
  300. {
  301. struct isl_token *tok;
  302. isl_pw_aff_list *list = NULL;
  303. int min;
  304. tok = isl_stream_next_token(s);
  305. if (!tok)
  306. goto error;
  307. min = tok->type == ISL_TOKEN_MIN;
  308. isl_token_free(tok);
  309. if (isl_stream_eat(s, '('))
  310. goto error;
  311. list = accept_affine_list(s, isl_space_copy(space), v);
  312. if (!list)
  313. goto error;
  314. if (isl_stream_eat(s, ')'))
  315. goto error;
  316. isl_space_free(space);
  317. return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
  318. error:
  319. isl_space_free(space);
  320. isl_pw_aff_list_free(list);
  321. return NULL;
  322. }
  323. /* Is "tok" the start of an integer division?
  324. */
  325. static int is_start_of_div(struct isl_token *tok)
  326. {
  327. if (!tok)
  328. return 0;
  329. if (tok->type == '[')
  330. return 1;
  331. if (tok->type == ISL_TOKEN_FLOOR)
  332. return 1;
  333. if (tok->type == ISL_TOKEN_CEIL)
  334. return 1;
  335. if (tok->type == ISL_TOKEN_FLOORD)
  336. return 1;
  337. if (tok->type == ISL_TOKEN_CEILD)
  338. return 1;
  339. return 0;
  340. }
  341. /* Read an integer division from "s" and return it as an isl_pw_aff.
  342. *
  343. * The integer division can be of the form
  344. *
  345. * [<affine expression>]
  346. * floor(<affine expression>)
  347. * ceil(<affine expression>)
  348. * floord(<affine expression>,<denominator>)
  349. * ceild(<affine expression>,<denominator>)
  350. */
  351. static __isl_give isl_pw_aff *accept_div(__isl_keep isl_stream *s,
  352. __isl_take isl_space *space, struct vars *v)
  353. {
  354. struct isl_token *tok;
  355. int f = 0;
  356. int c = 0;
  357. int extra = 0;
  358. isl_pw_aff *pwaff = NULL;
  359. if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
  360. extra = f = 1;
  361. else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
  362. extra = c = 1;
  363. else if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOOR))
  364. f = 1;
  365. else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEIL))
  366. c = 1;
  367. if (f || c) {
  368. if (isl_stream_eat(s, '('))
  369. goto error;
  370. } else {
  371. if (isl_stream_eat(s, '['))
  372. goto error;
  373. }
  374. pwaff = accept_affine(s, isl_space_copy(space), v);
  375. if (extra) {
  376. if (isl_stream_eat(s, ','))
  377. goto error;
  378. tok = next_token(s);
  379. if (!tok)
  380. goto error;
  381. if (tok->type != ISL_TOKEN_VALUE) {
  382. isl_stream_error(s, tok, "expected denominator");
  383. isl_stream_push_token(s, tok);
  384. goto error;
  385. }
  386. pwaff = isl_pw_aff_scale_down(pwaff, tok->u.v);
  387. isl_token_free(tok);
  388. }
  389. if (c)
  390. pwaff = isl_pw_aff_ceil(pwaff);
  391. else
  392. pwaff = isl_pw_aff_floor(pwaff);
  393. if (f || c) {
  394. if (isl_stream_eat(s, ')'))
  395. goto error;
  396. } else {
  397. if (isl_stream_eat(s, ']'))
  398. goto error;
  399. }
  400. isl_space_free(space);
  401. return pwaff;
  402. error:
  403. isl_space_free(space);
  404. isl_pw_aff_free(pwaff);
  405. return NULL;
  406. }
  407. /* Divide "pa" by an integer constant read from the stream.
  408. */
  409. static __isl_give isl_pw_aff *pw_aff_div_by_cst(__isl_keep isl_stream *s,
  410. __isl_take isl_pw_aff *pa)
  411. {
  412. isl_int f;
  413. isl_int_init(f);
  414. isl_int_set_si(f, 1);
  415. if (accept_cst_factor(s, &f) < 0)
  416. pa = isl_pw_aff_free(pa);
  417. pa = isl_pw_aff_scale_down(pa, f);
  418. isl_int_clear(f);
  419. return pa;
  420. }
  421. static __isl_give isl_pw_aff *accept_affine_factor(__isl_keep isl_stream *s,
  422. __isl_take isl_space *space, struct vars *v)
  423. {
  424. struct isl_token *tok = NULL;
  425. isl_pw_aff *res = NULL;
  426. tok = next_token(s);
  427. if (!tok) {
  428. isl_stream_error(s, NULL, "unexpected EOF");
  429. goto error;
  430. }
  431. if (tok->type == ISL_TOKEN_AFF) {
  432. res = isl_pw_aff_copy(tok->u.pwaff);
  433. isl_token_free(tok);
  434. } else if (tok->type == ISL_TOKEN_IDENT) {
  435. int n = v->n;
  436. int pos = vars_pos(v, tok->u.s, -1);
  437. isl_aff *aff;
  438. if (pos < 0)
  439. goto error;
  440. if (pos >= n) {
  441. vars_drop(v, v->n - n);
  442. isl_stream_error(s, tok, "unknown identifier");
  443. goto error;
  444. }
  445. aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(space)));
  446. if (!aff)
  447. goto error;
  448. aff->v = isl_vec_set_element_si(aff->v, 2 + pos, 1);
  449. if (!aff->v)
  450. aff = isl_aff_free(aff);
  451. res = isl_pw_aff_from_aff(aff);
  452. isl_token_free(tok);
  453. } else if (tok->type == ISL_TOKEN_VALUE) {
  454. if (isl_stream_eat_if_available(s, '*')) {
  455. res = accept_affine_factor(s, isl_space_copy(space), v);
  456. res = isl_pw_aff_scale(res, tok->u.v);
  457. } else {
  458. isl_local_space *ls;
  459. isl_aff *aff;
  460. ls = isl_local_space_from_space(isl_space_copy(space));
  461. aff = isl_aff_zero_on_domain(ls);
  462. aff = isl_aff_add_constant(aff, tok->u.v);
  463. res = isl_pw_aff_from_aff(aff);
  464. }
  465. isl_token_free(tok);
  466. } else if (tok->type == '(') {
  467. isl_token_free(tok);
  468. tok = NULL;
  469. res = accept_affine(s, isl_space_copy(space), v);
  470. if (!res)
  471. goto error;
  472. if (isl_stream_eat(s, ')'))
  473. goto error;
  474. } else if (is_start_of_div(tok)) {
  475. isl_stream_push_token(s, tok);
  476. tok = NULL;
  477. res = accept_div(s, isl_space_copy(space), v);
  478. } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
  479. isl_stream_push_token(s, tok);
  480. tok = NULL;
  481. res = accept_minmax(s, isl_space_copy(space), v);
  482. } else {
  483. isl_stream_error(s, tok, "expecting factor");
  484. goto error;
  485. }
  486. if (isl_stream_eat_if_available(s, '%') ||
  487. isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
  488. isl_space_free(space);
  489. return affine_mod(s, v, res);
  490. }
  491. if (isl_stream_eat_if_available(s, '*')) {
  492. isl_int f;
  493. isl_int_init(f);
  494. isl_int_set_si(f, 1);
  495. if (accept_cst_factor(s, &f) < 0) {
  496. isl_int_clear(f);
  497. goto error2;
  498. }
  499. res = isl_pw_aff_scale(res, f);
  500. isl_int_clear(f);
  501. }
  502. if (isl_stream_eat_if_available(s, '/'))
  503. res = pw_aff_div_by_cst(s, res);
  504. if (isl_stream_eat_if_available(s, ISL_TOKEN_INT_DIV))
  505. res = isl_pw_aff_floor(pw_aff_div_by_cst(s, res));
  506. isl_space_free(space);
  507. return res;
  508. error:
  509. isl_token_free(tok);
  510. error2:
  511. isl_pw_aff_free(res);
  512. isl_space_free(space);
  513. return NULL;
  514. }
  515. static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
  516. {
  517. isl_aff *aff;
  518. isl_space *space;
  519. space = isl_pw_aff_get_domain_space(pwaff);
  520. aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
  521. aff = isl_aff_add_constant(aff, v);
  522. return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
  523. }
  524. /* Return a piecewise affine expression defined on the specified domain
  525. * that represents NaN.
  526. */
  527. static __isl_give isl_pw_aff *nan_on_domain(__isl_keep isl_space *space)
  528. {
  529. return isl_pw_aff_nan_on_domain_space(isl_space_copy(space));
  530. }
  531. static __isl_give isl_pw_aff *accept_affine(__isl_keep isl_stream *s,
  532. __isl_take isl_space *space, struct vars *v)
  533. {
  534. struct isl_token *tok = NULL;
  535. isl_local_space *ls;
  536. isl_pw_aff *res;
  537. int sign = 1;
  538. ls = isl_local_space_from_space(isl_space_copy(space));
  539. res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
  540. if (!res)
  541. goto error;
  542. for (;;) {
  543. tok = next_token(s);
  544. if (!tok) {
  545. isl_stream_error(s, NULL, "unexpected EOF");
  546. goto error;
  547. }
  548. if (tok->type == '-') {
  549. sign = -sign;
  550. isl_token_free(tok);
  551. continue;
  552. }
  553. if (tok->type == '(' || is_start_of_div(tok) ||
  554. tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
  555. tok->type == ISL_TOKEN_IDENT ||
  556. tok->type == ISL_TOKEN_AFF) {
  557. isl_pw_aff *term;
  558. isl_stream_push_token(s, tok);
  559. tok = NULL;
  560. term = accept_affine_factor(s,
  561. isl_space_copy(space), v);
  562. if (sign < 0)
  563. res = isl_pw_aff_sub(res, term);
  564. else
  565. res = isl_pw_aff_add(res, term);
  566. if (!res)
  567. goto error;
  568. sign = 1;
  569. } else if (tok->type == ISL_TOKEN_VALUE) {
  570. if (sign < 0)
  571. isl_int_neg(tok->u.v, tok->u.v);
  572. if (isl_stream_eat_if_available(s, '*') ||
  573. isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
  574. isl_pw_aff *term;
  575. term = accept_affine_factor(s,
  576. isl_space_copy(space), v);
  577. term = isl_pw_aff_scale(term, tok->u.v);
  578. res = isl_pw_aff_add(res, term);
  579. if (!res)
  580. goto error;
  581. } else {
  582. if (isl_stream_eat_if_available(s,
  583. ISL_TOKEN_INT_DIV) &&
  584. int_div_by_cst(s, &tok->u.v) < 0)
  585. goto error;
  586. res = add_cst(res, tok->u.v);
  587. }
  588. sign = 1;
  589. } else if (tok->type == ISL_TOKEN_NAN) {
  590. res = isl_pw_aff_add(res, nan_on_domain(space));
  591. } else {
  592. isl_stream_error(s, tok, "unexpected isl_token");
  593. isl_stream_push_token(s, tok);
  594. isl_pw_aff_free(res);
  595. isl_space_free(space);
  596. return NULL;
  597. }
  598. isl_token_free(tok);
  599. tok = next_token(s);
  600. if (tok && tok->type == '-') {
  601. sign = -sign;
  602. isl_token_free(tok);
  603. } else if (tok && tok->type == '+') {
  604. /* nothing */
  605. isl_token_free(tok);
  606. } else if (tok && tok->type == ISL_TOKEN_VALUE &&
  607. isl_int_is_neg(tok->u.v)) {
  608. isl_stream_push_token(s, tok);
  609. } else {
  610. if (tok)
  611. isl_stream_push_token(s, tok);
  612. break;
  613. }
  614. }
  615. isl_space_free(space);
  616. return res;
  617. error:
  618. isl_space_free(space);
  619. isl_token_free(tok);
  620. isl_pw_aff_free(res);
  621. return NULL;
  622. }
  623. /* Is "type" the type of a comparison operator between lists
  624. * of affine expressions?
  625. */
  626. static int is_list_comparator_type(int type)
  627. {
  628. switch (type) {
  629. case ISL_TOKEN_LEX_LT:
  630. case ISL_TOKEN_LEX_GT:
  631. case ISL_TOKEN_LEX_LE:
  632. case ISL_TOKEN_LEX_GE:
  633. return 1;
  634. default:
  635. return 0;
  636. }
  637. }
  638. static int is_comparator(struct isl_token *tok)
  639. {
  640. if (!tok)
  641. return 0;
  642. if (is_list_comparator_type(tok->type))
  643. return 1;
  644. switch (tok->type) {
  645. case ISL_TOKEN_LT:
  646. case ISL_TOKEN_GT:
  647. case ISL_TOKEN_LE:
  648. case ISL_TOKEN_GE:
  649. case ISL_TOKEN_NE:
  650. case '=':
  651. return 1;
  652. default:
  653. return 0;
  654. }
  655. }
  656. static __isl_give isl_map *read_formula(__isl_keep isl_stream *s,
  657. struct vars *v, __isl_take isl_map *map, int rational);
  658. static __isl_give isl_pw_aff *accept_extended_affine(__isl_keep isl_stream *s,
  659. __isl_take isl_space *space, struct vars *v, int rational);
  660. /* Accept a ternary operator, given the first argument.
  661. */
  662. static __isl_give isl_pw_aff *accept_ternary(__isl_keep isl_stream *s,
  663. __isl_take isl_map *cond, struct vars *v, int rational)
  664. {
  665. isl_space *space;
  666. isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
  667. if (!cond)
  668. return NULL;
  669. if (isl_stream_eat(s, '?'))
  670. goto error;
  671. space = isl_space_wrap(isl_map_get_space(cond));
  672. pwaff1 = accept_extended_affine(s, space, v, rational);
  673. if (!pwaff1)
  674. goto error;
  675. if (isl_stream_eat(s, ':'))
  676. goto error;
  677. space = isl_pw_aff_get_domain_space(pwaff1);
  678. pwaff2 = accept_extended_affine(s, space, v, rational);
  679. if (!pwaff2)
  680. goto error;
  681. pa_cond = isl_set_indicator_function(isl_map_wrap(cond));
  682. return isl_pw_aff_cond(pa_cond, pwaff1, pwaff2);
  683. error:
  684. isl_map_free(cond);
  685. isl_pw_aff_free(pwaff1);
  686. isl_pw_aff_free(pwaff2);
  687. return NULL;
  688. }
  689. /* Set *line and *col to those of the next token, if any.
  690. */
  691. static void set_current_line_col(__isl_keep isl_stream *s, int *line, int *col)
  692. {
  693. struct isl_token *tok;
  694. tok = isl_stream_next_token(s);
  695. if (!tok)
  696. return;
  697. *line = tok->line;
  698. *col = tok->col;
  699. isl_stream_push_token(s, tok);
  700. }
  701. /* Push a token encapsulating "pa" onto "s", with the given
  702. * line and column.
  703. */
  704. static isl_stat push_aff(__isl_keep isl_stream *s, int line, int col,
  705. __isl_take isl_pw_aff *pa)
  706. {
  707. struct isl_token *tok;
  708. tok = isl_token_new(s->ctx, line, col, 0);
  709. if (!tok)
  710. goto error;
  711. tok->type = ISL_TOKEN_AFF;
  712. tok->u.pwaff = pa;
  713. isl_stream_push_token(s, tok);
  714. return isl_stat_ok;
  715. error:
  716. isl_pw_aff_free(pa);
  717. return isl_stat_error;
  718. }
  719. /* Is the next token a comparison operator?
  720. */
  721. static int next_is_comparator(__isl_keep isl_stream *s)
  722. {
  723. int is_comp;
  724. struct isl_token *tok;
  725. tok = isl_stream_next_token(s);
  726. if (!tok)
  727. return 0;
  728. is_comp = is_comparator(tok);
  729. isl_stream_push_token(s, tok);
  730. return is_comp;
  731. }
  732. /* Accept an affine expression that may involve ternary operators.
  733. * We first read an affine expression.
  734. * If it is not followed by a comparison operator, we simply return it.
  735. * Otherwise, we assume the affine expression is part of the first
  736. * argument of a ternary operator and try to parse that.
  737. */
  738. static __isl_give isl_pw_aff *accept_extended_affine(__isl_keep isl_stream *s,
  739. __isl_take isl_space *space, struct vars *v, int rational)
  740. {
  741. isl_map *cond;
  742. isl_pw_aff *pwaff;
  743. int line = -1, col = -1;
  744. set_current_line_col(s, &line, &col);
  745. pwaff = accept_affine(s, space, v);
  746. if (rational)
  747. pwaff = isl_pw_aff_set_rational(pwaff);
  748. if (!pwaff)
  749. return NULL;
  750. if (!next_is_comparator(s))
  751. return pwaff;
  752. space = isl_pw_aff_get_domain_space(pwaff);
  753. cond = isl_map_universe(isl_space_unwrap(space));
  754. if (push_aff(s, line, col, pwaff) < 0)
  755. cond = isl_map_free(cond);
  756. if (!cond)
  757. return NULL;
  758. cond = read_formula(s, v, cond, rational);
  759. return accept_ternary(s, cond, v, rational);
  760. }
  761. static __isl_give isl_map *read_var_def(__isl_keep isl_stream *s,
  762. __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
  763. int rational)
  764. {
  765. isl_pw_aff *def;
  766. isl_size pos;
  767. isl_map *def_map;
  768. if (type == isl_dim_param)
  769. pos = isl_map_dim(map, isl_dim_param);
  770. else {
  771. pos = isl_map_dim(map, isl_dim_in);
  772. if (type == isl_dim_out) {
  773. isl_size n_out = isl_map_dim(map, isl_dim_out);
  774. if (pos < 0 || n_out < 0)
  775. return isl_map_free(map);
  776. pos += n_out;
  777. }
  778. type = isl_dim_in;
  779. }
  780. if (pos < 0)
  781. return isl_map_free(map);
  782. --pos;
  783. def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
  784. v, rational);
  785. def_map = isl_map_from_pw_aff(def);
  786. def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
  787. def_map = isl_set_unwrap(isl_map_domain(def_map));
  788. map = isl_map_intersect(map, def_map);
  789. return map;
  790. }
  791. static __isl_give isl_pw_aff_list *accept_affine_list(__isl_keep isl_stream *s,
  792. __isl_take isl_space *space, struct vars *v)
  793. {
  794. isl_pw_aff *pwaff;
  795. isl_pw_aff_list *list;
  796. struct isl_token *tok = NULL;
  797. pwaff = accept_affine(s, isl_space_copy(space), v);
  798. list = isl_pw_aff_list_from_pw_aff(pwaff);
  799. if (!list)
  800. goto error;
  801. for (;;) {
  802. tok = isl_stream_next_token(s);
  803. if (!tok) {
  804. isl_stream_error(s, NULL, "unexpected EOF");
  805. goto error;
  806. }
  807. if (tok->type != ',') {
  808. isl_stream_push_token(s, tok);
  809. break;
  810. }
  811. isl_token_free(tok);
  812. pwaff = accept_affine(s, isl_space_copy(space), v);
  813. list = isl_pw_aff_list_concat(list,
  814. isl_pw_aff_list_from_pw_aff(pwaff));
  815. if (!list)
  816. goto error;
  817. }
  818. isl_space_free(space);
  819. return list;
  820. error:
  821. isl_space_free(space);
  822. isl_pw_aff_list_free(list);
  823. return NULL;
  824. }
  825. static __isl_give isl_map *read_defined_var_list(__isl_keep isl_stream *s,
  826. struct vars *v, __isl_take isl_map *map, int rational)
  827. {
  828. struct isl_token *tok;
  829. while ((tok = isl_stream_next_token(s)) != NULL) {
  830. int p;
  831. int n = v->n;
  832. if (tok->type != ISL_TOKEN_IDENT)
  833. break;
  834. p = vars_pos(v, tok->u.s, -1);
  835. if (p < 0)
  836. goto error;
  837. if (p < n) {
  838. isl_stream_error(s, tok, "expecting unique identifier");
  839. goto error;
  840. }
  841. map = isl_map_add_dims(map, isl_dim_out, 1);
  842. isl_token_free(tok);
  843. tok = isl_stream_next_token(s);
  844. if (tok && tok->type == '=') {
  845. isl_token_free(tok);
  846. map = read_var_def(s, map, isl_dim_out, v, rational);
  847. tok = isl_stream_next_token(s);
  848. }
  849. if (!tok || tok->type != ',')
  850. break;
  851. isl_token_free(tok);
  852. }
  853. if (tok)
  854. isl_stream_push_token(s, tok);
  855. return map;
  856. error:
  857. isl_token_free(tok);
  858. isl_map_free(map);
  859. return NULL;
  860. }
  861. static int next_is_tuple(__isl_keep isl_stream *s)
  862. {
  863. struct isl_token *tok;
  864. int is_tuple;
  865. tok = isl_stream_next_token(s);
  866. if (!tok)
  867. return 0;
  868. if (tok->type == '[') {
  869. isl_stream_push_token(s, tok);
  870. return 1;
  871. }
  872. if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
  873. isl_stream_push_token(s, tok);
  874. return 0;
  875. }
  876. is_tuple = isl_stream_next_token_is(s, '[');
  877. isl_stream_push_token(s, tok);
  878. return is_tuple;
  879. }
  880. /* Does the next token mark the end of a tuple element?
  881. */
  882. static int next_is_end_tuple_element(__isl_keep isl_stream *s)
  883. {
  884. return isl_stream_next_token_is(s, ',') ||
  885. isl_stream_next_token_is(s, ']');
  886. }
  887. /* Is the next token one that necessarily forms the start of a condition?
  888. */
  889. static int next_is_condition_start(__isl_keep isl_stream *s)
  890. {
  891. return isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
  892. isl_stream_next_token_is(s, ISL_TOKEN_NOT) ||
  893. isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
  894. isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
  895. isl_stream_next_token_is(s, ISL_TOKEN_MAP);
  896. }
  897. /* Is "pa" an expression in term of earlier dimensions?
  898. * The alternative is that the dimension is defined to be equal to itself,
  899. * meaning that it has a universe domain and an expression that depends
  900. * on itself. "i" is the position of the expression in a sequence
  901. * of "n" expressions. The final dimensions of "pa" correspond to
  902. * these "n" expressions.
  903. */
  904. static isl_bool pw_aff_is_expr(__isl_keep isl_pw_aff *pa, int i, int n)
  905. {
  906. isl_aff *aff;
  907. if (!pa)
  908. return isl_bool_error;
  909. if (pa->n != 1)
  910. return isl_bool_true;
  911. if (!isl_set_plain_is_universe(pa->p[0].set))
  912. return isl_bool_true;
  913. aff = pa->p[0].aff;
  914. if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
  915. return isl_bool_true;
  916. return isl_bool_false;
  917. }
  918. /* Does the tuple contain any dimensions that are defined
  919. * in terms of earlier dimensions?
  920. */
  921. static isl_bool tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
  922. {
  923. int i;
  924. isl_size n;
  925. isl_bool has_expr = isl_bool_false;
  926. isl_pw_aff *pa;
  927. n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
  928. if (n < 0)
  929. return isl_bool_error;
  930. for (i = 0; i < n; ++i) {
  931. pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
  932. has_expr = pw_aff_is_expr(pa, i, n);
  933. isl_pw_aff_free(pa);
  934. if (has_expr < 0 || has_expr)
  935. break;
  936. }
  937. return has_expr;
  938. }
  939. /* Set the name of dimension "pos" in "space" to "name".
  940. * During printing, we add primes if the same name appears more than once
  941. * to distinguish the occurrences. Here, we remove those primes from "name"
  942. * before setting the name of the dimension.
  943. */
  944. static __isl_give isl_space *space_set_dim_name(__isl_take isl_space *space,
  945. int pos, char *name)
  946. {
  947. char *prime;
  948. if (!name)
  949. return space;
  950. prime = strchr(name, '\'');
  951. if (prime)
  952. *prime = '\0';
  953. space = isl_space_set_dim_name(space, isl_dim_out, pos, name);
  954. if (prime)
  955. *prime = '\'';
  956. return space;
  957. }
  958. /* Construct an isl_pw_aff defined on a "space" (with v->n variables)
  959. * that is equal to the last of those variables.
  960. */
  961. static __isl_give isl_pw_aff *identity_tuple_el_on_space(
  962. __isl_take isl_space *space, struct vars *v)
  963. {
  964. isl_aff *aff;
  965. aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
  966. aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n - 1, 1);
  967. return isl_pw_aff_from_aff(aff);
  968. }
  969. /* Construct an isl_pw_aff defined on the domain space of "pa"
  970. * that is equal to the last variable in "v".
  971. *
  972. * That is, if D is the domain space of "pa", then construct
  973. *
  974. * D[..., i] -> i.
  975. */
  976. static __isl_give isl_pw_aff *init_range(__isl_keep isl_pw_aff *pa,
  977. struct vars *v)
  978. {
  979. isl_space *space;
  980. space = isl_pw_aff_get_domain_space(pa);
  981. return identity_tuple_el_on_space(space, v);
  982. }
  983. /* Impose the lower bound "lower" on the variable represented by "range_pa".
  984. *
  985. * In particular, "range_pa" is of the form
  986. *
  987. * D[..., i] -> i : C
  988. *
  989. * with D also the domains space of "lower' and "C" some constraints.
  990. *
  991. * Return the expression
  992. *
  993. * D[..., i] -> i : C and i >= lower
  994. */
  995. static __isl_give isl_pw_aff *set_lower(__isl_take isl_pw_aff *range_pa,
  996. __isl_take isl_pw_aff *lower)
  997. {
  998. isl_set *range;
  999. range = isl_pw_aff_ge_set(isl_pw_aff_copy(range_pa), lower);
  1000. return isl_pw_aff_intersect_domain(range_pa, range);
  1001. }
  1002. /* Impose the upper bound "upper" on the variable represented by "range_pa".
  1003. *
  1004. * In particular, "range_pa" is of the form
  1005. *
  1006. * D[..., i] -> i : C
  1007. *
  1008. * with D also the domains space of "upper' and "C" some constraints.
  1009. *
  1010. * Return the expression
  1011. *
  1012. * D[..., i] -> i : C and i <= upper
  1013. */
  1014. static __isl_give isl_pw_aff *set_upper(__isl_take isl_pw_aff *range_pa,
  1015. __isl_take isl_pw_aff *upper)
  1016. {
  1017. isl_set *range;
  1018. range = isl_pw_aff_le_set(isl_pw_aff_copy(range_pa), upper);
  1019. return isl_pw_aff_intersect_domain(range_pa, range);
  1020. }
  1021. /* Construct a piecewise affine expression corresponding
  1022. * to the last variable in "v" that is greater than or equal to "pa".
  1023. *
  1024. * In particular, if D is the domain space of "pa",
  1025. * then construct the expression
  1026. *
  1027. * D[..., i] -> i,
  1028. *
  1029. * impose lower bound "pa" and return
  1030. *
  1031. * D[..., i] -> i : i >= pa
  1032. */
  1033. static __isl_give isl_pw_aff *construct_lower(__isl_take isl_pw_aff *pa,
  1034. struct vars *v)
  1035. {
  1036. return set_lower(init_range(pa, v), pa);
  1037. }
  1038. /* Construct a piecewise affine expression corresponding
  1039. * to the last variable in "v" that is smaller than or equal to "pa".
  1040. *
  1041. * In particular, if D is the domain space of "pa",
  1042. * then construct the expression
  1043. *
  1044. * D[..., i] -> i,
  1045. *
  1046. * impose lower bound "pa" and return
  1047. *
  1048. * D[..., i] -> i : i <= pa
  1049. */
  1050. static __isl_give isl_pw_aff *construct_upper(__isl_take isl_pw_aff *pa,
  1051. struct vars *v)
  1052. {
  1053. return set_upper(init_range(pa, v), pa);
  1054. }
  1055. /* Construct a piecewise affine expression corresponding
  1056. * to the last variable in "v" that ranges between "pa" and "pa2".
  1057. *
  1058. * In particular, if D is the domain space of "pa" (and "pa2"),
  1059. * then construct the expression
  1060. *
  1061. * D[..., i] -> i,
  1062. *
  1063. * impose lower bound "pa" and upper bound "pa2" and return
  1064. *
  1065. * D[..., i] -> i : pa <= i <= pa2
  1066. */
  1067. static __isl_give isl_pw_aff *construct_range(__isl_take isl_pw_aff *pa,
  1068. __isl_take isl_pw_aff *pa2, struct vars *v)
  1069. {
  1070. return set_upper(set_lower(init_range(pa, v), pa), pa2);
  1071. }
  1072. static int resolve_paren_expr(__isl_keep isl_stream *s,
  1073. struct vars *v, __isl_take isl_map *map, int rational);
  1074. /* Given that the (piecewise) affine expression "pa"
  1075. * has just been parsed, followed by a colon,
  1076. * continue parsing as part of a piecewise affine expression.
  1077. *
  1078. * In particular, check if the colon is followed by a condition.
  1079. * If so, parse the conditions(a) on "pa" and include them in the domain.
  1080. * Otherwise, if the colon is followed by another (piecewise) affine expression
  1081. * then consider the two expressions as endpoints of a range of values and
  1082. * return a piecewise affine expression that takes values in that range.
  1083. * Note that an affine expression followed by a comparison operator
  1084. * is considered to be part of a condition.
  1085. * If the colon is not followed by anything (inside the tuple element),
  1086. * then consider "pa" as a lower bound on a range of values without upper bound
  1087. * and return a piecewise affine expression that takes values in that range.
  1088. */
  1089. static __isl_give isl_pw_aff *update_piecewise_affine_colon(
  1090. __isl_take isl_pw_aff *pa, __isl_keep isl_stream *s,
  1091. struct vars *v, int rational)
  1092. {
  1093. isl_space *dom_space;
  1094. isl_map *map;
  1095. dom_space = isl_pw_aff_get_domain_space(pa);
  1096. map = isl_map_universe(isl_space_from_domain(dom_space));
  1097. if (isl_stream_next_token_is(s, '('))
  1098. if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
  1099. goto error;
  1100. if (next_is_end_tuple_element(s)) {
  1101. isl_map_free(map);
  1102. return construct_lower(pa, v);
  1103. }
  1104. if (!next_is_condition_start(s)) {
  1105. int line = -1, col = -1;
  1106. isl_space *space;
  1107. isl_pw_aff *pa2;
  1108. set_current_line_col(s, &line, &col);
  1109. space = isl_space_wrap(isl_map_get_space(map));
  1110. pa2 = accept_affine(s, space, v);
  1111. if (rational)
  1112. pa2 = isl_pw_aff_set_rational(pa2);
  1113. if (!next_is_comparator(s)) {
  1114. isl_map_free(map);
  1115. pa2 = isl_pw_aff_domain_factor_domain(pa2);
  1116. return construct_range(pa, pa2, v);
  1117. }
  1118. if (push_aff(s, line, col, pa2) < 0)
  1119. goto error;
  1120. }
  1121. map = read_formula(s, v, map, rational);
  1122. pa = isl_pw_aff_intersect_domain(pa, isl_map_domain(map));
  1123. return pa;
  1124. error:
  1125. isl_map_free(map);
  1126. isl_pw_aff_free(pa);
  1127. return NULL;
  1128. }
  1129. /* Accept a piecewise affine expression.
  1130. *
  1131. * At the outer level, the piecewise affine expression may be of the form
  1132. *
  1133. * aff1 : condition1; aff2 : conditions2; ...
  1134. *
  1135. * or one of
  1136. *
  1137. * aff :
  1138. * aff1 : aff2
  1139. * : aff
  1140. * :
  1141. *
  1142. * or simply
  1143. *
  1144. * aff
  1145. *
  1146. * each of the affine expressions may in turn include ternary operators.
  1147. *
  1148. * If the first token is a colon, then the expression must be
  1149. * ":" or ": aff2", depending on whether anything follows the colon
  1150. * inside the tuple element.
  1151. * The first is considered to represent an arbitrary value.
  1152. * The second is considered to represent a range of values
  1153. * with the given upper bound and no lower bound.
  1154. *
  1155. * There may be parentheses around some subexpression of "aff1"
  1156. * around "aff1" itself, around "aff1 : condition1" and/or
  1157. * around the entire piecewise affine expression.
  1158. * We therefore remove the opening parenthesis (if any) from the stream
  1159. * in case the closing parenthesis follows the colon, but if the closing
  1160. * parenthesis is the first thing in the stream after the parsed affine
  1161. * expression, we push the parsed expression onto the stream and parse
  1162. * again in case the parentheses enclose some subexpression of "aff1".
  1163. */
  1164. static __isl_give isl_pw_aff *accept_piecewise_affine(__isl_keep isl_stream *s,
  1165. __isl_take isl_space *space, struct vars *v, int rational)
  1166. {
  1167. isl_pw_aff *res;
  1168. isl_space *res_space;
  1169. if (isl_stream_eat_if_available(s, ':')) {
  1170. if (next_is_end_tuple_element(s))
  1171. return identity_tuple_el_on_space(space, v);
  1172. else
  1173. return construct_upper(accept_affine(s, space, v), v);
  1174. }
  1175. res_space = isl_space_from_domain(isl_space_copy(space));
  1176. res_space = isl_space_add_dims(res_space, isl_dim_out, 1);
  1177. res = isl_pw_aff_empty(res_space);
  1178. do {
  1179. isl_pw_aff *pa;
  1180. int seen_paren;
  1181. int line = -1, col = -1;
  1182. set_current_line_col(s, &line, &col);
  1183. seen_paren = isl_stream_eat_if_available(s, '(');
  1184. if (seen_paren)
  1185. pa = accept_piecewise_affine(s, isl_space_copy(space),
  1186. v, rational);
  1187. else
  1188. pa = accept_extended_affine(s, isl_space_copy(space),
  1189. v, rational);
  1190. if (seen_paren && isl_stream_eat_if_available(s, ')')) {
  1191. seen_paren = 0;
  1192. if (push_aff(s, line, col, pa) < 0)
  1193. goto error;
  1194. pa = accept_extended_affine(s, isl_space_copy(space),
  1195. v, rational);
  1196. }
  1197. if (isl_stream_eat_if_available(s, ':'))
  1198. pa = update_piecewise_affine_colon(pa, s, v, rational);
  1199. res = isl_pw_aff_union_add(res, pa);
  1200. if (seen_paren && isl_stream_eat(s, ')'))
  1201. goto error;
  1202. } while (isl_stream_eat_if_available(s, ';'));
  1203. isl_space_free(space);
  1204. return res;
  1205. error:
  1206. isl_space_free(space);
  1207. return isl_pw_aff_free(res);
  1208. }
  1209. /* Read an affine expression from "s" for use in read_tuple.
  1210. *
  1211. * accept_extended_affine requires a wrapped space as input.
  1212. * read_tuple on the other hand expects each isl_pw_aff
  1213. * to have an anonymous space. We therefore adjust the space
  1214. * of the isl_pw_aff before returning it.
  1215. */
  1216. static __isl_give isl_pw_aff *read_tuple_var_def(__isl_keep isl_stream *s,
  1217. struct vars *v, int rational)
  1218. {
  1219. isl_space *space;
  1220. isl_pw_aff *def;
  1221. space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
  1222. def = accept_piecewise_affine(s, space, v, rational);
  1223. def = isl_pw_aff_domain_factor_domain(def);
  1224. return def;
  1225. }
  1226. /* Read a list of tuple elements by calling "read_el" on each of them and
  1227. * return a space with the same number of set dimensions derived from
  1228. * the parameter space "space" and possibly updated by "read_el".
  1229. * The elements in the list are separated by either "," or "][".
  1230. * If "comma" is set then only "," is allowed.
  1231. */
  1232. static __isl_give isl_space *read_tuple_list(__isl_keep isl_stream *s,
  1233. struct vars *v, __isl_take isl_space *space, int rational, int comma,
  1234. __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
  1235. struct vars *v, __isl_take isl_space *space, int rational,
  1236. void *user),
  1237. void *user)
  1238. {
  1239. if (!space)
  1240. return NULL;
  1241. space = isl_space_set_from_params(space);
  1242. if (isl_stream_next_token_is(s, ']'))
  1243. return space;
  1244. for (;;) {
  1245. struct isl_token *tok;
  1246. space = isl_space_add_dims(space, isl_dim_set, 1);
  1247. space = read_el(s, v, space, rational, user);
  1248. if (!space)
  1249. return NULL;
  1250. tok = isl_stream_next_token(s);
  1251. if (!comma && tok && tok->type == ']' &&
  1252. isl_stream_next_token_is(s, '[')) {
  1253. isl_token_free(tok);
  1254. tok = isl_stream_next_token(s);
  1255. } else if (!tok || tok->type != ',') {
  1256. if (tok)
  1257. isl_stream_push_token(s, tok);
  1258. break;
  1259. }
  1260. isl_token_free(tok);
  1261. }
  1262. return space;
  1263. }
  1264. /* Read a tuple space from "s" derived from the parameter space "space".
  1265. * Call "read_el" on each element in the tuples.
  1266. */
  1267. static __isl_give isl_space *read_tuple_space(__isl_keep isl_stream *s,
  1268. struct vars *v, __isl_take isl_space *space, int rational, int comma,
  1269. __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
  1270. struct vars *v, __isl_take isl_space *space, int rational,
  1271. void *user),
  1272. void *user)
  1273. {
  1274. struct isl_token *tok;
  1275. char *name = NULL;
  1276. isl_space *res = NULL;
  1277. tok = isl_stream_next_token(s);
  1278. if (!tok)
  1279. goto error;
  1280. if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
  1281. name = strdup(tok->u.s);
  1282. isl_token_free(tok);
  1283. if (!name)
  1284. goto error;
  1285. } else
  1286. isl_stream_push_token(s, tok);
  1287. if (isl_stream_eat(s, '['))
  1288. goto error;
  1289. if (next_is_tuple(s)) {
  1290. isl_space *out;
  1291. res = read_tuple_space(s, v, isl_space_copy(space),
  1292. rational, comma, read_el, user);
  1293. if (isl_stream_eat(s, ISL_TOKEN_TO))
  1294. goto error;
  1295. out = read_tuple_space(s, v, isl_space_copy(space),
  1296. rational, comma, read_el, user);
  1297. res = isl_space_product(res, out);
  1298. } else
  1299. res = read_tuple_list(s, v, isl_space_copy(space),
  1300. rational, comma, read_el, user);
  1301. if (isl_stream_eat(s, ']'))
  1302. goto error;
  1303. if (name) {
  1304. res = isl_space_set_tuple_name(res, isl_dim_set, name);
  1305. free(name);
  1306. }
  1307. isl_space_free(space);
  1308. return res;
  1309. error:
  1310. free(name);
  1311. isl_space_free(res);
  1312. isl_space_free(space);
  1313. return NULL;
  1314. }
  1315. /* Construct an isl_pw_aff defined on a space with v->n variables
  1316. * that is equal to the last of those variables.
  1317. */
  1318. static __isl_give isl_pw_aff *identity_tuple_el(struct vars *v)
  1319. {
  1320. isl_space *space;
  1321. space = isl_space_set_alloc(v->ctx, 0, v->n);
  1322. return identity_tuple_el_on_space(space, v);
  1323. }
  1324. /* This function is called for each element in a tuple inside read_tuple.
  1325. * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
  1326. * over a space containing all variables in "v" defined so far.
  1327. * The isl_pw_aff expresses the new variable in terms of earlier variables
  1328. * if a definition is provided. Otherwise, it is represented as being
  1329. * equal to itself.
  1330. * Add the isl_pw_aff to *list.
  1331. * If the new variable was named, then adjust "space" accordingly and
  1332. * return the updated space.
  1333. */
  1334. static __isl_give isl_space *read_tuple_pw_aff_el(__isl_keep isl_stream *s,
  1335. struct vars *v, __isl_take isl_space *space, int rational, void *user)
  1336. {
  1337. isl_pw_aff_list **list = (isl_pw_aff_list **) user;
  1338. isl_pw_aff *pa;
  1339. struct isl_token *tok;
  1340. int new_name = 0;
  1341. tok = next_token(s);
  1342. if (!tok) {
  1343. isl_stream_error(s, NULL, "unexpected EOF");
  1344. return isl_space_free(space);
  1345. }
  1346. if (tok->type == ISL_TOKEN_IDENT) {
  1347. int n = v->n;
  1348. int p = vars_pos(v, tok->u.s, -1);
  1349. if (p < 0)
  1350. goto error;
  1351. new_name = p >= n;
  1352. }
  1353. if (tok->type == '*') {
  1354. if (vars_add_anon(v) < 0)
  1355. goto error;
  1356. isl_token_free(tok);
  1357. pa = identity_tuple_el(v);
  1358. } else if (new_name) {
  1359. isl_size pos = isl_space_dim(space, isl_dim_out);
  1360. if (pos < 0)
  1361. goto error;
  1362. pos -= 1;
  1363. space = space_set_dim_name(space, pos, v->v->name);
  1364. isl_token_free(tok);
  1365. if (isl_stream_eat_if_available(s, '='))
  1366. pa = read_tuple_var_def(s, v, rational);
  1367. else
  1368. pa = identity_tuple_el(v);
  1369. } else {
  1370. isl_stream_push_token(s, tok);
  1371. tok = NULL;
  1372. if (vars_add_anon(v) < 0)
  1373. goto error;
  1374. pa = read_tuple_var_def(s, v, rational);
  1375. }
  1376. *list = isl_pw_aff_list_add(*list, pa);
  1377. if (!*list)
  1378. return isl_space_free(space);
  1379. return space;
  1380. error:
  1381. isl_token_free(tok);
  1382. return isl_space_free(space);
  1383. }
  1384. /* Read a tuple and represent it as an isl_multi_pw_aff.
  1385. * The range space of the isl_multi_pw_aff is the space of the tuple.
  1386. * The domain space is an anonymous space
  1387. * with a dimension for each variable in the set of variables in "v",
  1388. * including the variables in the range.
  1389. * If a given dimension is not defined in terms of earlier dimensions in
  1390. * the input, then the corresponding isl_pw_aff is set equal to one time
  1391. * the variable corresponding to the dimension being defined.
  1392. *
  1393. * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
  1394. * Each element in this list is defined over a space representing
  1395. * the variables defined so far. We need to adjust the earlier
  1396. * elements to have as many variables in the domain as the final
  1397. * element in the list.
  1398. */
  1399. static __isl_give isl_multi_pw_aff *read_tuple(__isl_keep isl_stream *s,
  1400. struct vars *v, int rational, int comma)
  1401. {
  1402. int i;
  1403. isl_size n;
  1404. isl_space *space;
  1405. isl_pw_aff_list *list;
  1406. space = isl_space_params_alloc(v->ctx, 0);
  1407. list = isl_pw_aff_list_alloc(s->ctx, 0);
  1408. space = read_tuple_space(s, v, space, rational, comma,
  1409. &read_tuple_pw_aff_el, &list);
  1410. n = isl_space_dim(space, isl_dim_set);
  1411. if (n < 0)
  1412. space = isl_space_free(space);
  1413. for (i = 0; i + 1 < n; ++i) {
  1414. isl_pw_aff *pa;
  1415. pa = isl_pw_aff_list_get_pw_aff(list, i);
  1416. pa = isl_pw_aff_add_dims(pa, isl_dim_in, n - (i + 1));
  1417. list = isl_pw_aff_list_set_pw_aff(list, i, pa);
  1418. }
  1419. space = isl_space_from_range(space);
  1420. space = isl_space_add_dims(space, isl_dim_in, v->n);
  1421. return isl_multi_pw_aff_from_pw_aff_list(space, list);
  1422. }
  1423. /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
  1424. * We first create the appropriate space in "map" based on the range
  1425. * space of this isl_multi_pw_aff. Then, we add equalities based
  1426. * on the affine expressions. These live in an anonymous space,
  1427. * however, so we first need to reset the space to that of "map".
  1428. */
  1429. static __isl_give isl_map *map_from_tuple(__isl_take isl_multi_pw_aff *tuple,
  1430. __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
  1431. int rational)
  1432. {
  1433. int i;
  1434. isl_size n;
  1435. isl_ctx *ctx;
  1436. isl_space *space = NULL;
  1437. n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
  1438. if (!map || n < 0)
  1439. goto error;
  1440. ctx = isl_multi_pw_aff_get_ctx(tuple);
  1441. space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
  1442. if (!space)
  1443. goto error;
  1444. if (type == isl_dim_param) {
  1445. if (isl_space_has_tuple_name(space, isl_dim_set) ||
  1446. isl_space_is_wrapping(space)) {
  1447. isl_die(ctx, isl_error_invalid,
  1448. "parameter tuples cannot be named or nested",
  1449. goto error);
  1450. }
  1451. map = isl_map_add_dims(map, type, n);
  1452. for (i = 0; i < n; ++i) {
  1453. isl_id *id;
  1454. if (!isl_space_has_dim_name(space, isl_dim_set, i))
  1455. isl_die(ctx, isl_error_invalid,
  1456. "parameters must be named",
  1457. goto error);
  1458. id = isl_space_get_dim_id(space, isl_dim_set, i);
  1459. map = isl_map_set_dim_id(map, isl_dim_param, i, id);
  1460. }
  1461. } else if (type == isl_dim_in) {
  1462. isl_set *set;
  1463. set = isl_set_universe(isl_space_copy(space));
  1464. if (rational)
  1465. set = isl_set_set_rational(set);
  1466. set = isl_set_intersect_params(set, isl_map_params(map));
  1467. map = isl_map_from_domain(set);
  1468. } else {
  1469. isl_set *set;
  1470. set = isl_set_universe(isl_space_copy(space));
  1471. if (rational)
  1472. set = isl_set_set_rational(set);
  1473. map = isl_map_from_domain_and_range(isl_map_domain(map), set);
  1474. }
  1475. for (i = 0; i < n; ++i) {
  1476. isl_pw_aff *pa;
  1477. isl_space *space;
  1478. isl_aff *aff;
  1479. isl_set *set;
  1480. isl_map *map_i;
  1481. pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
  1482. space = isl_pw_aff_get_domain_space(pa);
  1483. aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
  1484. aff = isl_aff_add_coefficient_si(aff,
  1485. isl_dim_in, v->n - n + i, -1);
  1486. pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
  1487. if (rational)
  1488. pa = isl_pw_aff_set_rational(pa);
  1489. set = isl_pw_aff_zero_set(pa);
  1490. map_i = isl_map_from_range(set);
  1491. map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
  1492. map = isl_map_intersect(map, map_i);
  1493. }
  1494. isl_space_free(space);
  1495. isl_multi_pw_aff_free(tuple);
  1496. return map;
  1497. error:
  1498. isl_space_free(space);
  1499. isl_multi_pw_aff_free(tuple);
  1500. isl_map_free(map);
  1501. return NULL;
  1502. }
  1503. /* Read a tuple from "s" and add it to "map".
  1504. * The tuple is initially represented as an isl_multi_pw_aff and
  1505. * then added to "map".
  1506. */
  1507. static __isl_give isl_map *read_map_tuple(__isl_keep isl_stream *s,
  1508. __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
  1509. int rational, int comma)
  1510. {
  1511. isl_multi_pw_aff *tuple;
  1512. tuple = read_tuple(s, v, rational, comma);
  1513. if (!tuple)
  1514. return isl_map_free(map);
  1515. return map_from_tuple(tuple, map, type, v, rational);
  1516. }
  1517. /* Given two equal-length lists of piecewise affine expression with the space
  1518. * of "set" as domain, construct a set in the same space that expresses
  1519. * that "left" and "right" satisfy the comparison "type".
  1520. *
  1521. * A space is constructed of the same dimension as the number of elements
  1522. * in the two lists. The comparison is then expressed in a map from
  1523. * this space to itself and wrapped into a set. Finally the two lists
  1524. * of piecewise affine expressions are plugged into this set.
  1525. *
  1526. * Let S be the space of "set" and T the constructed space.
  1527. * The lists are first changed into two isl_multi_pw_affs in S -> T and
  1528. * then combined into an isl_multi_pw_aff in S -> [T -> T],
  1529. * while the comparison is first expressed in T -> T, then [T -> T]
  1530. * and finally in S.
  1531. */
  1532. static __isl_give isl_set *list_cmp(__isl_keep isl_set *set, int type,
  1533. __isl_take isl_pw_aff_list *left, __isl_take isl_pw_aff_list *right)
  1534. {
  1535. isl_space *space;
  1536. isl_size n;
  1537. isl_multi_pw_aff *mpa1, *mpa2;
  1538. n = isl_pw_aff_list_n_pw_aff(left);
  1539. if (!set || n < 0 || !right)
  1540. goto error;
  1541. space = isl_set_get_space(set);
  1542. space = isl_space_from_domain(space);
  1543. space = isl_space_add_dims(space, isl_dim_out, n);
  1544. mpa1 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), left);
  1545. mpa2 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), right);
  1546. mpa1 = isl_multi_pw_aff_range_product(mpa1, mpa2);
  1547. space = isl_space_range(space);
  1548. switch (type) {
  1549. case ISL_TOKEN_LEX_LT:
  1550. set = isl_map_wrap(isl_map_lex_lt(space));
  1551. break;
  1552. case ISL_TOKEN_LEX_GT:
  1553. set = isl_map_wrap(isl_map_lex_gt(space));
  1554. break;
  1555. case ISL_TOKEN_LEX_LE:
  1556. set = isl_map_wrap(isl_map_lex_le(space));
  1557. break;
  1558. case ISL_TOKEN_LEX_GE:
  1559. set = isl_map_wrap(isl_map_lex_ge(space));
  1560. break;
  1561. default:
  1562. isl_multi_pw_aff_free(mpa1);
  1563. isl_space_free(space);
  1564. isl_die(isl_set_get_ctx(set), isl_error_internal,
  1565. "unhandled list comparison type", return NULL);
  1566. }
  1567. set = isl_set_preimage_multi_pw_aff(set, mpa1);
  1568. return set;
  1569. error:
  1570. isl_pw_aff_list_free(left);
  1571. isl_pw_aff_list_free(right);
  1572. return NULL;
  1573. }
  1574. /* Construct constraints of the form
  1575. *
  1576. * a op b
  1577. *
  1578. * where a is an element in "left", op is an operator of type "type" and
  1579. * b is an element in "right", add the constraints to "set" and return
  1580. * the result.
  1581. * "rational" is set if the constraints should be treated as
  1582. * a rational constraints.
  1583. *
  1584. * If "type" is the type of a comparison operator between lists
  1585. * of affine expressions, then a single (compound) constraint
  1586. * is constructed by list_cmp instead.
  1587. */
  1588. static __isl_give isl_set *construct_constraints(
  1589. __isl_take isl_set *set, int type,
  1590. __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
  1591. int rational)
  1592. {
  1593. isl_set *cond;
  1594. left = isl_pw_aff_list_copy(left);
  1595. right = isl_pw_aff_list_copy(right);
  1596. if (rational) {
  1597. left = isl_pw_aff_list_set_rational(left);
  1598. right = isl_pw_aff_list_set_rational(right);
  1599. }
  1600. if (is_list_comparator_type(type))
  1601. cond = list_cmp(set, type, left, right);
  1602. else if (type == ISL_TOKEN_LE)
  1603. cond = isl_pw_aff_list_le_set(left, right);
  1604. else if (type == ISL_TOKEN_GE)
  1605. cond = isl_pw_aff_list_ge_set(left, right);
  1606. else if (type == ISL_TOKEN_LT)
  1607. cond = isl_pw_aff_list_lt_set(left, right);
  1608. else if (type == ISL_TOKEN_GT)
  1609. cond = isl_pw_aff_list_gt_set(left, right);
  1610. else if (type == ISL_TOKEN_NE)
  1611. cond = isl_pw_aff_list_ne_set(left, right);
  1612. else
  1613. cond = isl_pw_aff_list_eq_set(left, right);
  1614. return isl_set_intersect(set, cond);
  1615. }
  1616. /* Read a constraint from "s", add it to "map" and return the result.
  1617. * "v" contains a description of the identifiers parsed so far.
  1618. * "rational" is set if the constraint should be treated as
  1619. * a rational constraint.
  1620. * The constraint read from "s" may be applied to multiple pairs
  1621. * of affine expressions and may be chained.
  1622. * In particular, a list of affine expressions is read, followed
  1623. * by a comparison operator and another list of affine expressions.
  1624. * The comparison operator is then applied to each pair of elements
  1625. * in the two lists and the results are added to "map".
  1626. * However, if the operator expects two lists of affine expressions,
  1627. * then it is applied directly to those lists and the two lists
  1628. * are required to have the same length.
  1629. * If the next token is another comparison operator, then another
  1630. * list of affine expressions is read and the process repeats.
  1631. *
  1632. * The processing is performed on a wrapped copy of "map" because
  1633. * an affine expression cannot have a binary relation as domain.
  1634. */
  1635. static __isl_give isl_map *add_constraint(__isl_keep isl_stream *s,
  1636. struct vars *v, __isl_take isl_map *map, int rational)
  1637. {
  1638. struct isl_token *tok;
  1639. int type;
  1640. isl_pw_aff_list *list1 = NULL, *list2 = NULL;
  1641. isl_size n1, n2;
  1642. isl_set *set;
  1643. set = isl_map_wrap(map);
  1644. list1 = accept_affine_list(s, isl_set_get_space(set), v);
  1645. if (!list1)
  1646. goto error;
  1647. tok = isl_stream_next_token(s);
  1648. if (!is_comparator(tok)) {
  1649. isl_stream_error(s, tok, "missing operator");
  1650. if (tok)
  1651. isl_stream_push_token(s, tok);
  1652. goto error;
  1653. }
  1654. type = tok->type;
  1655. isl_token_free(tok);
  1656. for (;;) {
  1657. list2 = accept_affine_list(s, isl_set_get_space(set), v);
  1658. n1 = isl_pw_aff_list_n_pw_aff(list1);
  1659. n2 = isl_pw_aff_list_n_pw_aff(list2);
  1660. if (n1 < 0 || n2 < 0)
  1661. goto error;
  1662. if (is_list_comparator_type(type) && n1 != n2) {
  1663. isl_stream_error(s, NULL,
  1664. "list arguments not of same size");
  1665. goto error;
  1666. }
  1667. set = construct_constraints(set, type, list1, list2, rational);
  1668. isl_pw_aff_list_free(list1);
  1669. list1 = list2;
  1670. if (!next_is_comparator(s))
  1671. break;
  1672. tok = isl_stream_next_token(s);
  1673. type = tok->type;
  1674. isl_token_free(tok);
  1675. }
  1676. isl_pw_aff_list_free(list1);
  1677. return isl_set_unwrap(set);
  1678. error:
  1679. isl_pw_aff_list_free(list1);
  1680. isl_pw_aff_list_free(list2);
  1681. isl_set_free(set);
  1682. return NULL;
  1683. }
  1684. static __isl_give isl_map *read_exists(__isl_keep isl_stream *s,
  1685. struct vars *v, __isl_take isl_map *map, int rational)
  1686. {
  1687. int n = v->n;
  1688. int seen_paren = isl_stream_eat_if_available(s, '(');
  1689. map = isl_map_from_domain(isl_map_wrap(map));
  1690. map = read_defined_var_list(s, v, map, rational);
  1691. if (isl_stream_eat(s, ':'))
  1692. goto error;
  1693. map = read_formula(s, v, map, rational);
  1694. map = isl_set_unwrap(isl_map_domain(map));
  1695. vars_drop(v, v->n - n);
  1696. if (seen_paren && isl_stream_eat(s, ')'))
  1697. goto error;
  1698. return map;
  1699. error:
  1700. isl_map_free(map);
  1701. return NULL;
  1702. }
  1703. /* Parse an expression between parentheses and push the result
  1704. * back on the stream.
  1705. *
  1706. * The parsed expression may be either an affine expression
  1707. * or a condition. The first type is pushed onto the stream
  1708. * as an isl_pw_aff, while the second is pushed as an isl_map.
  1709. *
  1710. * If the initial token indicates the start of a condition,
  1711. * we parse it as such.
  1712. * Otherwise, we first parse an affine expression and push
  1713. * that onto the stream. If the affine expression covers the
  1714. * entire expression between parentheses, we return.
  1715. * Otherwise, we assume that the affine expression is the
  1716. * start of a condition and continue parsing.
  1717. */
  1718. static int resolve_paren_expr(__isl_keep isl_stream *s,
  1719. struct vars *v, __isl_take isl_map *map, int rational)
  1720. {
  1721. struct isl_token *tok, *tok2;
  1722. int has_paren;
  1723. int line, col;
  1724. isl_pw_aff *pwaff;
  1725. tok = isl_stream_next_token(s);
  1726. if (!tok || tok->type != '(')
  1727. goto error;
  1728. if (isl_stream_next_token_is(s, '('))
  1729. if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
  1730. goto error;
  1731. if (next_is_condition_start(s)) {
  1732. map = read_formula(s, v, map, rational);
  1733. if (isl_stream_eat(s, ')'))
  1734. goto error;
  1735. tok->type = ISL_TOKEN_MAP;
  1736. tok->u.map = map;
  1737. isl_stream_push_token(s, tok);
  1738. return 0;
  1739. }
  1740. tok2 = isl_stream_next_token(s);
  1741. if (!tok2)
  1742. goto error;
  1743. line = tok2->line;
  1744. col = tok2->col;
  1745. isl_stream_push_token(s, tok2);
  1746. pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
  1747. if (!pwaff)
  1748. goto error;
  1749. has_paren = isl_stream_eat_if_available(s, ')');
  1750. if (push_aff(s, line, col, pwaff) < 0)
  1751. goto error;
  1752. if (has_paren) {
  1753. isl_token_free(tok);
  1754. isl_map_free(map);
  1755. return 0;
  1756. }
  1757. map = read_formula(s, v, map, rational);
  1758. if (isl_stream_eat(s, ')'))
  1759. goto error;
  1760. tok->type = ISL_TOKEN_MAP;
  1761. tok->u.map = map;
  1762. isl_stream_push_token(s, tok);
  1763. return 0;
  1764. error:
  1765. isl_token_free(tok);
  1766. isl_map_free(map);
  1767. return -1;
  1768. }
  1769. static __isl_give isl_map *read_conjunct(__isl_keep isl_stream *s,
  1770. struct vars *v, __isl_take isl_map *map, int rational)
  1771. {
  1772. if (isl_stream_next_token_is(s, '('))
  1773. if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
  1774. goto error;
  1775. if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
  1776. struct isl_token *tok;
  1777. tok = isl_stream_next_token(s);
  1778. if (!tok)
  1779. goto error;
  1780. isl_map_free(map);
  1781. map = isl_map_copy(tok->u.map);
  1782. isl_token_free(tok);
  1783. return map;
  1784. }
  1785. if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
  1786. return read_exists(s, v, map, rational);
  1787. if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
  1788. return map;
  1789. if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
  1790. isl_space *space = isl_map_get_space(map);
  1791. isl_map_free(map);
  1792. return isl_map_empty(space);
  1793. }
  1794. return add_constraint(s, v, map, rational);
  1795. error:
  1796. isl_map_free(map);
  1797. return NULL;
  1798. }
  1799. static __isl_give isl_map *read_conjuncts(__isl_keep isl_stream *s,
  1800. struct vars *v, __isl_take isl_map *map, int rational)
  1801. {
  1802. isl_map *res;
  1803. int negate;
  1804. negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
  1805. res = read_conjunct(s, v, isl_map_copy(map), rational);
  1806. if (negate)
  1807. res = isl_map_subtract(isl_map_copy(map), res);
  1808. while (res && isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
  1809. isl_map *res_i;
  1810. negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
  1811. res_i = read_conjunct(s, v, isl_map_copy(map), rational);
  1812. if (negate)
  1813. res = isl_map_subtract(res, res_i);
  1814. else
  1815. res = isl_map_intersect(res, res_i);
  1816. }
  1817. isl_map_free(map);
  1818. return res;
  1819. }
  1820. static __isl_give isl_map *read_disjuncts(__isl_keep isl_stream *s,
  1821. struct vars *v, __isl_take isl_map *map, int rational)
  1822. {
  1823. isl_map *res;
  1824. if (isl_stream_next_token_is(s, '}'))
  1825. return map;
  1826. res = read_conjuncts(s, v, isl_map_copy(map), rational);
  1827. while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
  1828. isl_map *res_i;
  1829. res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
  1830. res = isl_map_union(res, res_i);
  1831. }
  1832. isl_map_free(map);
  1833. return res;
  1834. }
  1835. /* Read a first order formula from "s", add the corresponding
  1836. * constraints to "map" and return the result.
  1837. *
  1838. * In particular, read a formula of the form
  1839. *
  1840. * a
  1841. *
  1842. * or
  1843. *
  1844. * a implies b
  1845. *
  1846. * where a and b are disjunctions.
  1847. *
  1848. * In the first case, map is replaced by
  1849. *
  1850. * map \cap { [..] : a }
  1851. *
  1852. * In the second case, it is replaced by
  1853. *
  1854. * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
  1855. */
  1856. static __isl_give isl_map *read_formula(__isl_keep isl_stream *s,
  1857. struct vars *v, __isl_take isl_map *map, int rational)
  1858. {
  1859. isl_map *res;
  1860. res = read_disjuncts(s, v, isl_map_copy(map), rational);
  1861. if (isl_stream_eat_if_available(s, ISL_TOKEN_IMPLIES)) {
  1862. isl_map *res2;
  1863. res = isl_map_subtract(isl_map_copy(map), res);
  1864. res2 = read_disjuncts(s, v, map, rational);
  1865. res = isl_map_union(res, res2);
  1866. } else
  1867. isl_map_free(map);
  1868. return res;
  1869. }
  1870. static isl_size polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
  1871. {
  1872. isl_size n_out, n_in, n_param, n_div;
  1873. n_param = isl_basic_map_dim(bmap, isl_dim_param);
  1874. n_in = isl_basic_map_dim(bmap, isl_dim_in);
  1875. n_out = isl_basic_map_dim(bmap, isl_dim_out);
  1876. n_div = isl_basic_map_dim(bmap, isl_dim_div);
  1877. if (n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
  1878. return isl_size_error;
  1879. if (pos < n_out)
  1880. return 1 + n_param + n_in + pos;
  1881. pos -= n_out;
  1882. if (pos < n_in)
  1883. return 1 + n_param + pos;
  1884. pos -= n_in;
  1885. if (pos < n_div)
  1886. return 1 + n_param + n_in + n_out + pos;
  1887. pos -= n_div;
  1888. if (pos < n_param)
  1889. return 1 + pos;
  1890. return 0;
  1891. }
  1892. static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
  1893. __isl_keep isl_stream *s, __isl_take isl_basic_map *bmap)
  1894. {
  1895. int j;
  1896. struct isl_token *tok;
  1897. int type;
  1898. int k;
  1899. isl_int *c;
  1900. isl_size total;
  1901. if (!bmap)
  1902. return NULL;
  1903. tok = isl_stream_next_token(s);
  1904. if (!tok || tok->type != ISL_TOKEN_VALUE) {
  1905. isl_stream_error(s, tok, "expecting coefficient");
  1906. if (tok)
  1907. isl_stream_push_token(s, tok);
  1908. goto error;
  1909. }
  1910. if (!tok->on_new_line) {
  1911. isl_stream_error(s, tok, "coefficient should appear on new line");
  1912. isl_stream_push_token(s, tok);
  1913. goto error;
  1914. }
  1915. type = isl_int_get_si(tok->u.v);
  1916. isl_token_free(tok);
  1917. isl_assert(s->ctx, type == 0 || type == 1, goto error);
  1918. if (type == 0) {
  1919. k = isl_basic_map_alloc_equality(bmap);
  1920. c = bmap->eq[k];
  1921. } else {
  1922. k = isl_basic_map_alloc_inequality(bmap);
  1923. c = bmap->ineq[k];
  1924. }
  1925. if (k < 0)
  1926. goto error;
  1927. total = isl_basic_map_dim(bmap, isl_dim_all);
  1928. if (total < 0)
  1929. return isl_basic_map_free(bmap);
  1930. for (j = 0; j < 1 + total; ++j) {
  1931. isl_size pos;
  1932. tok = isl_stream_next_token(s);
  1933. if (!tok || tok->type != ISL_TOKEN_VALUE) {
  1934. isl_stream_error(s, tok, "expecting coefficient");
  1935. if (tok)
  1936. isl_stream_push_token(s, tok);
  1937. goto error;
  1938. }
  1939. if (tok->on_new_line) {
  1940. isl_stream_error(s, tok,
  1941. "coefficient should not appear on new line");
  1942. isl_stream_push_token(s, tok);
  1943. goto error;
  1944. }
  1945. pos = polylib_pos_to_isl_pos(bmap, j);
  1946. if (pos >= 0)
  1947. isl_int_set(c[pos], tok->u.v);
  1948. isl_token_free(tok);
  1949. if (pos < 0)
  1950. return isl_basic_map_free(bmap);
  1951. }
  1952. return bmap;
  1953. error:
  1954. isl_basic_map_free(bmap);
  1955. return NULL;
  1956. }
  1957. static __isl_give isl_basic_map *basic_map_read_polylib(
  1958. __isl_keep isl_stream *s)
  1959. {
  1960. int i;
  1961. struct isl_token *tok;
  1962. struct isl_token *tok2;
  1963. int n_row, n_col;
  1964. int on_new_line;
  1965. unsigned in = 0, out, local = 0;
  1966. struct isl_basic_map *bmap = NULL;
  1967. int nparam = 0;
  1968. tok = isl_stream_next_token(s);
  1969. if (!tok) {
  1970. isl_stream_error(s, NULL, "unexpected EOF");
  1971. return NULL;
  1972. }
  1973. tok2 = isl_stream_next_token(s);
  1974. if (!tok2) {
  1975. isl_token_free(tok);
  1976. isl_stream_error(s, NULL, "unexpected EOF");
  1977. return NULL;
  1978. }
  1979. if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
  1980. isl_stream_push_token(s, tok2);
  1981. isl_stream_push_token(s, tok);
  1982. isl_stream_error(s, NULL,
  1983. "expecting constraint matrix dimensions");
  1984. return NULL;
  1985. }
  1986. n_row = isl_int_get_si(tok->u.v);
  1987. n_col = isl_int_get_si(tok2->u.v);
  1988. on_new_line = tok2->on_new_line;
  1989. isl_token_free(tok2);
  1990. isl_token_free(tok);
  1991. isl_assert(s->ctx, !on_new_line, return NULL);
  1992. isl_assert(s->ctx, n_row >= 0, return NULL);
  1993. isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
  1994. tok = isl_stream_next_token_on_same_line(s);
  1995. if (tok) {
  1996. if (tok->type != ISL_TOKEN_VALUE) {
  1997. isl_stream_error(s, tok,
  1998. "expecting number of output dimensions");
  1999. isl_stream_push_token(s, tok);
  2000. goto error;
  2001. }
  2002. out = isl_int_get_si(tok->u.v);
  2003. isl_token_free(tok);
  2004. tok = isl_stream_next_token_on_same_line(s);
  2005. if (!tok || tok->type != ISL_TOKEN_VALUE) {
  2006. isl_stream_error(s, tok,
  2007. "expecting number of input dimensions");
  2008. if (tok)
  2009. isl_stream_push_token(s, tok);
  2010. goto error;
  2011. }
  2012. in = isl_int_get_si(tok->u.v);
  2013. isl_token_free(tok);
  2014. tok = isl_stream_next_token_on_same_line(s);
  2015. if (!tok || tok->type != ISL_TOKEN_VALUE) {
  2016. isl_stream_error(s, tok,
  2017. "expecting number of existentials");
  2018. if (tok)
  2019. isl_stream_push_token(s, tok);
  2020. goto error;
  2021. }
  2022. local = isl_int_get_si(tok->u.v);
  2023. isl_token_free(tok);
  2024. tok = isl_stream_next_token_on_same_line(s);
  2025. if (!tok || tok->type != ISL_TOKEN_VALUE) {
  2026. isl_stream_error(s, tok,
  2027. "expecting number of parameters");
  2028. if (tok)
  2029. isl_stream_push_token(s, tok);
  2030. goto error;
  2031. }
  2032. nparam = isl_int_get_si(tok->u.v);
  2033. isl_token_free(tok);
  2034. if (n_col != 1 + out + in + local + nparam + 1) {
  2035. isl_stream_error(s, NULL,
  2036. "dimensions don't match");
  2037. goto error;
  2038. }
  2039. } else
  2040. out = n_col - 2 - nparam;
  2041. bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
  2042. if (!bmap)
  2043. return NULL;
  2044. for (i = 0; i < local; ++i) {
  2045. int k = isl_basic_map_alloc_div(bmap);
  2046. if (k < 0)
  2047. goto error;
  2048. isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
  2049. }
  2050. for (i = 0; i < n_row; ++i)
  2051. bmap = basic_map_read_polylib_constraint(s, bmap);
  2052. tok = isl_stream_next_token_on_same_line(s);
  2053. if (tok) {
  2054. isl_stream_error(s, tok, "unexpected extra token on line");
  2055. isl_stream_push_token(s, tok);
  2056. goto error;
  2057. }
  2058. bmap = isl_basic_map_simplify(bmap);
  2059. bmap = isl_basic_map_finalize(bmap);
  2060. return bmap;
  2061. error:
  2062. isl_basic_map_free(bmap);
  2063. return NULL;
  2064. }
  2065. static __isl_give isl_map *map_read_polylib(__isl_keep isl_stream *s)
  2066. {
  2067. struct isl_token *tok;
  2068. struct isl_token *tok2;
  2069. int i, n;
  2070. struct isl_map *map;
  2071. tok = isl_stream_next_token(s);
  2072. if (!tok) {
  2073. isl_stream_error(s, NULL, "unexpected EOF");
  2074. return NULL;
  2075. }
  2076. tok2 = isl_stream_next_token_on_same_line(s);
  2077. if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
  2078. isl_stream_push_token(s, tok2);
  2079. isl_stream_push_token(s, tok);
  2080. return isl_map_from_basic_map(basic_map_read_polylib(s));
  2081. }
  2082. if (tok2) {
  2083. isl_stream_error(s, tok2, "unexpected token");
  2084. isl_stream_push_token(s, tok2);
  2085. isl_stream_push_token(s, tok);
  2086. return NULL;
  2087. }
  2088. n = isl_int_get_si(tok->u.v);
  2089. isl_token_free(tok);
  2090. isl_assert(s->ctx, n >= 1, return NULL);
  2091. map = isl_map_from_basic_map(basic_map_read_polylib(s));
  2092. for (i = 1; map && i < n; ++i)
  2093. map = isl_map_union(map,
  2094. isl_map_from_basic_map(basic_map_read_polylib(s)));
  2095. return map;
  2096. }
  2097. static int optional_power(__isl_keep isl_stream *s)
  2098. {
  2099. int pow;
  2100. struct isl_token *tok;
  2101. tok = isl_stream_next_token(s);
  2102. if (!tok)
  2103. return 1;
  2104. if (tok->type != '^') {
  2105. isl_stream_push_token(s, tok);
  2106. return 1;
  2107. }
  2108. isl_token_free(tok);
  2109. tok = isl_stream_next_token(s);
  2110. if (!tok || tok->type != ISL_TOKEN_VALUE) {
  2111. isl_stream_error(s, tok, "expecting exponent");
  2112. if (tok)
  2113. isl_stream_push_token(s, tok);
  2114. return 1;
  2115. }
  2116. pow = isl_int_get_si(tok->u.v);
  2117. isl_token_free(tok);
  2118. return pow;
  2119. }
  2120. static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
  2121. __isl_keep isl_map *map, struct vars *v);
  2122. static __isl_give isl_pw_qpolynomial *read_factor(__isl_keep isl_stream *s,
  2123. __isl_keep isl_map *map, struct vars *v)
  2124. {
  2125. isl_pw_qpolynomial *pwqp;
  2126. struct isl_token *tok;
  2127. tok = next_token(s);
  2128. if (!tok) {
  2129. isl_stream_error(s, NULL, "unexpected EOF");
  2130. return NULL;
  2131. }
  2132. if (tok->type == '(') {
  2133. int pow;
  2134. isl_token_free(tok);
  2135. pwqp = read_term(s, map, v);
  2136. if (!pwqp)
  2137. return NULL;
  2138. if (isl_stream_eat(s, ')'))
  2139. goto error;
  2140. pow = optional_power(s);
  2141. pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
  2142. } else if (tok->type == ISL_TOKEN_VALUE) {
  2143. struct isl_token *tok2;
  2144. isl_qpolynomial *qp;
  2145. tok2 = isl_stream_next_token(s);
  2146. if (tok2 && tok2->type == '/') {
  2147. isl_token_free(tok2);
  2148. tok2 = next_token(s);
  2149. if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
  2150. isl_stream_error(s, tok2, "expected denominator");
  2151. isl_token_free(tok);
  2152. isl_token_free(tok2);
  2153. return NULL;
  2154. }
  2155. qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
  2156. tok->u.v, tok2->u.v);
  2157. isl_token_free(tok2);
  2158. } else {
  2159. isl_stream_push_token(s, tok2);
  2160. qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
  2161. tok->u.v);
  2162. }
  2163. isl_token_free(tok);
  2164. pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
  2165. } else if (tok->type == ISL_TOKEN_INFTY) {
  2166. isl_qpolynomial *qp;
  2167. isl_token_free(tok);
  2168. qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
  2169. pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
  2170. } else if (tok->type == ISL_TOKEN_NAN) {
  2171. isl_qpolynomial *qp;
  2172. isl_token_free(tok);
  2173. qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
  2174. pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
  2175. } else if (tok->type == ISL_TOKEN_IDENT) {
  2176. int n = v->n;
  2177. int pos = vars_pos(v, tok->u.s, -1);
  2178. int pow;
  2179. isl_qpolynomial *qp;
  2180. if (pos < 0) {
  2181. isl_token_free(tok);
  2182. return NULL;
  2183. }
  2184. if (pos >= n) {
  2185. vars_drop(v, v->n - n);
  2186. isl_stream_error(s, tok, "unknown identifier");
  2187. isl_token_free(tok);
  2188. return NULL;
  2189. }
  2190. isl_token_free(tok);
  2191. pow = optional_power(s);
  2192. qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
  2193. pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
  2194. } else if (is_start_of_div(tok)) {
  2195. isl_pw_aff *pwaff;
  2196. int pow;
  2197. isl_stream_push_token(s, tok);
  2198. pwaff = accept_div(s, isl_map_get_space(map), v);
  2199. pow = optional_power(s);
  2200. pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
  2201. pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
  2202. } else if (tok->type == '-') {
  2203. isl_token_free(tok);
  2204. pwqp = read_factor(s, map, v);
  2205. pwqp = isl_pw_qpolynomial_neg(pwqp);
  2206. } else {
  2207. isl_stream_error(s, tok, "unexpected isl_token");
  2208. isl_stream_push_token(s, tok);
  2209. return NULL;
  2210. }
  2211. if (isl_stream_eat_if_available(s, '*') ||
  2212. isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
  2213. isl_pw_qpolynomial *pwqp2;
  2214. pwqp2 = read_factor(s, map, v);
  2215. pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
  2216. }
  2217. return pwqp;
  2218. error:
  2219. isl_pw_qpolynomial_free(pwqp);
  2220. return NULL;
  2221. }
  2222. static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
  2223. __isl_keep isl_map *map, struct vars *v)
  2224. {
  2225. struct isl_token *tok;
  2226. isl_pw_qpolynomial *pwqp;
  2227. pwqp = read_factor(s, map, v);
  2228. for (;;) {
  2229. tok = next_token(s);
  2230. if (!tok)
  2231. return pwqp;
  2232. if (tok->type == '+') {
  2233. isl_pw_qpolynomial *pwqp2;
  2234. isl_token_free(tok);
  2235. pwqp2 = read_factor(s, map, v);
  2236. pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
  2237. } else if (tok->type == '-') {
  2238. isl_pw_qpolynomial *pwqp2;
  2239. isl_token_free(tok);
  2240. pwqp2 = read_factor(s, map, v);
  2241. pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
  2242. } else if (tok->type == ISL_TOKEN_VALUE &&
  2243. isl_int_is_neg(tok->u.v)) {
  2244. isl_pw_qpolynomial *pwqp2;
  2245. isl_stream_push_token(s, tok);
  2246. pwqp2 = read_factor(s, map, v);
  2247. pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
  2248. } else {
  2249. isl_stream_push_token(s, tok);
  2250. break;
  2251. }
  2252. }
  2253. return pwqp;
  2254. }
  2255. static __isl_give isl_map *read_optional_formula(__isl_keep isl_stream *s,
  2256. __isl_take isl_map *map, struct vars *v, int rational)
  2257. {
  2258. struct isl_token *tok;
  2259. tok = isl_stream_next_token(s);
  2260. if (!tok) {
  2261. isl_stream_error(s, NULL, "unexpected EOF");
  2262. goto error;
  2263. }
  2264. if (tok->type == ':' ||
  2265. (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
  2266. isl_token_free(tok);
  2267. map = read_formula(s, v, map, rational);
  2268. } else
  2269. isl_stream_push_token(s, tok);
  2270. return map;
  2271. error:
  2272. isl_map_free(map);
  2273. return NULL;
  2274. }
  2275. static struct isl_obj obj_read_poly(__isl_keep isl_stream *s,
  2276. __isl_take isl_map *map, struct vars *v, int n)
  2277. {
  2278. struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
  2279. isl_pw_qpolynomial *pwqp;
  2280. struct isl_set *set;
  2281. pwqp = read_term(s, map, v);
  2282. map = read_optional_formula(s, map, v, 0);
  2283. set = isl_map_range(map);
  2284. pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
  2285. vars_drop(v, v->n - n);
  2286. obj.v = pwqp;
  2287. return obj;
  2288. }
  2289. static struct isl_obj obj_read_poly_or_fold(__isl_keep isl_stream *s,
  2290. __isl_take isl_set *set, struct vars *v, int n)
  2291. {
  2292. int min, max;
  2293. struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
  2294. isl_pw_qpolynomial *pwqp;
  2295. isl_pw_qpolynomial_fold *pwf = NULL;
  2296. enum isl_fold fold;
  2297. max = isl_stream_eat_if_available(s, ISL_TOKEN_MAX);
  2298. min = !max && isl_stream_eat_if_available(s, ISL_TOKEN_MIN);
  2299. if (!min && !max)
  2300. return obj_read_poly(s, set, v, n);
  2301. fold = max ? isl_fold_max : isl_fold_min;
  2302. if (isl_stream_eat(s, '('))
  2303. goto error;
  2304. pwqp = read_term(s, set, v);
  2305. pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(fold, pwqp);
  2306. while (isl_stream_eat_if_available(s, ',')) {
  2307. isl_pw_qpolynomial_fold *pwf_i;
  2308. pwqp = read_term(s, set, v);
  2309. pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(fold, pwqp);
  2310. pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
  2311. }
  2312. if (isl_stream_eat(s, ')'))
  2313. goto error;
  2314. set = read_optional_formula(s, set, v, 0);
  2315. pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
  2316. vars_drop(v, v->n - n);
  2317. obj.v = pwf;
  2318. return obj;
  2319. error:
  2320. isl_set_free(set);
  2321. isl_pw_qpolynomial_fold_free(pwf);
  2322. obj.type = isl_obj_none;
  2323. return obj;
  2324. }
  2325. static int is_rational(__isl_keep isl_stream *s)
  2326. {
  2327. struct isl_token *tok;
  2328. tok = isl_stream_next_token(s);
  2329. if (!tok)
  2330. return 0;
  2331. if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
  2332. isl_token_free(tok);
  2333. isl_stream_eat(s, ':');
  2334. return 1;
  2335. }
  2336. isl_stream_push_token(s, tok);
  2337. return 0;
  2338. }
  2339. static struct isl_obj obj_read_body(__isl_keep isl_stream *s,
  2340. __isl_take isl_map *map, struct vars *v)
  2341. {
  2342. struct isl_token *tok;
  2343. struct isl_obj obj = { isl_obj_set, NULL };
  2344. int n = v->n;
  2345. int rational;
  2346. rational = is_rational(s);
  2347. if (rational)
  2348. map = isl_map_set_rational(map);
  2349. if (isl_stream_next_token_is(s, ':')) {
  2350. obj.type = isl_obj_set;
  2351. obj.v = read_optional_formula(s, map, v, rational);
  2352. return obj;
  2353. }
  2354. if (!next_is_tuple(s))
  2355. return obj_read_poly_or_fold(s, map, v, n);
  2356. map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
  2357. if (!map)
  2358. goto error;
  2359. tok = isl_stream_next_token(s);
  2360. if (!tok)
  2361. goto error;
  2362. if (tok->type == ISL_TOKEN_TO) {
  2363. obj.type = isl_obj_map;
  2364. isl_token_free(tok);
  2365. if (!next_is_tuple(s)) {
  2366. isl_set *set = isl_map_domain(map);
  2367. return obj_read_poly_or_fold(s, set, v, n);
  2368. }
  2369. map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
  2370. if (!map)
  2371. goto error;
  2372. } else {
  2373. map = isl_map_domain(map);
  2374. isl_stream_push_token(s, tok);
  2375. }
  2376. map = read_optional_formula(s, map, v, rational);
  2377. vars_drop(v, v->n - n);
  2378. obj.v = map;
  2379. return obj;
  2380. error:
  2381. isl_map_free(map);
  2382. obj.type = isl_obj_none;
  2383. return obj;
  2384. }
  2385. static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
  2386. {
  2387. if (obj.type == isl_obj_map) {
  2388. obj.v = isl_union_map_from_map(obj.v);
  2389. obj.type = isl_obj_union_map;
  2390. } else if (obj.type == isl_obj_set) {
  2391. obj.v = isl_union_set_from_set(obj.v);
  2392. obj.type = isl_obj_union_set;
  2393. } else if (obj.type == isl_obj_pw_qpolynomial) {
  2394. obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
  2395. obj.type = isl_obj_union_pw_qpolynomial;
  2396. } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
  2397. obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
  2398. obj.type = isl_obj_union_pw_qpolynomial_fold;
  2399. } else
  2400. isl_assert(ctx, 0, goto error);
  2401. return obj;
  2402. error:
  2403. obj.type->free(obj.v);
  2404. obj.type = isl_obj_none;
  2405. return obj;
  2406. }
  2407. static struct isl_obj obj_add(__isl_keep isl_stream *s,
  2408. struct isl_obj obj1, struct isl_obj obj2)
  2409. {
  2410. if (obj2.type == isl_obj_none || !obj2.v)
  2411. goto error;
  2412. if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
  2413. obj1 = to_union(s->ctx, obj1);
  2414. if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
  2415. obj2 = to_union(s->ctx, obj2);
  2416. if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
  2417. obj1 = to_union(s->ctx, obj1);
  2418. if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
  2419. obj2 = to_union(s->ctx, obj2);
  2420. if (obj1.type == isl_obj_pw_qpolynomial &&
  2421. obj2.type == isl_obj_union_pw_qpolynomial)
  2422. obj1 = to_union(s->ctx, obj1);
  2423. if (obj1.type == isl_obj_union_pw_qpolynomial &&
  2424. obj2.type == isl_obj_pw_qpolynomial)
  2425. obj2 = to_union(s->ctx, obj2);
  2426. if (obj1.type == isl_obj_pw_qpolynomial_fold &&
  2427. obj2.type == isl_obj_union_pw_qpolynomial_fold)
  2428. obj1 = to_union(s->ctx, obj1);
  2429. if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
  2430. obj2.type == isl_obj_pw_qpolynomial_fold)
  2431. obj2 = to_union(s->ctx, obj2);
  2432. if (obj1.type != obj2.type) {
  2433. isl_stream_error(s, NULL,
  2434. "attempt to combine incompatible objects");
  2435. goto error;
  2436. }
  2437. if (!obj1.type->add)
  2438. isl_die(s->ctx, isl_error_internal,
  2439. "combination not supported on object type", goto error);
  2440. if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
  2441. obj1 = to_union(s->ctx, obj1);
  2442. obj2 = to_union(s->ctx, obj2);
  2443. }
  2444. if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
  2445. obj1 = to_union(s->ctx, obj1);
  2446. obj2 = to_union(s->ctx, obj2);
  2447. }
  2448. if (obj1.type == isl_obj_pw_qpolynomial &&
  2449. !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
  2450. obj1 = to_union(s->ctx, obj1);
  2451. obj2 = to_union(s->ctx, obj2);
  2452. }
  2453. if (obj1.type == isl_obj_pw_qpolynomial_fold &&
  2454. !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
  2455. obj1 = to_union(s->ctx, obj1);
  2456. obj2 = to_union(s->ctx, obj2);
  2457. }
  2458. obj1.v = obj1.type->add(obj1.v, obj2.v);
  2459. return obj1;
  2460. error:
  2461. obj1.type->free(obj1.v);
  2462. obj2.type->free(obj2.v);
  2463. obj1.type = isl_obj_none;
  2464. obj1.v = NULL;
  2465. return obj1;
  2466. }
  2467. /* Are the first two tokens on "s", "domain" (either as a string
  2468. * or as an identifier) followed by ":"?
  2469. */
  2470. static int next_is_domain_colon(__isl_keep isl_stream *s)
  2471. {
  2472. struct isl_token *tok;
  2473. char *name;
  2474. int res;
  2475. tok = isl_stream_next_token(s);
  2476. if (!tok)
  2477. return 0;
  2478. if (tok->type != ISL_TOKEN_IDENT && tok->type != ISL_TOKEN_STRING) {
  2479. isl_stream_push_token(s, tok);
  2480. return 0;
  2481. }
  2482. name = isl_token_get_str(s->ctx, tok);
  2483. res = !strcmp(name, "domain") && isl_stream_next_token_is(s, ':');
  2484. free(name);
  2485. isl_stream_push_token(s, tok);
  2486. return res;
  2487. }
  2488. /* Do the first tokens on "s" look like a schedule?
  2489. *
  2490. * The root of a schedule is always a domain node, so the first thing
  2491. * we expect in the stream is a domain key, i.e., "domain" followed
  2492. * by ":". If the schedule was printed in YAML flow style, then
  2493. * we additionally expect a "{" to open the outer mapping.
  2494. */
  2495. static int next_is_schedule(__isl_keep isl_stream *s)
  2496. {
  2497. struct isl_token *tok;
  2498. int is_schedule;
  2499. tok = isl_stream_next_token(s);
  2500. if (!tok)
  2501. return 0;
  2502. if (tok->type != '{') {
  2503. isl_stream_push_token(s, tok);
  2504. return next_is_domain_colon(s);
  2505. }
  2506. is_schedule = next_is_domain_colon(s);
  2507. isl_stream_push_token(s, tok);
  2508. return is_schedule;
  2509. }
  2510. /* Read an isl_schedule from "s" and store it in an isl_obj.
  2511. */
  2512. static struct isl_obj schedule_read(__isl_keep isl_stream *s)
  2513. {
  2514. struct isl_obj obj;
  2515. obj.type = isl_obj_schedule;
  2516. obj.v = isl_stream_read_schedule(s);
  2517. return obj;
  2518. }
  2519. /* Read a disjunction of object bodies from "s".
  2520. * That is, read the inside of the braces, but not the braces themselves.
  2521. * "v" contains a description of the identifiers parsed so far.
  2522. * "map" contains information about the parameters.
  2523. */
  2524. static struct isl_obj obj_read_disjuncts(__isl_keep isl_stream *s,
  2525. struct vars *v, __isl_keep isl_map *map)
  2526. {
  2527. struct isl_obj obj = { isl_obj_set, NULL };
  2528. if (isl_stream_next_token_is(s, '}')) {
  2529. obj.type = isl_obj_union_set;
  2530. obj.v = isl_union_set_empty(isl_map_get_space(map));
  2531. return obj;
  2532. }
  2533. for (;;) {
  2534. struct isl_obj o;
  2535. o = obj_read_body(s, isl_map_copy(map), v);
  2536. if (!obj.v)
  2537. obj = o;
  2538. else
  2539. obj = obj_add(s, obj, o);
  2540. if (obj.type == isl_obj_none || !obj.v)
  2541. return obj;
  2542. if (!isl_stream_eat_if_available(s, ';'))
  2543. break;
  2544. if (isl_stream_next_token_is(s, '}'))
  2545. break;
  2546. }
  2547. return obj;
  2548. }
  2549. static struct isl_obj obj_read(__isl_keep isl_stream *s)
  2550. {
  2551. isl_map *map = NULL;
  2552. struct isl_token *tok;
  2553. struct vars *v = NULL;
  2554. struct isl_obj obj = { isl_obj_set, NULL };
  2555. if (next_is_schedule(s))
  2556. return schedule_read(s);
  2557. tok = next_token(s);
  2558. if (!tok) {
  2559. isl_stream_error(s, NULL, "unexpected EOF");
  2560. goto error;
  2561. }
  2562. if (tok->type == ISL_TOKEN_VALUE) {
  2563. struct isl_token *tok2;
  2564. struct isl_map *map;
  2565. tok2 = isl_stream_next_token(s);
  2566. if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
  2567. isl_int_is_neg(tok2->u.v)) {
  2568. if (tok2)
  2569. isl_stream_push_token(s, tok2);
  2570. obj.type = isl_obj_val;
  2571. obj.v = isl_val_int_from_isl_int(s->ctx, tok->u.v);
  2572. isl_token_free(tok);
  2573. return obj;
  2574. }
  2575. isl_stream_push_token(s, tok2);
  2576. isl_stream_push_token(s, tok);
  2577. map = map_read_polylib(s);
  2578. if (!map)
  2579. goto error;
  2580. if (isl_map_may_be_set(map))
  2581. obj.v = isl_map_range(map);
  2582. else {
  2583. obj.type = isl_obj_map;
  2584. obj.v = map;
  2585. }
  2586. return obj;
  2587. }
  2588. v = vars_new(s->ctx);
  2589. if (!v) {
  2590. isl_stream_push_token(s, tok);
  2591. goto error;
  2592. }
  2593. map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
  2594. if (tok->type == '[') {
  2595. isl_stream_push_token(s, tok);
  2596. map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
  2597. if (!map)
  2598. goto error;
  2599. tok = isl_stream_next_token(s);
  2600. if (!tok || tok->type != ISL_TOKEN_TO) {
  2601. isl_stream_error(s, tok, "expecting '->'");
  2602. if (tok)
  2603. isl_stream_push_token(s, tok);
  2604. goto error;
  2605. }
  2606. isl_token_free(tok);
  2607. tok = isl_stream_next_token(s);
  2608. }
  2609. if (!tok || tok->type != '{') {
  2610. isl_stream_error(s, tok, "expecting '{'");
  2611. if (tok)
  2612. isl_stream_push_token(s, tok);
  2613. goto error;
  2614. }
  2615. isl_token_free(tok);
  2616. tok = isl_stream_next_token(s);
  2617. if (!tok)
  2618. ;
  2619. else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
  2620. isl_token_free(tok);
  2621. if (isl_stream_eat(s, '='))
  2622. goto error;
  2623. map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
  2624. if (!map)
  2625. goto error;
  2626. } else
  2627. isl_stream_push_token(s, tok);
  2628. obj = obj_read_disjuncts(s, v, map);
  2629. if (obj.type == isl_obj_none || !obj.v)
  2630. goto error;
  2631. tok = isl_stream_next_token(s);
  2632. if (tok && tok->type == '}') {
  2633. isl_token_free(tok);
  2634. } else {
  2635. isl_stream_error(s, tok, "unexpected isl_token");
  2636. if (tok)
  2637. isl_token_free(tok);
  2638. goto error;
  2639. }
  2640. vars_free(v);
  2641. isl_map_free(map);
  2642. return obj;
  2643. error:
  2644. isl_map_free(map);
  2645. obj.type->free(obj.v);
  2646. if (v)
  2647. vars_free(v);
  2648. obj.v = NULL;
  2649. return obj;
  2650. }
  2651. struct isl_obj isl_stream_read_obj(__isl_keep isl_stream *s)
  2652. {
  2653. return obj_read(s);
  2654. }
  2655. __isl_give isl_map *isl_stream_read_map(__isl_keep isl_stream *s)
  2656. {
  2657. struct isl_obj obj;
  2658. obj = obj_read(s);
  2659. if (obj.v)
  2660. isl_assert(s->ctx, obj.type == isl_obj_map ||
  2661. obj.type == isl_obj_set, goto error);
  2662. if (obj.type == isl_obj_set)
  2663. obj.v = isl_map_from_range(obj.v);
  2664. return obj.v;
  2665. error:
  2666. obj.type->free(obj.v);
  2667. return NULL;
  2668. }
  2669. __isl_give isl_set *isl_stream_read_set(__isl_keep isl_stream *s)
  2670. {
  2671. struct isl_obj obj;
  2672. obj = obj_read(s);
  2673. if (obj.v) {
  2674. if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
  2675. obj.v = isl_map_range(obj.v);
  2676. obj.type = isl_obj_set;
  2677. }
  2678. isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
  2679. }
  2680. return obj.v;
  2681. error:
  2682. obj.type->free(obj.v);
  2683. return NULL;
  2684. }
  2685. __isl_give isl_union_map *isl_stream_read_union_map(__isl_keep isl_stream *s)
  2686. {
  2687. struct isl_obj obj;
  2688. obj = obj_read(s);
  2689. if (obj.type == isl_obj_map) {
  2690. obj.type = isl_obj_union_map;
  2691. obj.v = isl_union_map_from_map(obj.v);
  2692. }
  2693. if (obj.type == isl_obj_set) {
  2694. obj.type = isl_obj_union_set;
  2695. obj.v = isl_union_set_from_set(obj.v);
  2696. }
  2697. if (obj.v && obj.type == isl_obj_union_set &&
  2698. isl_union_set_is_empty(obj.v))
  2699. obj.type = isl_obj_union_map;
  2700. if (obj.v && obj.type != isl_obj_union_map)
  2701. isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
  2702. return obj.v;
  2703. error:
  2704. obj.type->free(obj.v);
  2705. return NULL;
  2706. }
  2707. /* Extract an isl_union_set from "obj".
  2708. * This only works if the object was detected as either a set
  2709. * (in which case it is converted to a union set) or a union set.
  2710. */
  2711. static __isl_give isl_union_set *extract_union_set(isl_ctx *ctx,
  2712. struct isl_obj obj)
  2713. {
  2714. if (obj.type == isl_obj_set) {
  2715. obj.type = isl_obj_union_set;
  2716. obj.v = isl_union_set_from_set(obj.v);
  2717. }
  2718. if (obj.v)
  2719. isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
  2720. return obj.v;
  2721. error:
  2722. obj.type->free(obj.v);
  2723. return NULL;
  2724. }
  2725. /* Read an isl_union_set from "s".
  2726. * First read a generic object and then try and extract
  2727. * an isl_union_set from that.
  2728. */
  2729. __isl_give isl_union_set *isl_stream_read_union_set(__isl_keep isl_stream *s)
  2730. {
  2731. struct isl_obj obj;
  2732. obj = obj_read(s);
  2733. return extract_union_set(s->ctx, obj);
  2734. }
  2735. static __isl_give isl_basic_map *basic_map_read(__isl_keep isl_stream *s)
  2736. {
  2737. struct isl_obj obj;
  2738. struct isl_map *map;
  2739. struct isl_basic_map *bmap;
  2740. obj = obj_read(s);
  2741. if (obj.v && (obj.type != isl_obj_map && obj.type != isl_obj_set))
  2742. isl_die(s->ctx, isl_error_invalid, "not a (basic) set or map",
  2743. goto error);
  2744. map = obj.v;
  2745. if (!map)
  2746. return NULL;
  2747. if (map->n > 1)
  2748. isl_die(s->ctx, isl_error_invalid,
  2749. "set or map description involves "
  2750. "more than one disjunct", goto error);
  2751. if (map->n == 0)
  2752. bmap = isl_basic_map_empty(isl_map_get_space(map));
  2753. else
  2754. bmap = isl_basic_map_copy(map->p[0]);
  2755. isl_map_free(map);
  2756. return bmap;
  2757. error:
  2758. obj.type->free(obj.v);
  2759. return NULL;
  2760. }
  2761. static __isl_give isl_basic_set *basic_set_read(__isl_keep isl_stream *s)
  2762. {
  2763. isl_basic_map *bmap;
  2764. bmap = basic_map_read(s);
  2765. if (!bmap)
  2766. return NULL;
  2767. if (!isl_basic_map_may_be_set(bmap))
  2768. isl_die(s->ctx, isl_error_invalid,
  2769. "input is not a set", goto error);
  2770. return isl_basic_map_range(bmap);
  2771. error:
  2772. isl_basic_map_free(bmap);
  2773. return NULL;
  2774. }
  2775. __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
  2776. FILE *input)
  2777. {
  2778. struct isl_basic_map *bmap;
  2779. isl_stream *s = isl_stream_new_file(ctx, input);
  2780. if (!s)
  2781. return NULL;
  2782. bmap = basic_map_read(s);
  2783. isl_stream_free(s);
  2784. return bmap;
  2785. }
  2786. __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
  2787. FILE *input)
  2788. {
  2789. isl_basic_set *bset;
  2790. isl_stream *s = isl_stream_new_file(ctx, input);
  2791. if (!s)
  2792. return NULL;
  2793. bset = basic_set_read(s);
  2794. isl_stream_free(s);
  2795. return bset;
  2796. }
  2797. __isl_give isl_basic_map *isl_basic_map_read_from_str(isl_ctx *ctx,
  2798. const char *str)
  2799. {
  2800. struct isl_basic_map *bmap;
  2801. isl_stream *s = isl_stream_new_str(ctx, str);
  2802. if (!s)
  2803. return NULL;
  2804. bmap = basic_map_read(s);
  2805. isl_stream_free(s);
  2806. return bmap;
  2807. }
  2808. __isl_give isl_basic_set *isl_basic_set_read_from_str(isl_ctx *ctx,
  2809. const char *str)
  2810. {
  2811. isl_basic_set *bset;
  2812. isl_stream *s = isl_stream_new_str(ctx, str);
  2813. if (!s)
  2814. return NULL;
  2815. bset = basic_set_read(s);
  2816. isl_stream_free(s);
  2817. return bset;
  2818. }
  2819. __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
  2820. FILE *input)
  2821. {
  2822. struct isl_map *map;
  2823. isl_stream *s = isl_stream_new_file(ctx, input);
  2824. if (!s)
  2825. return NULL;
  2826. map = isl_stream_read_map(s);
  2827. isl_stream_free(s);
  2828. return map;
  2829. }
  2830. __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
  2831. const char *str)
  2832. {
  2833. struct isl_map *map;
  2834. isl_stream *s = isl_stream_new_str(ctx, str);
  2835. if (!s)
  2836. return NULL;
  2837. map = isl_stream_read_map(s);
  2838. isl_stream_free(s);
  2839. return map;
  2840. }
  2841. __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
  2842. FILE *input)
  2843. {
  2844. isl_set *set;
  2845. isl_stream *s = isl_stream_new_file(ctx, input);
  2846. if (!s)
  2847. return NULL;
  2848. set = isl_stream_read_set(s);
  2849. isl_stream_free(s);
  2850. return set;
  2851. }
  2852. __isl_give isl_set *isl_set_read_from_str(isl_ctx *ctx, const char *str)
  2853. {
  2854. isl_set *set;
  2855. isl_stream *s = isl_stream_new_str(ctx, str);
  2856. if (!s)
  2857. return NULL;
  2858. set = isl_stream_read_set(s);
  2859. isl_stream_free(s);
  2860. return set;
  2861. }
  2862. __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
  2863. FILE *input)
  2864. {
  2865. isl_union_map *umap;
  2866. isl_stream *s = isl_stream_new_file(ctx, input);
  2867. if (!s)
  2868. return NULL;
  2869. umap = isl_stream_read_union_map(s);
  2870. isl_stream_free(s);
  2871. return umap;
  2872. }
  2873. __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
  2874. const char *str)
  2875. {
  2876. isl_union_map *umap;
  2877. isl_stream *s = isl_stream_new_str(ctx, str);
  2878. if (!s)
  2879. return NULL;
  2880. umap = isl_stream_read_union_map(s);
  2881. isl_stream_free(s);
  2882. return umap;
  2883. }
  2884. __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
  2885. FILE *input)
  2886. {
  2887. isl_union_set *uset;
  2888. isl_stream *s = isl_stream_new_file(ctx, input);
  2889. if (!s)
  2890. return NULL;
  2891. uset = isl_stream_read_union_set(s);
  2892. isl_stream_free(s);
  2893. return uset;
  2894. }
  2895. __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
  2896. const char *str)
  2897. {
  2898. isl_union_set *uset;
  2899. isl_stream *s = isl_stream_new_str(ctx, str);
  2900. if (!s)
  2901. return NULL;
  2902. uset = isl_stream_read_union_set(s);
  2903. isl_stream_free(s);
  2904. return uset;
  2905. }
  2906. static __isl_give isl_vec *isl_vec_read_polylib(__isl_keep isl_stream *s)
  2907. {
  2908. struct isl_vec *vec = NULL;
  2909. struct isl_token *tok;
  2910. unsigned size;
  2911. int j;
  2912. tok = isl_stream_next_token(s);
  2913. if (!tok || tok->type != ISL_TOKEN_VALUE) {
  2914. isl_stream_error(s, tok, "expecting vector length");
  2915. goto error;
  2916. }
  2917. size = isl_int_get_si(tok->u.v);
  2918. isl_token_free(tok);
  2919. vec = isl_vec_alloc(s->ctx, size);
  2920. for (j = 0; j < size; ++j) {
  2921. tok = isl_stream_next_token(s);
  2922. if (!tok || tok->type != ISL_TOKEN_VALUE) {
  2923. isl_stream_error(s, tok, "expecting constant value");
  2924. goto error;
  2925. }
  2926. isl_int_set(vec->el[j], tok->u.v);
  2927. isl_token_free(tok);
  2928. }
  2929. return vec;
  2930. error:
  2931. isl_token_free(tok);
  2932. isl_vec_free(vec);
  2933. return NULL;
  2934. }
  2935. static __isl_give isl_vec *vec_read(__isl_keep isl_stream *s)
  2936. {
  2937. return isl_vec_read_polylib(s);
  2938. }
  2939. __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
  2940. {
  2941. isl_vec *v;
  2942. isl_stream *s = isl_stream_new_file(ctx, input);
  2943. if (!s)
  2944. return NULL;
  2945. v = vec_read(s);
  2946. isl_stream_free(s);
  2947. return v;
  2948. }
  2949. __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
  2950. __isl_keep isl_stream *s)
  2951. {
  2952. struct isl_obj obj;
  2953. obj = obj_read(s);
  2954. if (obj.v)
  2955. isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
  2956. goto error);
  2957. return obj.v;
  2958. error:
  2959. obj.type->free(obj.v);
  2960. return NULL;
  2961. }
  2962. __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
  2963. const char *str)
  2964. {
  2965. isl_pw_qpolynomial *pwqp;
  2966. isl_stream *s = isl_stream_new_str(ctx, str);
  2967. if (!s)
  2968. return NULL;
  2969. pwqp = isl_stream_read_pw_qpolynomial(s);
  2970. isl_stream_free(s);
  2971. return pwqp;
  2972. }
  2973. __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
  2974. FILE *input)
  2975. {
  2976. isl_pw_qpolynomial *pwqp;
  2977. isl_stream *s = isl_stream_new_file(ctx, input);
  2978. if (!s)
  2979. return NULL;
  2980. pwqp = isl_stream_read_pw_qpolynomial(s);
  2981. isl_stream_free(s);
  2982. return pwqp;
  2983. }
  2984. /* Read an isl_pw_qpolynomial_fold from "s".
  2985. * First read a generic object and
  2986. * then check that it is an isl_pw_qpolynomial_fold.
  2987. */
  2988. __isl_give isl_pw_qpolynomial_fold *isl_stream_read_pw_qpolynomial_fold(
  2989. __isl_keep isl_stream *s)
  2990. {
  2991. struct isl_obj obj;
  2992. obj = obj_read(s);
  2993. if (obj.v && obj.type != isl_obj_pw_qpolynomial_fold)
  2994. isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
  2995. return obj.v;
  2996. error:
  2997. obj.type->free(obj.v);
  2998. return NULL;
  2999. }
  3000. /* Read an isl_pw_qpolynomial_fold from "str".
  3001. */
  3002. __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_read_from_str(
  3003. isl_ctx *ctx, const char *str)
  3004. {
  3005. isl_pw_qpolynomial_fold *pwqp;
  3006. isl_stream *s;
  3007. s = isl_stream_new_str(ctx, str);
  3008. if (!s)
  3009. return NULL;
  3010. pwqp = isl_stream_read_pw_qpolynomial_fold(s);
  3011. isl_stream_free(s);
  3012. return pwqp;
  3013. }
  3014. /* Is the next token an identifier not in "v"?
  3015. */
  3016. static int next_is_fresh_ident(__isl_keep isl_stream *s, struct vars *v)
  3017. {
  3018. int n = v->n;
  3019. int fresh;
  3020. struct isl_token *tok;
  3021. tok = isl_stream_next_token(s);
  3022. if (!tok)
  3023. return 0;
  3024. fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
  3025. isl_stream_push_token(s, tok);
  3026. vars_drop(v, v->n - n);
  3027. return fresh;
  3028. }
  3029. /* First read the domain of the affine expression, which may be
  3030. * a parameter space or a set.
  3031. * The tricky part is that we don't know if the domain is a set or not,
  3032. * so when we are trying to read the domain, we may actually be reading
  3033. * the affine expression itself (defined on a parameter domains)
  3034. * If the tuple we are reading is named, we assume it's the domain.
  3035. * Also, if inside the tuple, the first thing we find is a nested tuple
  3036. * or a new identifier, we again assume it's the domain.
  3037. * Finally, if the tuple is empty, then it must be the domain
  3038. * since it does not contain an affine expression.
  3039. * Otherwise, we assume we are reading an affine expression.
  3040. */
  3041. static __isl_give isl_set *read_aff_domain(__isl_keep isl_stream *s,
  3042. __isl_take isl_set *dom, struct vars *v)
  3043. {
  3044. struct isl_token *tok, *tok2;
  3045. int is_empty;
  3046. tok = isl_stream_next_token(s);
  3047. if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
  3048. isl_stream_push_token(s, tok);
  3049. return read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
  3050. }
  3051. if (!tok || tok->type != '[') {
  3052. isl_stream_error(s, tok, "expecting '['");
  3053. goto error;
  3054. }
  3055. tok2 = isl_stream_next_token(s);
  3056. is_empty = tok2 && tok2->type == ']';
  3057. if (tok2)
  3058. isl_stream_push_token(s, tok2);
  3059. if (is_empty || next_is_tuple(s) || next_is_fresh_ident(s, v)) {
  3060. isl_stream_push_token(s, tok);
  3061. dom = read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
  3062. } else
  3063. isl_stream_push_token(s, tok);
  3064. return dom;
  3065. error:
  3066. if (tok)
  3067. isl_stream_push_token(s, tok);
  3068. isl_set_free(dom);
  3069. return NULL;
  3070. }
  3071. /* Read an affine expression from "s".
  3072. */
  3073. __isl_give isl_aff *isl_stream_read_aff(__isl_keep isl_stream *s)
  3074. {
  3075. isl_aff *aff;
  3076. isl_multi_aff *ma;
  3077. isl_size dim;
  3078. ma = isl_stream_read_multi_aff(s);
  3079. dim = isl_multi_aff_dim(ma, isl_dim_out);
  3080. if (dim < 0)
  3081. goto error;
  3082. if (dim != 1)
  3083. isl_die(s->ctx, isl_error_invalid,
  3084. "expecting single affine expression",
  3085. goto error);
  3086. aff = isl_multi_aff_get_aff(ma, 0);
  3087. isl_multi_aff_free(ma);
  3088. return aff;
  3089. error:
  3090. isl_multi_aff_free(ma);
  3091. return NULL;
  3092. }
  3093. /* Read a piecewise affine expression from "s" with domain (space) "dom".
  3094. */
  3095. static __isl_give isl_pw_aff *read_pw_aff_with_dom(__isl_keep isl_stream *s,
  3096. __isl_take isl_set *dom, struct vars *v)
  3097. {
  3098. isl_pw_aff *pwaff = NULL;
  3099. if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
  3100. goto error;
  3101. if (isl_stream_eat(s, '['))
  3102. goto error;
  3103. pwaff = accept_affine(s, isl_set_get_space(dom), v);
  3104. if (isl_stream_eat(s, ']'))
  3105. goto error;
  3106. dom = read_optional_formula(s, dom, v, 0);
  3107. pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
  3108. return pwaff;
  3109. error:
  3110. isl_set_free(dom);
  3111. isl_pw_aff_free(pwaff);
  3112. return NULL;
  3113. }
  3114. __isl_give isl_pw_aff *isl_stream_read_pw_aff(__isl_keep isl_stream *s)
  3115. {
  3116. struct vars *v;
  3117. isl_set *dom = NULL;
  3118. isl_set *aff_dom;
  3119. isl_pw_aff *pa = NULL;
  3120. int n;
  3121. v = vars_new(s->ctx);
  3122. if (!v)
  3123. return NULL;
  3124. dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
  3125. if (next_is_tuple(s)) {
  3126. dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
  3127. if (isl_stream_eat(s, ISL_TOKEN_TO))
  3128. goto error;
  3129. }
  3130. if (isl_stream_eat(s, '{'))
  3131. goto error;
  3132. n = v->n;
  3133. aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
  3134. pa = read_pw_aff_with_dom(s, aff_dom, v);
  3135. vars_drop(v, v->n - n);
  3136. while (isl_stream_eat_if_available(s, ';')) {
  3137. isl_pw_aff *pa_i;
  3138. n = v->n;
  3139. aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
  3140. pa_i = read_pw_aff_with_dom(s, aff_dom, v);
  3141. vars_drop(v, v->n - n);
  3142. pa = isl_pw_aff_union_add(pa, pa_i);
  3143. }
  3144. if (isl_stream_eat(s, '}'))
  3145. goto error;
  3146. vars_free(v);
  3147. isl_set_free(dom);
  3148. return pa;
  3149. error:
  3150. vars_free(v);
  3151. isl_set_free(dom);
  3152. isl_pw_aff_free(pa);
  3153. return NULL;
  3154. }
  3155. __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
  3156. {
  3157. isl_aff *aff;
  3158. isl_stream *s = isl_stream_new_str(ctx, str);
  3159. if (!s)
  3160. return NULL;
  3161. aff = isl_stream_read_aff(s);
  3162. isl_stream_free(s);
  3163. return aff;
  3164. }
  3165. __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
  3166. {
  3167. isl_pw_aff *pa;
  3168. isl_stream *s = isl_stream_new_str(ctx, str);
  3169. if (!s)
  3170. return NULL;
  3171. pa = isl_stream_read_pw_aff(s);
  3172. isl_stream_free(s);
  3173. return pa;
  3174. }
  3175. /* Extract an isl_multi_pw_aff with domain space "dom_space"
  3176. * from a tuple "tuple" read by read_tuple.
  3177. *
  3178. * Note that the function read_tuple accepts tuples where some output or
  3179. * set dimensions are defined in terms of other output or set dimensions
  3180. * since this function is also used to read maps. As a special case,
  3181. * read_tuple also accept dimensions that are defined in terms of themselves
  3182. * (i.e., that are not defined).
  3183. * These cases are not allowed when extracting an isl_multi_pw_aff so check
  3184. * that the definitions of the output/set dimensions do not involve any
  3185. * output/set dimensions.
  3186. * Finally, drop the output dimensions from the domain of the result
  3187. * of read_tuple (which is of the form [input, output] -> [output],
  3188. * with anonymous domain) and reset the space.
  3189. */
  3190. static __isl_give isl_multi_pw_aff *extract_mpa_from_tuple(
  3191. __isl_take isl_space *dom_space, __isl_keep isl_multi_pw_aff *tuple)
  3192. {
  3193. int i;
  3194. isl_size dim, n;
  3195. isl_space *space;
  3196. isl_multi_pw_aff *mpa;
  3197. n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
  3198. dim = isl_space_dim(dom_space, isl_dim_all);
  3199. if (n < 0 || dim < 0)
  3200. dom_space = isl_space_free(dom_space);
  3201. space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
  3202. space = isl_space_align_params(space, isl_space_copy(dom_space));
  3203. if (!isl_space_is_params(dom_space))
  3204. space = isl_space_map_from_domain_and_range(
  3205. isl_space_copy(dom_space), space);
  3206. isl_space_free(dom_space);
  3207. mpa = isl_multi_pw_aff_alloc(space);
  3208. for (i = 0; i < n; ++i) {
  3209. isl_pw_aff *pa;
  3210. pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
  3211. if (!pa)
  3212. return isl_multi_pw_aff_free(mpa);
  3213. if (isl_pw_aff_involves_dims(pa, isl_dim_in, dim, i + 1)) {
  3214. isl_ctx *ctx = isl_pw_aff_get_ctx(pa);
  3215. isl_pw_aff_free(pa);
  3216. isl_die(ctx, isl_error_invalid,
  3217. "not an affine expression",
  3218. return isl_multi_pw_aff_free(mpa));
  3219. }
  3220. pa = isl_pw_aff_drop_dims(pa, isl_dim_in, dim, n);
  3221. space = isl_multi_pw_aff_get_domain_space(mpa);
  3222. pa = isl_pw_aff_reset_domain_space(pa, space);
  3223. mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
  3224. }
  3225. return mpa;
  3226. }
  3227. /* Read a tuple of affine expressions, together with optional constraints
  3228. * on the domain from "s". "dom" represents the initial constraints
  3229. * on the domain.
  3230. *
  3231. * The isl_multi_aff may live in either a set or a map space.
  3232. * First read the first tuple and check if it is followed by a "->".
  3233. * If so, convert the tuple into the domain of the isl_multi_pw_aff and
  3234. * read in the next tuple. This tuple (or the first tuple if it was
  3235. * not followed by a "->") is then converted into an isl_multi_pw_aff
  3236. * through a call to extract_mpa_from_tuple.
  3237. * The result is converted to an isl_pw_multi_aff and
  3238. * its domain is intersected with the domain.
  3239. *
  3240. * Note that the last tuple may introduce new identifiers,
  3241. * but these cannot be referenced in the description of the domain.
  3242. */
  3243. static __isl_give isl_pw_multi_aff *read_conditional_multi_aff(
  3244. __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
  3245. {
  3246. isl_multi_pw_aff *tuple;
  3247. isl_multi_pw_aff *mpa;
  3248. isl_pw_multi_aff *pma;
  3249. int n = v->n;
  3250. int n_dom;
  3251. n_dom = v->n;
  3252. tuple = read_tuple(s, v, 0, 0);
  3253. if (!tuple)
  3254. goto error;
  3255. if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
  3256. isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
  3257. dom = isl_map_domain(map);
  3258. n_dom = v->n;
  3259. tuple = read_tuple(s, v, 0, 0);
  3260. if (!tuple)
  3261. goto error;
  3262. }
  3263. mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
  3264. isl_multi_pw_aff_free(tuple);
  3265. if (!mpa)
  3266. dom = isl_set_free(dom);
  3267. vars_drop(v, v->n - n_dom);
  3268. dom = read_optional_formula(s, dom, v, 0);
  3269. vars_drop(v, v->n - n);
  3270. pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
  3271. pma = isl_pw_multi_aff_intersect_domain(pma, dom);
  3272. return pma;
  3273. error:
  3274. isl_set_free(dom);
  3275. return NULL;
  3276. }
  3277. /* Read an isl_union_pw_multi_aff from "s".
  3278. *
  3279. * In particular, first read the parameters and then read a sequence
  3280. * of zero or more tuples of affine expressions with optional conditions and
  3281. * add them up.
  3282. */
  3283. __isl_give isl_union_pw_multi_aff *isl_stream_read_union_pw_multi_aff(
  3284. __isl_keep isl_stream *s)
  3285. {
  3286. struct vars *v;
  3287. isl_set *dom;
  3288. isl_union_pw_multi_aff *upma = NULL;
  3289. v = vars_new(s->ctx);
  3290. if (!v)
  3291. return NULL;
  3292. dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
  3293. if (next_is_tuple(s)) {
  3294. dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
  3295. if (isl_stream_eat(s, ISL_TOKEN_TO))
  3296. goto error;
  3297. }
  3298. if (isl_stream_eat(s, '{'))
  3299. goto error;
  3300. upma = isl_union_pw_multi_aff_empty(isl_set_get_space(dom));
  3301. do {
  3302. isl_pw_multi_aff *pma;
  3303. isl_union_pw_multi_aff *upma2;
  3304. if (isl_stream_next_token_is(s, '}'))
  3305. break;
  3306. pma = read_conditional_multi_aff(s, isl_set_copy(dom), v);
  3307. upma2 = isl_union_pw_multi_aff_from_pw_multi_aff(pma);
  3308. upma = isl_union_pw_multi_aff_union_add(upma, upma2);
  3309. if (!upma)
  3310. goto error;
  3311. } while (isl_stream_eat_if_available(s, ';'));
  3312. if (isl_stream_eat(s, '}'))
  3313. goto error;
  3314. isl_set_free(dom);
  3315. vars_free(v);
  3316. return upma;
  3317. error:
  3318. isl_union_pw_multi_aff_free(upma);
  3319. isl_set_free(dom);
  3320. vars_free(v);
  3321. return NULL;
  3322. }
  3323. /* Read an isl_pw_multi_aff from "s".
  3324. *
  3325. * Read a more generic isl_union_pw_multi_aff first and
  3326. * then check that the result lives in a single space.
  3327. */
  3328. __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(
  3329. __isl_keep isl_stream *s)
  3330. {
  3331. isl_bool single_space;
  3332. isl_union_pw_multi_aff *upma;
  3333. upma = isl_stream_read_union_pw_multi_aff(s);
  3334. single_space = isl_union_pw_multi_aff_isa_pw_multi_aff(upma);
  3335. if (single_space < 0)
  3336. upma = isl_union_pw_multi_aff_free(upma);
  3337. else if (!single_space)
  3338. isl_die(s->ctx, isl_error_invalid,
  3339. "expecting expression in single space",
  3340. upma = isl_union_pw_multi_aff_free(upma));
  3341. return isl_union_pw_multi_aff_as_pw_multi_aff(upma);
  3342. }
  3343. __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
  3344. const char *str)
  3345. {
  3346. isl_pw_multi_aff *pma;
  3347. isl_stream *s = isl_stream_new_str(ctx, str);
  3348. if (!s)
  3349. return NULL;
  3350. pma = isl_stream_read_pw_multi_aff(s);
  3351. isl_stream_free(s);
  3352. return pma;
  3353. }
  3354. /* Read an isl_union_pw_multi_aff from "str".
  3355. */
  3356. __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_read_from_str(
  3357. isl_ctx *ctx, const char *str)
  3358. {
  3359. isl_union_pw_multi_aff *upma;
  3360. isl_stream *s = isl_stream_new_str(ctx, str);
  3361. if (!s)
  3362. return NULL;
  3363. upma = isl_stream_read_union_pw_multi_aff(s);
  3364. isl_stream_free(s);
  3365. return upma;
  3366. }
  3367. /* Assuming "pa" represents a single affine expression defined on a universe
  3368. * domain, extract this affine expression.
  3369. */
  3370. static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
  3371. {
  3372. isl_aff *aff;
  3373. if (!pa)
  3374. return NULL;
  3375. if (pa->n != 1)
  3376. isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
  3377. "expecting single affine expression",
  3378. goto error);
  3379. if (!isl_set_plain_is_universe(pa->p[0].set))
  3380. isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
  3381. "expecting universe domain",
  3382. goto error);
  3383. aff = isl_aff_copy(pa->p[0].aff);
  3384. isl_pw_aff_free(pa);
  3385. return aff;
  3386. error:
  3387. isl_pw_aff_free(pa);
  3388. return NULL;
  3389. }
  3390. #undef BASE
  3391. #define BASE val
  3392. #include <isl_multi_read_no_explicit_domain_templ.c>
  3393. #undef BASE
  3394. #define BASE id
  3395. #include <isl_multi_read_no_explicit_domain_templ.c>
  3396. /* Read a multi-affine expression from "s".
  3397. * If the multi-affine expression has a domain, then the tuple
  3398. * representing this domain cannot involve any affine expressions.
  3399. * The tuple representing the actual expressions needs to consist
  3400. * of only affine expressions. Moreover, these expressions can
  3401. * only depend on parameters and input dimensions and not on other
  3402. * output dimensions.
  3403. */
  3404. __isl_give isl_multi_aff *isl_stream_read_multi_aff(__isl_keep isl_stream *s)
  3405. {
  3406. struct vars *v;
  3407. isl_set *dom = NULL;
  3408. isl_multi_pw_aff *tuple = NULL;
  3409. int i;
  3410. isl_size dim, n;
  3411. isl_space *space, *dom_space;
  3412. isl_multi_aff *ma = NULL;
  3413. v = vars_new(s->ctx);
  3414. if (!v)
  3415. return NULL;
  3416. dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
  3417. if (next_is_tuple(s)) {
  3418. dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
  3419. if (isl_stream_eat(s, ISL_TOKEN_TO))
  3420. goto error;
  3421. }
  3422. if (!isl_set_plain_is_universe(dom))
  3423. isl_die(s->ctx, isl_error_invalid,
  3424. "expecting universe parameter domain", goto error);
  3425. if (isl_stream_eat(s, '{'))
  3426. goto error;
  3427. tuple = read_tuple(s, v, 0, 0);
  3428. if (!tuple)
  3429. goto error;
  3430. if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
  3431. isl_set *set;
  3432. isl_space *space;
  3433. isl_bool has_expr;
  3434. has_expr = tuple_has_expr(tuple);
  3435. if (has_expr < 0)
  3436. goto error;
  3437. if (has_expr)
  3438. isl_die(s->ctx, isl_error_invalid,
  3439. "expecting universe domain", goto error);
  3440. space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
  3441. set = isl_set_universe(space);
  3442. dom = isl_set_intersect_params(set, dom);
  3443. isl_multi_pw_aff_free(tuple);
  3444. tuple = read_tuple(s, v, 0, 0);
  3445. if (!tuple)
  3446. goto error;
  3447. }
  3448. if (isl_stream_eat(s, '}'))
  3449. goto error;
  3450. n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
  3451. dim = isl_set_dim(dom, isl_dim_all);
  3452. if (n < 0 || dim < 0)
  3453. goto error;
  3454. dom_space = isl_set_get_space(dom);
  3455. space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
  3456. space = isl_space_align_params(space, isl_space_copy(dom_space));
  3457. if (!isl_space_is_params(dom_space))
  3458. space = isl_space_map_from_domain_and_range(
  3459. isl_space_copy(dom_space), space);
  3460. isl_space_free(dom_space);
  3461. ma = isl_multi_aff_alloc(space);
  3462. for (i = 0; i < n; ++i) {
  3463. isl_pw_aff *pa;
  3464. isl_aff *aff;
  3465. pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
  3466. aff = aff_from_pw_aff(pa);
  3467. if (!aff)
  3468. goto error;
  3469. if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
  3470. isl_aff_free(aff);
  3471. isl_die(s->ctx, isl_error_invalid,
  3472. "not an affine expression", goto error);
  3473. }
  3474. aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
  3475. space = isl_multi_aff_get_domain_space(ma);
  3476. aff = isl_aff_reset_domain_space(aff, space);
  3477. ma = isl_multi_aff_set_aff(ma, i, aff);
  3478. }
  3479. isl_multi_pw_aff_free(tuple);
  3480. vars_free(v);
  3481. isl_set_free(dom);
  3482. return ma;
  3483. error:
  3484. isl_multi_pw_aff_free(tuple);
  3485. vars_free(v);
  3486. isl_set_free(dom);
  3487. isl_multi_aff_free(ma);
  3488. return NULL;
  3489. }
  3490. __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
  3491. const char *str)
  3492. {
  3493. isl_multi_aff *maff;
  3494. isl_stream *s = isl_stream_new_str(ctx, str);
  3495. if (!s)
  3496. return NULL;
  3497. maff = isl_stream_read_multi_aff(s);
  3498. isl_stream_free(s);
  3499. return maff;
  3500. }
  3501. /* Read an isl_multi_pw_aff from "s".
  3502. *
  3503. * The input format is similar to that of map, except that any conditions
  3504. * on the domains should be specified inside the tuple since each
  3505. * piecewise affine expression may have a different domain.
  3506. * However, additional, shared conditions can also be specified.
  3507. * This is especially useful for setting the explicit domain
  3508. * of a zero-dimensional isl_multi_pw_aff.
  3509. *
  3510. * Since we do not know in advance if the isl_multi_pw_aff lives
  3511. * in a set or a map space, we first read the first tuple and check
  3512. * if it is followed by a "->". If so, we convert the tuple into
  3513. * the domain of the isl_multi_pw_aff and read in the next tuple.
  3514. * This tuple (or the first tuple if it was not followed by a "->")
  3515. * is then converted into the isl_multi_pw_aff through a call
  3516. * to extract_mpa_from_tuple and the domain of the result
  3517. * is intersected with the domain.
  3518. *
  3519. * Note that the last tuple may introduce new identifiers,
  3520. * but these cannot be referenced in the description of the domain.
  3521. */
  3522. __isl_give isl_multi_pw_aff *isl_stream_read_multi_pw_aff(
  3523. __isl_keep isl_stream *s)
  3524. {
  3525. int n_dom;
  3526. struct vars *v;
  3527. isl_set *dom = NULL;
  3528. isl_multi_pw_aff *tuple = NULL;
  3529. isl_multi_pw_aff *mpa = NULL;
  3530. v = vars_new(s->ctx);
  3531. if (!v)
  3532. return NULL;
  3533. dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
  3534. if (next_is_tuple(s)) {
  3535. dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
  3536. if (isl_stream_eat(s, ISL_TOKEN_TO))
  3537. goto error;
  3538. }
  3539. if (isl_stream_eat(s, '{'))
  3540. goto error;
  3541. n_dom = v->n;
  3542. tuple = read_tuple(s, v, 0, 0);
  3543. if (!tuple)
  3544. goto error;
  3545. if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
  3546. isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
  3547. dom = isl_map_domain(map);
  3548. n_dom = v->n;
  3549. tuple = read_tuple(s, v, 0, 0);
  3550. if (!tuple)
  3551. goto error;
  3552. }
  3553. vars_drop(v, v->n - n_dom);
  3554. if (isl_stream_eat_if_available(s, ':'))
  3555. dom = read_formula(s, v, dom, 0);
  3556. if (isl_stream_eat(s, '}'))
  3557. goto error;
  3558. mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
  3559. isl_multi_pw_aff_free(tuple);
  3560. vars_free(v);
  3561. mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
  3562. return mpa;
  3563. error:
  3564. isl_multi_pw_aff_free(tuple);
  3565. vars_free(v);
  3566. isl_set_free(dom);
  3567. isl_multi_pw_aff_free(mpa);
  3568. return NULL;
  3569. }
  3570. /* Read an isl_multi_pw_aff from "str".
  3571. */
  3572. __isl_give isl_multi_pw_aff *isl_multi_pw_aff_read_from_str(isl_ctx *ctx,
  3573. const char *str)
  3574. {
  3575. isl_multi_pw_aff *mpa;
  3576. isl_stream *s = isl_stream_new_str(ctx, str);
  3577. if (!s)
  3578. return NULL;
  3579. mpa = isl_stream_read_multi_pw_aff(s);
  3580. isl_stream_free(s);
  3581. return mpa;
  3582. }
  3583. /* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
  3584. */
  3585. static __isl_give isl_union_pw_aff *read_union_pw_aff_with_dom(
  3586. __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
  3587. {
  3588. isl_pw_aff *pa;
  3589. isl_union_pw_aff *upa = NULL;
  3590. isl_set *aff_dom;
  3591. int n;
  3592. n = v->n;
  3593. aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
  3594. pa = read_pw_aff_with_dom(s, aff_dom, v);
  3595. vars_drop(v, v->n - n);
  3596. upa = isl_union_pw_aff_from_pw_aff(pa);
  3597. while (isl_stream_eat_if_available(s, ';')) {
  3598. isl_pw_aff *pa_i;
  3599. isl_union_pw_aff *upa_i;
  3600. n = v->n;
  3601. aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
  3602. pa_i = read_pw_aff_with_dom(s, aff_dom, v);
  3603. vars_drop(v, v->n - n);
  3604. upa_i = isl_union_pw_aff_from_pw_aff(pa_i);
  3605. upa = isl_union_pw_aff_union_add(upa, upa_i);
  3606. }
  3607. isl_set_free(dom);
  3608. return upa;
  3609. }
  3610. /* Read an isl_union_pw_aff from "s".
  3611. *
  3612. * First check if there are any paramters, then read in the opening brace
  3613. * and use read_union_pw_aff_with_dom to read in the body of
  3614. * the isl_union_pw_aff. Finally, read the closing brace.
  3615. */
  3616. __isl_give isl_union_pw_aff *isl_stream_read_union_pw_aff(
  3617. __isl_keep isl_stream *s)
  3618. {
  3619. struct vars *v;
  3620. isl_set *dom;
  3621. isl_union_pw_aff *upa = NULL;
  3622. v = vars_new(s->ctx);
  3623. if (!v)
  3624. return NULL;
  3625. dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
  3626. if (next_is_tuple(s)) {
  3627. dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
  3628. if (isl_stream_eat(s, ISL_TOKEN_TO))
  3629. goto error;
  3630. }
  3631. if (isl_stream_eat(s, '{'))
  3632. goto error;
  3633. upa = read_union_pw_aff_with_dom(s, isl_set_copy(dom), v);
  3634. if (isl_stream_eat(s, '}'))
  3635. goto error;
  3636. vars_free(v);
  3637. isl_set_free(dom);
  3638. return upa;
  3639. error:
  3640. vars_free(v);
  3641. isl_set_free(dom);
  3642. isl_union_pw_aff_free(upa);
  3643. return NULL;
  3644. }
  3645. /* Read an isl_union_pw_aff from "str".
  3646. */
  3647. __isl_give isl_union_pw_aff *isl_union_pw_aff_read_from_str(isl_ctx *ctx,
  3648. const char *str)
  3649. {
  3650. isl_union_pw_aff *upa;
  3651. isl_stream *s = isl_stream_new_str(ctx, str);
  3652. if (!s)
  3653. return NULL;
  3654. upa = isl_stream_read_union_pw_aff(s);
  3655. isl_stream_free(s);
  3656. return upa;
  3657. }
  3658. /* This function is called for each element in a tuple inside
  3659. * isl_stream_read_multi_union_pw_aff.
  3660. *
  3661. * Read a '{', the union piecewise affine expression body and a '}' and
  3662. * add the isl_union_pw_aff to *list.
  3663. */
  3664. static __isl_give isl_space *read_union_pw_aff_el(__isl_keep isl_stream *s,
  3665. struct vars *v, __isl_take isl_space *space, int rational, void *user)
  3666. {
  3667. isl_set *dom;
  3668. isl_union_pw_aff *upa;
  3669. isl_union_pw_aff_list **list = (isl_union_pw_aff_list **) user;
  3670. dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
  3671. if (isl_stream_eat(s, '{'))
  3672. goto error;
  3673. upa = read_union_pw_aff_with_dom(s, dom, v);
  3674. *list = isl_union_pw_aff_list_add(*list, upa);
  3675. if (isl_stream_eat(s, '}'))
  3676. return isl_space_free(space);
  3677. if (!*list)
  3678. return isl_space_free(space);
  3679. return space;
  3680. error:
  3681. isl_set_free(dom);
  3682. return isl_space_free(space);
  3683. }
  3684. /* Do the next tokens in "s" correspond to an empty tuple?
  3685. * In particular, does the stream start with a '[', followed by a ']',
  3686. * not followed by a "->"?
  3687. */
  3688. static int next_is_empty_tuple(__isl_keep isl_stream *s)
  3689. {
  3690. struct isl_token *tok, *tok2, *tok3;
  3691. int is_empty_tuple = 0;
  3692. tok = isl_stream_next_token(s);
  3693. if (!tok)
  3694. return 0;
  3695. if (tok->type != '[') {
  3696. isl_stream_push_token(s, tok);
  3697. return 0;
  3698. }
  3699. tok2 = isl_stream_next_token(s);
  3700. if (tok2 && tok2->type == ']') {
  3701. tok3 = isl_stream_next_token(s);
  3702. is_empty_tuple = !tok || tok->type != ISL_TOKEN_TO;
  3703. if (tok3)
  3704. isl_stream_push_token(s, tok3);
  3705. }
  3706. if (tok2)
  3707. isl_stream_push_token(s, tok2);
  3708. isl_stream_push_token(s, tok);
  3709. return is_empty_tuple;
  3710. }
  3711. /* Do the next tokens in "s" correspond to a tuple of parameters?
  3712. * In particular, does the stream start with a '[' that is not
  3713. * followed by a '{' or a nested tuple?
  3714. */
  3715. static int next_is_param_tuple(__isl_keep isl_stream *s)
  3716. {
  3717. struct isl_token *tok, *tok2;
  3718. int is_tuple;
  3719. tok = isl_stream_next_token(s);
  3720. if (!tok)
  3721. return 0;
  3722. if (tok->type != '[' || next_is_tuple(s)) {
  3723. isl_stream_push_token(s, tok);
  3724. return 0;
  3725. }
  3726. tok2 = isl_stream_next_token(s);
  3727. is_tuple = tok2 && tok2->type != '{';
  3728. if (tok2)
  3729. isl_stream_push_token(s, tok2);
  3730. isl_stream_push_token(s, tok);
  3731. return is_tuple;
  3732. }
  3733. /* Read the core of a body of an isl_multi_union_pw_aff from "s",
  3734. * i.e., everything except the parameter specification and
  3735. * without shared domain constraints.
  3736. * "v" contains a description of the identifiers parsed so far.
  3737. * The parameters, if any, are specified by "space".
  3738. *
  3739. * The body is of the form
  3740. *
  3741. * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
  3742. *
  3743. * Read the tuple, collecting the individual isl_union_pw_aff
  3744. * elements in a list and construct the result from the tuple space and
  3745. * the list.
  3746. */
  3747. static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_body_core(
  3748. __isl_keep isl_stream *s, struct vars *v, __isl_take isl_space *space)
  3749. {
  3750. isl_union_pw_aff_list *list;
  3751. isl_multi_union_pw_aff *mupa;
  3752. list = isl_union_pw_aff_list_alloc(s->ctx, 0);
  3753. space = read_tuple_space(s, v, space, 1, 0,
  3754. &read_union_pw_aff_el, &list);
  3755. mupa = isl_multi_union_pw_aff_from_union_pw_aff_list(space, list);
  3756. return mupa;
  3757. }
  3758. /* Read the body of an isl_union_set from "s",
  3759. * i.e., everything except the parameter specification.
  3760. * "v" contains a description of the identifiers parsed so far.
  3761. * The parameters, if any, are specified by "space".
  3762. *
  3763. * First read a generic disjunction of object bodies and then try and extract
  3764. * an isl_union_set from that.
  3765. */
  3766. static __isl_give isl_union_set *read_union_set_body(__isl_keep isl_stream *s,
  3767. struct vars *v, __isl_take isl_space *space)
  3768. {
  3769. struct isl_obj obj = { isl_obj_set, NULL };
  3770. isl_map *map;
  3771. map = isl_set_universe(space);
  3772. if (isl_stream_eat(s, '{') < 0)
  3773. goto error;
  3774. obj = obj_read_disjuncts(s, v, map);
  3775. if (isl_stream_eat(s, '}') < 0)
  3776. goto error;
  3777. isl_map_free(map);
  3778. return extract_union_set(s->ctx, obj);
  3779. error:
  3780. obj.type->free(obj.v);
  3781. isl_map_free(map);
  3782. return NULL;
  3783. }
  3784. /* Read the body of an isl_multi_union_pw_aff from "s",
  3785. * i.e., everything except the parameter specification.
  3786. * "v" contains a description of the identifiers parsed so far.
  3787. * The parameters, if any, are specified by "space".
  3788. *
  3789. * In particular, handle the special case with shared domain constraints.
  3790. * These are specified as
  3791. *
  3792. * ([...] : ...)
  3793. *
  3794. * and are especially useful for setting the explicit domain
  3795. * of a zero-dimensional isl_multi_union_pw_aff.
  3796. * The core isl_multi_union_pw_aff body ([...]) is read by
  3797. * read_multi_union_pw_aff_body_core.
  3798. */
  3799. static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_body(
  3800. __isl_keep isl_stream *s, struct vars *v, __isl_take isl_space *space)
  3801. {
  3802. isl_multi_union_pw_aff *mupa;
  3803. if (!isl_stream_next_token_is(s, '('))
  3804. return read_multi_union_pw_aff_body_core(s, v, space);
  3805. if (isl_stream_eat(s, '(') < 0)
  3806. goto error;
  3807. mupa = read_multi_union_pw_aff_body_core(s, v, isl_space_copy(space));
  3808. if (isl_stream_eat_if_available(s, ':')) {
  3809. isl_union_set *dom;
  3810. dom = read_union_set_body(s, v, space);
  3811. mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
  3812. } else {
  3813. isl_space_free(space);
  3814. }
  3815. if (isl_stream_eat(s, ')') < 0)
  3816. return isl_multi_union_pw_aff_free(mupa);
  3817. return mupa;
  3818. error:
  3819. isl_space_free(space);
  3820. return NULL;
  3821. }
  3822. /* Read an isl_multi_union_pw_aff from "s".
  3823. *
  3824. * The input has the form
  3825. *
  3826. * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
  3827. *
  3828. * or
  3829. *
  3830. * [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
  3831. *
  3832. * Additionally, a shared domain may be specified as
  3833. *
  3834. * ([..] : ...)
  3835. *
  3836. * or
  3837. *
  3838. * [..] -> ([..] : ...)
  3839. *
  3840. * The first case is handled by the caller, the second case
  3841. * is handled by read_multi_union_pw_aff_body.
  3842. *
  3843. * We first check for the special case of an empty tuple "[]".
  3844. * Then we check if there are any parameters.
  3845. * Finally, read the tuple and construct the result.
  3846. */
  3847. static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_core(
  3848. __isl_keep isl_stream *s)
  3849. {
  3850. struct vars *v;
  3851. isl_set *dom = NULL;
  3852. isl_space *space;
  3853. isl_multi_union_pw_aff *mupa = NULL;
  3854. if (next_is_empty_tuple(s)) {
  3855. if (isl_stream_eat(s, '['))
  3856. return NULL;
  3857. if (isl_stream_eat(s, ']'))
  3858. return NULL;
  3859. space = isl_space_set_alloc(s->ctx, 0, 0);
  3860. return isl_multi_union_pw_aff_zero(space);
  3861. }
  3862. v = vars_new(s->ctx);
  3863. if (!v)
  3864. return NULL;
  3865. dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
  3866. if (next_is_param_tuple(s)) {
  3867. dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
  3868. if (isl_stream_eat(s, ISL_TOKEN_TO))
  3869. goto error;
  3870. }
  3871. space = isl_set_get_space(dom);
  3872. isl_set_free(dom);
  3873. mupa = read_multi_union_pw_aff_body(s, v, space);
  3874. vars_free(v);
  3875. return mupa;
  3876. error:
  3877. vars_free(v);
  3878. isl_set_free(dom);
  3879. isl_multi_union_pw_aff_free(mupa);
  3880. return NULL;
  3881. }
  3882. /* Read an isl_multi_union_pw_aff from "s".
  3883. *
  3884. * In particular, handle the special case with shared domain constraints.
  3885. * These are specified as
  3886. *
  3887. * ([...] : ...)
  3888. *
  3889. * and are especially useful for setting the explicit domain
  3890. * of a zero-dimensional isl_multi_union_pw_aff.
  3891. * The core isl_multi_union_pw_aff ([...]) is read by
  3892. * read_multi_union_pw_aff_core.
  3893. */
  3894. __isl_give isl_multi_union_pw_aff *isl_stream_read_multi_union_pw_aff(
  3895. __isl_keep isl_stream *s)
  3896. {
  3897. isl_multi_union_pw_aff *mupa;
  3898. if (!isl_stream_next_token_is(s, '('))
  3899. return read_multi_union_pw_aff_core(s);
  3900. if (isl_stream_eat(s, '(') < 0)
  3901. return NULL;
  3902. mupa = read_multi_union_pw_aff_core(s);
  3903. if (isl_stream_eat_if_available(s, ':')) {
  3904. isl_union_set *dom;
  3905. dom = isl_stream_read_union_set(s);
  3906. mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
  3907. }
  3908. if (isl_stream_eat(s, ')') < 0)
  3909. return isl_multi_union_pw_aff_free(mupa);
  3910. return mupa;
  3911. }
  3912. /* Read an isl_multi_union_pw_aff from "str".
  3913. */
  3914. __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_read_from_str(
  3915. isl_ctx *ctx, const char *str)
  3916. {
  3917. isl_multi_union_pw_aff *mupa;
  3918. isl_stream *s = isl_stream_new_str(ctx, str);
  3919. if (!s)
  3920. return NULL;
  3921. mupa = isl_stream_read_multi_union_pw_aff(s);
  3922. isl_stream_free(s);
  3923. return mupa;
  3924. }
  3925. __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
  3926. __isl_keep isl_stream *s)
  3927. {
  3928. struct isl_obj obj;
  3929. obj = obj_read(s);
  3930. if (obj.type == isl_obj_pw_qpolynomial) {
  3931. obj.type = isl_obj_union_pw_qpolynomial;
  3932. obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
  3933. }
  3934. if (obj.v)
  3935. isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
  3936. goto error);
  3937. return obj.v;
  3938. error:
  3939. obj.type->free(obj.v);
  3940. return NULL;
  3941. }
  3942. __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
  3943. isl_ctx *ctx, const char *str)
  3944. {
  3945. isl_union_pw_qpolynomial *upwqp;
  3946. isl_stream *s = isl_stream_new_str(ctx, str);
  3947. if (!s)
  3948. return NULL;
  3949. upwqp = isl_stream_read_union_pw_qpolynomial(s);
  3950. isl_stream_free(s);
  3951. return upwqp;
  3952. }