isl_output.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646
  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 <stdlib.h>
  17. #include <string.h>
  18. #include <isl_ctx_private.h>
  19. #include <isl_map_private.h>
  20. #include <isl/set.h>
  21. #include <isl_seq.h>
  22. #include <isl_polynomial_private.h>
  23. #include <isl_printer_private.h>
  24. #include <isl_space_private.h>
  25. #include <isl_mat_private.h>
  26. #include <isl_vec_private.h>
  27. #include <isl/union_set.h>
  28. #include <isl/union_map.h>
  29. #include <isl/constraint.h>
  30. #include <isl_local.h>
  31. #include <isl_local_space_private.h>
  32. #include <isl_aff_private.h>
  33. #include <isl_id_private.h>
  34. #include <isl_val_private.h>
  35. #include <isl_constraint_private.h>
  36. #include <isl/ast_build.h>
  37. #include <isl_sort.h>
  38. #include <isl_output_private.h>
  39. #include <bset_to_bmap.c>
  40. #include <set_to_map.c>
  41. #include <uset_to_umap.c>
  42. static const char *s_to[2] = { " -> ", " \\to " };
  43. static const char *s_and[2] = { " and ", " \\wedge " };
  44. static const char *s_or[2] = { " or ", " \\vee " };
  45. static const char *s_le[2] = { "<=", "\\le" };
  46. static const char *s_ge[2] = { ">=", "\\ge" };
  47. static const char *s_open_set[2] = { "{ ", "\\{\\, " };
  48. static const char *s_close_set[2] = { " }", " \\,\\}" };
  49. static const char *s_open_list[2] = { "[", "(" };
  50. static const char *s_close_list[2] = { "]", ")" };
  51. static const char *s_such_that[2] = { " : ", " \\mid " };
  52. static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
  53. static const char *s_close_exists[2] = { ")", "" };
  54. static const char *s_div_prefix[2] = { "e", "\\alpha_" };
  55. static const char *s_mod[2] = { "mod", "\\bmod" };
  56. static const char *s_param_prefix[2] = { "p", "p_" };
  57. static const char *s_input_prefix[2] = { "i", "i_" };
  58. static const char *s_output_prefix[2] = { "o", "o_" };
  59. static __isl_give isl_printer *print_constraint_polylib(
  60. struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
  61. {
  62. int i;
  63. isl_size n_in = isl_basic_map_dim(bmap, isl_dim_in);
  64. isl_size n_out = isl_basic_map_dim(bmap, isl_dim_out);
  65. isl_size nparam = isl_basic_map_dim(bmap, isl_dim_param);
  66. isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
  67. if (n_in < 0 || n_out < 0 || nparam < 0)
  68. return isl_printer_free(p);
  69. p = isl_printer_start_line(p);
  70. p = isl_printer_print_int(p, ineq);
  71. for (i = 0; i < n_out; ++i) {
  72. p = isl_printer_print_str(p, " ");
  73. p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
  74. }
  75. for (i = 0; i < n_in; ++i) {
  76. p = isl_printer_print_str(p, " ");
  77. p = isl_printer_print_isl_int(p, c[1+nparam+i]);
  78. }
  79. for (i = 0; i < bmap->n_div; ++i) {
  80. p = isl_printer_print_str(p, " ");
  81. p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
  82. }
  83. for (i = 0; i < nparam; ++i) {
  84. p = isl_printer_print_str(p, " ");
  85. p = isl_printer_print_isl_int(p, c[1+i]);
  86. }
  87. p = isl_printer_print_str(p, " ");
  88. p = isl_printer_print_isl_int(p, c[0]);
  89. p = isl_printer_end_line(p);
  90. return p;
  91. }
  92. static __isl_give isl_printer *print_constraints_polylib(
  93. struct isl_basic_map *bmap, __isl_take isl_printer *p)
  94. {
  95. int i;
  96. p = isl_printer_set_isl_int_width(p, 5);
  97. for (i = 0; i < bmap->n_eq; ++i)
  98. p = print_constraint_polylib(bmap, 0, i, p);
  99. for (i = 0; i < bmap->n_ineq; ++i)
  100. p = print_constraint_polylib(bmap, 1, i, p);
  101. return p;
  102. }
  103. static __isl_give isl_printer *bset_print_constraints_polylib(
  104. struct isl_basic_set *bset, __isl_take isl_printer *p)
  105. {
  106. return print_constraints_polylib(bset_to_bmap(bset), p);
  107. }
  108. static __isl_give isl_printer *isl_basic_map_print_polylib(
  109. __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
  110. {
  111. isl_size total;
  112. total = isl_basic_map_dim(bmap, isl_dim_all);
  113. if (total < 0)
  114. return isl_printer_free(p);
  115. p = isl_printer_start_line(p);
  116. p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
  117. p = isl_printer_print_str(p, " ");
  118. p = isl_printer_print_int(p, 1 + total + 1);
  119. if (ext) {
  120. isl_size n_in = isl_basic_map_dim(bmap, isl_dim_in);
  121. isl_size n_out = isl_basic_map_dim(bmap, isl_dim_out);
  122. isl_size n_div = isl_basic_map_dim(bmap, isl_dim_div);
  123. isl_size nparam = isl_basic_map_dim(bmap, isl_dim_param);
  124. if (n_in < 0 || n_out < 0 || n_div < 0 || nparam < 0)
  125. return isl_printer_free(p);
  126. p = isl_printer_print_str(p, " ");
  127. p = isl_printer_print_int(p, n_out);
  128. p = isl_printer_print_str(p, " ");
  129. p = isl_printer_print_int(p, n_in);
  130. p = isl_printer_print_str(p, " ");
  131. p = isl_printer_print_int(p, n_div);
  132. p = isl_printer_print_str(p, " ");
  133. p = isl_printer_print_int(p, nparam);
  134. }
  135. p = isl_printer_end_line(p);
  136. return print_constraints_polylib(bmap, p);
  137. }
  138. static __isl_give isl_printer *isl_basic_set_print_polylib(
  139. __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
  140. {
  141. return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
  142. }
  143. static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
  144. __isl_take isl_printer *p, int ext)
  145. {
  146. int i;
  147. p = isl_printer_start_line(p);
  148. p = isl_printer_print_int(p, map->n);
  149. p = isl_printer_end_line(p);
  150. for (i = 0; i < map->n; ++i) {
  151. p = isl_printer_start_line(p);
  152. p = isl_printer_end_line(p);
  153. p = isl_basic_map_print_polylib(map->p[i], p, ext);
  154. }
  155. return p;
  156. }
  157. static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
  158. __isl_take isl_printer *p, int ext)
  159. {
  160. return isl_map_print_polylib(set_to_map(set), p, ext);
  161. }
  162. static isl_size count_same_name(__isl_keep isl_space *space,
  163. enum isl_dim_type type, unsigned pos, const char *name)
  164. {
  165. enum isl_dim_type t;
  166. int p;
  167. isl_size s;
  168. int count = 0;
  169. for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
  170. s = t == type ? pos : isl_space_dim(space, t);
  171. if (s < 0)
  172. return isl_size_error;
  173. for (p = 0; p < s; ++p) {
  174. const char *n = isl_space_get_dim_name(space, t, p);
  175. if (n && !strcmp(n, name))
  176. count++;
  177. }
  178. }
  179. return count;
  180. }
  181. /* Print the name of the variable of type "type" and position "pos"
  182. * in "space" to "p".
  183. */
  184. static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
  185. __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
  186. int latex)
  187. {
  188. const char *name;
  189. char buffer[20];
  190. isl_size primes;
  191. name = type == isl_dim_div ? NULL
  192. : isl_space_get_dim_name(space, type, pos);
  193. if (!name) {
  194. const char *prefix;
  195. if (type == isl_dim_param)
  196. prefix = s_param_prefix[latex];
  197. else if (type == isl_dim_div)
  198. prefix = s_div_prefix[latex];
  199. else if (isl_space_is_set(space) || type == isl_dim_in)
  200. prefix = s_input_prefix[latex];
  201. else
  202. prefix = s_output_prefix[latex];
  203. snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
  204. name = buffer;
  205. }
  206. primes = count_same_name(space, name == buffer ? isl_dim_div : type,
  207. pos, name);
  208. if (primes < 0)
  209. return isl_printer_free(p);
  210. p = isl_printer_print_str(p, name);
  211. while (primes-- > 0)
  212. p = isl_printer_print_str(p, "'");
  213. return p;
  214. }
  215. static isl_stat pos2type(__isl_keep isl_space *space,
  216. enum isl_dim_type *type, unsigned *pos)
  217. {
  218. isl_size n_in = isl_space_dim(space, isl_dim_in);
  219. isl_size n_out = isl_space_dim(space, isl_dim_out);
  220. isl_size nparam = isl_space_dim(space, isl_dim_param);
  221. if (n_in < 0 || n_out < 0 || nparam < 0)
  222. return isl_stat_error;
  223. if (*pos < 1 + nparam) {
  224. *type = isl_dim_param;
  225. *pos -= 1;
  226. } else if (*pos < 1 + nparam + n_in) {
  227. *type = isl_dim_in;
  228. *pos -= 1 + nparam;
  229. } else if (*pos < 1 + nparam + n_in + n_out) {
  230. *type = isl_dim_out;
  231. *pos -= 1 + nparam + n_in;
  232. } else {
  233. *type = isl_dim_div;
  234. *pos -= 1 + nparam + n_in + n_out;
  235. }
  236. return isl_stat_ok;
  237. }
  238. /* Can the div expression of the integer division at position "row" of "div"
  239. * be printed?
  240. * In particular, are the div expressions available and does the selected
  241. * variable have a known explicit representation?
  242. * Furthermore, the Omega format does not allow any div expressions
  243. * to be printed.
  244. */
  245. static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
  246. __isl_keep isl_mat *div, int pos)
  247. {
  248. if (p->output_format == ISL_FORMAT_OMEGA)
  249. return isl_bool_false;
  250. if (!div)
  251. return isl_bool_false;
  252. return isl_bool_not(isl_local_div_is_marked_unknown(div, pos));
  253. }
  254. static __isl_give isl_printer *print_div(__isl_keep isl_space *space,
  255. __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
  256. static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
  257. __isl_keep isl_mat *div,
  258. isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
  259. {
  260. enum isl_dim_type type;
  261. int print_div_def;
  262. if (!p || !space)
  263. return isl_printer_free(p);
  264. if (pos == 0)
  265. return isl_printer_print_isl_int(p, c);
  266. if (pos2type(space, &type, &pos) < 0)
  267. return isl_printer_free(p);
  268. print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
  269. if (isl_int_is_one(c))
  270. ;
  271. else if (isl_int_is_negone(c))
  272. p = isl_printer_print_str(p, "-");
  273. else {
  274. p = isl_printer_print_isl_int(p, c);
  275. if (p->output_format == ISL_FORMAT_C || print_div_def)
  276. p = isl_printer_print_str(p, "*");
  277. }
  278. if (print_div_def)
  279. p = print_div(space, div, pos, p);
  280. else
  281. p = print_name(space, p, type, pos, latex);
  282. return p;
  283. }
  284. static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *space,
  285. __isl_keep isl_mat *div,
  286. __isl_take isl_printer *p, isl_int *c, int len)
  287. {
  288. int i;
  289. int first;
  290. for (i = 0, first = 1; i < len; ++i) {
  291. int flip = 0;
  292. if (isl_int_is_zero(c[i]))
  293. continue;
  294. if (!first) {
  295. if (isl_int_is_neg(c[i])) {
  296. flip = 1;
  297. isl_int_neg(c[i], c[i]);
  298. p = isl_printer_print_str(p, " - ");
  299. } else
  300. p = isl_printer_print_str(p, " + ");
  301. }
  302. first = 0;
  303. p = print_term(space, div, c[i], i, p, 0);
  304. if (flip)
  305. isl_int_neg(c[i], c[i]);
  306. }
  307. if (first)
  308. p = isl_printer_print_str(p, "0");
  309. return p;
  310. }
  311. /* Print an affine expression "c"
  312. * to "p", with the variable names taken from "space" and
  313. * the integer division definitions taken from "div".
  314. */
  315. static __isl_give isl_printer *print_affine(__isl_take isl_printer *p,
  316. __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c)
  317. {
  318. isl_size n_div, total;
  319. unsigned len;
  320. total = isl_space_dim(space, isl_dim_all);
  321. n_div = isl_mat_rows(div);
  322. if (total < 0 || n_div < 0)
  323. return isl_printer_free(p);
  324. len = 1 + total + n_div;
  325. return print_affine_of_len(space, div, p, c, len);
  326. }
  327. /* offset is the offset of local_space inside data->type of data->space.
  328. */
  329. static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
  330. __isl_keep isl_space *local_space, enum isl_dim_type local_type,
  331. struct isl_print_space_data *data, int offset)
  332. {
  333. int i;
  334. isl_size dim;
  335. if (data->space != local_space && local_type == isl_dim_out)
  336. offset += local_space->n_in;
  337. dim = isl_space_dim(local_space, local_type);
  338. if (dim < 0)
  339. return isl_printer_free(p);
  340. for (i = 0; i < dim; ++i) {
  341. if (i)
  342. p = isl_printer_print_str(p, ", ");
  343. if (data->print_dim)
  344. p = data->print_dim(p, data, offset + i);
  345. else
  346. p = print_name(data->space, p, data->type, offset + i,
  347. data->latex);
  348. }
  349. return p;
  350. }
  351. static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
  352. __isl_keep isl_space *space, enum isl_dim_type type)
  353. {
  354. struct isl_print_space_data data = { .space = space, .type = type };
  355. return print_nested_var_list(p, space, type, &data, 0);
  356. }
  357. static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
  358. __isl_keep isl_space *local_dim,
  359. struct isl_print_space_data *data, int offset);
  360. static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
  361. __isl_keep isl_space *local_space, enum isl_dim_type local_type,
  362. struct isl_print_space_data *data, int offset)
  363. {
  364. const char *name = NULL;
  365. isl_size n = isl_space_dim(local_space, local_type);
  366. if (n < 0)
  367. return isl_printer_free(p);
  368. if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
  369. name = isl_space_get_tuple_name(local_space, local_type);
  370. if (name) {
  371. if (data->latex)
  372. p = isl_printer_print_str(p, "\\mathrm{");
  373. p = isl_printer_print_str(p, name);
  374. if (data->latex)
  375. p = isl_printer_print_str(p, "}");
  376. }
  377. }
  378. if (!data->latex || n != 1 || name)
  379. p = isl_printer_print_str(p, s_open_list[data->latex]);
  380. if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
  381. local_space->nested[local_type - isl_dim_in]) {
  382. if (data->space != local_space && local_type == isl_dim_out)
  383. offset += local_space->n_in;
  384. p = print_nested_map_dim(p,
  385. local_space->nested[local_type - isl_dim_in],
  386. data, offset);
  387. } else
  388. p = print_nested_var_list(p, local_space, local_type, data,
  389. offset);
  390. if (!data->latex || n != 1 || name)
  391. p = isl_printer_print_str(p, s_close_list[data->latex]);
  392. return p;
  393. }
  394. static __isl_give isl_printer *print_tuple(__isl_keep isl_space *space,
  395. __isl_take isl_printer *p, enum isl_dim_type type,
  396. struct isl_print_space_data *data)
  397. {
  398. data->space = space;
  399. data->type = type;
  400. return print_nested_tuple(p, space, type, data, 0);
  401. }
  402. static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
  403. __isl_keep isl_space *local_dim,
  404. struct isl_print_space_data *data, int offset)
  405. {
  406. p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
  407. p = isl_printer_print_str(p, s_to[data->latex]);
  408. p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
  409. return p;
  410. }
  411. __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
  412. __isl_take isl_printer *p, int rational,
  413. struct isl_print_space_data *data)
  414. {
  415. if (rational && !data->latex)
  416. p = isl_printer_print_str(p, "rat: ");
  417. if (isl_space_is_params(space))
  418. ;
  419. else if (isl_space_is_set(space))
  420. p = print_tuple(space, p, isl_dim_set, data);
  421. else {
  422. p = print_tuple(space, p, isl_dim_in, data);
  423. p = isl_printer_print_str(p, s_to[data->latex]);
  424. p = print_tuple(space, p, isl_dim_out, data);
  425. }
  426. return p;
  427. }
  428. static __isl_give isl_printer *print_omega_parameters(
  429. __isl_keep isl_space *space, __isl_take isl_printer *p)
  430. {
  431. isl_size nparam = isl_space_dim(space, isl_dim_param);
  432. if (nparam < 0)
  433. return isl_printer_free(p);
  434. if (nparam == 0)
  435. return p;
  436. p = isl_printer_start_line(p);
  437. p = isl_printer_print_str(p, "symbolic ");
  438. p = print_var_list(p, space, isl_dim_param);
  439. p = isl_printer_print_str(p, ";");
  440. p = isl_printer_end_line(p);
  441. return p;
  442. }
  443. /* Does the inequality constraint following "i" in "bmap"
  444. * have an opposite value for the same last coefficient?
  445. * "last" is the position of the last coefficient of inequality "i".
  446. * If the next constraint is a div constraint, then it is ignored
  447. * since div constraints are not printed.
  448. */
  449. static isl_bool next_is_opposite(__isl_keep isl_basic_map *bmap, int i,
  450. int last)
  451. {
  452. int r;
  453. isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
  454. unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
  455. if (total < 0)
  456. return isl_bool_error;
  457. if (i + 1 >= bmap->n_ineq)
  458. return isl_bool_false;
  459. if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
  460. return isl_bool_false;
  461. if (last >= o_div) {
  462. isl_bool is_div;
  463. is_div = isl_basic_map_is_div_constraint(bmap,
  464. bmap->ineq[i + 1], last - o_div);
  465. if (is_div < 0)
  466. return isl_bool_error;
  467. if (is_div)
  468. return isl_bool_false;
  469. }
  470. r = isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
  471. !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
  472. return isl_bool_ok(r);
  473. }
  474. /* Return a string representation of the operator used when
  475. * printing a constraint where the LHS is greater than or equal to the LHS
  476. * (sign > 0) or smaller than or equal to the LHS (sign < 0).
  477. * If "strict" is set, then return the strict version of the comparison
  478. * operator.
  479. */
  480. static const char *constraint_op(int sign, int strict, int latex)
  481. {
  482. if (strict)
  483. return sign < 0 ? "<" : ">";
  484. if (sign < 0)
  485. return s_le[latex];
  486. else
  487. return s_ge[latex];
  488. }
  489. /* Print one side of a constraint "c" to "p", with
  490. * the variable names taken from "space" and the integer division definitions
  491. * taken from "div".
  492. * "last" is the position of the last non-zero coefficient.
  493. * Let c' be the result of zeroing out this coefficient, then
  494. * the partial constraint
  495. *
  496. * c' op
  497. *
  498. * is printed.
  499. */
  500. static __isl_give isl_printer *print_half_constraint(__isl_take isl_printer *p,
  501. __isl_keep isl_space *space, __isl_keep isl_mat *div,
  502. isl_int *c, int last, const char *op, int latex)
  503. {
  504. isl_int_set_si(c[last], 0);
  505. p = print_affine(p, space, div, c);
  506. p = isl_printer_print_str(p, " ");
  507. p = isl_printer_print_str(p, op);
  508. p = isl_printer_print_str(p, " ");
  509. return p;
  510. }
  511. /* Print a constraint "c" to "p", with the variable names
  512. * taken from "space" and the integer division definitions taken from "div".
  513. * "last" is the position of the last non-zero coefficient, which is
  514. * moreover assumed to be negative.
  515. * Let c' be the result of zeroing out this coefficient, then
  516. * the constraint is printed in the form
  517. *
  518. * -c[last] op c'
  519. */
  520. static __isl_give isl_printer *print_constraint(__isl_take isl_printer *p,
  521. __isl_keep isl_space *space, __isl_keep isl_mat *div,
  522. isl_int *c, int last, const char *op, int latex)
  523. {
  524. isl_int_abs(c[last], c[last]);
  525. p = print_term(space, div, c[last], last, p, latex);
  526. p = isl_printer_print_str(p, " ");
  527. p = isl_printer_print_str(p, op);
  528. p = isl_printer_print_str(p, " ");
  529. isl_int_set_si(c[last], 0);
  530. p = print_affine(p, space, div, c);
  531. return p;
  532. }
  533. /* Given an integer division
  534. *
  535. * floor(f/m)
  536. *
  537. * at position "pos" in "div", print the corresponding modulo expression
  538. *
  539. * (f) mod m
  540. *
  541. * to "p". The variable names are taken from "space", while any
  542. * nested integer division definitions are taken from "div".
  543. */
  544. static __isl_give isl_printer *print_mod(__isl_take isl_printer *p,
  545. __isl_keep isl_space *space, __isl_keep isl_mat *div, int pos,
  546. int latex)
  547. {
  548. if (!p || !div)
  549. return isl_printer_free(p);
  550. p = isl_printer_print_str(p, "(");
  551. p = print_affine_of_len(space, div, p,
  552. div->row[pos] + 1, div->n_col - 1);
  553. p = isl_printer_print_str(p, ") ");
  554. p = isl_printer_print_str(p, s_mod[latex]);
  555. p = isl_printer_print_str(p, " ");
  556. p = isl_printer_print_isl_int(p, div->row[pos][0]);
  557. return p;
  558. }
  559. /* Given an equality constraint with a non-zero coefficient "c"
  560. * in position "pos", is this term of the form
  561. *
  562. * a m floor(g/m),
  563. *
  564. * with c = a m?
  565. * Return the position of the corresponding integer division if so.
  566. * Return the number of integer divisions if not.
  567. * Return isl_size_error on error.
  568. *
  569. * Modulo constraints are currently not printed in C format.
  570. * Other than that, "pos" needs to correspond to an integer division
  571. * with explicit representation and "c" needs to be a multiple
  572. * of the denominator of the integer division.
  573. */
  574. static isl_size print_as_modulo_pos(__isl_keep isl_printer *p,
  575. __isl_keep isl_space *space, __isl_keep isl_mat *div, unsigned pos,
  576. isl_int c)
  577. {
  578. isl_bool can_print;
  579. isl_size n_div;
  580. enum isl_dim_type type;
  581. n_div = isl_mat_rows(div);
  582. if (!p || !space || n_div < 0)
  583. return isl_size_error;
  584. if (p->output_format == ISL_FORMAT_C)
  585. return n_div;
  586. if (pos2type(space, &type, &pos) < 0)
  587. return isl_size_error;
  588. if (type != isl_dim_div)
  589. return n_div;
  590. can_print = can_print_div_expr(p, div, pos);
  591. if (can_print < 0)
  592. return isl_size_error;
  593. if (!can_print)
  594. return n_div;
  595. if (!isl_int_is_divisible_by(c, div->row[pos][0]))
  596. return n_div;
  597. return pos;
  598. }
  599. /* Print equality constraint "c" to "p" as a modulo constraint,
  600. * with the variable names taken from "space" and
  601. * the integer division definitions taken from "div".
  602. * "last" is the position of the last non-zero coefficient, which is
  603. * moreover assumed to be negative and a multiple of the denominator
  604. * of the corresponding integer division. "div_pos" is the corresponding
  605. * position in the sequence of integer divisions.
  606. *
  607. * The equality is of the form
  608. *
  609. * f - a m floor(g/m) = 0.
  610. *
  611. * Print it as
  612. *
  613. * a (g mod m) = -f + a g
  614. */
  615. static __isl_give isl_printer *print_eq_mod_constraint(
  616. __isl_take isl_printer *p, __isl_keep isl_space *space,
  617. __isl_keep isl_mat *div, unsigned div_pos,
  618. isl_int *c, int last, int latex)
  619. {
  620. isl_ctx *ctx;
  621. int multiple;
  622. ctx = isl_printer_get_ctx(p);
  623. isl_int_divexact(c[last], c[last], div->row[div_pos][0]);
  624. isl_int_abs(c[last], c[last]);
  625. multiple = !isl_int_is_one(c[last]);
  626. if (multiple) {
  627. p = isl_printer_print_isl_int(p, c[last]);
  628. p = isl_printer_print_str(p, "*(");
  629. }
  630. p = print_mod(p, space, div, div_pos, latex);
  631. if (multiple)
  632. p = isl_printer_print_str(p, ")");
  633. p = isl_printer_print_str(p, " = ");
  634. isl_seq_combine(c, ctx->negone, c,
  635. c[last], div->row[div_pos] + 1, last);
  636. isl_int_set_si(c[last], 0);
  637. p = print_affine(p, space, div, c);
  638. return p;
  639. }
  640. /* Print equality constraint "c" to "p", with the variable names
  641. * taken from "space" and the integer division definitions taken from "div".
  642. * "last" is the position of the last non-zero coefficient, which is
  643. * moreover assumed to be negative.
  644. *
  645. * If possible, print the equality constraint as a modulo constraint.
  646. */
  647. static __isl_give isl_printer *print_eq_constraint(__isl_take isl_printer *p,
  648. __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c,
  649. int last, int latex)
  650. {
  651. isl_size n_div;
  652. isl_size div_pos;
  653. n_div = isl_mat_rows(div);
  654. div_pos = print_as_modulo_pos(p, space, div, last, c[last]);
  655. if (n_div < 0 || div_pos < 0)
  656. return isl_printer_free(p);
  657. if (div_pos < n_div)
  658. return print_eq_mod_constraint(p, space, div, div_pos,
  659. c, last, latex);
  660. return print_constraint(p, space, div, c, last, "=", latex);
  661. }
  662. /* Print the constraints of "bmap" to "p".
  663. * The names of the variables are taken from "space" and
  664. * the integer division definitions are taken from "div".
  665. * Div constraints are only printed in "dump" mode.
  666. * The constraints are sorted prior to printing (except in "dump" mode).
  667. *
  668. * If x is the last variable with a non-zero coefficient,
  669. * then a lower bound
  670. *
  671. * f - a x >= 0
  672. *
  673. * is printed as
  674. *
  675. * a x <= f
  676. *
  677. * while an upper bound
  678. *
  679. * f + a x >= 0
  680. *
  681. * is printed as
  682. *
  683. * a x >= -f
  684. *
  685. * If the next constraint has an opposite sign for the same last coefficient,
  686. * then it is printed as
  687. *
  688. * f >= a x
  689. *
  690. * or
  691. *
  692. * -f <= a x
  693. *
  694. * instead. In fact, the "a x" part is not printed explicitly, but
  695. * reused from the next constraint, which is therefore treated as
  696. * a first constraint in the conjunction.
  697. *
  698. * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
  699. * the comparison operator is replaced by the strict variant.
  700. * Essentially, ">= 1" is replaced by "> 0".
  701. */
  702. static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
  703. __isl_keep isl_space *space, __isl_keep isl_mat *div,
  704. __isl_take isl_printer *p, int latex)
  705. {
  706. int i;
  707. isl_vec *c = NULL;
  708. int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
  709. isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
  710. unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
  711. int first = 1;
  712. int dump;
  713. if (total < 0 || !p)
  714. return isl_printer_free(p);
  715. bmap = isl_basic_map_copy(bmap);
  716. dump = p->dump;
  717. if (!dump)
  718. bmap = isl_basic_map_sort_constraints(bmap);
  719. if (!bmap)
  720. goto error;
  721. c = isl_vec_alloc(bmap->ctx, 1 + total);
  722. if (!c)
  723. goto error;
  724. for (i = bmap->n_eq - 1; i >= 0; --i) {
  725. int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
  726. if (l < 0) {
  727. if (i != bmap->n_eq - 1)
  728. p = isl_printer_print_str(p, s_and[latex]);
  729. p = isl_printer_print_str(p, "0 = 0");
  730. continue;
  731. }
  732. if (!first)
  733. p = isl_printer_print_str(p, s_and[latex]);
  734. if (isl_int_is_neg(bmap->eq[i][l]))
  735. isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
  736. else
  737. isl_seq_neg(c->el, bmap->eq[i], 1 + total);
  738. p = print_eq_constraint(p, space, div, c->el, l, latex);
  739. first = 0;
  740. }
  741. for (i = 0; i < bmap->n_ineq; ++i) {
  742. isl_bool combine;
  743. int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
  744. int strict;
  745. int s;
  746. const char *op;
  747. if (l < 0)
  748. continue;
  749. if (!dump && l >= o_div &&
  750. can_print_div_expr(p, div, l - o_div)) {
  751. isl_bool is_div;
  752. is_div = isl_basic_map_is_div_constraint(bmap,
  753. bmap->ineq[i], l - o_div);
  754. if (is_div < 0)
  755. goto error;
  756. if (is_div)
  757. continue;
  758. }
  759. if (!first)
  760. p = isl_printer_print_str(p, s_and[latex]);
  761. s = isl_int_sgn(bmap->ineq[i][l]);
  762. strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
  763. if (s < 0)
  764. isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
  765. else
  766. isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
  767. if (strict)
  768. isl_int_set_si(c->el[0], 0);
  769. combine = dump ? isl_bool_false : next_is_opposite(bmap, i, l);
  770. if (combine < 0)
  771. goto error;
  772. if (combine) {
  773. op = constraint_op(-s, strict, latex);
  774. p = print_half_constraint(p, space, div, c->el, l,
  775. op, latex);
  776. first = 1;
  777. } else {
  778. op = constraint_op(s, strict, latex);
  779. p = print_constraint(p, space, div, c->el, l,
  780. op, latex);
  781. first = 0;
  782. }
  783. }
  784. isl_basic_map_free(bmap);
  785. isl_vec_free(c);
  786. return p;
  787. error:
  788. isl_basic_map_free(bmap);
  789. isl_vec_free(c);
  790. isl_printer_free(p);
  791. return NULL;
  792. }
  793. static __isl_give isl_printer *print_div(__isl_keep isl_space *space,
  794. __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
  795. {
  796. int c;
  797. if (!p || !div)
  798. return isl_printer_free(p);
  799. c = p->output_format == ISL_FORMAT_C;
  800. p = isl_printer_print_str(p, c ? "floord(" : "floor((");
  801. p = print_affine_of_len(space, div, p,
  802. div->row[pos] + 1, div->n_col - 1);
  803. p = isl_printer_print_str(p, c ? ", " : ")/");
  804. p = isl_printer_print_isl_int(p, div->row[pos][0]);
  805. p = isl_printer_print_str(p, ")");
  806. return p;
  807. }
  808. /* Print a comma separated list of div names, except those that have
  809. * a definition that can be printed.
  810. * If "print_defined_divs" is set, then those div names are printed
  811. * as well, along with their definitions.
  812. */
  813. static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
  814. __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
  815. int print_defined_divs)
  816. {
  817. int i;
  818. int first = 1;
  819. isl_size n_div;
  820. n_div = isl_mat_rows(div);
  821. if (!p || !space || n_div < 0)
  822. return isl_printer_free(p);
  823. for (i = 0; i < n_div; ++i) {
  824. if (!print_defined_divs && can_print_div_expr(p, div, i))
  825. continue;
  826. if (!first)
  827. p = isl_printer_print_str(p, ", ");
  828. p = print_name(space, p, isl_dim_div, i, latex);
  829. first = 0;
  830. if (!can_print_div_expr(p, div, i))
  831. continue;
  832. p = isl_printer_print_str(p, " = ");
  833. p = print_div(space, div, i, p);
  834. }
  835. return p;
  836. }
  837. /* Does printing an object with local variables described by "div"
  838. * require an "exists" clause?
  839. * That is, are there any local variables without an explicit representation?
  840. * An exists clause is also needed in "dump" mode because
  841. * explicit div representations are not printed inline in that case.
  842. */
  843. static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
  844. {
  845. int i;
  846. isl_size n;
  847. n = isl_mat_rows(div);
  848. if (!p || n < 0)
  849. return isl_bool_error;
  850. if (n == 0)
  851. return isl_bool_false;
  852. if (p->dump)
  853. return isl_bool_true;
  854. for (i = 0; i < n; ++i)
  855. if (!can_print_div_expr(p, div, i))
  856. return isl_bool_true;
  857. return isl_bool_false;
  858. }
  859. /* Print the start of an exists clause, i.e.,
  860. *
  861. * (exists variables:
  862. *
  863. * In dump mode, local variables with an explicit definition are printed
  864. * as well because they will not be printed inline.
  865. */
  866. static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
  867. __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
  868. {
  869. int dump;
  870. if (!p)
  871. return NULL;
  872. dump = p->dump;
  873. p = isl_printer_print_str(p, s_open_exists[latex]);
  874. p = print_div_list(p, space, div, latex, dump);
  875. p = isl_printer_print_str(p, ": ");
  876. return p;
  877. }
  878. /* Remove the explicit representations of all local variables in "div".
  879. */
  880. static __isl_give isl_mat *mark_all_unknown(__isl_take isl_mat *div)
  881. {
  882. int i;
  883. isl_size n_div;
  884. n_div = isl_mat_rows(div);
  885. if (n_div < 0)
  886. return isl_mat_free(div);
  887. for (i = 0; i < n_div; ++i)
  888. div = isl_mat_set_element_si(div, i, 0, 0);
  889. return div;
  890. }
  891. /* Print the constraints of "bmap" to "p".
  892. * The names of the variables are taken from "space".
  893. * "latex" is set if the constraints should be printed in LaTeX format.
  894. * Do not print inline explicit div representations in "dump" mode.
  895. */
  896. static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
  897. __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
  898. {
  899. int dump;
  900. isl_mat *div;
  901. isl_bool exists;
  902. if (!p)
  903. return NULL;
  904. dump = p->dump;
  905. div = isl_basic_map_get_divs(bmap);
  906. exists = need_exists(p, div);
  907. if (exists >= 0 && exists)
  908. p = open_exists(p, space, div, latex);
  909. if (dump)
  910. div = mark_all_unknown(div);
  911. p = print_constraints(bmap, space, div, p, latex);
  912. isl_mat_free(div);
  913. if (exists >= 0 && exists)
  914. p = isl_printer_print_str(p, s_close_exists[latex]);
  915. return p;
  916. }
  917. /* Print a colon followed by the constraints of "bmap"
  918. * to "p", provided there are any constraints.
  919. * The names of the variables are taken from "space".
  920. * "latex" is set if the constraints should be printed in LaTeX format.
  921. */
  922. static __isl_give isl_printer *print_optional_disjunct(
  923. __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
  924. __isl_take isl_printer *p, int latex)
  925. {
  926. if (isl_basic_map_plain_is_universe(bmap))
  927. return p;
  928. p = isl_printer_print_str(p, ": ");
  929. p = print_disjunct(bmap, space, p, latex);
  930. return p;
  931. }
  932. static __isl_give isl_printer *basic_map_print_omega(
  933. __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
  934. {
  935. p = isl_printer_print_str(p, "{ [");
  936. p = print_var_list(p, bmap->dim, isl_dim_in);
  937. p = isl_printer_print_str(p, "] -> [");
  938. p = print_var_list(p, bmap->dim, isl_dim_out);
  939. p = isl_printer_print_str(p, "] ");
  940. p = print_optional_disjunct(bmap, bmap->dim, p, 0);
  941. p = isl_printer_print_str(p, " }");
  942. return p;
  943. }
  944. static __isl_give isl_printer *basic_set_print_omega(
  945. __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
  946. {
  947. p = isl_printer_print_str(p, "{ [");
  948. p = print_var_list(p, bset->dim, isl_dim_set);
  949. p = isl_printer_print_str(p, "] ");
  950. p = print_optional_disjunct(bset, bset->dim, p, 0);
  951. p = isl_printer_print_str(p, " }");
  952. return p;
  953. }
  954. static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
  955. __isl_take isl_printer *p)
  956. {
  957. int i;
  958. for (i = 0; i < map->n; ++i) {
  959. if (i)
  960. p = isl_printer_print_str(p, " union ");
  961. p = basic_map_print_omega(map->p[i], p);
  962. }
  963. return p;
  964. }
  965. static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
  966. __isl_take isl_printer *p)
  967. {
  968. int i;
  969. for (i = 0; i < set->n; ++i) {
  970. if (i)
  971. p = isl_printer_print_str(p, " union ");
  972. p = basic_set_print_omega(set->p[i], p);
  973. }
  974. return p;
  975. }
  976. /* Print the list of parameters in "space", followed by an arrow, to "p",
  977. * if there are any parameters.
  978. */
  979. static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
  980. __isl_keep isl_space *space, struct isl_print_space_data *data)
  981. {
  982. isl_size nparam;
  983. nparam = isl_space_dim(space, isl_dim_param);
  984. if (!p || nparam < 0)
  985. return isl_printer_free(p);
  986. if (nparam == 0)
  987. return p;
  988. p = print_tuple(space, p, isl_dim_param, data);
  989. p = isl_printer_print_str(p, s_to[data->latex]);
  990. return p;
  991. }
  992. static __isl_give isl_printer *isl_basic_map_print_isl(
  993. __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
  994. int latex)
  995. {
  996. struct isl_print_space_data data = { .latex = latex };
  997. int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
  998. p = print_param_tuple(p, bmap->dim, &data);
  999. p = isl_printer_print_str(p, "{ ");
  1000. p = isl_print_space(bmap->dim, p, rational, &data);
  1001. p = isl_printer_print_str(p, " : ");
  1002. p = print_disjunct(bmap, bmap->dim, p, latex);
  1003. p = isl_printer_print_str(p, " }");
  1004. return p;
  1005. }
  1006. /* Print the disjuncts of a map (or set) "map" to "p".
  1007. * The names of the variables are taken from "space".
  1008. * "latex" is set if the constraints should be printed in LaTeX format.
  1009. */
  1010. static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
  1011. __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
  1012. {
  1013. int i;
  1014. if (map->n == 0)
  1015. p = isl_printer_print_str(p, "false");
  1016. for (i = 0; i < map->n; ++i) {
  1017. if (i)
  1018. p = isl_printer_print_str(p, s_or[latex]);
  1019. if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
  1020. p = isl_printer_print_str(p, "(");
  1021. p = print_disjunct(map->p[i], space, p, latex);
  1022. if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
  1023. p = isl_printer_print_str(p, ")");
  1024. }
  1025. return p;
  1026. }
  1027. /* Print the disjuncts of a map (or set) "map" to "p".
  1028. * The names of the variables are taken from "space".
  1029. * "hull" describes constraints shared by all disjuncts of "map".
  1030. * "latex" is set if the constraints should be printed in LaTeX format.
  1031. *
  1032. * Print the disjuncts as a conjunction of "hull" and
  1033. * the result of removing the constraints of "hull" from "map".
  1034. * If this result turns out to be the universe, then simply print "hull".
  1035. */
  1036. static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
  1037. __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
  1038. __isl_take isl_printer *p, int latex)
  1039. {
  1040. isl_bool is_universe;
  1041. p = print_disjunct(hull, space, p, latex);
  1042. map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
  1043. is_universe = isl_map_plain_is_universe(map);
  1044. if (is_universe < 0)
  1045. goto error;
  1046. if (!is_universe) {
  1047. p = isl_printer_print_str(p, s_and[latex]);
  1048. p = isl_printer_print_str(p, "(");
  1049. p = print_disjuncts_core(map, space, p, latex);
  1050. p = isl_printer_print_str(p, ")");
  1051. }
  1052. isl_map_free(map);
  1053. return p;
  1054. error:
  1055. isl_map_free(map);
  1056. isl_printer_free(p);
  1057. return NULL;
  1058. }
  1059. /* Print the disjuncts of a map (or set) "map" to "p".
  1060. * The names of the variables are taken from "space".
  1061. * "latex" is set if the constraints should be printed in LaTeX format.
  1062. *
  1063. * If there are at least two disjuncts and "dump" mode is not turned out,
  1064. * check for any shared constraints among all disjuncts.
  1065. * If there are any, then print them separately in print_disjuncts_in_hull.
  1066. */
  1067. static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
  1068. __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
  1069. {
  1070. if (isl_map_plain_is_universe(map))
  1071. return p;
  1072. p = isl_printer_print_str(p, s_such_that[latex]);
  1073. if (!p)
  1074. return NULL;
  1075. if (!p->dump && map->n >= 2) {
  1076. isl_basic_map *hull;
  1077. isl_bool is_universe;
  1078. hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
  1079. is_universe = isl_basic_map_plain_is_universe(hull);
  1080. if (is_universe < 0)
  1081. p = isl_printer_free(p);
  1082. else if (!is_universe)
  1083. return print_disjuncts_in_hull(map, space, hull,
  1084. p, latex);
  1085. isl_basic_map_free(hull);
  1086. }
  1087. return print_disjuncts_core(map, space, p, latex);
  1088. }
  1089. /* Print the disjuncts of a map (or set).
  1090. * The names of the variables are taken from "space".
  1091. * "latex" is set if the constraints should be printed in LaTeX format.
  1092. *
  1093. * If the map turns out to be a universal parameter domain, then
  1094. * we need to print the colon. Otherwise, the output looks identical
  1095. * to the empty set.
  1096. */
  1097. static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
  1098. __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
  1099. {
  1100. if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
  1101. return isl_printer_print_str(p, s_such_that[latex]);
  1102. else
  1103. return print_disjuncts(map, space, p, latex);
  1104. }
  1105. /* Print the disjuncts of a set.
  1106. * The names of the variables are taken from "space".
  1107. * "latex" is set if the constraints should be printed in LaTeX format.
  1108. */
  1109. static __isl_give isl_printer *print_disjuncts_set(__isl_keep isl_set *set,
  1110. __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
  1111. {
  1112. return print_disjuncts_map(set_to_map(set), space, p, latex);
  1113. }
  1114. struct isl_aff_split {
  1115. isl_basic_map *aff;
  1116. isl_map *map;
  1117. };
  1118. static void free_split(__isl_take struct isl_aff_split *split, int n)
  1119. {
  1120. int i;
  1121. if (!split)
  1122. return;
  1123. for (i = 0; i < n; ++i) {
  1124. isl_basic_map_free(split[i].aff);
  1125. isl_map_free(split[i].map);
  1126. }
  1127. free(split);
  1128. }
  1129. static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
  1130. {
  1131. int i, j;
  1132. isl_size nparam, n_in, n_out, total;
  1133. bmap = isl_basic_map_cow(bmap);
  1134. if (!bmap)
  1135. return NULL;
  1136. bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
  1137. nparam = isl_basic_map_dim(bmap, isl_dim_param);
  1138. n_in = isl_basic_map_dim(bmap, isl_dim_in);
  1139. n_out = isl_basic_map_dim(bmap, isl_dim_out);
  1140. total = isl_basic_map_dim(bmap, isl_dim_all);
  1141. if (n_in < 0 || n_out < 0 || nparam < 0 || total < 0)
  1142. return isl_basic_map_free(bmap);
  1143. for (i = bmap->n_eq - 1; i >= 0; --i) {
  1144. j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
  1145. if (j >= nparam && j < nparam + n_in + n_out &&
  1146. (isl_int_is_one(bmap->eq[i][1 + j]) ||
  1147. isl_int_is_negone(bmap->eq[i][1 + j])))
  1148. continue;
  1149. if (isl_basic_map_drop_equality(bmap, i) < 0)
  1150. goto error;
  1151. }
  1152. bmap = isl_basic_map_finalize(bmap);
  1153. return bmap;
  1154. error:
  1155. isl_basic_map_free(bmap);
  1156. return NULL;
  1157. }
  1158. static int aff_split_cmp(const void *p1, const void *p2, void *user)
  1159. {
  1160. const struct isl_aff_split *s1, *s2;
  1161. s1 = (const struct isl_aff_split *) p1;
  1162. s2 = (const struct isl_aff_split *) p2;
  1163. return isl_basic_map_plain_cmp(s1->aff, s2->aff);
  1164. }
  1165. static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
  1166. __isl_keep isl_basic_map *aff)
  1167. {
  1168. int i, j;
  1169. isl_size v_div;
  1170. v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
  1171. if (v_div < 0 || !aff)
  1172. goto error;
  1173. for (i = bmap->n_eq - 1; i >= 0; --i) {
  1174. if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div,
  1175. bmap->n_div) != -1)
  1176. continue;
  1177. for (j = 0; j < aff->n_eq; ++j) {
  1178. if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + v_div) &&
  1179. !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + v_div))
  1180. continue;
  1181. if (isl_basic_map_drop_equality(bmap, i) < 0)
  1182. goto error;
  1183. break;
  1184. }
  1185. }
  1186. return bmap;
  1187. error:
  1188. isl_basic_map_free(bmap);
  1189. return NULL;
  1190. }
  1191. static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
  1192. {
  1193. int i, n;
  1194. struct isl_aff_split *split;
  1195. isl_ctx *ctx;
  1196. ctx = isl_map_get_ctx(map);
  1197. split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
  1198. if (!split)
  1199. return NULL;
  1200. for (i = 0; i < map->n; ++i) {
  1201. isl_basic_map *bmap;
  1202. split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
  1203. bmap = isl_basic_map_copy(map->p[i]);
  1204. bmap = isl_basic_map_cow(bmap);
  1205. bmap = drop_aff(bmap, split[i].aff);
  1206. split[i].map = isl_map_from_basic_map(bmap);
  1207. if (!split[i].aff || !split[i].map)
  1208. goto error;
  1209. }
  1210. if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
  1211. &aff_split_cmp, NULL) < 0)
  1212. goto error;
  1213. n = map->n;
  1214. for (i = n - 1; i >= 1; --i) {
  1215. if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
  1216. split[i].aff))
  1217. continue;
  1218. isl_basic_map_free(split[i].aff);
  1219. split[i - 1].map = isl_map_union(split[i - 1].map,
  1220. split[i].map);
  1221. if (i != n - 1)
  1222. split[i] = split[n - 1];
  1223. split[n - 1].aff = NULL;
  1224. split[n - 1].map = NULL;
  1225. --n;
  1226. }
  1227. return split;
  1228. error:
  1229. free_split(split, map->n);
  1230. return NULL;
  1231. }
  1232. static int defining_equality(__isl_keep isl_basic_map *eq,
  1233. __isl_keep isl_space *space, enum isl_dim_type type, int pos)
  1234. {
  1235. int i;
  1236. isl_size total;
  1237. total = isl_basic_map_dim(eq, isl_dim_all);
  1238. if (total < 0)
  1239. return -1;
  1240. pos += isl_space_offset(space, type);
  1241. for (i = 0; i < eq->n_eq; ++i) {
  1242. if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
  1243. continue;
  1244. if (isl_int_is_one(eq->eq[i][1 + pos]))
  1245. isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
  1246. return i;
  1247. }
  1248. return -1;
  1249. }
  1250. /* Print dimension "pos" of data->space to "p".
  1251. *
  1252. * data->user is assumed to be an isl_basic_map keeping track of equalities.
  1253. *
  1254. * If the current dimension is defined by these equalities, then print
  1255. * the corresponding expression, assigned to the name of the dimension
  1256. * if there is any. Otherwise, print the name of the dimension.
  1257. */
  1258. static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
  1259. struct isl_print_space_data *data, unsigned pos)
  1260. {
  1261. isl_basic_map *eq = data->user;
  1262. int j;
  1263. j = defining_equality(eq, data->space, data->type, pos);
  1264. if (j >= 0) {
  1265. if (isl_space_has_dim_name(data->space, data->type, pos)) {
  1266. p = print_name(data->space, p, data->type, pos,
  1267. data->latex);
  1268. p = isl_printer_print_str(p, " = ");
  1269. }
  1270. pos += 1 + isl_space_offset(data->space, data->type);
  1271. p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
  1272. } else {
  1273. p = print_name(data->space, p, data->type, pos, data->latex);
  1274. }
  1275. return p;
  1276. }
  1277. static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
  1278. struct isl_aff_split *split, int n, __isl_keep isl_space *space)
  1279. {
  1280. struct isl_print_space_data data = { 0 };
  1281. int i;
  1282. int rational;
  1283. data.print_dim = &print_dim_eq;
  1284. for (i = 0; i < n; ++i) {
  1285. if (!split[i].map)
  1286. break;
  1287. rational = split[i].map->n > 0 &&
  1288. ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
  1289. if (i)
  1290. p = isl_printer_print_str(p, "; ");
  1291. data.user = split[i].aff;
  1292. p = isl_print_space(space, p, rational, &data);
  1293. p = print_disjuncts_map(split[i].map, space, p, 0);
  1294. }
  1295. return p;
  1296. }
  1297. static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
  1298. __isl_take isl_printer *p)
  1299. {
  1300. struct isl_print_space_data data = { 0 };
  1301. struct isl_aff_split *split = NULL;
  1302. int rational;
  1303. if (!p || !map)
  1304. return isl_printer_free(p);
  1305. if (!p->dump && map->n > 0)
  1306. split = split_aff(map);
  1307. if (split) {
  1308. p = print_split_map(p, split, map->n, map->dim);
  1309. } else {
  1310. rational = map->n > 0 &&
  1311. ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
  1312. p = isl_print_space(map->dim, p, rational, &data);
  1313. p = print_disjuncts_map(map, map->dim, p, 0);
  1314. }
  1315. free_split(split, map->n);
  1316. return p;
  1317. }
  1318. static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
  1319. __isl_take isl_printer *p)
  1320. {
  1321. struct isl_print_space_data data = { 0 };
  1322. p = print_param_tuple(p, map->dim, &data);
  1323. p = isl_printer_print_str(p, s_open_set[0]);
  1324. p = isl_map_print_isl_body(map, p);
  1325. p = isl_printer_print_str(p, s_close_set[0]);
  1326. return p;
  1327. }
  1328. static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
  1329. __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
  1330. {
  1331. struct isl_print_space_data data = { 0 };
  1332. data.latex = 1;
  1333. p = print_param_tuple(p, map->dim, &data);
  1334. p = isl_printer_print_str(p, s_open_set[1]);
  1335. data.print_dim = &print_dim_eq;
  1336. data.user = aff;
  1337. p = isl_print_space(map->dim, p, 0, &data);
  1338. p = print_disjuncts_map(map, map->dim, p, 1);
  1339. p = isl_printer_print_str(p, s_close_set[1]);
  1340. return p;
  1341. }
  1342. static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
  1343. __isl_take isl_printer *p)
  1344. {
  1345. int i;
  1346. struct isl_aff_split *split = NULL;
  1347. if (map->n > 0)
  1348. split = split_aff(map);
  1349. if (!split)
  1350. return print_latex_map(map, p, NULL);
  1351. for (i = 0; i < map->n; ++i) {
  1352. if (!split[i].map)
  1353. break;
  1354. if (i)
  1355. p = isl_printer_print_str(p, " \\cup ");
  1356. p = print_latex_map(split[i].map, p, split[i].aff);
  1357. }
  1358. free_split(split, map->n);
  1359. return p;
  1360. }
  1361. __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
  1362. __isl_keep isl_basic_map *bmap)
  1363. {
  1364. if (!p || !bmap)
  1365. goto error;
  1366. if (p->output_format == ISL_FORMAT_ISL)
  1367. return isl_basic_map_print_isl(bmap, p, 0);
  1368. else if (p->output_format == ISL_FORMAT_OMEGA)
  1369. return basic_map_print_omega(bmap, p);
  1370. isl_assert(bmap->ctx, 0, goto error);
  1371. error:
  1372. isl_printer_free(p);
  1373. return NULL;
  1374. }
  1375. __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
  1376. __isl_keep isl_basic_set *bset)
  1377. {
  1378. if (!p || !bset)
  1379. goto error;
  1380. if (p->output_format == ISL_FORMAT_ISL)
  1381. return isl_basic_map_print_isl(bset, p, 0);
  1382. else if (p->output_format == ISL_FORMAT_POLYLIB)
  1383. return isl_basic_set_print_polylib(bset, p, 0);
  1384. else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
  1385. return isl_basic_set_print_polylib(bset, p, 1);
  1386. else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
  1387. return bset_print_constraints_polylib(bset, p);
  1388. else if (p->output_format == ISL_FORMAT_OMEGA)
  1389. return basic_set_print_omega(bset, p);
  1390. isl_assert(p->ctx, 0, goto error);
  1391. error:
  1392. isl_printer_free(p);
  1393. return NULL;
  1394. }
  1395. __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
  1396. __isl_keep isl_set *set)
  1397. {
  1398. if (!p || !set)
  1399. goto error;
  1400. if (p->output_format == ISL_FORMAT_ISL)
  1401. return isl_map_print_isl(set_to_map(set), p);
  1402. else if (p->output_format == ISL_FORMAT_POLYLIB)
  1403. return isl_set_print_polylib(set, p, 0);
  1404. else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
  1405. return isl_set_print_polylib(set, p, 1);
  1406. else if (p->output_format == ISL_FORMAT_OMEGA)
  1407. return isl_set_print_omega(set, p);
  1408. else if (p->output_format == ISL_FORMAT_LATEX)
  1409. return isl_map_print_latex(set_to_map(set), p);
  1410. isl_assert(set->ctx, 0, goto error);
  1411. error:
  1412. isl_printer_free(p);
  1413. return NULL;
  1414. }
  1415. __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
  1416. __isl_keep isl_map *map)
  1417. {
  1418. if (!p || !map)
  1419. goto error;
  1420. if (p->output_format == ISL_FORMAT_ISL)
  1421. return isl_map_print_isl(map, p);
  1422. else if (p->output_format == ISL_FORMAT_POLYLIB)
  1423. return isl_map_print_polylib(map, p, 0);
  1424. else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
  1425. return isl_map_print_polylib(map, p, 1);
  1426. else if (p->output_format == ISL_FORMAT_OMEGA)
  1427. return isl_map_print_omega(map, p);
  1428. else if (p->output_format == ISL_FORMAT_LATEX)
  1429. return isl_map_print_latex(map, p);
  1430. isl_assert(map->ctx, 0, goto error);
  1431. error:
  1432. isl_printer_free(p);
  1433. return NULL;
  1434. }
  1435. struct isl_union_print_data {
  1436. isl_printer *p;
  1437. int first;
  1438. };
  1439. static isl_stat print_map_body(__isl_take isl_map *map, void *user)
  1440. {
  1441. struct isl_union_print_data *data;
  1442. data = (struct isl_union_print_data *)user;
  1443. if (!data->first)
  1444. data->p = isl_printer_print_str(data->p, "; ");
  1445. data->first = 0;
  1446. data->p = isl_map_print_isl_body(map, data->p);
  1447. isl_map_free(map);
  1448. return isl_stat_ok;
  1449. }
  1450. /* Print the body of "umap" (everything except the parameter declarations)
  1451. * to "p" in isl format.
  1452. */
  1453. static __isl_give isl_printer *isl_printer_print_union_map_isl_body(
  1454. __isl_take isl_printer *p, __isl_keep isl_union_map *umap)
  1455. {
  1456. struct isl_union_print_data data;
  1457. p = isl_printer_print_str(p, s_open_set[0]);
  1458. data.p = p;
  1459. data.first = 1;
  1460. isl_union_map_foreach_map(umap, &print_map_body, &data);
  1461. p = data.p;
  1462. p = isl_printer_print_str(p, s_close_set[0]);
  1463. return p;
  1464. }
  1465. /* Print the body of "uset" (everything except the parameter declarations)
  1466. * to "p" in isl format.
  1467. */
  1468. static __isl_give isl_printer *isl_printer_print_union_set_isl_body(
  1469. __isl_take isl_printer *p, __isl_keep isl_union_set *uset)
  1470. {
  1471. return isl_printer_print_union_map_isl_body(p, uset_to_umap(uset));
  1472. }
  1473. /* Print the isl_union_map "umap" to "p" in isl format.
  1474. */
  1475. static __isl_give isl_printer *isl_union_map_print_isl(
  1476. __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
  1477. {
  1478. struct isl_print_space_data space_data = { 0 };
  1479. isl_space *space;
  1480. space = isl_union_map_get_space(umap);
  1481. p = print_param_tuple(p, space, &space_data);
  1482. isl_space_free(space);
  1483. p = isl_printer_print_union_map_isl_body(p, umap);
  1484. return p;
  1485. }
  1486. static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
  1487. {
  1488. struct isl_union_print_data *data;
  1489. data = (struct isl_union_print_data *)user;
  1490. if (!data->first)
  1491. data->p = isl_printer_print_str(data->p, " \\cup ");
  1492. data->first = 0;
  1493. data->p = isl_map_print_latex(map, data->p);
  1494. isl_map_free(map);
  1495. return isl_stat_ok;
  1496. }
  1497. static __isl_give isl_printer *isl_union_map_print_latex(
  1498. __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
  1499. {
  1500. struct isl_union_print_data data = { p, 1 };
  1501. isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
  1502. p = data.p;
  1503. return p;
  1504. }
  1505. __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
  1506. __isl_keep isl_union_map *umap)
  1507. {
  1508. if (!p || !umap)
  1509. goto error;
  1510. if (p->output_format == ISL_FORMAT_ISL)
  1511. return isl_union_map_print_isl(umap, p);
  1512. if (p->output_format == ISL_FORMAT_LATEX)
  1513. return isl_union_map_print_latex(umap, p);
  1514. isl_die(p->ctx, isl_error_invalid,
  1515. "invalid output format for isl_union_map", goto error);
  1516. error:
  1517. isl_printer_free(p);
  1518. return NULL;
  1519. }
  1520. __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
  1521. __isl_keep isl_union_set *uset)
  1522. {
  1523. if (!p || !uset)
  1524. goto error;
  1525. if (p->output_format == ISL_FORMAT_ISL)
  1526. return isl_union_map_print_isl(uset_to_umap(uset), p);
  1527. if (p->output_format == ISL_FORMAT_LATEX)
  1528. return isl_union_map_print_latex(uset_to_umap(uset), p);
  1529. isl_die(p->ctx, isl_error_invalid,
  1530. "invalid output format for isl_union_set", goto error);
  1531. error:
  1532. isl_printer_free(p);
  1533. return NULL;
  1534. }
  1535. static isl_size poly_rec_n_non_zero(__isl_keep isl_poly_rec *rec)
  1536. {
  1537. int i;
  1538. int n;
  1539. if (!rec)
  1540. return isl_size_error;
  1541. for (i = 0, n = 0; i < rec->n; ++i) {
  1542. isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
  1543. if (is_zero < 0)
  1544. return isl_size_error;
  1545. if (!is_zero)
  1546. ++n;
  1547. }
  1548. return n;
  1549. }
  1550. static __isl_give isl_printer *poly_print_cst(__isl_keep isl_poly *poly,
  1551. __isl_take isl_printer *p, int first)
  1552. {
  1553. isl_poly_cst *cst;
  1554. int neg;
  1555. cst = isl_poly_as_cst(poly);
  1556. if (!cst)
  1557. goto error;
  1558. neg = !first && isl_int_is_neg(cst->n);
  1559. if (!first)
  1560. p = isl_printer_print_str(p, neg ? " - " : " + ");
  1561. if (neg)
  1562. isl_int_neg(cst->n, cst->n);
  1563. if (isl_int_is_zero(cst->d)) {
  1564. int sgn = isl_int_sgn(cst->n);
  1565. p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
  1566. sgn == 0 ? "NaN" : "infty");
  1567. } else
  1568. p = isl_printer_print_isl_int(p, cst->n);
  1569. if (neg)
  1570. isl_int_neg(cst->n, cst->n);
  1571. if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
  1572. p = isl_printer_print_str(p, "/");
  1573. p = isl_printer_print_isl_int(p, cst->d);
  1574. }
  1575. return p;
  1576. error:
  1577. isl_printer_free(p);
  1578. return NULL;
  1579. }
  1580. static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
  1581. __isl_keep isl_space *space, __isl_keep isl_mat *div, int var)
  1582. {
  1583. isl_size total;
  1584. total = isl_space_dim(space, isl_dim_all);
  1585. if (total < 0)
  1586. return isl_printer_free(p);
  1587. if (var < total)
  1588. p = print_term(space, NULL, space->ctx->one, 1 + var, p, 0);
  1589. else
  1590. p = print_div(space, div, var - total, p);
  1591. return p;
  1592. }
  1593. static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
  1594. __isl_keep isl_space *space, __isl_keep isl_mat *div, int var, int exp)
  1595. {
  1596. p = print_base(p, space, div, var);
  1597. if (exp == 1)
  1598. return p;
  1599. if (p->output_format == ISL_FORMAT_C) {
  1600. int i;
  1601. for (i = 1; i < exp; ++i) {
  1602. p = isl_printer_print_str(p, "*");
  1603. p = print_base(p, space, div, var);
  1604. }
  1605. } else {
  1606. p = isl_printer_print_str(p, "^");
  1607. p = isl_printer_print_int(p, exp);
  1608. }
  1609. return p;
  1610. }
  1611. /* Print the polynomial "poly" defined over the domain space "space" and
  1612. * local variables defined by "div" to "p".
  1613. */
  1614. static __isl_give isl_printer *poly_print(__isl_keep isl_poly *poly,
  1615. __isl_keep isl_space *space, __isl_keep isl_mat *div,
  1616. __isl_take isl_printer *p)
  1617. {
  1618. int i, first, print_parens;
  1619. isl_size n;
  1620. isl_bool is_cst;
  1621. isl_poly_rec *rec;
  1622. is_cst = isl_poly_is_cst(poly);
  1623. if (!p || is_cst < 0 || !space || !div)
  1624. goto error;
  1625. if (is_cst)
  1626. return poly_print_cst(poly, p, 1);
  1627. rec = isl_poly_as_rec(poly);
  1628. n = poly_rec_n_non_zero(rec);
  1629. if (n < 0)
  1630. return isl_printer_free(p);
  1631. print_parens = n > 1;
  1632. if (print_parens)
  1633. p = isl_printer_print_str(p, "(");
  1634. for (i = 0, first = 1; i < rec->n; ++i) {
  1635. isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
  1636. isl_bool is_one = isl_poly_is_one(rec->p[i]);
  1637. isl_bool is_negone = isl_poly_is_negone(rec->p[i]);
  1638. isl_bool is_cst = isl_poly_is_cst(rec->p[i]);
  1639. if (is_zero < 0 || is_one < 0 || is_negone < 0)
  1640. return isl_printer_free(p);
  1641. if (is_zero)
  1642. continue;
  1643. if (is_negone) {
  1644. if (!i)
  1645. p = isl_printer_print_str(p, "-1");
  1646. else if (first)
  1647. p = isl_printer_print_str(p, "-");
  1648. else
  1649. p = isl_printer_print_str(p, " - ");
  1650. } else if (is_cst && !is_one)
  1651. p = poly_print_cst(rec->p[i], p, first);
  1652. else {
  1653. if (!first)
  1654. p = isl_printer_print_str(p, " + ");
  1655. if (i == 0 || !is_one)
  1656. p = poly_print(rec->p[i], space, div, p);
  1657. }
  1658. first = 0;
  1659. if (i == 0)
  1660. continue;
  1661. if (!is_one && !is_negone)
  1662. p = isl_printer_print_str(p, " * ");
  1663. p = print_pow(p, space, div, rec->poly.var, i);
  1664. }
  1665. if (print_parens)
  1666. p = isl_printer_print_str(p, ")");
  1667. return p;
  1668. error:
  1669. isl_printer_free(p);
  1670. return NULL;
  1671. }
  1672. static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
  1673. __isl_keep isl_qpolynomial *qp)
  1674. {
  1675. if (!p || !qp)
  1676. goto error;
  1677. p = poly_print(qp->poly, qp->dim, qp->div, p);
  1678. return p;
  1679. error:
  1680. isl_printer_free(p);
  1681. return NULL;
  1682. }
  1683. static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
  1684. __isl_keep isl_qpolynomial *qp)
  1685. {
  1686. struct isl_print_space_data data = { 0 };
  1687. if (!p || !qp)
  1688. goto error;
  1689. p = print_param_tuple(p, qp->dim, &data);
  1690. p = isl_printer_print_str(p, "{ ");
  1691. if (!isl_space_is_params(qp->dim)) {
  1692. p = isl_print_space(qp->dim, p, 0, &data);
  1693. p = isl_printer_print_str(p, " -> ");
  1694. }
  1695. p = print_qpolynomial(p, qp);
  1696. p = isl_printer_print_str(p, " }");
  1697. return p;
  1698. error:
  1699. isl_printer_free(p);
  1700. return NULL;
  1701. }
  1702. /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
  1703. * taken from the domain space "space".
  1704. */
  1705. static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
  1706. __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
  1707. {
  1708. isl_bool is_one;
  1709. isl_val *den;
  1710. den = isl_qpolynomial_get_den(qp);
  1711. qp = isl_qpolynomial_copy(qp);
  1712. qp = isl_qpolynomial_scale_val(qp, isl_val_copy(den));
  1713. is_one = isl_val_is_one(den);
  1714. if (is_one < 0)
  1715. p = isl_printer_free(p);
  1716. if (!is_one)
  1717. p = isl_printer_print_str(p, "(");
  1718. if (qp)
  1719. p = poly_print(qp->poly, space, qp->div, p);
  1720. else
  1721. p = isl_printer_free(p);
  1722. if (!is_one) {
  1723. p = isl_printer_print_str(p, ")/");
  1724. p = isl_printer_print_val(p, den);
  1725. }
  1726. isl_qpolynomial_free(qp);
  1727. isl_val_free(den);
  1728. return p;
  1729. }
  1730. __isl_give isl_printer *isl_printer_print_qpolynomial(
  1731. __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
  1732. {
  1733. if (!p || !qp)
  1734. goto error;
  1735. if (p->output_format == ISL_FORMAT_ISL)
  1736. return print_qpolynomial_isl(p, qp);
  1737. else if (p->output_format == ISL_FORMAT_C)
  1738. return print_qpolynomial_c(p, qp->dim, qp);
  1739. else
  1740. isl_die(qp->dim->ctx, isl_error_unsupported,
  1741. "output format not supported for isl_qpolynomials",
  1742. goto error);
  1743. error:
  1744. isl_printer_free(p);
  1745. return NULL;
  1746. }
  1747. void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
  1748. unsigned output_format)
  1749. {
  1750. isl_printer *p;
  1751. if (!qp)
  1752. return;
  1753. isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
  1754. p = isl_printer_to_file(qp->dim->ctx, out);
  1755. p = isl_printer_print_qpolynomial(p, qp);
  1756. isl_printer_free(p);
  1757. }
  1758. static __isl_give isl_printer *qpolynomial_fold_print(
  1759. __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
  1760. {
  1761. int i;
  1762. isl_qpolynomial_list *list;
  1763. isl_size n;
  1764. list = isl_qpolynomial_fold_peek_list(fold);
  1765. n = isl_qpolynomial_list_size(list);
  1766. if (n < 0)
  1767. return isl_printer_free(p);
  1768. if (fold->type == isl_fold_min)
  1769. p = isl_printer_print_str(p, "min");
  1770. else if (fold->type == isl_fold_max)
  1771. p = isl_printer_print_str(p, "max");
  1772. p = isl_printer_print_str(p, "(");
  1773. for (i = 0; i < n; ++i) {
  1774. isl_qpolynomial *qp;
  1775. if (i)
  1776. p = isl_printer_print_str(p, ", ");
  1777. qp = isl_qpolynomial_list_peek(list, i);
  1778. p = print_qpolynomial(p, qp);
  1779. }
  1780. p = isl_printer_print_str(p, ")");
  1781. return p;
  1782. }
  1783. void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
  1784. FILE *out, unsigned output_format)
  1785. {
  1786. isl_printer *p;
  1787. if (!fold)
  1788. return;
  1789. isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
  1790. p = isl_printer_to_file(fold->dim->ctx, out);
  1791. p = isl_printer_print_qpolynomial_fold(p, fold);
  1792. isl_printer_free(p);
  1793. }
  1794. static __isl_give isl_printer *isl_pwqp_print_isl_body(
  1795. __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
  1796. {
  1797. struct isl_print_space_data data = { 0 };
  1798. int i = 0;
  1799. for (i = 0; i < pwqp->n; ++i) {
  1800. isl_space *space;
  1801. if (i)
  1802. p = isl_printer_print_str(p, "; ");
  1803. space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
  1804. if (!isl_space_is_params(space)) {
  1805. p = isl_print_space(space, p, 0, &data);
  1806. p = isl_printer_print_str(p, " -> ");
  1807. }
  1808. p = print_qpolynomial(p, pwqp->p[i].qp);
  1809. p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
  1810. isl_space_free(space);
  1811. }
  1812. return p;
  1813. }
  1814. static __isl_give isl_printer *print_pw_qpolynomial_isl(
  1815. __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
  1816. {
  1817. struct isl_print_space_data data = { 0 };
  1818. if (!p || !pwqp)
  1819. goto error;
  1820. p = print_param_tuple(p, pwqp->dim, &data);
  1821. p = isl_printer_print_str(p, "{ ");
  1822. if (pwqp->n == 0) {
  1823. if (!isl_space_is_set(pwqp->dim)) {
  1824. p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
  1825. p = isl_printer_print_str(p, " -> ");
  1826. }
  1827. p = isl_printer_print_str(p, "0");
  1828. }
  1829. p = isl_pwqp_print_isl_body(p, pwqp);
  1830. p = isl_printer_print_str(p, " }");
  1831. return p;
  1832. error:
  1833. isl_printer_free(p);
  1834. return NULL;
  1835. }
  1836. void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
  1837. unsigned output_format)
  1838. {
  1839. isl_printer *p;
  1840. if (!pwqp)
  1841. return;
  1842. p = isl_printer_to_file(pwqp->dim->ctx, out);
  1843. p = isl_printer_set_output_format(p, output_format);
  1844. p = isl_printer_print_pw_qpolynomial(p, pwqp);
  1845. isl_printer_free(p);
  1846. }
  1847. static __isl_give isl_printer *isl_pwf_print_isl_body(
  1848. __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
  1849. {
  1850. struct isl_print_space_data data = { 0 };
  1851. int i = 0;
  1852. for (i = 0; i < pwf->n; ++i) {
  1853. isl_space *space;
  1854. if (i)
  1855. p = isl_printer_print_str(p, "; ");
  1856. space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
  1857. if (!isl_space_is_params(space)) {
  1858. p = isl_print_space(space, p, 0, &data);
  1859. p = isl_printer_print_str(p, " -> ");
  1860. }
  1861. p = qpolynomial_fold_print(pwf->p[i].fold, p);
  1862. p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
  1863. isl_space_free(space);
  1864. }
  1865. return p;
  1866. }
  1867. static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
  1868. __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
  1869. {
  1870. struct isl_print_space_data data = { 0 };
  1871. p = print_param_tuple(p, pwf->dim, &data);
  1872. p = isl_printer_print_str(p, "{ ");
  1873. if (pwf->n == 0) {
  1874. if (!isl_space_is_set(pwf->dim)) {
  1875. p = print_tuple(pwf->dim, p, isl_dim_in, &data);
  1876. p = isl_printer_print_str(p, " -> ");
  1877. }
  1878. p = isl_printer_print_str(p, "0");
  1879. }
  1880. p = isl_pwf_print_isl_body(p, pwf);
  1881. p = isl_printer_print_str(p, " }");
  1882. return p;
  1883. }
  1884. static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
  1885. __isl_keep isl_local_space *ls, isl_int *c);
  1886. /* We skip the constraint if it is implied by the div expression.
  1887. *
  1888. * *first indicates whether this is the first constraint in the conjunction and
  1889. * is updated if the constraint is actually printed.
  1890. */
  1891. static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
  1892. __isl_keep isl_local_space *ls, isl_int *c, const char *op, int *first)
  1893. {
  1894. unsigned o_div;
  1895. isl_size n_div;
  1896. int div;
  1897. o_div = isl_local_space_offset(ls, isl_dim_div);
  1898. n_div = isl_local_space_dim(ls, isl_dim_div);
  1899. if (n_div < 0)
  1900. return isl_printer_free(p);
  1901. div = isl_seq_last_non_zero(c + o_div, n_div);
  1902. if (div >= 0) {
  1903. isl_bool is_div = isl_local_space_is_div_constraint(ls, c, div);
  1904. if (is_div < 0)
  1905. return isl_printer_free(p);
  1906. if (is_div)
  1907. return p;
  1908. }
  1909. if (!*first)
  1910. p = isl_printer_print_str(p, " && ");
  1911. p = print_ls_affine_c(p, ls, c);
  1912. p = isl_printer_print_str(p, " ");
  1913. p = isl_printer_print_str(p, op);
  1914. p = isl_printer_print_str(p, " 0");
  1915. *first = 0;
  1916. return p;
  1917. }
  1918. static __isl_give isl_printer *print_ls_partial_affine_c(
  1919. __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
  1920. isl_int *c, unsigned len);
  1921. static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
  1922. __isl_keep isl_space *space, __isl_keep isl_basic_set *bset)
  1923. {
  1924. int i, j;
  1925. int first = 1;
  1926. isl_size n_div = isl_basic_set_dim(bset, isl_dim_div);
  1927. isl_size total = isl_basic_set_dim(bset, isl_dim_all);
  1928. isl_mat *div;
  1929. isl_local_space *ls;
  1930. if (n_div < 0 || total < 0)
  1931. return isl_printer_free(p);
  1932. total -= n_div;
  1933. div = isl_basic_set_get_divs(bset);
  1934. ls = isl_local_space_alloc_div(isl_space_copy(space), div);
  1935. for (i = 0; i < bset->n_eq; ++i) {
  1936. j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
  1937. if (j < 0)
  1938. p = print_constraint_c(p, ls,
  1939. bset->eq[i], "==", &first);
  1940. else {
  1941. if (i)
  1942. p = isl_printer_print_str(p, " && ");
  1943. p = isl_printer_print_str(p, "(");
  1944. p = print_ls_partial_affine_c(p, ls, bset->eq[i],
  1945. 1 + total + j);
  1946. p = isl_printer_print_str(p, ") % ");
  1947. p = isl_printer_print_isl_int(p,
  1948. bset->eq[i][1 + total + j]);
  1949. p = isl_printer_print_str(p, " == 0");
  1950. first = 0;
  1951. }
  1952. }
  1953. for (i = 0; i < bset->n_ineq; ++i)
  1954. p = print_constraint_c(p, ls, bset->ineq[i], ">=", &first);
  1955. isl_local_space_free(ls);
  1956. return p;
  1957. }
  1958. static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
  1959. __isl_keep isl_space *space, __isl_keep isl_set *set)
  1960. {
  1961. int i;
  1962. if (!set)
  1963. return isl_printer_free(p);
  1964. if (set->n == 0)
  1965. p = isl_printer_print_str(p, "0");
  1966. for (i = 0; i < set->n; ++i) {
  1967. if (i)
  1968. p = isl_printer_print_str(p, " || ");
  1969. if (set->n > 1)
  1970. p = isl_printer_print_str(p, "(");
  1971. p = print_basic_set_c(p, space, set->p[i]);
  1972. if (set->n > 1)
  1973. p = isl_printer_print_str(p, ")");
  1974. }
  1975. return p;
  1976. }
  1977. /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
  1978. */
  1979. static __isl_give isl_printer *print_pw_qpolynomial_c(
  1980. __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
  1981. {
  1982. int i;
  1983. isl_space *space;
  1984. space = isl_pw_qpolynomial_get_domain_space(pwqp);
  1985. if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
  1986. p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
  1987. isl_space_free(space);
  1988. return p;
  1989. }
  1990. for (i = 0; i < pwqp->n; ++i) {
  1991. p = isl_printer_print_str(p, "(");
  1992. p = print_set_c(p, space, pwqp->p[i].set);
  1993. p = isl_printer_print_str(p, ") ? (");
  1994. p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
  1995. p = isl_printer_print_str(p, ") : ");
  1996. }
  1997. isl_space_free(space);
  1998. p = isl_printer_print_str(p, "0");
  1999. return p;
  2000. }
  2001. __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
  2002. __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
  2003. {
  2004. if (!p || !pwqp)
  2005. goto error;
  2006. if (p->output_format == ISL_FORMAT_ISL)
  2007. return print_pw_qpolynomial_isl(p, pwqp);
  2008. else if (p->output_format == ISL_FORMAT_C)
  2009. return print_pw_qpolynomial_c(p, pwqp);
  2010. isl_assert(p->ctx, 0, goto error);
  2011. error:
  2012. isl_printer_free(p);
  2013. return NULL;
  2014. }
  2015. static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
  2016. {
  2017. struct isl_union_print_data *data;
  2018. data = (struct isl_union_print_data *)user;
  2019. if (!data->first)
  2020. data->p = isl_printer_print_str(data->p, "; ");
  2021. data->first = 0;
  2022. data->p = isl_pwqp_print_isl_body(data->p, pwqp);
  2023. isl_pw_qpolynomial_free(pwqp);
  2024. return isl_stat_ok;
  2025. }
  2026. static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
  2027. __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
  2028. {
  2029. struct isl_union_print_data data;
  2030. struct isl_print_space_data space_data = { 0 };
  2031. isl_space *space;
  2032. space = isl_union_pw_qpolynomial_get_space(upwqp);
  2033. p = print_param_tuple(p, space, &space_data);
  2034. isl_space_free(space);
  2035. p = isl_printer_print_str(p, "{ ");
  2036. data.p = p;
  2037. data.first = 1;
  2038. isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
  2039. &data);
  2040. p = data.p;
  2041. p = isl_printer_print_str(p, " }");
  2042. return p;
  2043. }
  2044. __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
  2045. __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
  2046. {
  2047. if (!p || !upwqp)
  2048. goto error;
  2049. if (p->output_format == ISL_FORMAT_ISL)
  2050. return print_union_pw_qpolynomial_isl(p, upwqp);
  2051. isl_die(p->ctx, isl_error_invalid,
  2052. "invalid output format for isl_union_pw_qpolynomial",
  2053. goto error);
  2054. error:
  2055. isl_printer_free(p);
  2056. return NULL;
  2057. }
  2058. /* Print the quasi-polynomial reduction "fold" to "p" in C format,
  2059. * with the variable names taken from the domain space "space".
  2060. */
  2061. static __isl_give isl_printer *print_qpolynomial_fold_c(
  2062. __isl_take isl_printer *p, __isl_keep isl_space *space,
  2063. __isl_keep isl_qpolynomial_fold *fold)
  2064. {
  2065. int i;
  2066. isl_qpolynomial_list *list;
  2067. isl_size n;
  2068. list = isl_qpolynomial_fold_peek_list(fold);
  2069. n = isl_qpolynomial_list_size(list);
  2070. if (n < 0)
  2071. return isl_printer_free(p);
  2072. for (i = 0; i < n - 1; ++i)
  2073. if (fold->type == isl_fold_min)
  2074. p = isl_printer_print_str(p, "min(");
  2075. else if (fold->type == isl_fold_max)
  2076. p = isl_printer_print_str(p, "max(");
  2077. for (i = 0; i < n; ++i) {
  2078. isl_qpolynomial *qp;
  2079. if (i)
  2080. p = isl_printer_print_str(p, ", ");
  2081. qp = isl_qpolynomial_list_peek(list, i);
  2082. p = print_qpolynomial_c(p, space, qp);
  2083. if (i)
  2084. p = isl_printer_print_str(p, ")");
  2085. }
  2086. return p;
  2087. }
  2088. __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
  2089. __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
  2090. {
  2091. if (!p || !fold)
  2092. goto error;
  2093. if (p->output_format == ISL_FORMAT_ISL)
  2094. return qpolynomial_fold_print(fold, p);
  2095. else if (p->output_format == ISL_FORMAT_C)
  2096. return print_qpolynomial_fold_c(p, fold->dim, fold);
  2097. isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
  2098. goto error);
  2099. error:
  2100. isl_printer_free(p);
  2101. return NULL;
  2102. }
  2103. /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
  2104. */
  2105. static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
  2106. __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
  2107. {
  2108. int i;
  2109. isl_space *space;
  2110. space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
  2111. if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
  2112. p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
  2113. isl_space_free(space);
  2114. return p;
  2115. }
  2116. for (i = 0; i < pwf->n; ++i) {
  2117. p = isl_printer_print_str(p, "(");
  2118. p = print_set_c(p, space, pwf->p[i].set);
  2119. p = isl_printer_print_str(p, ") ? (");
  2120. p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
  2121. p = isl_printer_print_str(p, ") : ");
  2122. }
  2123. isl_space_free(space);
  2124. p = isl_printer_print_str(p, "0");
  2125. return p;
  2126. }
  2127. __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
  2128. __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
  2129. {
  2130. if (!p || !pwf)
  2131. goto error;
  2132. if (p->output_format == ISL_FORMAT_ISL)
  2133. return print_pw_qpolynomial_fold_isl(p, pwf);
  2134. else if (p->output_format == ISL_FORMAT_C)
  2135. return print_pw_qpolynomial_fold_c(p, pwf);
  2136. isl_assert(p->ctx, 0, goto error);
  2137. error:
  2138. isl_printer_free(p);
  2139. return NULL;
  2140. }
  2141. void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
  2142. FILE *out, unsigned output_format)
  2143. {
  2144. isl_printer *p;
  2145. if (!pwf)
  2146. return;
  2147. p = isl_printer_to_file(pwf->dim->ctx, out);
  2148. p = isl_printer_set_output_format(p, output_format);
  2149. p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
  2150. isl_printer_free(p);
  2151. }
  2152. static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
  2153. void *user)
  2154. {
  2155. struct isl_union_print_data *data;
  2156. data = (struct isl_union_print_data *)user;
  2157. if (!data->first)
  2158. data->p = isl_printer_print_str(data->p, "; ");
  2159. data->first = 0;
  2160. data->p = isl_pwf_print_isl_body(data->p, pwf);
  2161. isl_pw_qpolynomial_fold_free(pwf);
  2162. return isl_stat_ok;
  2163. }
  2164. static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
  2165. __isl_take isl_printer *p,
  2166. __isl_keep isl_union_pw_qpolynomial_fold *upwf)
  2167. {
  2168. struct isl_union_print_data data;
  2169. struct isl_print_space_data space_data = { 0 };
  2170. isl_space *space;
  2171. space = isl_union_pw_qpolynomial_fold_get_space(upwf);
  2172. p = print_param_tuple(p, space, &space_data);
  2173. isl_space_free(space);
  2174. p = isl_printer_print_str(p, "{ ");
  2175. data.p = p;
  2176. data.first = 1;
  2177. isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
  2178. &print_pwf_body, &data);
  2179. p = data.p;
  2180. p = isl_printer_print_str(p, " }");
  2181. return p;
  2182. }
  2183. __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
  2184. __isl_take isl_printer *p,
  2185. __isl_keep isl_union_pw_qpolynomial_fold *upwf)
  2186. {
  2187. if (!p || !upwf)
  2188. goto error;
  2189. if (p->output_format == ISL_FORMAT_ISL)
  2190. return print_union_pw_qpolynomial_fold_isl(p, upwf);
  2191. isl_die(p->ctx, isl_error_invalid,
  2192. "invalid output format for isl_union_pw_qpolynomial_fold",
  2193. goto error);
  2194. error:
  2195. isl_printer_free(p);
  2196. return NULL;
  2197. }
  2198. /* Print the isl_constraint "c" to "p".
  2199. */
  2200. __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
  2201. __isl_keep isl_constraint *c)
  2202. {
  2203. struct isl_print_space_data data = { 0 };
  2204. isl_local_space *ls;
  2205. isl_space *space;
  2206. isl_bool exists;
  2207. if (!p || !c)
  2208. goto error;
  2209. ls = isl_constraint_get_local_space(c);
  2210. if (!ls)
  2211. return isl_printer_free(p);
  2212. space = isl_local_space_get_space(ls);
  2213. p = print_param_tuple(p, space, &data);
  2214. p = isl_printer_print_str(p, "{ ");
  2215. p = isl_print_space(space, p, 0, &data);
  2216. p = isl_printer_print_str(p, " : ");
  2217. exists = need_exists(p, ls->div);
  2218. if (exists < 0)
  2219. p = isl_printer_free(p);
  2220. if (exists >= 0 && exists)
  2221. p = open_exists(p, space, ls->div, 0);
  2222. p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
  2223. if (isl_constraint_is_equality(c))
  2224. p = isl_printer_print_str(p, " = 0");
  2225. else
  2226. p = isl_printer_print_str(p, " >= 0");
  2227. if (exists >= 0 && exists)
  2228. p = isl_printer_print_str(p, s_close_exists[0]);
  2229. p = isl_printer_print_str(p, " }");
  2230. isl_space_free(space);
  2231. isl_local_space_free(ls);
  2232. return p;
  2233. error:
  2234. isl_printer_free(p);
  2235. return NULL;
  2236. }
  2237. static __isl_give isl_printer *isl_printer_print_space_isl(
  2238. __isl_take isl_printer *p, __isl_keep isl_space *space)
  2239. {
  2240. struct isl_print_space_data data = { 0 };
  2241. if (!space)
  2242. goto error;
  2243. p = print_param_tuple(p, space, &data);
  2244. p = isl_printer_print_str(p, "{ ");
  2245. if (isl_space_is_params(space))
  2246. p = isl_printer_print_str(p, s_such_that[0]);
  2247. else
  2248. p = isl_print_space(space, p, 0, &data);
  2249. p = isl_printer_print_str(p, " }");
  2250. return p;
  2251. error:
  2252. isl_printer_free(p);
  2253. return NULL;
  2254. }
  2255. __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
  2256. __isl_keep isl_space *space)
  2257. {
  2258. if (!p || !space)
  2259. return isl_printer_free(p);
  2260. if (p->output_format == ISL_FORMAT_ISL)
  2261. return isl_printer_print_space_isl(p, space);
  2262. else if (p->output_format == ISL_FORMAT_OMEGA)
  2263. return print_omega_parameters(space, p);
  2264. isl_die(isl_space_get_ctx(space), isl_error_unsupported,
  2265. "output format not supported for space",
  2266. return isl_printer_free(p));
  2267. }
  2268. __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
  2269. __isl_keep isl_local_space *ls)
  2270. {
  2271. struct isl_print_space_data data = { 0 };
  2272. isl_size n_div;
  2273. n_div = isl_local_space_dim(ls, isl_dim_div);
  2274. if (n_div < 0)
  2275. goto error;
  2276. p = print_param_tuple(p, ls->dim, &data);
  2277. p = isl_printer_print_str(p, "{ ");
  2278. p = isl_print_space(ls->dim, p, 0, &data);
  2279. if (n_div > 0) {
  2280. p = isl_printer_print_str(p, " : ");
  2281. p = isl_printer_print_str(p, s_open_exists[0]);
  2282. p = print_div_list(p, ls->dim, ls->div, 0, 1);
  2283. p = isl_printer_print_str(p, s_close_exists[0]);
  2284. } else if (isl_space_is_params(ls->dim))
  2285. p = isl_printer_print_str(p, s_such_that[0]);
  2286. p = isl_printer_print_str(p, " }");
  2287. return p;
  2288. error:
  2289. isl_printer_free(p);
  2290. return NULL;
  2291. }
  2292. /* Look for the last of the "n" integer divisions that is used in "aff" and
  2293. * that can be printed as a modulo and
  2294. * return the position of this integer division.
  2295. * Return "n" if no such integer division can be found.
  2296. * Return isl_size_error on error.
  2297. *
  2298. * In particular, look for an integer division that appears in "aff"
  2299. * with a coefficient that is a multiple of the denominator
  2300. * of the integer division.
  2301. * That is, check if the numerator of "aff" is of the form
  2302. *
  2303. * f(...) + a m floor(g/m)
  2304. *
  2305. * and return the position of "floor(g/m)".
  2306. *
  2307. * Note that, unlike print_as_modulo_pos, no check needs to be made
  2308. * for whether the integer division can be printed, since it will
  2309. * need to be printed as an integer division anyway if it is not printed
  2310. * as a modulo.
  2311. */
  2312. static isl_size last_modulo(__isl_keep isl_printer *p, __isl_keep isl_aff *aff,
  2313. unsigned n)
  2314. {
  2315. isl_size o_div;
  2316. int i;
  2317. if (n == 0)
  2318. return n;
  2319. o_div = isl_aff_domain_offset(aff, isl_dim_div);
  2320. if (o_div < 0)
  2321. return isl_size_error;
  2322. for (i = n - 1; i >= 0; --i) {
  2323. if (isl_int_is_zero(aff->v->el[1 + o_div + i]))
  2324. continue;
  2325. if (isl_int_is_divisible_by(aff->v->el[1 + o_div + i],
  2326. aff->ls->div->row[i][0]))
  2327. return i;
  2328. }
  2329. return n;
  2330. }
  2331. /* Print the numerator of the affine expression "aff" to "p",
  2332. * with the variable names taken from "space".
  2333. */
  2334. static __isl_give isl_printer *print_aff_num_base(__isl_take isl_printer *p,
  2335. __isl_keep isl_space *space, __isl_keep isl_aff *aff)
  2336. {
  2337. isl_size total;
  2338. total = isl_aff_domain_dim(aff, isl_dim_all);
  2339. if (total < 0)
  2340. return isl_printer_free(p);
  2341. p = print_affine_of_len(space, aff->ls->div, p,
  2342. aff->v->el + 1, 1 + total);
  2343. return p;
  2344. }
  2345. static __isl_give isl_printer *print_aff_num(__isl_take isl_printer *p,
  2346. __isl_keep isl_space *space, __isl_keep isl_aff *aff);
  2347. /* Print the modulo term "c" * ("aff" mod "mod") to "p",
  2348. * with the variable names taken from "space".
  2349. * If "first" is set, then this is the first term of an expression.
  2350. */
  2351. static __isl_give isl_printer *print_mod_term(__isl_take isl_printer *p,
  2352. __isl_keep isl_space *space, __isl_keep isl_aff *aff, int first,
  2353. __isl_take isl_val *c, __isl_keep isl_val *mod)
  2354. {
  2355. isl_bool is_one, is_neg;
  2356. is_neg = isl_val_is_neg(c);
  2357. if (is_neg < 0)
  2358. p = isl_printer_free(p);
  2359. if (!first) {
  2360. if (is_neg)
  2361. c = isl_val_neg(c);
  2362. p = isl_printer_print_str(p, is_neg ? " - " : " + ");
  2363. }
  2364. is_one = isl_val_is_one(c);
  2365. if (is_one < 0)
  2366. p = isl_printer_free(p);
  2367. if (!is_one) {
  2368. p = isl_printer_print_val(p, c);
  2369. p = isl_printer_print_str(p, "*(");
  2370. }
  2371. p = isl_printer_print_str(p, "(");
  2372. p = print_aff_num(p, space, aff);
  2373. p = isl_printer_print_str(p, ")");
  2374. p = isl_printer_print_str(p, " mod ");
  2375. p = isl_printer_print_val(p, mod);
  2376. if (!is_one)
  2377. p = isl_printer_print_str(p, ")");
  2378. isl_val_free(c);
  2379. return p;
  2380. }
  2381. /* Print the numerator of the affine expression "aff" to "p",
  2382. * with the variable names taken from "space",
  2383. * given that the numerator of "aff" is of the form
  2384. *
  2385. * f(...) + a m floor(g/m)
  2386. *
  2387. * with "floor(g/m)" the integer division at position "last".
  2388. *
  2389. * First replace "aff" by its numerator and rewrite it as
  2390. *
  2391. * f(...) + a g - a (g mod m)
  2392. *
  2393. * Recursively write out (the numerator of) "f(...) + a g"
  2394. * (which may involve other modulo expressions) and
  2395. * then write out "- a (g mod m)".
  2396. */
  2397. static __isl_give isl_printer *print_aff_num_mod(__isl_take isl_printer *p,
  2398. __isl_keep isl_space *space, __isl_keep isl_aff *aff, unsigned last)
  2399. {
  2400. isl_bool is_zero;
  2401. isl_val *a, *m;
  2402. isl_aff *div, *term;
  2403. aff = isl_aff_copy(aff);
  2404. aff = isl_aff_scale_val(aff, isl_aff_get_denominator_val(aff));
  2405. a = isl_aff_get_coefficient_val(aff, isl_dim_div, last);
  2406. aff = isl_aff_set_coefficient_si(aff, isl_dim_div, last, 0);
  2407. div = isl_aff_get_div(aff, last);
  2408. m = isl_aff_get_denominator_val(div);
  2409. a = isl_val_div(a, isl_val_copy(m));
  2410. div = isl_aff_scale_val(div, isl_val_copy(m));
  2411. term = isl_aff_scale_val(isl_aff_copy(div), isl_val_copy(a));
  2412. aff = isl_aff_add(aff, term);
  2413. is_zero = isl_aff_plain_is_zero(aff);
  2414. if (is_zero < 0) {
  2415. p = isl_printer_free(p);
  2416. } else {
  2417. if (!is_zero)
  2418. p = print_aff_num(p, space, aff);
  2419. a = isl_val_neg(a);
  2420. p = print_mod_term(p, space, div, is_zero, isl_val_copy(a), m);
  2421. }
  2422. isl_val_free(a);
  2423. isl_val_free(m);
  2424. isl_aff_free(aff);
  2425. isl_aff_free(div);
  2426. return p;
  2427. }
  2428. /* Print the numerator of the affine expression "aff" to "p",
  2429. * with the variable names taken from "space",
  2430. * separating out any (obvious) modulo expressions.
  2431. *
  2432. * In particular, look for modulo expressions in "aff",
  2433. * separating them out if found and simply printing out "aff" otherwise.
  2434. */
  2435. static __isl_give isl_printer *print_aff_num(__isl_take isl_printer *p,
  2436. __isl_keep isl_space *space, __isl_keep isl_aff *aff)
  2437. {
  2438. isl_size n_div, mod;
  2439. n_div = isl_aff_dim(aff, isl_dim_div);
  2440. if (n_div < 0)
  2441. return isl_printer_free(p);
  2442. mod = last_modulo(p, aff, n_div);
  2443. if (mod < 0)
  2444. return isl_printer_free(p);
  2445. if (mod < n_div)
  2446. return print_aff_num_mod(p, space, aff, mod);
  2447. else
  2448. return print_aff_num_base(p, space, aff);
  2449. }
  2450. /* Print the (potentially rational) affine expression "aff" to "p",
  2451. * with the variable names taken from "space".
  2452. */
  2453. static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
  2454. __isl_keep isl_space *space, __isl_keep isl_aff *aff)
  2455. {
  2456. if (isl_aff_is_nan(aff))
  2457. return isl_printer_print_str(p, "NaN");
  2458. p = isl_printer_print_str(p, "(");
  2459. p = print_aff_num(p, space, aff);
  2460. if (isl_int_is_one(aff->v->el[0]))
  2461. p = isl_printer_print_str(p, ")");
  2462. else {
  2463. p = isl_printer_print_str(p, ")/");
  2464. p = isl_printer_print_isl_int(p, aff->v->el[0]);
  2465. }
  2466. return p;
  2467. }
  2468. static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
  2469. __isl_keep isl_aff *aff)
  2470. {
  2471. struct isl_print_space_data data = { 0 };
  2472. if (isl_space_is_params(aff->ls->dim))
  2473. ;
  2474. else {
  2475. p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
  2476. p = isl_printer_print_str(p, " -> ");
  2477. }
  2478. p = isl_printer_print_str(p, "[");
  2479. p = print_aff_body(p, aff->ls->dim, aff);
  2480. p = isl_printer_print_str(p, "]");
  2481. return p;
  2482. }
  2483. static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
  2484. __isl_keep isl_aff *aff)
  2485. {
  2486. struct isl_print_space_data data = { 0 };
  2487. if (!aff)
  2488. goto error;
  2489. p = print_param_tuple(p, aff->ls->dim, &data);
  2490. p = isl_printer_print_str(p, "{ ");
  2491. p = print_aff(p, aff);
  2492. p = isl_printer_print_str(p, " }");
  2493. return p;
  2494. error:
  2495. isl_printer_free(p);
  2496. return NULL;
  2497. }
  2498. /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
  2499. * sequence of affine expressions, each followed by constraints.
  2500. */
  2501. static __isl_give isl_printer *print_pw_aff_body(
  2502. __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
  2503. {
  2504. int i;
  2505. if (!pa)
  2506. return isl_printer_free(p);
  2507. for (i = 0; i < pa->n; ++i) {
  2508. isl_space *space;
  2509. if (i)
  2510. p = isl_printer_print_str(p, "; ");
  2511. p = print_aff(p, pa->p[i].aff);
  2512. space = isl_aff_get_domain_space(pa->p[i].aff);
  2513. p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
  2514. isl_space_free(space);
  2515. }
  2516. return p;
  2517. }
  2518. static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
  2519. __isl_keep isl_pw_aff *pwaff)
  2520. {
  2521. struct isl_print_space_data data = { 0 };
  2522. if (!pwaff)
  2523. goto error;
  2524. p = print_param_tuple(p, pwaff->dim, &data);
  2525. p = isl_printer_print_str(p, "{ ");
  2526. p = print_pw_aff_body(p, pwaff);
  2527. p = isl_printer_print_str(p, " }");
  2528. return p;
  2529. error:
  2530. isl_printer_free(p);
  2531. return NULL;
  2532. }
  2533. static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
  2534. __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
  2535. {
  2536. if (type == isl_dim_div) {
  2537. p = isl_printer_print_str(p, "floord(");
  2538. p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
  2539. p = isl_printer_print_str(p, ", ");
  2540. p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
  2541. p = isl_printer_print_str(p, ")");
  2542. } else {
  2543. const char *name;
  2544. name = isl_space_get_dim_name(ls->dim, type, pos);
  2545. if (!name)
  2546. name = "UNNAMED";
  2547. p = isl_printer_print_str(p, name);
  2548. }
  2549. return p;
  2550. }
  2551. static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
  2552. __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
  2553. {
  2554. enum isl_dim_type type;
  2555. if (!p || !ls)
  2556. return isl_printer_free(p);
  2557. if (pos == 0)
  2558. return isl_printer_print_isl_int(p, c);
  2559. if (isl_int_is_one(c))
  2560. ;
  2561. else if (isl_int_is_negone(c))
  2562. p = isl_printer_print_str(p, "-");
  2563. else {
  2564. p = isl_printer_print_isl_int(p, c);
  2565. p = isl_printer_print_str(p, "*");
  2566. }
  2567. if (pos2type(ls->dim, &type, &pos) < 0)
  2568. return isl_printer_free(p);
  2569. p = print_ls_name_c(p, ls, type, pos);
  2570. return p;
  2571. }
  2572. static __isl_give isl_printer *print_ls_partial_affine_c(
  2573. __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
  2574. isl_int *c, unsigned len)
  2575. {
  2576. int i;
  2577. int first;
  2578. for (i = 0, first = 1; i < len; ++i) {
  2579. int flip = 0;
  2580. if (isl_int_is_zero(c[i]))
  2581. continue;
  2582. if (!first) {
  2583. if (isl_int_is_neg(c[i])) {
  2584. flip = 1;
  2585. isl_int_neg(c[i], c[i]);
  2586. p = isl_printer_print_str(p, " - ");
  2587. } else
  2588. p = isl_printer_print_str(p, " + ");
  2589. }
  2590. first = 0;
  2591. p = print_ls_term_c(p, ls, c[i], i);
  2592. if (flip)
  2593. isl_int_neg(c[i], c[i]);
  2594. }
  2595. if (first)
  2596. p = isl_printer_print_str(p, "0");
  2597. return p;
  2598. }
  2599. static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
  2600. __isl_keep isl_local_space *ls, isl_int *c)
  2601. {
  2602. isl_size total = isl_local_space_dim(ls, isl_dim_all);
  2603. if (total < 0)
  2604. return isl_printer_free(p);
  2605. return print_ls_partial_affine_c(p, ls, c, 1 + total);
  2606. }
  2607. static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
  2608. __isl_keep isl_aff *aff)
  2609. {
  2610. isl_size total;
  2611. total = isl_aff_domain_dim(aff, isl_dim_all);
  2612. if (total < 0)
  2613. return isl_printer_free(p);
  2614. if (!isl_int_is_one(aff->v->el[0]))
  2615. p = isl_printer_print_str(p, "(");
  2616. p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
  2617. if (!isl_int_is_one(aff->v->el[0])) {
  2618. p = isl_printer_print_str(p, ")/");
  2619. p = isl_printer_print_isl_int(p, aff->v->el[0]);
  2620. }
  2621. return p;
  2622. }
  2623. /* In the C format, we cannot express that "pwaff" may be undefined
  2624. * on parts of the domain space. We therefore assume that the expression
  2625. * will only be evaluated on its definition domain and compute the gist
  2626. * of each cell with respect to this domain.
  2627. */
  2628. static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
  2629. __isl_keep isl_pw_aff *pwaff)
  2630. {
  2631. isl_set *domain;
  2632. isl_ast_build *build;
  2633. isl_ast_expr *expr;
  2634. if (pwaff->n < 1)
  2635. isl_die(p->ctx, isl_error_unsupported,
  2636. "cannot print empty isl_pw_aff in C format",
  2637. return isl_printer_free(p));
  2638. domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
  2639. build = isl_ast_build_from_context(domain);
  2640. expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
  2641. p = isl_printer_print_ast_expr(p, expr);
  2642. isl_ast_expr_free(expr);
  2643. isl_ast_build_free(build);
  2644. return p;
  2645. }
  2646. __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
  2647. __isl_keep isl_aff *aff)
  2648. {
  2649. if (!p || !aff)
  2650. goto error;
  2651. if (p->output_format == ISL_FORMAT_ISL)
  2652. return print_aff_isl(p, aff);
  2653. else if (p->output_format == ISL_FORMAT_C)
  2654. return print_aff_c(p, aff);
  2655. isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
  2656. goto error);
  2657. error:
  2658. isl_printer_free(p);
  2659. return NULL;
  2660. }
  2661. __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
  2662. __isl_keep isl_pw_aff *pwaff)
  2663. {
  2664. if (!p || !pwaff)
  2665. goto error;
  2666. if (p->output_format == ISL_FORMAT_ISL)
  2667. return print_pw_aff_isl(p, pwaff);
  2668. else if (p->output_format == ISL_FORMAT_C)
  2669. return print_pw_aff_c(p, pwaff);
  2670. isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
  2671. goto error);
  2672. error:
  2673. isl_printer_free(p);
  2674. return NULL;
  2675. }
  2676. /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
  2677. * Each isl_pw_aff itself is also printed as semicolon delimited
  2678. * sequence of pieces.
  2679. * If data->first = 1, then this is the first in the sequence.
  2680. * Update data->first to tell the next element that it is not the first.
  2681. */
  2682. static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
  2683. void *user)
  2684. {
  2685. struct isl_union_print_data *data;
  2686. data = (struct isl_union_print_data *) user;
  2687. if (!data->first)
  2688. data->p = isl_printer_print_str(data->p, "; ");
  2689. data->first = 0;
  2690. data->p = print_pw_aff_body(data->p, pa);
  2691. isl_pw_aff_free(pa);
  2692. return data->p ? isl_stat_ok : isl_stat_error;
  2693. }
  2694. /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
  2695. * sequence of affine expressions, each followed by constraints,
  2696. * with the sequence enclosed in braces.
  2697. */
  2698. static __isl_give isl_printer *print_union_pw_aff_body(
  2699. __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
  2700. {
  2701. struct isl_union_print_data data = { p, 1 };
  2702. p = isl_printer_print_str(p, s_open_set[0]);
  2703. data.p = p;
  2704. if (isl_union_pw_aff_foreach_pw_aff(upa,
  2705. &print_pw_aff_body_wrap, &data) < 0)
  2706. data.p = isl_printer_free(data.p);
  2707. p = data.p;
  2708. p = isl_printer_print_str(p, s_close_set[0]);
  2709. return p;
  2710. }
  2711. /* Print the isl_union_pw_aff "upa" to "p" in isl format.
  2712. *
  2713. * The individual isl_pw_affs are delimited by a semicolon.
  2714. */
  2715. static __isl_give isl_printer *print_union_pw_aff_isl(
  2716. __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
  2717. {
  2718. struct isl_print_space_data data = { 0 };
  2719. isl_space *space;
  2720. space = isl_union_pw_aff_get_space(upa);
  2721. p = print_param_tuple(p, space, &data);
  2722. isl_space_free(space);
  2723. p = print_union_pw_aff_body(p, upa);
  2724. return p;
  2725. }
  2726. /* Print the isl_union_pw_aff "upa" to "p".
  2727. *
  2728. * We currently only support an isl format.
  2729. */
  2730. __isl_give isl_printer *isl_printer_print_union_pw_aff(
  2731. __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
  2732. {
  2733. if (!p || !upa)
  2734. return isl_printer_free(p);
  2735. if (p->output_format == ISL_FORMAT_ISL)
  2736. return print_union_pw_aff_isl(p, upa);
  2737. isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
  2738. "unsupported output format", return isl_printer_free(p));
  2739. }
  2740. /* Print dimension "pos" of data->space to "p".
  2741. *
  2742. * data->user is assumed to be an isl_multi_aff.
  2743. *
  2744. * If the current dimension is an output dimension, then print
  2745. * the corresponding expression. Otherwise, print the name of the dimension.
  2746. * Make sure to use the domain space for printing names as
  2747. * that is the space that will be used for printing constraints (if any).
  2748. */
  2749. static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
  2750. struct isl_print_space_data *data, unsigned pos)
  2751. {
  2752. isl_multi_aff *ma = data->user;
  2753. isl_space *space;
  2754. space = isl_multi_aff_get_domain_space(ma);
  2755. if (data->type == isl_dim_out) {
  2756. p = print_aff_body(p, space, ma->u.p[pos]);
  2757. } else {
  2758. enum isl_dim_type type = data->type;
  2759. if (type == isl_dim_in)
  2760. type = isl_dim_set;
  2761. p = print_name(space, p, type, pos, data->latex);
  2762. }
  2763. isl_space_free(space);
  2764. return p;
  2765. }
  2766. static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
  2767. __isl_keep isl_multi_aff *maff)
  2768. {
  2769. struct isl_print_space_data data = { 0 };
  2770. data.print_dim = &print_dim_ma;
  2771. data.user = maff;
  2772. return isl_print_space(maff->space, p, 0, &data);
  2773. }
  2774. static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
  2775. __isl_keep isl_multi_aff *maff)
  2776. {
  2777. struct isl_print_space_data data = { 0 };
  2778. if (!maff)
  2779. goto error;
  2780. p = print_param_tuple(p, maff->space, &data);
  2781. p = isl_printer_print_str(p, "{ ");
  2782. p = print_multi_aff(p, maff);
  2783. p = isl_printer_print_str(p, " }");
  2784. return p;
  2785. error:
  2786. isl_printer_free(p);
  2787. return NULL;
  2788. }
  2789. __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
  2790. __isl_keep isl_multi_aff *maff)
  2791. {
  2792. if (!p || !maff)
  2793. goto error;
  2794. if (p->output_format == ISL_FORMAT_ISL)
  2795. return print_multi_aff_isl(p, maff);
  2796. isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
  2797. goto error);
  2798. error:
  2799. isl_printer_free(p);
  2800. return NULL;
  2801. }
  2802. static __isl_give isl_printer *print_pw_multi_aff_body(
  2803. __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
  2804. {
  2805. int i;
  2806. if (!pma)
  2807. goto error;
  2808. for (i = 0; i < pma->n; ++i) {
  2809. isl_space *space;
  2810. if (i)
  2811. p = isl_printer_print_str(p, "; ");
  2812. p = print_multi_aff(p, pma->p[i].maff);
  2813. space = isl_multi_aff_get_domain_space(pma->p[i].maff);
  2814. p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
  2815. isl_space_free(space);
  2816. }
  2817. return p;
  2818. error:
  2819. isl_printer_free(p);
  2820. return NULL;
  2821. }
  2822. static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
  2823. __isl_keep isl_pw_multi_aff *pma)
  2824. {
  2825. struct isl_print_space_data data = { 0 };
  2826. if (!pma)
  2827. goto error;
  2828. p = print_param_tuple(p, pma->dim, &data);
  2829. p = isl_printer_print_str(p, "{ ");
  2830. p = print_pw_multi_aff_body(p, pma);
  2831. p = isl_printer_print_str(p, " }");
  2832. return p;
  2833. error:
  2834. isl_printer_free(p);
  2835. return NULL;
  2836. }
  2837. /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
  2838. * to "p".
  2839. */
  2840. static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
  2841. __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
  2842. {
  2843. int i;
  2844. isl_space *space;
  2845. space = isl_pw_multi_aff_get_domain_space(pma);
  2846. for (i = 0; i < pma->n - 1; ++i) {
  2847. p = isl_printer_print_str(p, "(");
  2848. p = print_set_c(p, space, pma->p[i].set);
  2849. p = isl_printer_print_str(p, ") ? (");
  2850. p = print_aff_c(p, pma->p[i].maff->u.p[0]);
  2851. p = isl_printer_print_str(p, ") : ");
  2852. }
  2853. isl_space_free(space);
  2854. return print_aff_c(p, pma->p[pma->n - 1].maff->u.p[0]);
  2855. }
  2856. static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
  2857. __isl_keep isl_pw_multi_aff *pma)
  2858. {
  2859. isl_size n;
  2860. const char *name;
  2861. if (!pma)
  2862. goto error;
  2863. if (pma->n < 1)
  2864. isl_die(p->ctx, isl_error_unsupported,
  2865. "cannot print empty isl_pw_multi_aff in C format",
  2866. goto error);
  2867. n = isl_pw_multi_aff_dim(pma, isl_dim_out);
  2868. if (n < 0)
  2869. return isl_printer_free(p);
  2870. name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
  2871. if (!name && n == 1)
  2872. return print_unnamed_pw_multi_aff_c(p, pma);
  2873. if (!name)
  2874. isl_die(p->ctx, isl_error_unsupported,
  2875. "cannot print unnamed isl_pw_multi_aff in C format",
  2876. goto error);
  2877. p = isl_printer_print_str(p, name);
  2878. if (n != 0)
  2879. isl_die(p->ctx, isl_error_unsupported,
  2880. "not supported yet", goto error);
  2881. return p;
  2882. error:
  2883. isl_printer_free(p);
  2884. return NULL;
  2885. }
  2886. __isl_give isl_printer *isl_printer_print_pw_multi_aff(
  2887. __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
  2888. {
  2889. if (!p || !pma)
  2890. goto error;
  2891. if (p->output_format == ISL_FORMAT_ISL)
  2892. return print_pw_multi_aff_isl(p, pma);
  2893. if (p->output_format == ISL_FORMAT_C)
  2894. return print_pw_multi_aff_c(p, pma);
  2895. isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
  2896. goto error);
  2897. error:
  2898. isl_printer_free(p);
  2899. return NULL;
  2900. }
  2901. static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
  2902. void *user)
  2903. {
  2904. struct isl_union_print_data *data;
  2905. data = (struct isl_union_print_data *) user;
  2906. if (!data->first)
  2907. data->p = isl_printer_print_str(data->p, "; ");
  2908. data->first = 0;
  2909. data->p = print_pw_multi_aff_body(data->p, pma);
  2910. isl_pw_multi_aff_free(pma);
  2911. return isl_stat_ok;
  2912. }
  2913. static __isl_give isl_printer *print_union_pw_multi_aff_isl(
  2914. __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
  2915. {
  2916. struct isl_union_print_data data;
  2917. struct isl_print_space_data space_data = { 0 };
  2918. isl_space *space;
  2919. space = isl_union_pw_multi_aff_get_space(upma);
  2920. p = print_param_tuple(p, space, &space_data);
  2921. isl_space_free(space);
  2922. p = isl_printer_print_str(p, s_open_set[0]);
  2923. data.p = p;
  2924. data.first = 1;
  2925. isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
  2926. &print_pw_multi_aff_body_wrap, &data);
  2927. p = data.p;
  2928. p = isl_printer_print_str(p, s_close_set[0]);
  2929. return p;
  2930. }
  2931. __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
  2932. __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
  2933. {
  2934. if (!p || !upma)
  2935. goto error;
  2936. if (p->output_format == ISL_FORMAT_ISL)
  2937. return print_union_pw_multi_aff_isl(p, upma);
  2938. isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
  2939. goto error);
  2940. error:
  2941. isl_printer_free(p);
  2942. return NULL;
  2943. }
  2944. /* Print dimension "pos" of data->space to "p".
  2945. *
  2946. * data->user is assumed to be an isl_multi_pw_aff.
  2947. *
  2948. * If the current dimension is an output dimension, then print
  2949. * the corresponding piecewise affine expression.
  2950. * Otherwise, print the name of the dimension.
  2951. * Make sure to use the same space in both cases.
  2952. * In particular, use the domain space for printing names as
  2953. * that is the space that is used for printing constraints.
  2954. */
  2955. static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
  2956. struct isl_print_space_data *data, unsigned pos)
  2957. {
  2958. int i;
  2959. int need_parens;
  2960. isl_space *space;
  2961. isl_multi_pw_aff *mpa = data->user;
  2962. isl_pw_aff *pa;
  2963. if (data->type != isl_dim_out) {
  2964. enum isl_dim_type type = data->type;
  2965. if (type == isl_dim_in)
  2966. type = isl_dim_set;
  2967. space = isl_multi_pw_aff_get_domain_space(mpa);
  2968. p = print_name(space, p, type, pos, data->latex);
  2969. isl_space_free(space);
  2970. return p;
  2971. }
  2972. pa = mpa->u.p[pos];
  2973. if (pa->n == 0)
  2974. return isl_printer_print_str(p, "(0 : false)");
  2975. need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
  2976. if (need_parens)
  2977. p = isl_printer_print_str(p, "(");
  2978. space = isl_multi_pw_aff_get_domain_space(mpa);
  2979. for (i = 0; i < pa->n; ++i) {
  2980. if (i)
  2981. p = isl_printer_print_str(p, "; ");
  2982. p = print_aff_body(p, space, pa->p[i].aff);
  2983. p = print_disjuncts(pa->p[i].set, space, p, 0);
  2984. }
  2985. isl_space_free(space);
  2986. if (need_parens)
  2987. p = isl_printer_print_str(p, ")");
  2988. return p;
  2989. }
  2990. /* Print "mpa" to "p" in isl format.
  2991. *
  2992. * If "mpa" is zero-dimensional and has a non-trivial explicit domain,
  2993. * then it is printed after the tuple of affine expressions.
  2994. */
  2995. static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
  2996. __isl_keep isl_multi_pw_aff *mpa)
  2997. {
  2998. struct isl_print_space_data data = { 0 };
  2999. isl_bool has_domain;
  3000. if (!mpa)
  3001. return isl_printer_free(p);
  3002. p = print_param_tuple(p, mpa->space, &data);
  3003. p = isl_printer_print_str(p, "{ ");
  3004. data.print_dim = &print_dim_mpa;
  3005. data.user = mpa;
  3006. p = isl_print_space(mpa->space, p, 0, &data);
  3007. has_domain = isl_multi_pw_aff_has_non_trivial_domain(mpa);
  3008. if (has_domain < 0)
  3009. return isl_printer_free(p);
  3010. if (has_domain) {
  3011. isl_space *space;
  3012. space = isl_space_domain(isl_space_copy(mpa->space));
  3013. p = print_disjuncts_set(mpa->u.dom, space, p, 0);
  3014. isl_space_free(space);
  3015. }
  3016. p = isl_printer_print_str(p, " }");
  3017. return p;
  3018. }
  3019. __isl_give isl_printer *isl_printer_print_multi_pw_aff(
  3020. __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
  3021. {
  3022. if (!p || !mpa)
  3023. return isl_printer_free(p);
  3024. if (p->output_format == ISL_FORMAT_ISL)
  3025. return print_multi_pw_aff_isl(p, mpa);
  3026. isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
  3027. return isl_printer_free(p));
  3028. }
  3029. /* Print dimension "pos" of data->space to "p".
  3030. *
  3031. * data->user is assumed to be an isl_multi_val.
  3032. *
  3033. * If the current dimension is an output dimension, then print
  3034. * the corresponding value. Otherwise, print the name of the dimension.
  3035. */
  3036. static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
  3037. struct isl_print_space_data *data, unsigned pos)
  3038. {
  3039. isl_multi_val *mv = data->user;
  3040. if (data->type == isl_dim_out)
  3041. return isl_printer_print_val(p, mv->u.p[pos]);
  3042. else
  3043. return print_name(data->space, p, data->type, pos, data->latex);
  3044. }
  3045. /* Print the isl_multi_val "mv" to "p" in isl format.
  3046. */
  3047. static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
  3048. __isl_keep isl_multi_val *mv)
  3049. {
  3050. struct isl_print_space_data data = { 0 };
  3051. if (!mv)
  3052. return isl_printer_free(p);
  3053. p = print_param_tuple(p, mv->space, &data);
  3054. p = isl_printer_print_str(p, "{ ");
  3055. data.print_dim = &print_dim_mv;
  3056. data.user = mv;
  3057. p = isl_print_space(mv->space, p, 0, &data);
  3058. p = isl_printer_print_str(p, " }");
  3059. return p;
  3060. }
  3061. /* Print the isl_multi_val "mv" to "p".
  3062. *
  3063. * Currently only supported in isl format.
  3064. */
  3065. __isl_give isl_printer *isl_printer_print_multi_val(
  3066. __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
  3067. {
  3068. if (!p || !mv)
  3069. return isl_printer_free(p);
  3070. if (p->output_format == ISL_FORMAT_ISL)
  3071. return print_multi_val_isl(p, mv);
  3072. isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
  3073. return isl_printer_free(p));
  3074. }
  3075. /* Print dimension "pos" of data->space to "p".
  3076. *
  3077. * data->user is assumed to be an isl_multi_id.
  3078. *
  3079. * If the current dimension is an output dimension, then print
  3080. * the corresponding identifier. Otherwise, print the name of the dimension.
  3081. */
  3082. static __isl_give isl_printer *print_dim_mi(__isl_take isl_printer *p,
  3083. struct isl_print_space_data *data, unsigned pos)
  3084. {
  3085. isl_multi_id *mi = data->user;
  3086. if (data->type == isl_dim_out)
  3087. return isl_printer_print_id(p, mi->u.p[pos]);
  3088. else
  3089. return print_name(data->space, p, data->type, pos, data->latex);
  3090. }
  3091. /* Print the isl_multi_id "mi" to "p" in isl format.
  3092. */
  3093. static __isl_give isl_printer *print_multi_id_isl(__isl_take isl_printer *p,
  3094. __isl_keep isl_multi_id *mi)
  3095. {
  3096. isl_space *space;
  3097. struct isl_print_space_data data = { 0 };
  3098. space = isl_multi_id_peek_space(mi);
  3099. p = print_param_tuple(p, space, &data);
  3100. p = isl_printer_print_str(p, "{ ");
  3101. data.print_dim = &print_dim_mi;
  3102. data.user = mi;
  3103. p = isl_print_space(space, p, 0, &data);
  3104. p = isl_printer_print_str(p, " }");
  3105. return p;
  3106. }
  3107. /* Print the isl_multi_id "mi" to "p".
  3108. *
  3109. * Currently only supported in isl format.
  3110. */
  3111. __isl_give isl_printer *isl_printer_print_multi_id(
  3112. __isl_take isl_printer *p, __isl_keep isl_multi_id *mi)
  3113. {
  3114. if (!p || !mi)
  3115. return isl_printer_free(p);
  3116. if (p->output_format == ISL_FORMAT_ISL)
  3117. return print_multi_id_isl(p, mi);
  3118. isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
  3119. "unsupported output format", return isl_printer_free(p));
  3120. }
  3121. /* Print dimension "pos" of data->space to "p".
  3122. *
  3123. * data->user is assumed to be an isl_multi_union_pw_aff.
  3124. *
  3125. * The current dimension is necessarily a set dimension, so
  3126. * we print the corresponding isl_union_pw_aff, including
  3127. * the braces.
  3128. */
  3129. static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
  3130. struct isl_print_space_data *data, unsigned pos)
  3131. {
  3132. isl_multi_union_pw_aff *mupa = data->user;
  3133. isl_union_pw_aff *upa;
  3134. upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
  3135. p = print_union_pw_aff_body(p, upa);
  3136. isl_union_pw_aff_free(upa);
  3137. return p;
  3138. }
  3139. /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
  3140. *
  3141. * If "mupa" is zero-dimensional and has a non-trivial explicit domain,
  3142. * then it is printed after the tuple of affine expressions.
  3143. * In order to clarify that this domain belongs to the expression,
  3144. * the tuple along with the domain are placed inside parentheses.
  3145. * If "mupa" has any parameters, then the opening parenthesis
  3146. * appears after the parameter declarations.
  3147. */
  3148. static __isl_give isl_printer *print_multi_union_pw_aff_isl(
  3149. __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
  3150. {
  3151. struct isl_print_space_data data = { 0 };
  3152. isl_bool has_domain;
  3153. isl_space *space;
  3154. if (!mupa)
  3155. return isl_printer_free(p);
  3156. has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
  3157. if (has_domain < 0)
  3158. return isl_printer_free(p);
  3159. space = isl_multi_union_pw_aff_get_space(mupa);
  3160. p = print_param_tuple(p, space, &data);
  3161. if (has_domain)
  3162. p = isl_printer_print_str(p, "(");
  3163. data.print_dim = &print_union_pw_aff_dim;
  3164. data.user = mupa;
  3165. p = isl_print_space(space, p, 0, &data);
  3166. isl_space_free(space);
  3167. if (has_domain) {
  3168. p = isl_printer_print_str(p, " : ");
  3169. p = isl_printer_print_union_set_isl_body(p, mupa->u.dom);
  3170. p = isl_printer_print_str(p, ")");
  3171. }
  3172. return p;
  3173. }
  3174. /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
  3175. *
  3176. * We currently only support an isl format.
  3177. */
  3178. __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
  3179. __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
  3180. {
  3181. if (!p || !mupa)
  3182. return isl_printer_free(p);
  3183. if (p->output_format == ISL_FORMAT_ISL)
  3184. return print_multi_union_pw_aff_isl(p, mupa);
  3185. isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
  3186. "unsupported output format", return isl_printer_free(p));
  3187. }