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