isl_ast.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972
  1. /*
  2. * Copyright 2012-2013 Ecole Normale Superieure
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege,
  7. * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
  8. */
  9. #include <string.h>
  10. #include <isl/id.h>
  11. #include <isl/val.h>
  12. #include <isl_ast_private.h>
  13. #undef EL_BASE
  14. #define EL_BASE ast_expr
  15. #include <isl_list_templ.c>
  16. #undef EL_BASE
  17. #define EL_BASE ast_node
  18. #include <isl_list_templ.c>
  19. isl_ctx *isl_ast_print_options_get_ctx(
  20. __isl_keep isl_ast_print_options *options)
  21. {
  22. return options ? options->ctx : NULL;
  23. }
  24. __isl_give isl_ast_print_options *isl_ast_print_options_alloc(isl_ctx *ctx)
  25. {
  26. isl_ast_print_options *options;
  27. options = isl_calloc_type(ctx, isl_ast_print_options);
  28. if (!options)
  29. return NULL;
  30. options->ctx = ctx;
  31. isl_ctx_ref(ctx);
  32. options->ref = 1;
  33. return options;
  34. }
  35. __isl_give isl_ast_print_options *isl_ast_print_options_dup(
  36. __isl_keep isl_ast_print_options *options)
  37. {
  38. isl_ctx *ctx;
  39. isl_ast_print_options *dup;
  40. if (!options)
  41. return NULL;
  42. ctx = isl_ast_print_options_get_ctx(options);
  43. dup = isl_ast_print_options_alloc(ctx);
  44. if (!dup)
  45. return NULL;
  46. dup->print_for = options->print_for;
  47. dup->print_for_user = options->print_for_user;
  48. dup->print_user = options->print_user;
  49. dup->print_user_user = options->print_user_user;
  50. return dup;
  51. }
  52. __isl_give isl_ast_print_options *isl_ast_print_options_cow(
  53. __isl_take isl_ast_print_options *options)
  54. {
  55. if (!options)
  56. return NULL;
  57. if (options->ref == 1)
  58. return options;
  59. options->ref--;
  60. return isl_ast_print_options_dup(options);
  61. }
  62. __isl_give isl_ast_print_options *isl_ast_print_options_copy(
  63. __isl_keep isl_ast_print_options *options)
  64. {
  65. if (!options)
  66. return NULL;
  67. options->ref++;
  68. return options;
  69. }
  70. __isl_null isl_ast_print_options *isl_ast_print_options_free(
  71. __isl_take isl_ast_print_options *options)
  72. {
  73. if (!options)
  74. return NULL;
  75. if (--options->ref > 0)
  76. return NULL;
  77. isl_ctx_deref(options->ctx);
  78. free(options);
  79. return NULL;
  80. }
  81. /* Set the print_user callback of "options" to "print_user".
  82. *
  83. * If this callback is set, then it is used to print user nodes in the AST.
  84. * Otherwise, the expression associated to the user node is printed.
  85. */
  86. __isl_give isl_ast_print_options *isl_ast_print_options_set_print_user(
  87. __isl_take isl_ast_print_options *options,
  88. __isl_give isl_printer *(*print_user)(__isl_take isl_printer *p,
  89. __isl_take isl_ast_print_options *options,
  90. __isl_keep isl_ast_node *node, void *user),
  91. void *user)
  92. {
  93. options = isl_ast_print_options_cow(options);
  94. if (!options)
  95. return NULL;
  96. options->print_user = print_user;
  97. options->print_user_user = user;
  98. return options;
  99. }
  100. /* Set the print_for callback of "options" to "print_for".
  101. *
  102. * If this callback is set, then it used to print for nodes in the AST.
  103. */
  104. __isl_give isl_ast_print_options *isl_ast_print_options_set_print_for(
  105. __isl_take isl_ast_print_options *options,
  106. __isl_give isl_printer *(*print_for)(__isl_take isl_printer *p,
  107. __isl_take isl_ast_print_options *options,
  108. __isl_keep isl_ast_node *node, void *user),
  109. void *user)
  110. {
  111. options = isl_ast_print_options_cow(options);
  112. if (!options)
  113. return NULL;
  114. options->print_for = print_for;
  115. options->print_for_user = user;
  116. return options;
  117. }
  118. __isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr)
  119. {
  120. if (!expr)
  121. return NULL;
  122. expr->ref++;
  123. return expr;
  124. }
  125. __isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr)
  126. {
  127. int i;
  128. isl_ctx *ctx;
  129. isl_ast_expr *dup;
  130. if (!expr)
  131. return NULL;
  132. ctx = isl_ast_expr_get_ctx(expr);
  133. switch (expr->type) {
  134. case isl_ast_expr_int:
  135. dup = isl_ast_expr_from_val(isl_val_copy(expr->u.v));
  136. break;
  137. case isl_ast_expr_id:
  138. dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id));
  139. break;
  140. case isl_ast_expr_op:
  141. dup = isl_ast_expr_alloc_op(ctx,
  142. expr->u.op.op, expr->u.op.n_arg);
  143. if (!dup)
  144. return NULL;
  145. for (i = 0; i < expr->u.op.n_arg; ++i)
  146. dup->u.op.args[i] =
  147. isl_ast_expr_copy(expr->u.op.args[i]);
  148. break;
  149. case isl_ast_expr_error:
  150. dup = NULL;
  151. }
  152. if (!dup)
  153. return NULL;
  154. return dup;
  155. }
  156. __isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr)
  157. {
  158. if (!expr)
  159. return NULL;
  160. if (expr->ref == 1)
  161. return expr;
  162. expr->ref--;
  163. return isl_ast_expr_dup(expr);
  164. }
  165. __isl_null isl_ast_expr *isl_ast_expr_free(__isl_take isl_ast_expr *expr)
  166. {
  167. int i;
  168. if (!expr)
  169. return NULL;
  170. if (--expr->ref > 0)
  171. return NULL;
  172. isl_ctx_deref(expr->ctx);
  173. switch (expr->type) {
  174. case isl_ast_expr_int:
  175. isl_val_free(expr->u.v);
  176. break;
  177. case isl_ast_expr_id:
  178. isl_id_free(expr->u.id);
  179. break;
  180. case isl_ast_expr_op:
  181. if (expr->u.op.args)
  182. for (i = 0; i < expr->u.op.n_arg; ++i)
  183. isl_ast_expr_free(expr->u.op.args[i]);
  184. free(expr->u.op.args);
  185. break;
  186. case isl_ast_expr_error:
  187. break;
  188. }
  189. free(expr);
  190. return NULL;
  191. }
  192. isl_ctx *isl_ast_expr_get_ctx(__isl_keep isl_ast_expr *expr)
  193. {
  194. return expr ? expr->ctx : NULL;
  195. }
  196. enum isl_ast_expr_type isl_ast_expr_get_type(__isl_keep isl_ast_expr *expr)
  197. {
  198. return expr ? expr->type : isl_ast_expr_error;
  199. }
  200. /* Return the integer value represented by "expr".
  201. */
  202. __isl_give isl_val *isl_ast_expr_int_get_val(__isl_keep isl_ast_expr *expr)
  203. {
  204. if (!expr)
  205. return NULL;
  206. if (expr->type != isl_ast_expr_int)
  207. isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
  208. "expression not an int", return NULL);
  209. return isl_val_copy(expr->u.v);
  210. }
  211. /* This is an alternative name for the function above.
  212. */
  213. __isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr)
  214. {
  215. return isl_ast_expr_int_get_val(expr);
  216. }
  217. __isl_give isl_id *isl_ast_expr_id_get_id(__isl_keep isl_ast_expr *expr)
  218. {
  219. if (!expr)
  220. return NULL;
  221. if (expr->type != isl_ast_expr_id)
  222. isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
  223. "expression not an identifier", return NULL);
  224. return isl_id_copy(expr->u.id);
  225. }
  226. /* This is an alternative name for the function above.
  227. */
  228. __isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
  229. {
  230. return isl_ast_expr_id_get_id(expr);
  231. }
  232. /* Return the type of operation represented by "expr".
  233. */
  234. enum isl_ast_expr_op_type isl_ast_expr_op_get_type(
  235. __isl_keep isl_ast_expr *expr)
  236. {
  237. if (!expr)
  238. return isl_ast_expr_op_error;
  239. if (expr->type != isl_ast_expr_op)
  240. isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
  241. "expression not an operation",
  242. return isl_ast_expr_op_error);
  243. return expr->u.op.op;
  244. }
  245. /* This is an alternative name for the function above.
  246. */
  247. enum isl_ast_expr_op_type isl_ast_expr_get_op_type(
  248. __isl_keep isl_ast_expr *expr)
  249. {
  250. return isl_ast_expr_op_get_type(expr);
  251. }
  252. /* Return the number of arguments of the operation represented by "expr".
  253. */
  254. isl_size isl_ast_expr_op_get_n_arg(__isl_keep isl_ast_expr *expr)
  255. {
  256. if (!expr)
  257. return isl_size_error;
  258. if (expr->type != isl_ast_expr_op)
  259. isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
  260. "expression not an operation", return isl_size_error);
  261. return expr->u.op.n_arg;
  262. }
  263. /* This is an alternative name for the function above.
  264. */
  265. isl_size isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
  266. {
  267. return isl_ast_expr_op_get_n_arg(expr);
  268. }
  269. /* Return the argument at position "pos" of the operation represented by "expr".
  270. */
  271. __isl_give isl_ast_expr *isl_ast_expr_op_get_arg(__isl_keep isl_ast_expr *expr,
  272. int pos)
  273. {
  274. if (!expr)
  275. return NULL;
  276. if (expr->type != isl_ast_expr_op)
  277. isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
  278. "expression not an operation", return NULL);
  279. if (pos < 0 || pos >= expr->u.op.n_arg)
  280. isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
  281. "index out of bounds", return NULL);
  282. return isl_ast_expr_copy(expr->u.op.args[pos]);
  283. }
  284. /* This is an alternative name for the function above.
  285. */
  286. __isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
  287. int pos)
  288. {
  289. return isl_ast_expr_op_get_arg(expr, pos);
  290. }
  291. /* Replace the argument at position "pos" of "expr" by "arg".
  292. */
  293. __isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
  294. int pos, __isl_take isl_ast_expr *arg)
  295. {
  296. expr = isl_ast_expr_cow(expr);
  297. if (!expr || !arg)
  298. goto error;
  299. if (expr->type != isl_ast_expr_op)
  300. isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
  301. "expression not an operation", goto error);
  302. if (pos < 0 || pos >= expr->u.op.n_arg)
  303. isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
  304. "index out of bounds", goto error);
  305. isl_ast_expr_free(expr->u.op.args[pos]);
  306. expr->u.op.args[pos] = arg;
  307. return expr;
  308. error:
  309. isl_ast_expr_free(arg);
  310. return isl_ast_expr_free(expr);
  311. }
  312. /* Is "expr1" equal to "expr2"?
  313. */
  314. isl_bool isl_ast_expr_is_equal(__isl_keep isl_ast_expr *expr1,
  315. __isl_keep isl_ast_expr *expr2)
  316. {
  317. int i;
  318. if (!expr1 || !expr2)
  319. return isl_bool_error;
  320. if (expr1 == expr2)
  321. return isl_bool_true;
  322. if (expr1->type != expr2->type)
  323. return isl_bool_false;
  324. switch (expr1->type) {
  325. case isl_ast_expr_int:
  326. return isl_val_eq(expr1->u.v, expr2->u.v);
  327. case isl_ast_expr_id:
  328. return isl_bool_ok(expr1->u.id == expr2->u.id);
  329. case isl_ast_expr_op:
  330. if (expr1->u.op.op != expr2->u.op.op)
  331. return isl_bool_false;
  332. if (expr1->u.op.n_arg != expr2->u.op.n_arg)
  333. return isl_bool_false;
  334. for (i = 0; i < expr1->u.op.n_arg; ++i) {
  335. isl_bool equal;
  336. equal = isl_ast_expr_is_equal(expr1->u.op.args[i],
  337. expr2->u.op.args[i]);
  338. if (equal < 0 || !equal)
  339. return equal;
  340. }
  341. return isl_bool_true;
  342. case isl_ast_expr_error:
  343. return isl_bool_error;
  344. }
  345. isl_die(isl_ast_expr_get_ctx(expr1), isl_error_internal,
  346. "unhandled case", return isl_bool_error);
  347. }
  348. /* Create a new operation expression of operation type "op",
  349. * with "n_arg" as yet unspecified arguments.
  350. */
  351. __isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
  352. enum isl_ast_expr_op_type op, int n_arg)
  353. {
  354. isl_ast_expr *expr;
  355. expr = isl_calloc_type(ctx, isl_ast_expr);
  356. if (!expr)
  357. return NULL;
  358. expr->ctx = ctx;
  359. isl_ctx_ref(ctx);
  360. expr->ref = 1;
  361. expr->type = isl_ast_expr_op;
  362. expr->u.op.op = op;
  363. expr->u.op.n_arg = n_arg;
  364. expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg);
  365. if (n_arg && !expr->u.op.args)
  366. return isl_ast_expr_free(expr);
  367. return expr;
  368. }
  369. /* Create a new id expression representing "id".
  370. */
  371. __isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
  372. {
  373. isl_ctx *ctx;
  374. isl_ast_expr *expr;
  375. if (!id)
  376. return NULL;
  377. ctx = isl_id_get_ctx(id);
  378. expr = isl_calloc_type(ctx, isl_ast_expr);
  379. if (!expr)
  380. goto error;
  381. expr->ctx = ctx;
  382. isl_ctx_ref(ctx);
  383. expr->ref = 1;
  384. expr->type = isl_ast_expr_id;
  385. expr->u.id = id;
  386. return expr;
  387. error:
  388. isl_id_free(id);
  389. return NULL;
  390. }
  391. /* Create a new integer expression representing "i".
  392. */
  393. __isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
  394. {
  395. isl_ast_expr *expr;
  396. expr = isl_calloc_type(ctx, isl_ast_expr);
  397. if (!expr)
  398. return NULL;
  399. expr->ctx = ctx;
  400. isl_ctx_ref(ctx);
  401. expr->ref = 1;
  402. expr->type = isl_ast_expr_int;
  403. expr->u.v = isl_val_int_from_si(ctx, i);
  404. if (!expr->u.v)
  405. return isl_ast_expr_free(expr);
  406. return expr;
  407. }
  408. /* Create a new integer expression representing "v".
  409. */
  410. __isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v)
  411. {
  412. isl_ctx *ctx;
  413. isl_ast_expr *expr;
  414. if (!v)
  415. return NULL;
  416. if (!isl_val_is_int(v))
  417. isl_die(isl_val_get_ctx(v), isl_error_invalid,
  418. "expecting integer value", goto error);
  419. ctx = isl_val_get_ctx(v);
  420. expr = isl_calloc_type(ctx, isl_ast_expr);
  421. if (!expr)
  422. goto error;
  423. expr->ctx = ctx;
  424. isl_ctx_ref(ctx);
  425. expr->ref = 1;
  426. expr->type = isl_ast_expr_int;
  427. expr->u.v = v;
  428. return expr;
  429. error:
  430. isl_val_free(v);
  431. return NULL;
  432. }
  433. /* Create an expression representing the unary operation "type" applied to
  434. * "arg".
  435. */
  436. __isl_give isl_ast_expr *isl_ast_expr_alloc_unary(
  437. enum isl_ast_expr_op_type type, __isl_take isl_ast_expr *arg)
  438. {
  439. isl_ctx *ctx;
  440. isl_ast_expr *expr = NULL;
  441. if (!arg)
  442. return NULL;
  443. ctx = isl_ast_expr_get_ctx(arg);
  444. expr = isl_ast_expr_alloc_op(ctx, type, 1);
  445. if (!expr)
  446. goto error;
  447. expr->u.op.args[0] = arg;
  448. return expr;
  449. error:
  450. isl_ast_expr_free(arg);
  451. return NULL;
  452. }
  453. /* Create an expression representing the negation of "arg".
  454. */
  455. __isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
  456. {
  457. return isl_ast_expr_alloc_unary(isl_ast_expr_op_minus, arg);
  458. }
  459. /* Create an expression representing the address of "expr".
  460. */
  461. __isl_give isl_ast_expr *isl_ast_expr_address_of(__isl_take isl_ast_expr *expr)
  462. {
  463. if (!expr)
  464. return NULL;
  465. if (isl_ast_expr_get_type(expr) != isl_ast_expr_op ||
  466. isl_ast_expr_get_op_type(expr) != isl_ast_expr_op_access)
  467. isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
  468. "can only take address of access expressions",
  469. return isl_ast_expr_free(expr));
  470. return isl_ast_expr_alloc_unary(isl_ast_expr_op_address_of, expr);
  471. }
  472. /* Create an expression representing the binary operation "type"
  473. * applied to "expr1" and "expr2".
  474. */
  475. __isl_give isl_ast_expr *isl_ast_expr_alloc_binary(
  476. enum isl_ast_expr_op_type type,
  477. __isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
  478. {
  479. isl_ctx *ctx;
  480. isl_ast_expr *expr = NULL;
  481. if (!expr1 || !expr2)
  482. goto error;
  483. ctx = isl_ast_expr_get_ctx(expr1);
  484. expr = isl_ast_expr_alloc_op(ctx, type, 2);
  485. if (!expr)
  486. goto error;
  487. expr->u.op.args[0] = expr1;
  488. expr->u.op.args[1] = expr2;
  489. return expr;
  490. error:
  491. isl_ast_expr_free(expr1);
  492. isl_ast_expr_free(expr2);
  493. return NULL;
  494. }
  495. /* Create an expression representing the sum of "expr1" and "expr2".
  496. */
  497. __isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
  498. __isl_take isl_ast_expr *expr2)
  499. {
  500. return isl_ast_expr_alloc_binary(isl_ast_expr_op_add, expr1, expr2);
  501. }
  502. /* Create an expression representing the difference of "expr1" and "expr2".
  503. */
  504. __isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
  505. __isl_take isl_ast_expr *expr2)
  506. {
  507. return isl_ast_expr_alloc_binary(isl_ast_expr_op_sub, expr1, expr2);
  508. }
  509. /* Create an expression representing the product of "expr1" and "expr2".
  510. */
  511. __isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
  512. __isl_take isl_ast_expr *expr2)
  513. {
  514. return isl_ast_expr_alloc_binary(isl_ast_expr_op_mul, expr1, expr2);
  515. }
  516. /* Create an expression representing the quotient of "expr1" and "expr2".
  517. */
  518. __isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
  519. __isl_take isl_ast_expr *expr2)
  520. {
  521. return isl_ast_expr_alloc_binary(isl_ast_expr_op_div, expr1, expr2);
  522. }
  523. /* Create an expression representing the quotient of the integer
  524. * division of "expr1" by "expr2", where "expr1" is known to be
  525. * non-negative.
  526. */
  527. __isl_give isl_ast_expr *isl_ast_expr_pdiv_q(__isl_take isl_ast_expr *expr1,
  528. __isl_take isl_ast_expr *expr2)
  529. {
  530. return isl_ast_expr_alloc_binary(isl_ast_expr_op_pdiv_q, expr1, expr2);
  531. }
  532. /* Create an expression representing the remainder of the integer
  533. * division of "expr1" by "expr2", where "expr1" is known to be
  534. * non-negative.
  535. */
  536. __isl_give isl_ast_expr *isl_ast_expr_pdiv_r(__isl_take isl_ast_expr *expr1,
  537. __isl_take isl_ast_expr *expr2)
  538. {
  539. return isl_ast_expr_alloc_binary(isl_ast_expr_op_pdiv_r, expr1, expr2);
  540. }
  541. /* Create an expression representing the conjunction of "expr1" and "expr2".
  542. */
  543. __isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
  544. __isl_take isl_ast_expr *expr2)
  545. {
  546. return isl_ast_expr_alloc_binary(isl_ast_expr_op_and, expr1, expr2);
  547. }
  548. /* Create an expression representing the conjunction of "expr1" and "expr2",
  549. * where "expr2" is evaluated only if "expr1" is evaluated to true.
  550. */
  551. __isl_give isl_ast_expr *isl_ast_expr_and_then(__isl_take isl_ast_expr *expr1,
  552. __isl_take isl_ast_expr *expr2)
  553. {
  554. return isl_ast_expr_alloc_binary(isl_ast_expr_op_and_then, expr1, expr2);
  555. }
  556. /* Create an expression representing the disjunction of "expr1" and "expr2".
  557. */
  558. __isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
  559. __isl_take isl_ast_expr *expr2)
  560. {
  561. return isl_ast_expr_alloc_binary(isl_ast_expr_op_or, expr1, expr2);
  562. }
  563. /* Create an expression representing the disjunction of "expr1" and "expr2",
  564. * where "expr2" is evaluated only if "expr1" is evaluated to false.
  565. */
  566. __isl_give isl_ast_expr *isl_ast_expr_or_else(__isl_take isl_ast_expr *expr1,
  567. __isl_take isl_ast_expr *expr2)
  568. {
  569. return isl_ast_expr_alloc_binary(isl_ast_expr_op_or_else, expr1, expr2);
  570. }
  571. /* Create an expression representing "expr1" less than or equal to "expr2".
  572. */
  573. __isl_give isl_ast_expr *isl_ast_expr_le(__isl_take isl_ast_expr *expr1,
  574. __isl_take isl_ast_expr *expr2)
  575. {
  576. return isl_ast_expr_alloc_binary(isl_ast_expr_op_le, expr1, expr2);
  577. }
  578. /* Create an expression representing "expr1" less than "expr2".
  579. */
  580. __isl_give isl_ast_expr *isl_ast_expr_lt(__isl_take isl_ast_expr *expr1,
  581. __isl_take isl_ast_expr *expr2)
  582. {
  583. return isl_ast_expr_alloc_binary(isl_ast_expr_op_lt, expr1, expr2);
  584. }
  585. /* Create an expression representing "expr1" greater than or equal to "expr2".
  586. */
  587. __isl_give isl_ast_expr *isl_ast_expr_ge(__isl_take isl_ast_expr *expr1,
  588. __isl_take isl_ast_expr *expr2)
  589. {
  590. return isl_ast_expr_alloc_binary(isl_ast_expr_op_ge, expr1, expr2);
  591. }
  592. /* Create an expression representing "expr1" greater than "expr2".
  593. */
  594. __isl_give isl_ast_expr *isl_ast_expr_gt(__isl_take isl_ast_expr *expr1,
  595. __isl_take isl_ast_expr *expr2)
  596. {
  597. return isl_ast_expr_alloc_binary(isl_ast_expr_op_gt, expr1, expr2);
  598. }
  599. /* Create an expression representing "expr1" equal to "expr2".
  600. */
  601. __isl_give isl_ast_expr *isl_ast_expr_eq(__isl_take isl_ast_expr *expr1,
  602. __isl_take isl_ast_expr *expr2)
  603. {
  604. return isl_ast_expr_alloc_binary(isl_ast_expr_op_eq, expr1, expr2);
  605. }
  606. /* Create an expression of type "type" with as arguments "arg0" followed
  607. * by "arguments".
  608. */
  609. static __isl_give isl_ast_expr *ast_expr_with_arguments(
  610. enum isl_ast_expr_op_type type, __isl_take isl_ast_expr *arg0,
  611. __isl_take isl_ast_expr_list *arguments)
  612. {
  613. int i;
  614. isl_size n;
  615. isl_ctx *ctx;
  616. isl_ast_expr *res = NULL;
  617. if (!arg0 || !arguments)
  618. goto error;
  619. ctx = isl_ast_expr_get_ctx(arg0);
  620. n = isl_ast_expr_list_n_ast_expr(arguments);
  621. if (n < 0)
  622. goto error;
  623. res = isl_ast_expr_alloc_op(ctx, type, 1 + n);
  624. if (!res)
  625. goto error;
  626. for (i = 0; i < n; ++i) {
  627. isl_ast_expr *arg;
  628. arg = isl_ast_expr_list_get_ast_expr(arguments, i);
  629. res->u.op.args[1 + i] = arg;
  630. if (!arg)
  631. goto error;
  632. }
  633. res->u.op.args[0] = arg0;
  634. isl_ast_expr_list_free(arguments);
  635. return res;
  636. error:
  637. isl_ast_expr_free(arg0);
  638. isl_ast_expr_list_free(arguments);
  639. isl_ast_expr_free(res);
  640. return NULL;
  641. }
  642. /* Create an expression representing an access to "array" with index
  643. * expressions "indices".
  644. */
  645. __isl_give isl_ast_expr *isl_ast_expr_access(__isl_take isl_ast_expr *array,
  646. __isl_take isl_ast_expr_list *indices)
  647. {
  648. return ast_expr_with_arguments(isl_ast_expr_op_access, array, indices);
  649. }
  650. /* Create an expression representing a call to "function" with argument
  651. * expressions "arguments".
  652. */
  653. __isl_give isl_ast_expr *isl_ast_expr_call(__isl_take isl_ast_expr *function,
  654. __isl_take isl_ast_expr_list *arguments)
  655. {
  656. return ast_expr_with_arguments(isl_ast_expr_op_call, function, arguments);
  657. }
  658. /* For each subexpression of "expr" of type isl_ast_expr_id,
  659. * if it appears in "id2expr", then replace it by the corresponding
  660. * expression.
  661. */
  662. __isl_give isl_ast_expr *isl_ast_expr_substitute_ids(
  663. __isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr)
  664. {
  665. int i;
  666. isl_maybe_isl_ast_expr m;
  667. if (!expr || !id2expr)
  668. goto error;
  669. switch (expr->type) {
  670. case isl_ast_expr_int:
  671. break;
  672. case isl_ast_expr_id:
  673. m = isl_id_to_ast_expr_try_get(id2expr, expr->u.id);
  674. if (m.valid < 0)
  675. goto error;
  676. if (!m.valid)
  677. break;
  678. isl_ast_expr_free(expr);
  679. expr = m.value;
  680. break;
  681. case isl_ast_expr_op:
  682. for (i = 0; i < expr->u.op.n_arg; ++i) {
  683. isl_ast_expr *arg;
  684. arg = isl_ast_expr_copy(expr->u.op.args[i]);
  685. arg = isl_ast_expr_substitute_ids(arg,
  686. isl_id_to_ast_expr_copy(id2expr));
  687. if (arg == expr->u.op.args[i]) {
  688. isl_ast_expr_free(arg);
  689. continue;
  690. }
  691. if (!arg)
  692. expr = isl_ast_expr_free(expr);
  693. expr = isl_ast_expr_cow(expr);
  694. if (!expr) {
  695. isl_ast_expr_free(arg);
  696. break;
  697. }
  698. isl_ast_expr_free(expr->u.op.args[i]);
  699. expr->u.op.args[i] = arg;
  700. }
  701. break;
  702. case isl_ast_expr_error:
  703. expr = isl_ast_expr_free(expr);
  704. break;
  705. }
  706. isl_id_to_ast_expr_free(id2expr);
  707. return expr;
  708. error:
  709. isl_ast_expr_free(expr);
  710. isl_id_to_ast_expr_free(id2expr);
  711. return NULL;
  712. }
  713. isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
  714. {
  715. return node ? node->ctx : NULL;
  716. }
  717. enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
  718. {
  719. return node ? node->type : isl_ast_node_error;
  720. }
  721. __isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
  722. enum isl_ast_node_type type)
  723. {
  724. isl_ast_node *node;
  725. node = isl_calloc_type(ctx, isl_ast_node);
  726. if (!node)
  727. return NULL;
  728. node->ctx = ctx;
  729. isl_ctx_ref(ctx);
  730. node->ref = 1;
  731. node->type = type;
  732. return node;
  733. }
  734. /* Create an if node with the given guard.
  735. *
  736. * The then body needs to be filled in later.
  737. */
  738. __isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
  739. {
  740. isl_ast_node *node;
  741. if (!guard)
  742. return NULL;
  743. node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
  744. if (!node)
  745. goto error;
  746. node->u.i.guard = guard;
  747. return node;
  748. error:
  749. isl_ast_expr_free(guard);
  750. return NULL;
  751. }
  752. /* Create a for node with the given iterator.
  753. *
  754. * The remaining fields need to be filled in later.
  755. */
  756. __isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
  757. {
  758. isl_ast_node *node;
  759. isl_ctx *ctx;
  760. if (!id)
  761. return NULL;
  762. ctx = isl_id_get_ctx(id);
  763. node = isl_ast_node_alloc(ctx, isl_ast_node_for);
  764. if (!node)
  765. goto error;
  766. node->u.f.iterator = isl_ast_expr_from_id(id);
  767. if (!node->u.f.iterator)
  768. return isl_ast_node_free(node);
  769. return node;
  770. error:
  771. isl_id_free(id);
  772. return NULL;
  773. }
  774. /* Create a mark node, marking "node" with "id".
  775. */
  776. __isl_give isl_ast_node *isl_ast_node_alloc_mark(__isl_take isl_id *id,
  777. __isl_take isl_ast_node *node)
  778. {
  779. isl_ctx *ctx;
  780. isl_ast_node *mark;
  781. if (!id || !node)
  782. goto error;
  783. ctx = isl_id_get_ctx(id);
  784. mark = isl_ast_node_alloc(ctx, isl_ast_node_mark);
  785. if (!mark)
  786. goto error;
  787. mark->u.m.mark = id;
  788. mark->u.m.node = node;
  789. return mark;
  790. error:
  791. isl_id_free(id);
  792. isl_ast_node_free(node);
  793. return NULL;
  794. }
  795. /* Create a user node evaluating "expr".
  796. */
  797. __isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr)
  798. {
  799. isl_ctx *ctx;
  800. isl_ast_node *node;
  801. if (!expr)
  802. return NULL;
  803. ctx = isl_ast_expr_get_ctx(expr);
  804. node = isl_ast_node_alloc(ctx, isl_ast_node_user);
  805. if (!node)
  806. goto error;
  807. node->u.e.expr = expr;
  808. return node;
  809. error:
  810. isl_ast_expr_free(expr);
  811. return NULL;
  812. }
  813. /* Create a block node with the given children.
  814. */
  815. __isl_give isl_ast_node *isl_ast_node_alloc_block(
  816. __isl_take isl_ast_node_list *list)
  817. {
  818. isl_ast_node *node;
  819. isl_ctx *ctx;
  820. if (!list)
  821. return NULL;
  822. ctx = isl_ast_node_list_get_ctx(list);
  823. node = isl_ast_node_alloc(ctx, isl_ast_node_block);
  824. if (!node)
  825. goto error;
  826. node->u.b.children = list;
  827. return node;
  828. error:
  829. isl_ast_node_list_free(list);
  830. return NULL;
  831. }
  832. /* Represent the given list of nodes as a single node, either by
  833. * extract the node from a single element list or by creating
  834. * a block node with the list of nodes as children.
  835. */
  836. __isl_give isl_ast_node *isl_ast_node_from_ast_node_list(
  837. __isl_take isl_ast_node_list *list)
  838. {
  839. isl_size n;
  840. isl_ast_node *node;
  841. n = isl_ast_node_list_n_ast_node(list);
  842. if (n < 0)
  843. goto error;
  844. if (n != 1)
  845. return isl_ast_node_alloc_block(list);
  846. node = isl_ast_node_list_get_ast_node(list, 0);
  847. isl_ast_node_list_free(list);
  848. return node;
  849. error:
  850. isl_ast_node_list_free(list);
  851. return NULL;
  852. }
  853. __isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node)
  854. {
  855. if (!node)
  856. return NULL;
  857. node->ref++;
  858. return node;
  859. }
  860. __isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node)
  861. {
  862. isl_ast_node *dup;
  863. if (!node)
  864. return NULL;
  865. dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type);
  866. if (!dup)
  867. return NULL;
  868. switch (node->type) {
  869. case isl_ast_node_if:
  870. dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard);
  871. dup->u.i.then = isl_ast_node_copy(node->u.i.then);
  872. dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node);
  873. if (!dup->u.i.guard || !dup->u.i.then ||
  874. (node->u.i.else_node && !dup->u.i.else_node))
  875. return isl_ast_node_free(dup);
  876. break;
  877. case isl_ast_node_for:
  878. dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator);
  879. dup->u.f.init = isl_ast_expr_copy(node->u.f.init);
  880. dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond);
  881. dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc);
  882. dup->u.f.body = isl_ast_node_copy(node->u.f.body);
  883. if (!dup->u.f.iterator || !dup->u.f.init || !dup->u.f.cond ||
  884. !dup->u.f.inc || !dup->u.f.body)
  885. return isl_ast_node_free(dup);
  886. break;
  887. case isl_ast_node_block:
  888. dup->u.b.children = isl_ast_node_list_copy(node->u.b.children);
  889. if (!dup->u.b.children)
  890. return isl_ast_node_free(dup);
  891. break;
  892. case isl_ast_node_mark:
  893. dup->u.m.mark = isl_id_copy(node->u.m.mark);
  894. dup->u.m.node = isl_ast_node_copy(node->u.m.node);
  895. if (!dup->u.m.mark || !dup->u.m.node)
  896. return isl_ast_node_free(dup);
  897. break;
  898. case isl_ast_node_user:
  899. dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr);
  900. if (!dup->u.e.expr)
  901. return isl_ast_node_free(dup);
  902. break;
  903. case isl_ast_node_error:
  904. break;
  905. }
  906. return dup;
  907. }
  908. __isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node)
  909. {
  910. if (!node)
  911. return NULL;
  912. if (node->ref == 1)
  913. return node;
  914. node->ref--;
  915. return isl_ast_node_dup(node);
  916. }
  917. __isl_null isl_ast_node *isl_ast_node_free(__isl_take isl_ast_node *node)
  918. {
  919. if (!node)
  920. return NULL;
  921. if (--node->ref > 0)
  922. return NULL;
  923. switch (node->type) {
  924. case isl_ast_node_if:
  925. isl_ast_expr_free(node->u.i.guard);
  926. isl_ast_node_free(node->u.i.then);
  927. isl_ast_node_free(node->u.i.else_node);
  928. break;
  929. case isl_ast_node_for:
  930. isl_ast_expr_free(node->u.f.iterator);
  931. isl_ast_expr_free(node->u.f.init);
  932. isl_ast_expr_free(node->u.f.cond);
  933. isl_ast_expr_free(node->u.f.inc);
  934. isl_ast_node_free(node->u.f.body);
  935. break;
  936. case isl_ast_node_block:
  937. isl_ast_node_list_free(node->u.b.children);
  938. break;
  939. case isl_ast_node_mark:
  940. isl_id_free(node->u.m.mark);
  941. isl_ast_node_free(node->u.m.node);
  942. break;
  943. case isl_ast_node_user:
  944. isl_ast_expr_free(node->u.e.expr);
  945. break;
  946. case isl_ast_node_error:
  947. break;
  948. }
  949. isl_id_free(node->annotation);
  950. isl_ctx_deref(node->ctx);
  951. free(node);
  952. return NULL;
  953. }
  954. /* Replace the body of the for node "node" by "body".
  955. */
  956. __isl_give isl_ast_node *isl_ast_node_for_set_body(
  957. __isl_take isl_ast_node *node, __isl_take isl_ast_node *body)
  958. {
  959. node = isl_ast_node_cow(node);
  960. if (!node || !body)
  961. goto error;
  962. if (node->type != isl_ast_node_for)
  963. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  964. "not a for node", goto error);
  965. isl_ast_node_free(node->u.f.body);
  966. node->u.f.body = body;
  967. return node;
  968. error:
  969. isl_ast_node_free(node);
  970. isl_ast_node_free(body);
  971. return NULL;
  972. }
  973. __isl_give isl_ast_node *isl_ast_node_for_get_body(
  974. __isl_keep isl_ast_node *node)
  975. {
  976. if (!node)
  977. return NULL;
  978. if (node->type != isl_ast_node_for)
  979. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  980. "not a for node", return NULL);
  981. return isl_ast_node_copy(node->u.f.body);
  982. }
  983. /* Mark the given for node as being degenerate.
  984. */
  985. __isl_give isl_ast_node *isl_ast_node_for_mark_degenerate(
  986. __isl_take isl_ast_node *node)
  987. {
  988. node = isl_ast_node_cow(node);
  989. if (!node)
  990. return NULL;
  991. node->u.f.degenerate = 1;
  992. return node;
  993. }
  994. isl_bool isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node)
  995. {
  996. if (!node)
  997. return isl_bool_error;
  998. if (node->type != isl_ast_node_for)
  999. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1000. "not a for node", return isl_bool_error);
  1001. return isl_bool_ok(node->u.f.degenerate);
  1002. }
  1003. __isl_give isl_ast_expr *isl_ast_node_for_get_iterator(
  1004. __isl_keep isl_ast_node *node)
  1005. {
  1006. if (!node)
  1007. return NULL;
  1008. if (node->type != isl_ast_node_for)
  1009. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1010. "not a for node", return NULL);
  1011. return isl_ast_expr_copy(node->u.f.iterator);
  1012. }
  1013. __isl_give isl_ast_expr *isl_ast_node_for_get_init(
  1014. __isl_keep isl_ast_node *node)
  1015. {
  1016. if (!node)
  1017. return NULL;
  1018. if (node->type != isl_ast_node_for)
  1019. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1020. "not a for node", return NULL);
  1021. return isl_ast_expr_copy(node->u.f.init);
  1022. }
  1023. /* Return the condition expression of the given for node.
  1024. *
  1025. * If the for node is degenerate, then the condition is not explicitly
  1026. * stored in the node. Instead, it is constructed as
  1027. *
  1028. * iterator <= init
  1029. */
  1030. __isl_give isl_ast_expr *isl_ast_node_for_get_cond(
  1031. __isl_keep isl_ast_node *node)
  1032. {
  1033. if (!node)
  1034. return NULL;
  1035. if (node->type != isl_ast_node_for)
  1036. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1037. "not a for node", return NULL);
  1038. if (!node->u.f.degenerate)
  1039. return isl_ast_expr_copy(node->u.f.cond);
  1040. return isl_ast_expr_alloc_binary(isl_ast_expr_op_le,
  1041. isl_ast_expr_copy(node->u.f.iterator),
  1042. isl_ast_expr_copy(node->u.f.init));
  1043. }
  1044. /* Return the increment of the given for node.
  1045. *
  1046. * If the for node is degenerate, then the increment is not explicitly
  1047. * stored in the node. We simply return "1".
  1048. */
  1049. __isl_give isl_ast_expr *isl_ast_node_for_get_inc(
  1050. __isl_keep isl_ast_node *node)
  1051. {
  1052. if (!node)
  1053. return NULL;
  1054. if (node->type != isl_ast_node_for)
  1055. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1056. "not a for node", return NULL);
  1057. if (!node->u.f.degenerate)
  1058. return isl_ast_expr_copy(node->u.f.inc);
  1059. return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1);
  1060. }
  1061. /* Replace the then branch of the if node "node" by "child".
  1062. */
  1063. __isl_give isl_ast_node *isl_ast_node_if_set_then(
  1064. __isl_take isl_ast_node *node, __isl_take isl_ast_node *child)
  1065. {
  1066. node = isl_ast_node_cow(node);
  1067. if (!node || !child)
  1068. goto error;
  1069. if (node->type != isl_ast_node_if)
  1070. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1071. "not an if node", goto error);
  1072. isl_ast_node_free(node->u.i.then);
  1073. node->u.i.then = child;
  1074. return node;
  1075. error:
  1076. isl_ast_node_free(node);
  1077. isl_ast_node_free(child);
  1078. return NULL;
  1079. }
  1080. /* Return the then-node of the given if-node.
  1081. */
  1082. __isl_give isl_ast_node *isl_ast_node_if_get_then_node(
  1083. __isl_keep isl_ast_node *node)
  1084. {
  1085. if (!node)
  1086. return NULL;
  1087. if (node->type != isl_ast_node_if)
  1088. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1089. "not an if node", return NULL);
  1090. return isl_ast_node_copy(node->u.i.then);
  1091. }
  1092. /* This is an alternative name for the function above.
  1093. */
  1094. __isl_give isl_ast_node *isl_ast_node_if_get_then(
  1095. __isl_keep isl_ast_node *node)
  1096. {
  1097. return isl_ast_node_if_get_then_node(node);
  1098. }
  1099. /* Does the given if-node have an else-node?
  1100. */
  1101. isl_bool isl_ast_node_if_has_else_node(__isl_keep isl_ast_node *node)
  1102. {
  1103. if (!node)
  1104. return isl_bool_error;
  1105. if (node->type != isl_ast_node_if)
  1106. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1107. "not an if node", return isl_bool_error);
  1108. return isl_bool_ok(node->u.i.else_node != NULL);
  1109. }
  1110. /* This is an alternative name for the function above.
  1111. */
  1112. isl_bool isl_ast_node_if_has_else(__isl_keep isl_ast_node *node)
  1113. {
  1114. return isl_ast_node_if_has_else_node(node);
  1115. }
  1116. /* Return the else-node of the given if-node,
  1117. * assuming there is one.
  1118. */
  1119. __isl_give isl_ast_node *isl_ast_node_if_get_else_node(
  1120. __isl_keep isl_ast_node *node)
  1121. {
  1122. if (!node)
  1123. return NULL;
  1124. if (node->type != isl_ast_node_if)
  1125. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1126. "not an if node", return NULL);
  1127. return isl_ast_node_copy(node->u.i.else_node);
  1128. }
  1129. /* This is an alternative name for the function above.
  1130. */
  1131. __isl_give isl_ast_node *isl_ast_node_if_get_else(
  1132. __isl_keep isl_ast_node *node)
  1133. {
  1134. return isl_ast_node_if_get_else_node(node);
  1135. }
  1136. __isl_give isl_ast_expr *isl_ast_node_if_get_cond(
  1137. __isl_keep isl_ast_node *node)
  1138. {
  1139. if (!node)
  1140. return NULL;
  1141. if (node->type != isl_ast_node_if)
  1142. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1143. "not a guard node", return NULL);
  1144. return isl_ast_expr_copy(node->u.i.guard);
  1145. }
  1146. __isl_give isl_ast_node_list *isl_ast_node_block_get_children(
  1147. __isl_keep isl_ast_node *node)
  1148. {
  1149. if (!node)
  1150. return NULL;
  1151. if (node->type != isl_ast_node_block)
  1152. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1153. "not a block node", return NULL);
  1154. return isl_ast_node_list_copy(node->u.b.children);
  1155. }
  1156. __isl_give isl_ast_expr *isl_ast_node_user_get_expr(
  1157. __isl_keep isl_ast_node *node)
  1158. {
  1159. if (!node)
  1160. return NULL;
  1161. if (node->type != isl_ast_node_user)
  1162. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1163. "not a user node", return NULL);
  1164. return isl_ast_expr_copy(node->u.e.expr);
  1165. }
  1166. /* Return the mark identifier of the mark node "node".
  1167. */
  1168. __isl_give isl_id *isl_ast_node_mark_get_id(__isl_keep isl_ast_node *node)
  1169. {
  1170. if (!node)
  1171. return NULL;
  1172. if (node->type != isl_ast_node_mark)
  1173. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1174. "not a mark node", return NULL);
  1175. return isl_id_copy(node->u.m.mark);
  1176. }
  1177. /* Return the node marked by mark node "node".
  1178. */
  1179. __isl_give isl_ast_node *isl_ast_node_mark_get_node(
  1180. __isl_keep isl_ast_node *node)
  1181. {
  1182. if (!node)
  1183. return NULL;
  1184. if (node->type != isl_ast_node_mark)
  1185. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  1186. "not a mark node", return NULL);
  1187. return isl_ast_node_copy(node->u.m.node);
  1188. }
  1189. __isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node)
  1190. {
  1191. return node ? isl_id_copy(node->annotation) : NULL;
  1192. }
  1193. /* Replace node->annotation by "annotation".
  1194. */
  1195. __isl_give isl_ast_node *isl_ast_node_set_annotation(
  1196. __isl_take isl_ast_node *node, __isl_take isl_id *annotation)
  1197. {
  1198. node = isl_ast_node_cow(node);
  1199. if (!node || !annotation)
  1200. goto error;
  1201. isl_id_free(node->annotation);
  1202. node->annotation = annotation;
  1203. return node;
  1204. error:
  1205. isl_id_free(annotation);
  1206. return isl_ast_node_free(node);
  1207. }
  1208. /* Traverse the elements of "list" and all their descendants
  1209. * in depth first preorder.
  1210. *
  1211. * Return isl_stat_ok on success and isl_stat_error on failure.
  1212. */
  1213. static isl_stat nodelist_foreach(__isl_keep isl_ast_node_list *list,
  1214. isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
  1215. {
  1216. int i;
  1217. if (!list)
  1218. return isl_stat_error;
  1219. for (i = 0; i < list->n; ++i) {
  1220. isl_stat ok;
  1221. isl_ast_node *node = list->p[i];
  1222. ok = isl_ast_node_foreach_descendant_top_down(node, fn, user);
  1223. if (ok < 0)
  1224. return isl_stat_error;
  1225. }
  1226. return isl_stat_ok;
  1227. }
  1228. /* Traverse the descendants of "node" (including the node itself)
  1229. * in depth first preorder.
  1230. *
  1231. * If "fn" returns isl_bool_error on any of the nodes, then the traversal
  1232. * is aborted.
  1233. * If "fn" returns isl_bool_false on any of the nodes, then the subtree rooted
  1234. * at that node is skipped.
  1235. *
  1236. * Return isl_stat_ok on success and isl_stat_error on failure.
  1237. */
  1238. isl_stat isl_ast_node_foreach_descendant_top_down(
  1239. __isl_keep isl_ast_node *node,
  1240. isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
  1241. {
  1242. isl_bool more;
  1243. isl_stat ok;
  1244. if (!node)
  1245. return isl_stat_error;
  1246. more = fn(node, user);
  1247. if (more < 0)
  1248. return isl_stat_error;
  1249. if (!more)
  1250. return isl_stat_ok;
  1251. switch (node->type) {
  1252. case isl_ast_node_for:
  1253. node = node->u.f.body;
  1254. return isl_ast_node_foreach_descendant_top_down(node, fn, user);
  1255. case isl_ast_node_if:
  1256. ok = isl_ast_node_foreach_descendant_top_down(node->u.i.then,
  1257. fn, user);
  1258. if (ok < 0)
  1259. return isl_stat_error;
  1260. if (!node->u.i.else_node)
  1261. return isl_stat_ok;
  1262. node = node->u.i.else_node;
  1263. return isl_ast_node_foreach_descendant_top_down(node, fn, user);
  1264. case isl_ast_node_block:
  1265. return nodelist_foreach(node->u.b.children, fn, user);
  1266. case isl_ast_node_mark:
  1267. node = node->u.m.node;
  1268. return isl_ast_node_foreach_descendant_top_down(node, fn, user);
  1269. case isl_ast_node_user:
  1270. break;
  1271. case isl_ast_node_error:
  1272. return isl_stat_error;
  1273. }
  1274. return isl_stat_ok;
  1275. }
  1276. /* Textual C representation of the various operators.
  1277. */
  1278. static char *op_str_c[] = {
  1279. [isl_ast_expr_op_and] = "&&",
  1280. [isl_ast_expr_op_and_then] = "&&",
  1281. [isl_ast_expr_op_or] = "||",
  1282. [isl_ast_expr_op_or_else] = "||",
  1283. [isl_ast_expr_op_max] = "max",
  1284. [isl_ast_expr_op_min] = "min",
  1285. [isl_ast_expr_op_minus] = "-",
  1286. [isl_ast_expr_op_add] = "+",
  1287. [isl_ast_expr_op_sub] = "-",
  1288. [isl_ast_expr_op_mul] = "*",
  1289. [isl_ast_expr_op_fdiv_q] = "floord",
  1290. [isl_ast_expr_op_pdiv_q] = "/",
  1291. [isl_ast_expr_op_pdiv_r] = "%",
  1292. [isl_ast_expr_op_zdiv_r] = "%",
  1293. [isl_ast_expr_op_div] = "/",
  1294. [isl_ast_expr_op_eq] = "==",
  1295. [isl_ast_expr_op_le] = "<=",
  1296. [isl_ast_expr_op_ge] = ">=",
  1297. [isl_ast_expr_op_lt] = "<",
  1298. [isl_ast_expr_op_gt] = ">",
  1299. [isl_ast_expr_op_member] = ".",
  1300. [isl_ast_expr_op_address_of] = "&"
  1301. };
  1302. /* Precedence in C of the various operators.
  1303. * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
  1304. * Lowest value means highest precedence.
  1305. */
  1306. static int op_prec[] = {
  1307. [isl_ast_expr_op_and] = 13,
  1308. [isl_ast_expr_op_and_then] = 13,
  1309. [isl_ast_expr_op_or] = 14,
  1310. [isl_ast_expr_op_or_else] = 14,
  1311. [isl_ast_expr_op_max] = 2,
  1312. [isl_ast_expr_op_min] = 2,
  1313. [isl_ast_expr_op_minus] = 3,
  1314. [isl_ast_expr_op_add] = 6,
  1315. [isl_ast_expr_op_sub] = 6,
  1316. [isl_ast_expr_op_mul] = 5,
  1317. [isl_ast_expr_op_div] = 5,
  1318. [isl_ast_expr_op_fdiv_q] = 2,
  1319. [isl_ast_expr_op_pdiv_q] = 5,
  1320. [isl_ast_expr_op_pdiv_r] = 5,
  1321. [isl_ast_expr_op_zdiv_r] = 5,
  1322. [isl_ast_expr_op_cond] = 15,
  1323. [isl_ast_expr_op_select] = 15,
  1324. [isl_ast_expr_op_eq] = 9,
  1325. [isl_ast_expr_op_le] = 8,
  1326. [isl_ast_expr_op_ge] = 8,
  1327. [isl_ast_expr_op_lt] = 8,
  1328. [isl_ast_expr_op_gt] = 8,
  1329. [isl_ast_expr_op_call] = 2,
  1330. [isl_ast_expr_op_access] = 2,
  1331. [isl_ast_expr_op_member] = 2,
  1332. [isl_ast_expr_op_address_of] = 3
  1333. };
  1334. /* Is the operator left-to-right associative?
  1335. */
  1336. static int op_left[] = {
  1337. [isl_ast_expr_op_and] = 1,
  1338. [isl_ast_expr_op_and_then] = 1,
  1339. [isl_ast_expr_op_or] = 1,
  1340. [isl_ast_expr_op_or_else] = 1,
  1341. [isl_ast_expr_op_max] = 1,
  1342. [isl_ast_expr_op_min] = 1,
  1343. [isl_ast_expr_op_minus] = 0,
  1344. [isl_ast_expr_op_add] = 1,
  1345. [isl_ast_expr_op_sub] = 1,
  1346. [isl_ast_expr_op_mul] = 1,
  1347. [isl_ast_expr_op_div] = 1,
  1348. [isl_ast_expr_op_fdiv_q] = 1,
  1349. [isl_ast_expr_op_pdiv_q] = 1,
  1350. [isl_ast_expr_op_pdiv_r] = 1,
  1351. [isl_ast_expr_op_zdiv_r] = 1,
  1352. [isl_ast_expr_op_cond] = 0,
  1353. [isl_ast_expr_op_select] = 0,
  1354. [isl_ast_expr_op_eq] = 1,
  1355. [isl_ast_expr_op_le] = 1,
  1356. [isl_ast_expr_op_ge] = 1,
  1357. [isl_ast_expr_op_lt] = 1,
  1358. [isl_ast_expr_op_gt] = 1,
  1359. [isl_ast_expr_op_call] = 1,
  1360. [isl_ast_expr_op_access] = 1,
  1361. [isl_ast_expr_op_member] = 1,
  1362. [isl_ast_expr_op_address_of] = 0
  1363. };
  1364. static int is_and(enum isl_ast_expr_op_type op)
  1365. {
  1366. return op == isl_ast_expr_op_and || op == isl_ast_expr_op_and_then;
  1367. }
  1368. static int is_or(enum isl_ast_expr_op_type op)
  1369. {
  1370. return op == isl_ast_expr_op_or || op == isl_ast_expr_op_or_else;
  1371. }
  1372. static int is_add_sub(enum isl_ast_expr_op_type op)
  1373. {
  1374. return op == isl_ast_expr_op_add || op == isl_ast_expr_op_sub;
  1375. }
  1376. static int is_div_mod(enum isl_ast_expr_op_type op)
  1377. {
  1378. return op == isl_ast_expr_op_div ||
  1379. op == isl_ast_expr_op_pdiv_r ||
  1380. op == isl_ast_expr_op_zdiv_r;
  1381. }
  1382. static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p,
  1383. __isl_keep isl_ast_expr *expr);
  1384. /* Do we need/want parentheses around "expr" as a subexpression of
  1385. * an "op" operation? If "left" is set, then "expr" is the left-most
  1386. * operand.
  1387. *
  1388. * We only need parentheses if "expr" represents an operation.
  1389. *
  1390. * If op has a higher precedence than expr->u.op.op, then we need
  1391. * parentheses.
  1392. * If op and expr->u.op.op have the same precedence, but the operations
  1393. * are performed in an order that is different from the associativity,
  1394. * then we need parentheses.
  1395. *
  1396. * An and inside an or technically does not require parentheses,
  1397. * but some compilers complain about that, so we add them anyway.
  1398. *
  1399. * Computations such as "a / b * c" and "a % b + c" can be somewhat
  1400. * difficult to read, so we add parentheses for those as well.
  1401. */
  1402. static int sub_expr_need_parens(enum isl_ast_expr_op_type op,
  1403. __isl_keep isl_ast_expr *expr, int left)
  1404. {
  1405. if (expr->type != isl_ast_expr_op)
  1406. return 0;
  1407. if (op_prec[expr->u.op.op] > op_prec[op])
  1408. return 1;
  1409. if (op_prec[expr->u.op.op] == op_prec[op] && left != op_left[op])
  1410. return 1;
  1411. if (is_or(op) && is_and(expr->u.op.op))
  1412. return 1;
  1413. if (op == isl_ast_expr_op_mul && expr->u.op.op != isl_ast_expr_op_mul &&
  1414. op_prec[expr->u.op.op] == op_prec[op])
  1415. return 1;
  1416. if (is_add_sub(op) && is_div_mod(expr->u.op.op))
  1417. return 1;
  1418. return 0;
  1419. }
  1420. /* Print "expr" as a subexpression of an "op" operation in C format.
  1421. * If "left" is set, then "expr" is the left-most operand.
  1422. */
  1423. static __isl_give isl_printer *print_sub_expr_c(__isl_take isl_printer *p,
  1424. enum isl_ast_expr_op_type op, __isl_keep isl_ast_expr *expr, int left)
  1425. {
  1426. int need_parens;
  1427. need_parens = sub_expr_need_parens(op, expr, left);
  1428. if (need_parens)
  1429. p = isl_printer_print_str(p, "(");
  1430. p = print_ast_expr_c(p, expr);
  1431. if (need_parens)
  1432. p = isl_printer_print_str(p, ")");
  1433. return p;
  1434. }
  1435. #define isl_ast_expr_op_last isl_ast_expr_op_address_of
  1436. /* Data structure that holds the user-specified textual
  1437. * representations for the operators in C format.
  1438. * The entries are either NULL or copies of strings.
  1439. * A NULL entry means that the default name should be used.
  1440. */
  1441. struct isl_ast_expr_op_names {
  1442. char *op_str[isl_ast_expr_op_last + 1];
  1443. };
  1444. /* Create an empty struct isl_ast_expr_op_names.
  1445. */
  1446. static void *create_names(isl_ctx *ctx)
  1447. {
  1448. return isl_calloc_type(ctx, struct isl_ast_expr_op_names);
  1449. }
  1450. /* Free a struct isl_ast_expr_op_names along with all memory
  1451. * owned by the struct.
  1452. */
  1453. static void free_names(void *user)
  1454. {
  1455. int i;
  1456. struct isl_ast_expr_op_names *names = user;
  1457. if (!user)
  1458. return;
  1459. for (i = 0; i <= isl_ast_expr_op_last; ++i)
  1460. free(names->op_str[i]);
  1461. free(user);
  1462. }
  1463. /* Create an identifier that is used to store
  1464. * an isl_ast_expr_op_names note.
  1465. */
  1466. static __isl_give isl_id *names_id(isl_ctx *ctx)
  1467. {
  1468. return isl_id_alloc(ctx, "isl_ast_expr_op_type_names", NULL);
  1469. }
  1470. /* Ensure that "p" has a note identified by "id".
  1471. * If there is no such note yet, then it is created by "note_create" and
  1472. * scheduled do be freed by "note_free".
  1473. */
  1474. static __isl_give isl_printer *alloc_note(__isl_take isl_printer *p,
  1475. __isl_keep isl_id *id, void *(*note_create)(isl_ctx *),
  1476. void (*note_free)(void *))
  1477. {
  1478. isl_ctx *ctx;
  1479. isl_id *note_id;
  1480. isl_bool has_note;
  1481. void *note;
  1482. has_note = isl_printer_has_note(p, id);
  1483. if (has_note < 0)
  1484. return isl_printer_free(p);
  1485. if (has_note)
  1486. return p;
  1487. ctx = isl_printer_get_ctx(p);
  1488. note = note_create(ctx);
  1489. if (!note)
  1490. return isl_printer_free(p);
  1491. note_id = isl_id_alloc(ctx, NULL, note);
  1492. if (!note_id)
  1493. note_free(note);
  1494. else
  1495. note_id = isl_id_set_free_user(note_id, note_free);
  1496. p = isl_printer_set_note(p, isl_id_copy(id), note_id);
  1497. return p;
  1498. }
  1499. /* Ensure that "p" has an isl_ast_expr_op_names note identified by "id".
  1500. */
  1501. static __isl_give isl_printer *alloc_names(__isl_take isl_printer *p,
  1502. __isl_keep isl_id *id)
  1503. {
  1504. return alloc_note(p, id, &create_names, &free_names);
  1505. }
  1506. /* Retrieve the note identified by "id" from "p".
  1507. * The note is assumed to exist.
  1508. */
  1509. static void *get_note(__isl_keep isl_printer *p, __isl_keep isl_id *id)
  1510. {
  1511. void *note;
  1512. id = isl_printer_get_note(p, isl_id_copy(id));
  1513. note = isl_id_get_user(id);
  1514. isl_id_free(id);
  1515. return note;
  1516. }
  1517. /* Use "name" to print operations of type "type" to "p".
  1518. *
  1519. * Store the name in an isl_ast_expr_op_names note attached to "p", such that
  1520. * it can be retrieved by get_op_str.
  1521. */
  1522. __isl_give isl_printer *isl_ast_expr_op_type_set_print_name(
  1523. __isl_take isl_printer *p, enum isl_ast_expr_op_type type,
  1524. __isl_keep const char *name)
  1525. {
  1526. isl_id *id;
  1527. struct isl_ast_expr_op_names *names;
  1528. if (!p)
  1529. return NULL;
  1530. if (type > isl_ast_expr_op_last)
  1531. isl_die(isl_printer_get_ctx(p), isl_error_invalid,
  1532. "invalid type", return isl_printer_free(p));
  1533. id = names_id(isl_printer_get_ctx(p));
  1534. p = alloc_names(p, id);
  1535. names = get_note(p, id);
  1536. isl_id_free(id);
  1537. if (!names)
  1538. return isl_printer_free(p);
  1539. free(names->op_str[type]);
  1540. names->op_str[type] = strdup(name);
  1541. return p;
  1542. }
  1543. /* This is an alternative name for the function above.
  1544. */
  1545. __isl_give isl_printer *isl_ast_op_type_set_print_name(
  1546. __isl_take isl_printer *p, enum isl_ast_expr_op_type type,
  1547. __isl_keep const char *name)
  1548. {
  1549. return isl_ast_expr_op_type_set_print_name(p, type, name);
  1550. }
  1551. /* Return the textual representation of "type" in C format.
  1552. *
  1553. * If there is a user-specified name in an isl_ast_expr_op_names note
  1554. * associated to "p", then return that.
  1555. * Otherwise, return the default name in op_str_c.
  1556. */
  1557. static const char *get_op_str_c(__isl_keep isl_printer *p,
  1558. enum isl_ast_expr_op_type type)
  1559. {
  1560. isl_id *id;
  1561. isl_bool has_names;
  1562. struct isl_ast_expr_op_names *names = NULL;
  1563. id = names_id(isl_printer_get_ctx(p));
  1564. has_names = isl_printer_has_note(p, id);
  1565. if (has_names >= 0 && has_names)
  1566. names = get_note(p, id);
  1567. isl_id_free(id);
  1568. if (names && names->op_str[type])
  1569. return names->op_str[type];
  1570. return op_str_c[type];
  1571. }
  1572. /* Print a min or max reduction "expr" in C format.
  1573. */
  1574. static __isl_give isl_printer *print_min_max_c(__isl_take isl_printer *p,
  1575. __isl_keep isl_ast_expr *expr)
  1576. {
  1577. int i = 0;
  1578. for (i = 1; i < expr->u.op.n_arg; ++i) {
  1579. p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op));
  1580. p = isl_printer_print_str(p, "(");
  1581. }
  1582. p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
  1583. for (i = 1; i < expr->u.op.n_arg; ++i) {
  1584. p = isl_printer_print_str(p, ", ");
  1585. p = print_ast_expr_c(p, expr->u.op.args[i]);
  1586. p = isl_printer_print_str(p, ")");
  1587. }
  1588. return p;
  1589. }
  1590. /* Print a function call "expr" in C format.
  1591. *
  1592. * The first argument represents the function to be called.
  1593. */
  1594. static __isl_give isl_printer *print_call_c(__isl_take isl_printer *p,
  1595. __isl_keep isl_ast_expr *expr)
  1596. {
  1597. int i = 0;
  1598. p = print_ast_expr_c(p, expr->u.op.args[0]);
  1599. p = isl_printer_print_str(p, "(");
  1600. for (i = 1; i < expr->u.op.n_arg; ++i) {
  1601. if (i != 1)
  1602. p = isl_printer_print_str(p, ", ");
  1603. p = print_ast_expr_c(p, expr->u.op.args[i]);
  1604. }
  1605. p = isl_printer_print_str(p, ")");
  1606. return p;
  1607. }
  1608. /* Print an array access "expr" in C format.
  1609. *
  1610. * The first argument represents the array being accessed.
  1611. */
  1612. static __isl_give isl_printer *print_access_c(__isl_take isl_printer *p,
  1613. __isl_keep isl_ast_expr *expr)
  1614. {
  1615. int i = 0;
  1616. p = print_ast_expr_c(p, expr->u.op.args[0]);
  1617. for (i = 1; i < expr->u.op.n_arg; ++i) {
  1618. p = isl_printer_print_str(p, "[");
  1619. p = print_ast_expr_c(p, expr->u.op.args[i]);
  1620. p = isl_printer_print_str(p, "]");
  1621. }
  1622. return p;
  1623. }
  1624. /* Print "expr" to "p" in C format.
  1625. */
  1626. static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p,
  1627. __isl_keep isl_ast_expr *expr)
  1628. {
  1629. if (!p)
  1630. return NULL;
  1631. if (!expr)
  1632. return isl_printer_free(p);
  1633. switch (expr->type) {
  1634. case isl_ast_expr_op:
  1635. if (expr->u.op.op == isl_ast_expr_op_call) {
  1636. p = print_call_c(p, expr);
  1637. break;
  1638. }
  1639. if (expr->u.op.op == isl_ast_expr_op_access) {
  1640. p = print_access_c(p, expr);
  1641. break;
  1642. }
  1643. if (expr->u.op.n_arg == 1) {
  1644. p = isl_printer_print_str(p,
  1645. get_op_str_c(p, expr->u.op.op));
  1646. p = print_sub_expr_c(p, expr->u.op.op,
  1647. expr->u.op.args[0], 0);
  1648. break;
  1649. }
  1650. if (expr->u.op.op == isl_ast_expr_op_fdiv_q) {
  1651. const char *name;
  1652. name = get_op_str_c(p, isl_ast_expr_op_fdiv_q);
  1653. p = isl_printer_print_str(p, name);
  1654. p = isl_printer_print_str(p, "(");
  1655. p = print_ast_expr_c(p, expr->u.op.args[0]);
  1656. p = isl_printer_print_str(p, ", ");
  1657. p = print_ast_expr_c(p, expr->u.op.args[1]);
  1658. p = isl_printer_print_str(p, ")");
  1659. break;
  1660. }
  1661. if (expr->u.op.op == isl_ast_expr_op_max ||
  1662. expr->u.op.op == isl_ast_expr_op_min) {
  1663. p = print_min_max_c(p, expr);
  1664. break;
  1665. }
  1666. if (expr->u.op.op == isl_ast_expr_op_cond ||
  1667. expr->u.op.op == isl_ast_expr_op_select) {
  1668. p = print_ast_expr_c(p, expr->u.op.args[0]);
  1669. p = isl_printer_print_str(p, " ? ");
  1670. p = print_ast_expr_c(p, expr->u.op.args[1]);
  1671. p = isl_printer_print_str(p, " : ");
  1672. p = print_ast_expr_c(p, expr->u.op.args[2]);
  1673. break;
  1674. }
  1675. if (expr->u.op.n_arg != 2)
  1676. isl_die(isl_printer_get_ctx(p), isl_error_internal,
  1677. "operation should have two arguments",
  1678. return isl_printer_free(p));
  1679. p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[0], 1);
  1680. if (expr->u.op.op != isl_ast_expr_op_member)
  1681. p = isl_printer_print_str(p, " ");
  1682. p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op));
  1683. if (expr->u.op.op != isl_ast_expr_op_member)
  1684. p = isl_printer_print_str(p, " ");
  1685. p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[1], 0);
  1686. break;
  1687. case isl_ast_expr_id:
  1688. p = isl_printer_print_str(p, isl_id_get_name(expr->u.id));
  1689. break;
  1690. case isl_ast_expr_int:
  1691. p = isl_printer_print_val(p, expr->u.v);
  1692. break;
  1693. case isl_ast_expr_error:
  1694. break;
  1695. }
  1696. return p;
  1697. }
  1698. /* Textual representation of the isl_ast_expr_op_type elements
  1699. * for use in a YAML representation of an isl_ast_expr.
  1700. */
  1701. static char *op_str[] = {
  1702. [isl_ast_expr_op_and] = "and",
  1703. [isl_ast_expr_op_and_then] = "and_then",
  1704. [isl_ast_expr_op_or] = "or",
  1705. [isl_ast_expr_op_or_else] = "or_else",
  1706. [isl_ast_expr_op_max] = "max",
  1707. [isl_ast_expr_op_min] = "min",
  1708. [isl_ast_expr_op_minus] = "minus",
  1709. [isl_ast_expr_op_add] = "add",
  1710. [isl_ast_expr_op_sub] = "sub",
  1711. [isl_ast_expr_op_mul] = "mul",
  1712. [isl_ast_expr_op_div] = "div",
  1713. [isl_ast_expr_op_fdiv_q] = "fdiv_q",
  1714. [isl_ast_expr_op_pdiv_q] = "pdiv_q",
  1715. [isl_ast_expr_op_pdiv_r] = "pdiv_r",
  1716. [isl_ast_expr_op_zdiv_r] = "zdiv_r",
  1717. [isl_ast_expr_op_cond] = "cond",
  1718. [isl_ast_expr_op_select] = "select",
  1719. [isl_ast_expr_op_eq] = "eq",
  1720. [isl_ast_expr_op_le] = "le",
  1721. [isl_ast_expr_op_lt] = "lt",
  1722. [isl_ast_expr_op_ge] = "ge",
  1723. [isl_ast_expr_op_gt] = "gt",
  1724. [isl_ast_expr_op_call] = "call",
  1725. [isl_ast_expr_op_access] = "access",
  1726. [isl_ast_expr_op_member] = "member",
  1727. [isl_ast_expr_op_address_of] = "address_of"
  1728. };
  1729. static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p,
  1730. __isl_keep isl_ast_expr *expr);
  1731. /* Print the arguments of "expr" to "p" in isl format.
  1732. *
  1733. * If there are no arguments, then nothing needs to be printed.
  1734. * Otherwise add an "args" key to the current mapping with as value
  1735. * the list of arguments of "expr".
  1736. */
  1737. static __isl_give isl_printer *print_arguments(__isl_take isl_printer *p,
  1738. __isl_keep isl_ast_expr *expr)
  1739. {
  1740. int i;
  1741. isl_size n;
  1742. n = isl_ast_expr_get_op_n_arg(expr);
  1743. if (n < 0)
  1744. return isl_printer_free(p);
  1745. if (n == 0)
  1746. return p;
  1747. p = isl_printer_print_str(p, "args");
  1748. p = isl_printer_yaml_next(p);
  1749. p = isl_printer_yaml_start_sequence(p);
  1750. for (i = 0; i < n; ++i) {
  1751. isl_ast_expr *arg;
  1752. arg = isl_ast_expr_get_op_arg(expr, i);
  1753. p = print_ast_expr_isl(p, arg);
  1754. isl_ast_expr_free(arg);
  1755. p = isl_printer_yaml_next(p);
  1756. }
  1757. p = isl_printer_yaml_end_sequence(p);
  1758. return p;
  1759. }
  1760. /* Print "expr" to "p" in isl format.
  1761. *
  1762. * In particular, print the isl_ast_expr as a YAML document.
  1763. */
  1764. static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p,
  1765. __isl_keep isl_ast_expr *expr)
  1766. {
  1767. enum isl_ast_expr_type type;
  1768. enum isl_ast_expr_op_type op;
  1769. isl_id *id;
  1770. isl_val *v;
  1771. if (!expr)
  1772. return isl_printer_free(p);
  1773. p = isl_printer_yaml_start_mapping(p);
  1774. type = isl_ast_expr_get_type(expr);
  1775. switch (type) {
  1776. case isl_ast_expr_error:
  1777. return isl_printer_free(p);
  1778. case isl_ast_expr_op:
  1779. op = isl_ast_expr_get_op_type(expr);
  1780. if (op == isl_ast_expr_op_error)
  1781. return isl_printer_free(p);
  1782. p = isl_printer_print_str(p, "op");
  1783. p = isl_printer_yaml_next(p);
  1784. p = isl_printer_print_str(p, op_str[op]);
  1785. p = isl_printer_yaml_next(p);
  1786. p = print_arguments(p, expr);
  1787. break;
  1788. case isl_ast_expr_id:
  1789. p = isl_printer_print_str(p, "id");
  1790. p = isl_printer_yaml_next(p);
  1791. id = isl_ast_expr_get_id(expr);
  1792. p = isl_printer_print_id(p, id);
  1793. isl_id_free(id);
  1794. break;
  1795. case isl_ast_expr_int:
  1796. p = isl_printer_print_str(p, "val");
  1797. p = isl_printer_yaml_next(p);
  1798. v = isl_ast_expr_get_val(expr);
  1799. p = isl_printer_print_val(p, v);
  1800. isl_val_free(v);
  1801. break;
  1802. }
  1803. p = isl_printer_yaml_end_mapping(p);
  1804. return p;
  1805. }
  1806. /* Print "expr" to "p".
  1807. *
  1808. * Only an isl and a C format are supported.
  1809. */
  1810. __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p,
  1811. __isl_keep isl_ast_expr *expr)
  1812. {
  1813. int format;
  1814. if (!p)
  1815. return NULL;
  1816. format = isl_printer_get_output_format(p);
  1817. switch (format) {
  1818. case ISL_FORMAT_ISL:
  1819. p = print_ast_expr_isl(p, expr);
  1820. break;
  1821. case ISL_FORMAT_C:
  1822. p = print_ast_expr_c(p, expr);
  1823. break;
  1824. default:
  1825. isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
  1826. "output format not supported for ast_expr",
  1827. return isl_printer_free(p));
  1828. }
  1829. return p;
  1830. }
  1831. static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
  1832. __isl_keep isl_ast_node *node);
  1833. /* Print a YAML sequence containing the entries in "list" to "p".
  1834. */
  1835. static __isl_give isl_printer *print_ast_node_list(__isl_take isl_printer *p,
  1836. __isl_keep isl_ast_node_list *list)
  1837. {
  1838. int i;
  1839. isl_size n;
  1840. n = isl_ast_node_list_n_ast_node(list);
  1841. if (n < 0)
  1842. return isl_printer_free(p);
  1843. p = isl_printer_yaml_start_sequence(p);
  1844. for (i = 0; i < n; ++i) {
  1845. isl_ast_node *node;
  1846. node = isl_ast_node_list_get_ast_node(list, i);
  1847. p = print_ast_node_isl(p, node);
  1848. isl_ast_node_free(node);
  1849. p = isl_printer_yaml_next(p);
  1850. }
  1851. p = isl_printer_yaml_end_sequence(p);
  1852. return p;
  1853. }
  1854. /* Print "node" to "p" in "isl format".
  1855. *
  1856. * In particular, print the isl_ast_node as a YAML document.
  1857. */
  1858. static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
  1859. __isl_keep isl_ast_node *node)
  1860. {
  1861. switch (node->type) {
  1862. case isl_ast_node_for:
  1863. p = isl_printer_yaml_start_mapping(p);
  1864. p = isl_printer_print_str(p, "iterator");
  1865. p = isl_printer_yaml_next(p);
  1866. p = isl_printer_print_ast_expr(p, node->u.f.iterator);
  1867. p = isl_printer_yaml_next(p);
  1868. if (node->u.f.degenerate) {
  1869. p = isl_printer_print_str(p, "value");
  1870. p = isl_printer_yaml_next(p);
  1871. p = isl_printer_print_ast_expr(p, node->u.f.init);
  1872. p = isl_printer_yaml_next(p);
  1873. } else {
  1874. p = isl_printer_print_str(p, "init");
  1875. p = isl_printer_yaml_next(p);
  1876. p = isl_printer_print_ast_expr(p, node->u.f.init);
  1877. p = isl_printer_yaml_next(p);
  1878. p = isl_printer_print_str(p, "cond");
  1879. p = isl_printer_yaml_next(p);
  1880. p = isl_printer_print_ast_expr(p, node->u.f.cond);
  1881. p = isl_printer_yaml_next(p);
  1882. p = isl_printer_print_str(p, "inc");
  1883. p = isl_printer_yaml_next(p);
  1884. p = isl_printer_print_ast_expr(p, node->u.f.inc);
  1885. p = isl_printer_yaml_next(p);
  1886. }
  1887. if (node->u.f.body) {
  1888. p = isl_printer_print_str(p, "body");
  1889. p = isl_printer_yaml_next(p);
  1890. p = isl_printer_print_ast_node(p, node->u.f.body);
  1891. p = isl_printer_yaml_next(p);
  1892. }
  1893. p = isl_printer_yaml_end_mapping(p);
  1894. break;
  1895. case isl_ast_node_mark:
  1896. p = isl_printer_yaml_start_mapping(p);
  1897. p = isl_printer_print_str(p, "mark");
  1898. p = isl_printer_yaml_next(p);
  1899. p = isl_printer_print_id(p, node->u.m.mark);
  1900. p = isl_printer_yaml_next(p);
  1901. p = isl_printer_print_str(p, "node");
  1902. p = isl_printer_yaml_next(p);
  1903. p = isl_printer_print_ast_node(p, node->u.m.node);
  1904. p = isl_printer_yaml_end_mapping(p);
  1905. break;
  1906. case isl_ast_node_user:
  1907. p = isl_printer_yaml_start_mapping(p);
  1908. p = isl_printer_print_str(p, "user");
  1909. p = isl_printer_yaml_next(p);
  1910. p = isl_printer_print_ast_expr(p, node->u.e.expr);
  1911. p = isl_printer_yaml_end_mapping(p);
  1912. break;
  1913. case isl_ast_node_if:
  1914. p = isl_printer_yaml_start_mapping(p);
  1915. p = isl_printer_print_str(p, "guard");
  1916. p = isl_printer_yaml_next(p);
  1917. p = isl_printer_print_ast_expr(p, node->u.i.guard);
  1918. p = isl_printer_yaml_next(p);
  1919. if (node->u.i.then) {
  1920. p = isl_printer_print_str(p, "then");
  1921. p = isl_printer_yaml_next(p);
  1922. p = isl_printer_print_ast_node(p, node->u.i.then);
  1923. p = isl_printer_yaml_next(p);
  1924. }
  1925. if (node->u.i.else_node) {
  1926. p = isl_printer_print_str(p, "else");
  1927. p = isl_printer_yaml_next(p);
  1928. p = isl_printer_print_ast_node(p, node->u.i.else_node);
  1929. }
  1930. p = isl_printer_yaml_end_mapping(p);
  1931. break;
  1932. case isl_ast_node_block:
  1933. p = print_ast_node_list(p, node->u.b.children);
  1934. break;
  1935. case isl_ast_node_error:
  1936. break;
  1937. }
  1938. return p;
  1939. }
  1940. /* Do we need to print a block around the body "node" of a for or if node?
  1941. *
  1942. * If the node is a block, then we need to print a block.
  1943. * Also if the node is a degenerate for then we will print it as
  1944. * an assignment followed by the body of the for loop, so we need a block
  1945. * as well.
  1946. * If the node is an if node with an else, then we print a block
  1947. * to avoid spurious dangling else warnings emitted by some compilers.
  1948. * If the node is a mark, then in principle, we would have to check
  1949. * the child of the mark node. However, even if the child would not
  1950. * require us to print a block, for readability it is probably best
  1951. * to print a block anyway.
  1952. * If the ast_always_print_block option has been set, then we print a block.
  1953. */
  1954. static int need_block(__isl_keep isl_ast_node *node)
  1955. {
  1956. isl_ctx *ctx;
  1957. if (node->type == isl_ast_node_block)
  1958. return 1;
  1959. if (node->type == isl_ast_node_for && node->u.f.degenerate)
  1960. return 1;
  1961. if (node->type == isl_ast_node_if && node->u.i.else_node)
  1962. return 1;
  1963. if (node->type == isl_ast_node_mark)
  1964. return 1;
  1965. ctx = isl_ast_node_get_ctx(node);
  1966. return isl_options_get_ast_always_print_block(ctx);
  1967. }
  1968. static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
  1969. __isl_keep isl_ast_node *node,
  1970. __isl_keep isl_ast_print_options *options, int in_block, int in_list);
  1971. static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
  1972. __isl_keep isl_ast_node *node,
  1973. __isl_keep isl_ast_print_options *options, int new_line,
  1974. int force_block);
  1975. /* Print the body "node" of a for or if node.
  1976. * If "else_node" is set, then it is printed as well.
  1977. * If "force_block" is set, then print out the body as a block.
  1978. *
  1979. * We first check if we need to print out a block.
  1980. * We always print out a block if there is an else node to make
  1981. * sure that the else node is matched to the correct if node.
  1982. * For consistency, the corresponding else node is also printed as a block.
  1983. *
  1984. * If the else node is itself an if, then we print it as
  1985. *
  1986. * } else if (..) {
  1987. * }
  1988. *
  1989. * Otherwise the else node is printed as
  1990. *
  1991. * } else {
  1992. * node
  1993. * }
  1994. */
  1995. static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p,
  1996. __isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node,
  1997. __isl_keep isl_ast_print_options *options, int force_block)
  1998. {
  1999. if (!node)
  2000. return isl_printer_free(p);
  2001. if (!force_block && !else_node && !need_block(node)) {
  2002. p = isl_printer_end_line(p);
  2003. p = isl_printer_indent(p, 2);
  2004. p = isl_ast_node_print(node, p,
  2005. isl_ast_print_options_copy(options));
  2006. p = isl_printer_indent(p, -2);
  2007. return p;
  2008. }
  2009. p = isl_printer_print_str(p, " {");
  2010. p = isl_printer_end_line(p);
  2011. p = isl_printer_indent(p, 2);
  2012. p = print_ast_node_c(p, node, options, 1, 0);
  2013. p = isl_printer_indent(p, -2);
  2014. p = isl_printer_start_line(p);
  2015. p = isl_printer_print_str(p, "}");
  2016. if (else_node) {
  2017. if (else_node->type == isl_ast_node_if) {
  2018. p = isl_printer_print_str(p, " else ");
  2019. p = print_if_c(p, else_node, options, 0, 1);
  2020. } else {
  2021. p = isl_printer_print_str(p, " else");
  2022. p = print_body_c(p, else_node, NULL, options, 1);
  2023. }
  2024. } else
  2025. p = isl_printer_end_line(p);
  2026. return p;
  2027. }
  2028. /* Print the start of a compound statement.
  2029. */
  2030. static __isl_give isl_printer *start_block(__isl_take isl_printer *p)
  2031. {
  2032. p = isl_printer_start_line(p);
  2033. p = isl_printer_print_str(p, "{");
  2034. p = isl_printer_end_line(p);
  2035. p = isl_printer_indent(p, 2);
  2036. return p;
  2037. }
  2038. /* Print the end of a compound statement.
  2039. */
  2040. static __isl_give isl_printer *end_block(__isl_take isl_printer *p)
  2041. {
  2042. p = isl_printer_indent(p, -2);
  2043. p = isl_printer_start_line(p);
  2044. p = isl_printer_print_str(p, "}");
  2045. p = isl_printer_end_line(p);
  2046. return p;
  2047. }
  2048. /* Print the for node "node".
  2049. *
  2050. * If the for node is degenerate, it is printed as
  2051. *
  2052. * type iterator = init;
  2053. * body
  2054. *
  2055. * Otherwise, it is printed as
  2056. *
  2057. * for (type iterator = init; cond; iterator += inc)
  2058. * body
  2059. *
  2060. * "in_block" is set if we are currently inside a block.
  2061. * "in_list" is set if the current node is not alone in the block.
  2062. * If we are not in a block or if the current not is not alone in the block
  2063. * then we print a block around a degenerate for loop such that the variable
  2064. * declaration will not conflict with any potential other declaration
  2065. * of the same variable.
  2066. */
  2067. static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p,
  2068. __isl_keep isl_ast_node *node,
  2069. __isl_keep isl_ast_print_options *options, int in_block, int in_list)
  2070. {
  2071. isl_id *id;
  2072. const char *name;
  2073. const char *type;
  2074. type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p));
  2075. if (!node->u.f.degenerate) {
  2076. id = isl_ast_expr_get_id(node->u.f.iterator);
  2077. name = isl_id_get_name(id);
  2078. isl_id_free(id);
  2079. p = isl_printer_start_line(p);
  2080. p = isl_printer_print_str(p, "for (");
  2081. p = isl_printer_print_str(p, type);
  2082. p = isl_printer_print_str(p, " ");
  2083. p = isl_printer_print_str(p, name);
  2084. p = isl_printer_print_str(p, " = ");
  2085. p = isl_printer_print_ast_expr(p, node->u.f.init);
  2086. p = isl_printer_print_str(p, "; ");
  2087. p = isl_printer_print_ast_expr(p, node->u.f.cond);
  2088. p = isl_printer_print_str(p, "; ");
  2089. p = isl_printer_print_str(p, name);
  2090. p = isl_printer_print_str(p, " += ");
  2091. p = isl_printer_print_ast_expr(p, node->u.f.inc);
  2092. p = isl_printer_print_str(p, ")");
  2093. p = print_body_c(p, node->u.f.body, NULL, options, 0);
  2094. } else {
  2095. id = isl_ast_expr_get_id(node->u.f.iterator);
  2096. name = isl_id_get_name(id);
  2097. isl_id_free(id);
  2098. if (!in_block || in_list)
  2099. p = start_block(p);
  2100. p = isl_printer_start_line(p);
  2101. p = isl_printer_print_str(p, type);
  2102. p = isl_printer_print_str(p, " ");
  2103. p = isl_printer_print_str(p, name);
  2104. p = isl_printer_print_str(p, " = ");
  2105. p = isl_printer_print_ast_expr(p, node->u.f.init);
  2106. p = isl_printer_print_str(p, ";");
  2107. p = isl_printer_end_line(p);
  2108. p = print_ast_node_c(p, node->u.f.body, options, 1, 0);
  2109. if (!in_block || in_list)
  2110. p = end_block(p);
  2111. }
  2112. return p;
  2113. }
  2114. /* Print the if node "node".
  2115. * If "new_line" is set then the if node should be printed on a new line.
  2116. * If "force_block" is set, then print out the body as a block.
  2117. */
  2118. static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
  2119. __isl_keep isl_ast_node *node,
  2120. __isl_keep isl_ast_print_options *options, int new_line,
  2121. int force_block)
  2122. {
  2123. if (new_line)
  2124. p = isl_printer_start_line(p);
  2125. p = isl_printer_print_str(p, "if (");
  2126. p = isl_printer_print_ast_expr(p, node->u.i.guard);
  2127. p = isl_printer_print_str(p, ")");
  2128. p = print_body_c(p, node->u.i.then, node->u.i.else_node, options,
  2129. force_block);
  2130. return p;
  2131. }
  2132. /* Print the "node" to "p".
  2133. *
  2134. * "in_block" is set if we are currently inside a block.
  2135. * If so, we do not print a block around the children of a block node.
  2136. * We do this to avoid an extra block around the body of a degenerate
  2137. * for node.
  2138. *
  2139. * "in_list" is set if the current node is not alone in the block.
  2140. */
  2141. static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
  2142. __isl_keep isl_ast_node *node,
  2143. __isl_keep isl_ast_print_options *options, int in_block, int in_list)
  2144. {
  2145. switch (node->type) {
  2146. case isl_ast_node_for:
  2147. if (options->print_for)
  2148. return options->print_for(p,
  2149. isl_ast_print_options_copy(options),
  2150. node, options->print_for_user);
  2151. p = print_for_c(p, node, options, in_block, in_list);
  2152. break;
  2153. case isl_ast_node_if:
  2154. p = print_if_c(p, node, options, 1, 0);
  2155. break;
  2156. case isl_ast_node_block:
  2157. if (!in_block)
  2158. p = start_block(p);
  2159. p = isl_ast_node_list_print(node->u.b.children, p, options);
  2160. if (!in_block)
  2161. p = end_block(p);
  2162. break;
  2163. case isl_ast_node_mark:
  2164. p = isl_printer_start_line(p);
  2165. p = isl_printer_print_str(p, "// ");
  2166. p = isl_printer_print_str(p, isl_id_get_name(node->u.m.mark));
  2167. p = isl_printer_end_line(p);
  2168. p = print_ast_node_c(p, node->u.m.node, options, 0, in_list);
  2169. break;
  2170. case isl_ast_node_user:
  2171. if (options->print_user)
  2172. return options->print_user(p,
  2173. isl_ast_print_options_copy(options),
  2174. node, options->print_user_user);
  2175. p = isl_printer_start_line(p);
  2176. p = isl_printer_print_ast_expr(p, node->u.e.expr);
  2177. p = isl_printer_print_str(p, ";");
  2178. p = isl_printer_end_line(p);
  2179. break;
  2180. case isl_ast_node_error:
  2181. break;
  2182. }
  2183. return p;
  2184. }
  2185. /* Print the for node "node" to "p".
  2186. */
  2187. __isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node,
  2188. __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
  2189. {
  2190. if (!node || !options)
  2191. goto error;
  2192. if (node->type != isl_ast_node_for)
  2193. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  2194. "not a for node", goto error);
  2195. p = print_for_c(p, node, options, 0, 0);
  2196. isl_ast_print_options_free(options);
  2197. return p;
  2198. error:
  2199. isl_ast_print_options_free(options);
  2200. isl_printer_free(p);
  2201. return NULL;
  2202. }
  2203. /* Print the if node "node" to "p".
  2204. */
  2205. __isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node,
  2206. __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
  2207. {
  2208. if (!node || !options)
  2209. goto error;
  2210. if (node->type != isl_ast_node_if)
  2211. isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
  2212. "not an if node", goto error);
  2213. p = print_if_c(p, node, options, 1, 0);
  2214. isl_ast_print_options_free(options);
  2215. return p;
  2216. error:
  2217. isl_ast_print_options_free(options);
  2218. isl_printer_free(p);
  2219. return NULL;
  2220. }
  2221. /* Print "node" to "p".
  2222. *
  2223. * "node" is assumed to be either the outermost node in an AST or
  2224. * a node that is known not to be a block.
  2225. * If "node" is a block (and is therefore outermost) and
  2226. * if the ast_print_outermost_block options is not set,
  2227. * then act as if the printing occurs inside a block, such
  2228. * that no "extra" block will get printed.
  2229. */
  2230. __isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node,
  2231. __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
  2232. {
  2233. int in_block = 0;
  2234. if (!options || !node)
  2235. goto error;
  2236. if (node->type == isl_ast_node_block) {
  2237. isl_ctx *ctx;
  2238. ctx = isl_ast_node_get_ctx(node);
  2239. in_block = !isl_options_get_ast_print_outermost_block(ctx);
  2240. }
  2241. p = print_ast_node_c(p, node, options, in_block, 0);
  2242. isl_ast_print_options_free(options);
  2243. return p;
  2244. error:
  2245. isl_ast_print_options_free(options);
  2246. isl_printer_free(p);
  2247. return NULL;
  2248. }
  2249. /* Print "node" to "p".
  2250. */
  2251. __isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p,
  2252. __isl_keep isl_ast_node *node)
  2253. {
  2254. int format;
  2255. isl_ast_print_options *options;
  2256. if (!p)
  2257. return NULL;
  2258. format = isl_printer_get_output_format(p);
  2259. switch (format) {
  2260. case ISL_FORMAT_ISL:
  2261. p = print_ast_node_isl(p, node);
  2262. break;
  2263. case ISL_FORMAT_C:
  2264. options = isl_ast_print_options_alloc(isl_printer_get_ctx(p));
  2265. p = isl_ast_node_print(node, p, options);
  2266. break;
  2267. default:
  2268. isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
  2269. "output format not supported for ast_node",
  2270. return isl_printer_free(p));
  2271. }
  2272. return p;
  2273. }
  2274. /* Print the list of nodes "list" to "p".
  2275. */
  2276. __isl_give isl_printer *isl_ast_node_list_print(
  2277. __isl_keep isl_ast_node_list *list, __isl_take isl_printer *p,
  2278. __isl_keep isl_ast_print_options *options)
  2279. {
  2280. int i;
  2281. if (!p || !list || !options)
  2282. return isl_printer_free(p);
  2283. for (i = 0; i < list->n; ++i)
  2284. p = print_ast_node_c(p, list->p[i], options, 1, 1);
  2285. return p;
  2286. }
  2287. #define ISL_AST_MACRO_FDIV_Q (1 << 0)
  2288. #define ISL_AST_MACRO_MIN (1 << 1)
  2289. #define ISL_AST_MACRO_MAX (1 << 2)
  2290. #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FDIV_Q | \
  2291. ISL_AST_MACRO_MIN | \
  2292. ISL_AST_MACRO_MAX)
  2293. /* If "expr" contains an isl_ast_expr_op_min, isl_ast_expr_op_max or
  2294. * isl_ast_expr_op_fdiv_q then set the corresponding bit in "macros".
  2295. */
  2296. static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros)
  2297. {
  2298. int i;
  2299. if (macros == ISL_AST_MACRO_ALL)
  2300. return macros;
  2301. if (expr->type != isl_ast_expr_op)
  2302. return macros;
  2303. if (expr->u.op.op == isl_ast_expr_op_min)
  2304. macros |= ISL_AST_MACRO_MIN;
  2305. if (expr->u.op.op == isl_ast_expr_op_max)
  2306. macros |= ISL_AST_MACRO_MAX;
  2307. if (expr->u.op.op == isl_ast_expr_op_fdiv_q)
  2308. macros |= ISL_AST_MACRO_FDIV_Q;
  2309. for (i = 0; i < expr->u.op.n_arg; ++i)
  2310. macros = ast_expr_required_macros(expr->u.op.args[i], macros);
  2311. return macros;
  2312. }
  2313. static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
  2314. int macros);
  2315. /* If "node" contains an isl_ast_expr_op_min, isl_ast_expr_op_max or
  2316. * isl_ast_expr_op_fdiv_q then set the corresponding bit in "macros".
  2317. */
  2318. static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros)
  2319. {
  2320. if (macros == ISL_AST_MACRO_ALL)
  2321. return macros;
  2322. switch (node->type) {
  2323. case isl_ast_node_for:
  2324. macros = ast_expr_required_macros(node->u.f.init, macros);
  2325. if (!node->u.f.degenerate) {
  2326. macros = ast_expr_required_macros(node->u.f.cond,
  2327. macros);
  2328. macros = ast_expr_required_macros(node->u.f.inc,
  2329. macros);
  2330. }
  2331. macros = ast_node_required_macros(node->u.f.body, macros);
  2332. break;
  2333. case isl_ast_node_if:
  2334. macros = ast_expr_required_macros(node->u.i.guard, macros);
  2335. macros = ast_node_required_macros(node->u.i.then, macros);
  2336. if (node->u.i.else_node)
  2337. macros = ast_node_required_macros(node->u.i.else_node,
  2338. macros);
  2339. break;
  2340. case isl_ast_node_block:
  2341. macros = ast_node_list_required_macros(node->u.b.children,
  2342. macros);
  2343. break;
  2344. case isl_ast_node_mark:
  2345. macros = ast_node_required_macros(node->u.m.node, macros);
  2346. break;
  2347. case isl_ast_node_user:
  2348. macros = ast_expr_required_macros(node->u.e.expr, macros);
  2349. break;
  2350. case isl_ast_node_error:
  2351. break;
  2352. }
  2353. return macros;
  2354. }
  2355. /* If "list" contains an isl_ast_expr_op_min, isl_ast_expr_op_max or
  2356. * isl_ast_expr_op_fdiv_q then set the corresponding bit in "macros".
  2357. */
  2358. static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
  2359. int macros)
  2360. {
  2361. int i;
  2362. for (i = 0; i < list->n; ++i)
  2363. macros = ast_node_required_macros(list->p[i], macros);
  2364. return macros;
  2365. }
  2366. /* Data structure for keeping track of whether a macro definition
  2367. * for a given type has already been printed.
  2368. * The value is zero if no definition has been printed and non-zero otherwise.
  2369. */
  2370. struct isl_ast_expr_op_printed {
  2371. char printed[isl_ast_expr_op_last + 1];
  2372. };
  2373. /* Create an empty struct isl_ast_expr_op_printed.
  2374. */
  2375. static void *create_printed(isl_ctx *ctx)
  2376. {
  2377. return isl_calloc_type(ctx, struct isl_ast_expr_op_printed);
  2378. }
  2379. /* Free a struct isl_ast_expr_op_printed.
  2380. */
  2381. static void free_printed(void *user)
  2382. {
  2383. free(user);
  2384. }
  2385. /* Ensure that "p" has an isl_ast_expr_op_printed note identified by "id".
  2386. */
  2387. static __isl_give isl_printer *alloc_printed(__isl_take isl_printer *p,
  2388. __isl_keep isl_id *id)
  2389. {
  2390. return alloc_note(p, id, &create_printed, &free_printed);
  2391. }
  2392. /* Create an identifier that is used to store
  2393. * an isl_ast_expr_op_printed note.
  2394. */
  2395. static __isl_give isl_id *printed_id(isl_ctx *ctx)
  2396. {
  2397. return isl_id_alloc(ctx, "isl_ast_expr_op_type_printed", NULL);
  2398. }
  2399. /* Did the user specify that a macro definition should only be
  2400. * printed once and has a macro definition for "type" already
  2401. * been printed to "p"?
  2402. * If definitions should only be printed once, but a definition
  2403. * for "p" has not yet been printed, then mark it as having been
  2404. * printed so that it will not printed again.
  2405. * The actual printing is taken care of by the caller.
  2406. */
  2407. static isl_bool already_printed_once(__isl_keep isl_printer *p,
  2408. enum isl_ast_expr_op_type type)
  2409. {
  2410. isl_ctx *ctx;
  2411. isl_id *id;
  2412. struct isl_ast_expr_op_printed *printed;
  2413. if (!p)
  2414. return isl_bool_error;
  2415. ctx = isl_printer_get_ctx(p);
  2416. if (!isl_options_get_ast_print_macro_once(ctx))
  2417. return isl_bool_false;
  2418. if (type > isl_ast_expr_op_last)
  2419. isl_die(isl_printer_get_ctx(p), isl_error_invalid,
  2420. "invalid type", return isl_bool_error);
  2421. id = printed_id(isl_printer_get_ctx(p));
  2422. p = alloc_printed(p, id);
  2423. printed = get_note(p, id);
  2424. isl_id_free(id);
  2425. if (!printed)
  2426. return isl_bool_error;
  2427. if (printed->printed[type])
  2428. return isl_bool_true;
  2429. printed->printed[type] = 1;
  2430. return isl_bool_false;
  2431. }
  2432. /* Print a macro definition for the operator "type".
  2433. *
  2434. * If the user has specified that a macro definition should
  2435. * only be printed once to any given printer and if the macro definition
  2436. * has already been printed to "p", then do not print the definition.
  2437. */
  2438. __isl_give isl_printer *isl_ast_expr_op_type_print_macro(
  2439. enum isl_ast_expr_op_type type, __isl_take isl_printer *p)
  2440. {
  2441. isl_bool skip;
  2442. skip = already_printed_once(p, type);
  2443. if (skip < 0)
  2444. return isl_printer_free(p);
  2445. if (skip)
  2446. return p;
  2447. switch (type) {
  2448. case isl_ast_expr_op_min:
  2449. p = isl_printer_start_line(p);
  2450. p = isl_printer_print_str(p, "#define ");
  2451. p = isl_printer_print_str(p, get_op_str_c(p, type));
  2452. p = isl_printer_print_str(p,
  2453. "(x,y) ((x) < (y) ? (x) : (y))");
  2454. p = isl_printer_end_line(p);
  2455. break;
  2456. case isl_ast_expr_op_max:
  2457. p = isl_printer_start_line(p);
  2458. p = isl_printer_print_str(p, "#define ");
  2459. p = isl_printer_print_str(p, get_op_str_c(p, type));
  2460. p = isl_printer_print_str(p,
  2461. "(x,y) ((x) > (y) ? (x) : (y))");
  2462. p = isl_printer_end_line(p);
  2463. break;
  2464. case isl_ast_expr_op_fdiv_q:
  2465. p = isl_printer_start_line(p);
  2466. p = isl_printer_print_str(p, "#define ");
  2467. p = isl_printer_print_str(p, get_op_str_c(p, type));
  2468. p = isl_printer_print_str(p,
  2469. "(n,d) "
  2470. "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
  2471. p = isl_printer_end_line(p);
  2472. break;
  2473. default:
  2474. break;
  2475. }
  2476. return p;
  2477. }
  2478. /* This is an alternative name for the function above.
  2479. */
  2480. __isl_give isl_printer *isl_ast_op_type_print_macro(
  2481. enum isl_ast_expr_op_type type, __isl_take isl_printer *p)
  2482. {
  2483. return isl_ast_expr_op_type_print_macro(type, p);
  2484. }
  2485. /* Call "fn" for each type of operation represented in the "macros"
  2486. * bit vector.
  2487. */
  2488. static isl_stat foreach_ast_expr_op_type(int macros,
  2489. isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
  2490. {
  2491. if (macros & ISL_AST_MACRO_MIN && fn(isl_ast_expr_op_min, user) < 0)
  2492. return isl_stat_error;
  2493. if (macros & ISL_AST_MACRO_MAX && fn(isl_ast_expr_op_max, user) < 0)
  2494. return isl_stat_error;
  2495. if (macros & ISL_AST_MACRO_FDIV_Q &&
  2496. fn(isl_ast_expr_op_fdiv_q, user) < 0)
  2497. return isl_stat_error;
  2498. return isl_stat_ok;
  2499. }
  2500. /* Call "fn" for each type of operation that appears in "expr"
  2501. * and that requires a macro definition.
  2502. */
  2503. isl_stat isl_ast_expr_foreach_ast_expr_op_type(__isl_keep isl_ast_expr *expr,
  2504. isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
  2505. {
  2506. int macros;
  2507. if (!expr)
  2508. return isl_stat_error;
  2509. macros = ast_expr_required_macros(expr, 0);
  2510. return foreach_ast_expr_op_type(macros, fn, user);
  2511. }
  2512. /* This is an alternative name for the function above.
  2513. */
  2514. isl_stat isl_ast_expr_foreach_ast_op_type(__isl_keep isl_ast_expr *expr,
  2515. isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
  2516. {
  2517. return isl_ast_expr_foreach_ast_expr_op_type(expr, fn, user);
  2518. }
  2519. /* Call "fn" for each type of operation that appears in "node"
  2520. * and that requires a macro definition.
  2521. */
  2522. isl_stat isl_ast_node_foreach_ast_expr_op_type(__isl_keep isl_ast_node *node,
  2523. isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
  2524. {
  2525. int macros;
  2526. if (!node)
  2527. return isl_stat_error;
  2528. macros = ast_node_required_macros(node, 0);
  2529. return foreach_ast_expr_op_type(macros, fn, user);
  2530. }
  2531. /* This is an alternative name for the function above.
  2532. */
  2533. isl_stat isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node,
  2534. isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
  2535. {
  2536. return isl_ast_node_foreach_ast_expr_op_type(node, fn, user);
  2537. }
  2538. static isl_stat ast_op_type_print_macro(enum isl_ast_expr_op_type type,
  2539. void *user)
  2540. {
  2541. isl_printer **p = user;
  2542. *p = isl_ast_expr_op_type_print_macro(type, *p);
  2543. return isl_stat_ok;
  2544. }
  2545. /* Print macro definitions for all the macros used in the result
  2546. * of printing "expr".
  2547. */
  2548. __isl_give isl_printer *isl_ast_expr_print_macros(
  2549. __isl_keep isl_ast_expr *expr, __isl_take isl_printer *p)
  2550. {
  2551. if (isl_ast_expr_foreach_ast_expr_op_type(expr,
  2552. &ast_op_type_print_macro, &p) < 0)
  2553. return isl_printer_free(p);
  2554. return p;
  2555. }
  2556. /* Print macro definitions for all the macros used in the result
  2557. * of printing "node".
  2558. */
  2559. __isl_give isl_printer *isl_ast_node_print_macros(
  2560. __isl_keep isl_ast_node *node, __isl_take isl_printer *p)
  2561. {
  2562. if (isl_ast_node_foreach_ast_expr_op_type(node,
  2563. &ast_op_type_print_macro, &p) < 0)
  2564. return isl_printer_free(p);
  2565. return p;
  2566. }
  2567. /* Return a string containing C code representing this isl_ast_expr.
  2568. */
  2569. __isl_give char *isl_ast_expr_to_C_str(__isl_keep isl_ast_expr *expr)
  2570. {
  2571. isl_printer *p;
  2572. char *str;
  2573. if (!expr)
  2574. return NULL;
  2575. p = isl_printer_to_str(isl_ast_expr_get_ctx(expr));
  2576. p = isl_printer_set_output_format(p, ISL_FORMAT_C);
  2577. p = isl_printer_print_ast_expr(p, expr);
  2578. str = isl_printer_get_str(p);
  2579. isl_printer_free(p);
  2580. return str;
  2581. }
  2582. /* Return a string containing C code representing this isl_ast_node.
  2583. */
  2584. __isl_give char *isl_ast_node_to_C_str(__isl_keep isl_ast_node *node)
  2585. {
  2586. isl_printer *p;
  2587. char *str;
  2588. if (!node)
  2589. return NULL;
  2590. p = isl_printer_to_str(isl_ast_node_get_ctx(node));
  2591. p = isl_printer_set_output_format(p, ISL_FORMAT_C);
  2592. p = isl_printer_print_ast_node(p, node);
  2593. str = isl_printer_get_str(p);
  2594. isl_printer_free(p);
  2595. return str;
  2596. }