dump.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. * dump.c - Dump objects in the native FPD format
  3. *
  4. * Written 2009-2012, 2014-2015 by Werner Almesberger
  5. * Copyright 2009-2012, 2014-2015 by Werner Almesberger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <assert.h>
  16. #include <sys/types.h>
  17. #include "util.h"
  18. #include "unparse.h"
  19. #include "obj.h"
  20. #include "gui_status.h"
  21. #include "dump.h"
  22. /* ----- order items ------------------------------------------------------- */
  23. static void add_item(struct order **curr, struct vec *vec, struct obj *obj)
  24. {
  25. (*curr)->vec = vec;
  26. (*curr)->obj = obj;
  27. (*curr)++;
  28. }
  29. static int n_vec_refs(const struct vec *vec)
  30. {
  31. const struct vec *walk;
  32. int n;
  33. n = 0;
  34. for (walk = vec->frame->vecs; walk; walk = walk->next)
  35. if (walk->base == vec)
  36. n++;
  37. return n;
  38. }
  39. /*
  40. * If "prev" is non-NULL, we're looking for objects that need to be put after
  41. * the current vector (in "prev"). Only those objects need to be put there
  42. * that have at least one base that isn't the frame's origin.
  43. *
  44. * We could also make an exception for manually named vectors, but we get
  45. * better clustering without.
  46. */
  47. static int need(const struct vec *base, const struct vec *prev)
  48. {
  49. if (!base)
  50. return 0;
  51. #if 0
  52. if (base->name && *base->name != '_')
  53. return 0;
  54. #endif
  55. if (prev)
  56. return base == prev;
  57. return 1;
  58. }
  59. /*
  60. * If we need a vector that's defined later, we have to defer dumping the
  61. * object.
  62. */
  63. static int later(const struct vec *base, const struct vec *prev)
  64. {
  65. return base && !base->dumped;
  66. #if 0
  67. while (1) {
  68. prev = prev->next;
  69. if (!prev)
  70. break;
  71. if (base == prev)
  72. return 1;
  73. }
  74. return 0;
  75. #endif
  76. }
  77. static int may_put_obj_now(const struct obj *obj, const struct vec *prev)
  78. {
  79. int n, l;
  80. n = need(obj->base, prev);
  81. l = later(obj->base, prev);
  82. switch (obj->type) {
  83. case ot_frame:
  84. break;
  85. case ot_line:
  86. n |= need(obj->u.line.other, prev);
  87. l |= later(obj->u.line.other, prev);
  88. break;
  89. case ot_rect:
  90. n |= need(obj->u.rect.other, prev);
  91. l |= later(obj->u.rect.other, prev);
  92. break;
  93. case ot_pad:
  94. n |= need(obj->u.pad.other, prev);
  95. l |= later(obj->u.pad.other, prev);
  96. break;
  97. case ot_hole:
  98. n |= need(obj->u.hole.other, prev);
  99. l |= later(obj->u.hole.other, prev);
  100. break;
  101. case ot_arc:
  102. n |= need(obj->u.arc.start, prev);
  103. n |= need(obj->u.arc.end, prev);
  104. l |= later(obj->u.arc.start, prev);
  105. l |= later(obj->u.arc.end, prev);
  106. break;
  107. case ot_meas:
  108. return 0;
  109. default:
  110. abort();
  111. }
  112. return n && !l;
  113. }
  114. static void put_obj(struct order **curr, struct obj *obj,
  115. struct vec *prev)
  116. {
  117. if (obj->dumped)
  118. return;
  119. obj->dumped = 1;
  120. add_item(curr, prev, obj);
  121. }
  122. /*
  123. * Tricky logic ahead: when dumping a vector, we search for a vector that
  124. * depends on that vector for ".". If we find one, we dump it immediately after
  125. * this vector.
  126. */
  127. static void recurse_vec(struct order **curr, struct vec *vec)
  128. {
  129. struct vec *next;
  130. struct obj *obj;
  131. vec->dumped = 1;
  132. add_item(curr, vec, NULL);
  133. for (obj = vec->frame->objs; obj; obj = obj->next)
  134. if (may_put_obj_now(obj, vec))
  135. put_obj(curr, obj, vec);
  136. if (n_vec_refs(vec) == 1) {
  137. for (next = vec->next; next->base != vec; next = next->next);
  138. recurse_vec(curr, next);
  139. }
  140. }
  141. static void order_vecs(struct order **curr, struct vec *vecs)
  142. {
  143. struct vec *vec;
  144. for (vec = vecs; vec; vec = vec->next) {
  145. const char *name = (const char *) vec->base;
  146. if (!vec->base || *name || n_vec_refs(vec->base) != 1)
  147. recurse_vec(curr, vec);
  148. }
  149. }
  150. struct order *order_frame(const struct frame *frame)
  151. {
  152. struct order *order, *curr;
  153. struct vec *vec;
  154. struct obj *obj;
  155. int n = 0;
  156. for (vec = frame->vecs; vec; vec = vec->next)
  157. n++;
  158. for (obj = frame->objs; obj; obj = obj->next)
  159. if (obj->type != ot_meas)
  160. n++;
  161. for (vec = frame->vecs; vec; vec = vec->next)
  162. vec->dumped = 0;
  163. for (obj = frame->objs; obj; obj = obj->next)
  164. obj->dumped = 0;
  165. order = alloc_size(sizeof(*order)*(n+1));
  166. curr = order;
  167. order_vecs(&curr, frame->vecs);
  168. /* frames based on @ (anything else ?) */
  169. for (obj = frame->objs; obj; obj = obj->next)
  170. if (obj->type != ot_meas)
  171. put_obj(&curr, obj, NULL);
  172. assert(curr == order+n);
  173. add_item(&curr, NULL, NULL);
  174. return order;
  175. }
  176. /* ----- variables --------------------------------------------------------- */
  177. static void dump_var(FILE *file, const struct table *table,
  178. const char *indent)
  179. {
  180. char *s;
  181. s = unparse(table->rows->values->expr);
  182. fprintf(file, "%sset %s%s = %s\n\n", indent,
  183. table->vars->key ? "?" : "", table->vars->name, s);
  184. free(s);
  185. }
  186. static void dump_table(FILE *file, const struct table *table,
  187. const char *indent)
  188. {
  189. const struct var *var;
  190. const struct row *row;
  191. const struct value *value;
  192. char *s;
  193. if (table->vars && !table->vars->next &&
  194. table->rows && !table->rows->next) {
  195. dump_var(file, table, indent);
  196. return;
  197. }
  198. fprintf(file, "%stable\n%s {", indent, indent);
  199. for (var = table->vars; var; var = var->next)
  200. fprintf(file, "%s %s%s", var == table->vars ? "" : ",",
  201. var->key ? "?" : "", var->name);
  202. fprintf(file, " }\n");
  203. for (row = table->rows; row; row = row->next) {
  204. fprintf(file, "%s {", indent);
  205. for (value = row->values; value; value = value->next) {
  206. s = unparse(value->expr);
  207. fprintf(file, "%s %s",
  208. value == row->values? "" : ",", s);
  209. free(s);
  210. }
  211. fprintf(file, " }\n");
  212. }
  213. fprintf(file, "\n");
  214. }
  215. static void dump_loop(FILE *file, const struct loop *loop, const char *indent)
  216. {
  217. char *from, *to;
  218. from = unparse(loop->from.expr);
  219. to = unparse(loop->to.expr);
  220. fprintf(file, "%sloop %s = %s, %s\n\n",
  221. indent, loop->var.name, from, to);
  222. free(from);
  223. free(to);
  224. }
  225. /* ----- vectors and objects ----------------------------------------------- */
  226. static void generate_name(struct vec *base)
  227. {
  228. char tmp[10]; /* plenty */
  229. const char *s;
  230. struct vec *walk;
  231. int n = 0;
  232. while (1) {
  233. sprintf(tmp, "__%d", n);
  234. s = unique(tmp);
  235. for (walk = base->frame->vecs; walk; walk = walk->next)
  236. if (walk->name == s)
  237. break;
  238. if (!walk)
  239. break;
  240. n++;
  241. }
  242. base->name = s;
  243. }
  244. static const char *base_name(struct vec *base, const struct vec *next)
  245. {
  246. const char *name = (const char *) base;
  247. if (!base)
  248. return "@";
  249. if (*name)
  250. return name;
  251. if (next && base->next == next)
  252. return ".";
  253. if (!base->name)
  254. generate_name(base);
  255. return base->name;
  256. }
  257. static const char *obj_base_name(struct vec *base, const struct vec *prev)
  258. {
  259. if (base && base == prev)
  260. return ".";
  261. return base_name(base, NULL);
  262. }
  263. char *print_obj(const struct obj *obj, const struct vec *prev)
  264. {
  265. const char *base, *s1, *s3;
  266. char *s, *s2;
  267. base = obj_base_name(obj->base, prev);
  268. switch (obj->type) {
  269. case ot_frame:
  270. s = stralloc_printf("frame %s %s",
  271. obj->u.frame.ref->name, base);
  272. break;
  273. case ot_line:
  274. s1 = obj_base_name(obj->u.line.other, prev);
  275. s2 = unparse(obj->u.line.width);
  276. s = stralloc_printf("line %s %s %s", base, s1, s2);
  277. free(s2);
  278. break;
  279. case ot_rect:
  280. s1 = obj_base_name(obj->u.rect.other, prev);
  281. s2 = unparse(obj->u.rect.width);
  282. s = stralloc_printf("rect %s %s %s", base, s1, s2);
  283. free(s2);
  284. break;
  285. case ot_pad:
  286. s1 = obj_base_name(obj->u.pad.other, prev);
  287. switch (obj->u.pad.type) {
  288. case pt_normal:
  289. s2 = "";
  290. break;
  291. case pt_bare:
  292. s2 = " bare";
  293. break;
  294. case pt_trace:
  295. s2 = " trace";
  296. break;
  297. case pt_paste:
  298. s2 = " paste";
  299. break;
  300. case pt_mask:
  301. s2 = " mask";
  302. break;
  303. default:
  304. abort();
  305. }
  306. s = stralloc_printf("%spad \"%s\" %s %s%s",
  307. obj->u.pad.rounded ? "r" : "",
  308. obj->u.pad.name, base, s1, s2);
  309. break;
  310. case ot_hole:
  311. s1 = obj_base_name(obj->u.hole.other, prev);
  312. s = stralloc_printf("hole %s %s", base, s1);
  313. break;
  314. case ot_arc:
  315. s1 = obj_base_name(obj->u.arc.start, prev);
  316. s2 = unparse(obj->u.arc.width);
  317. if (obj->u.arc.start == obj->u.arc.end) {
  318. s = stralloc_printf("circ %s %s %s", base, s1, s2);
  319. } else {
  320. s3 = obj_base_name(obj->u.arc.end, prev);
  321. s = stralloc_printf("arc %s %s %s %s",
  322. base, s1, s3, s2);
  323. }
  324. free(s2);
  325. break;
  326. case ot_iprint:
  327. s2 = unparse(obj->u.iprint.expr);
  328. s = stralloc_printf("%%iprint %s", s2);
  329. free(s2);
  330. break;
  331. default:
  332. abort();
  333. }
  334. return s;
  335. }
  336. /* ----- print measurement ------------------------------------------------- */
  337. static const char *meas_type_name[mt_n] = {
  338. "meas", "measx", "measy",
  339. "meas", "measx", "measy",
  340. };
  341. static char *print_meas_base(struct vec *base, const struct frame_qual *qual)
  342. {
  343. const char *name;
  344. size_t n;
  345. const struct frame_qual *walk;
  346. char *s, *p;
  347. name = base_name(base, NULL);
  348. n = strlen(name)+1; /* vec\0 */
  349. for (walk = qual; walk; walk = walk->next)
  350. n += strlen(walk->frame->name)+1; /* frame/ */
  351. if (base->frame != frames)
  352. n += strlen(base->frame->name)+1; /* frame. */
  353. s = p = alloc_size(n);
  354. for (walk = qual; walk; walk = walk->next) {
  355. n = strlen(walk->frame->name);
  356. memcpy(p, walk->frame->name, n);
  357. p[n] = '/';
  358. p += n+1;
  359. }
  360. if (base->frame != frames) {
  361. n = strlen(base->frame->name);
  362. memcpy(p, base->frame->name, n);
  363. p[n] = '.';
  364. p += n+1;
  365. }
  366. strcpy(p, name);
  367. return s;
  368. }
  369. char *print_meas(const struct obj *obj)
  370. {
  371. char *s, *t;
  372. char *s1, *s2, *s3;
  373. assert(obj->type == ot_meas);
  374. s = stralloc_printf("%s ", meas_type_name[obj->u.meas.type]);
  375. if (obj->u.meas.label) {
  376. t = stralloc_printf("%s\"%s\" ", s, obj->u.meas.label);
  377. free(s);
  378. s = t;
  379. }
  380. s1 = print_meas_base(obj->base, obj->u.meas.low_qual);
  381. s2 = stralloc_printf(" %s ",
  382. obj->u.meas.type < 3 ? obj->u.meas.inverted ? "<-" : "->" :
  383. obj->u.meas.inverted ? "<<" : ">>");
  384. s3 = print_meas_base(obj->u.meas.high, obj->u.meas.high_qual);
  385. t = stralloc_printf("%s%s%s%s", s, s1, s2, s3);
  386. free(s);
  387. free(s1);
  388. free(s2);
  389. free(s3);
  390. s = t;
  391. if (!obj->u.meas.offset)
  392. return s;
  393. s1 = unparse(obj->u.meas.offset);
  394. t = stralloc_printf("%s %s", s, s1);
  395. free(s);
  396. free(s1);
  397. return t;
  398. }
  399. /* ----- print vector ------------------------------------------------------ */
  400. const char *print_label(struct vec *vec)
  401. {
  402. if (!vec->name)
  403. generate_name(vec);
  404. return vec->name;
  405. }
  406. char *print_vec(const struct vec *vec)
  407. {
  408. const char *base;
  409. char *x, *y, *s;
  410. base = base_name(vec->base, vec);
  411. x = unparse(vec->x);
  412. y = unparse(vec->y);
  413. if (vec->name)
  414. s = stralloc_printf("vec %s(%s, %s)", base, x, y);
  415. else
  416. s = stralloc_printf("vec %s(%s, %s)", base, x, y);
  417. free(x);
  418. free(y);
  419. return s;
  420. }
  421. /* ----- frames ------------------------------------------------------------ */
  422. static void dump_frame(FILE *file, struct frame *frame, const char *indent)
  423. {
  424. const struct table *table;
  425. const struct loop *loop;
  426. struct obj *obj;
  427. struct order *order;
  428. const struct order *item;
  429. char *s;
  430. const char *s1;
  431. if (frame->dumped)
  432. return;
  433. frame->dumped = 1;
  434. for (obj = frame->objs; obj; obj = obj->next)
  435. if (obj->type == ot_frame)
  436. dump_frame(file, obj->u.frame.ref, "\t");
  437. if (frame->name)
  438. fprintf(file, "frame %s {\n", frame->name);
  439. for (table = frame->tables; table; table = table->next)
  440. dump_table(file, table, indent);
  441. for (loop = frame->loops; loop; loop = loop->next)
  442. dump_loop(file, loop, indent);
  443. order = order_frame(frame);
  444. for (item = order; item->vec || item->obj; item++) {
  445. if (item->obj) {
  446. fprintf(file, "%s", indent);
  447. if (item->obj->name)
  448. fprintf(file, "%s: ", item->obj->name);
  449. s = print_obj(item->obj, item->vec);
  450. fprintf(file, "%s\n", s);
  451. } else {
  452. s1 = print_label(item->vec);
  453. s = print_vec(item->vec);
  454. fprintf(file, "%s%s: %s\n", indent, s1, s);
  455. }
  456. free(s);
  457. }
  458. free(order);
  459. for (obj = frame->objs; obj; obj = obj->next) {
  460. if (obj->dumped)
  461. continue;
  462. s = print_meas(obj);
  463. fprintf(file, "%s%s\n", indent, s);
  464. free(s);
  465. }
  466. if (frame->name)
  467. fprintf(file, "}\n\n");
  468. }
  469. /* ----- file -------------------------------------------------------------- */
  470. static void dump_unit(FILE *file)
  471. {
  472. switch (curr_unit) {
  473. case curr_unit_mm:
  474. fprintf(file, "unit mm\n");
  475. break;
  476. case curr_unit_mil:
  477. fprintf(file, "unit mil\n");
  478. break;
  479. case curr_unit_auto:
  480. fprintf(file, "unit auto\n");
  481. break;
  482. default:
  483. abort();
  484. }
  485. }
  486. static void dump_allow(FILE *file)
  487. {
  488. switch (allow_overlap) {
  489. case ao_none:
  490. break;
  491. case ao_touch:
  492. fprintf(file, "allow touch\n");
  493. break;
  494. case ao_any:
  495. fprintf(file, "allow overlap\n");
  496. break;
  497. default:
  498. abort();
  499. }
  500. if (!holes_linked)
  501. fprintf(file, "allow holes\n");
  502. }
  503. static void reverse_frames(FILE *file, struct frame *last)
  504. {
  505. if (last) {
  506. reverse_frames(file, last->next);
  507. dump_frame(file, last, "\t");
  508. }
  509. }
  510. int dump(FILE *file, const char *one)
  511. {
  512. struct frame *frame;
  513. assert(!one);
  514. fprintf(file, "%s\n", MACHINE_GENERATED);
  515. for (frame = frames; frame; frame = frame->next)
  516. frame->dumped = 0;
  517. reverse_frames(file, frames->next);
  518. fprintf(file, "package \"%s\"\n", pkg_name);
  519. dump_unit(file);
  520. dump_allow(file);
  521. fprintf(file, "\n");
  522. dump_frame(file, frames, "");
  523. fflush(file);
  524. return !ferror(file);
  525. }