PCFOP * read_const(const char * line) { char buf[LINE_MAX], *bitr; PCFOP * ret = malloc(sizeof(PCFOP)); struct const_op_data * data = malloc(sizeof(struct const_op_data)); check_alloc(ret); check_alloc(data); bitr = buf; ret->op = const_op; ret->data = data; bitr[0] = '\0'; line = skip_to_colon(line); line++; bitr = buf; line = read_token(line, bitr); if(strcmp(buf, "DEST") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->dest) == 1); } else if(strcmp(buf, "OP1") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->value) == 1); } else assert(0); line = skip_to_colon(line); line++; bitr = buf; line = read_token(line, bitr); if(strcmp(buf, "DEST") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->dest) == 1); } else if(strcmp(buf, "OP1") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->value) == 1); } else assert(0); return ret; }
PCFOP * read_mkptr(const char * line) { char buf[LINE_MAX], *bitr; PCFOP * ret = malloc(sizeof(PCFOP)); uint32_t * data = malloc(sizeof(uint32_t)); check_alloc(ret); check_alloc(data); ret->op = mkptr_op; ret->data = data; bitr = buf; line = skip_to_colon(line); line++; bitr = buf; line = read_token(line, bitr); if(strcmp(buf, "DEST") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", data) == 1); } else assert(0); return ret; }
PCFOP * read_initbase(const char * line) { char buf[LINE_MAX], *bitr; PCFOP * ret = malloc(sizeof(struct PCFOP)); check_alloc(ret); ret->op = initbase_op; uint32_t base; bitr = buf; bitr[0] = '\0'; line = skip_to_colon(line); line++; line = assert_token(line, buf, bitr, "BASE"); sscanf(line, "%d\n", &base); assert(base >= 0); ret->data = malloc(sizeof(uint32_t)); check_alloc(ret->data); *((uint32_t*)ret->data) = base; return ret; }
PCFOP * read_branch(const char * line) { char buf[LINE_MAX], *bitr; PCFOP * ret = (PCFOP*)malloc(sizeof(PCFOP)); uint32_t i = 0; struct branch_op_data * data = (struct branch_op_data*)malloc(sizeof(struct call_op_data)); check_alloc(ret); check_alloc(data); ret->op = branch_op; ret->data = data; bitr = buf; for(i = 0; i < 2; i++) { line = skip_to_colon(line); line++; bitr = buf; line = read_token(line, bitr); if(strcmp(buf, "CND") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->cnd_wire) == 1); } else if(strcmp(buf, "TARG") == 0) { ENTRY * ent = (ENTRY*)malloc(sizeof(ENTRY)); check_alloc(ent); while((line[0] == ' ') || (line[0] == '"')) { assert(line[0] != '\0'); line++; } bitr = buf; line = read_token(line, bitr); assert(buf[strlen(buf)-1] == '"'); buf[strlen(buf)-1] = '\0'; ent->key = malloc(strlen(buf)+1); strcpy(ent->key, buf); data->target = ent; } else { assert(0); } } return ret; }
PCFOP * read_gate(const char * line) { char buf[LINE_MAX], *bitr; PCFOP * ret = malloc(sizeof(PCFOP)); struct PCFGate * data = malloc(sizeof(struct PCFGate)); uint32_t i = 0; check_alloc(ret); check_alloc(data); bitr = buf; ret->op = gate_op; ret->data = data; data->tag = TAG_INTERNAL; bitr[0] = '\0'; for(i = 0; i < 4; i++) { line = skip_to_colon(line); line++; bitr = buf; line = read_token(line, bitr); if(strcmp(buf, "DEST") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->reswire) == 1); } else if(strcmp(buf, "OP1") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->wire1) == 1); } else if(strcmp(buf, "OP2") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->wire2) == 1); } else if(strcmp(buf, "TRUTH-TABLE") == 0) { bitr = buf; line = read_token(line, bitr); assert(buf[0] == '#'); assert(buf[1] == '*'); data->truth_table = (buf[2] == '1' ? 1 : 0) | (buf[3] == '1' ? 2 : 0) | (buf[4] == '1' ? 4 : 0) | (buf[5] == '1' ? 8 : 0); } else assert(0); } return ret; }
PCFOP * read_label(const char * line, struct PCFState * st, uint32_t iptr) { ENTRY * newent, * r; char buf[LINE_MAX], *bitr; PCFOP * ret = malloc(sizeof(struct PCFOP)); check_alloc(ret); ret->op = nop; bitr = buf; line = skip_to_colon(line); // Skip over the ':' line++; line = assert_token(line, buf, bitr, "STR"); while((line[0] == ' ') || (line[0] == '"')) { assert(line[0] != '\0'); line++; } bitr = buf; while((line[0] != ' ') && (line[0] != '"') && (line[0] != ')')) { assert(line[0] != '\0'); bitr[0] = line[0]; line++; bitr++; } bitr[0] = '\0'; newent = (ENTRY*)malloc(sizeof(ENTRY)); check_alloc(newent); newent->key = malloc(strlen(buf)+1); check_alloc(newent->key); strcpy(newent->key, buf); newent->data = malloc(sizeof(uint32_t)); check_alloc(newent->data); *((uint32_t*)newent->data) = iptr; //hsearch_r(*newent, ENTER, &r, st->labels); if(hsearch_r(*newent, ENTER, &r, st->labels) == 0) { fprintf(stderr, "Problem inserting hash table for %s %d: %s\n", newent->key, *((uint32_t*)newent->data), strerror(errno)); abort(); } return ret; }
static int read_val(struct cw_KTV_Reader *r, char *val, int max_len){ int c,n,quote; c = skip_to_colon(r); if (c==-1) return -1; c = skip_chars (r, " \t"); if (c=='"'){ quote=1; c=get_char(r); } else{ quote=0; } n=0; while(c!=EOF && n<max_len){ if (quote && c=='"'){ break; } if (c=='\n'){ break; } if (quote){ if (c=='\\'){ c = get_char(r); switch(c){ case 'n': c='\n'; break; case '\\': break; case '"': break; default: unget_char(r,c); c='\\'; } } } val[n++]=c; c=get_char(r); } if(!quote && n>0){ while(n>0){ if (isspace(val[n-1])) n--; else break; } } val[n]=0; return n; }
static int read_type(struct cw_KTV_Reader * r, char *type, int max_len) { int c,n; c = skip_to_colon(r); if (c==-1) return -1; if (c=='='){ unget_char(r,c); return sprintf(type,"%s","Str"); } c = skip_chars (r, " \t"); if (c==':'){ unget_char(r,c); sprintf(type,"%s","Str"); return 0; } if (!isalpha(c)){ if (c=='\n'){ unget_char(r,c); /*sprintf(p->error,"Error at line %d, pos %d: Unexpected EOL.", p->line, p->pos);*/ return -1; } /*sprintf(p->error,"Error at line %d, pos %d: Letter expected.", p->line, p->pos);*/ return -1; } n=0; while(c!=EOF && n<max_len){ if (!isalnum(c) && !strchr("_/-.()@#|{}[]",c)/*strchr(": \t\n\a",c)*/){ unget_char(r,c); break; } type[n]=c; c=get_char(r); n++; } type[n]=0; return n; }
PCFOP * read_arith(const char * line, void (*op)(struct PCFState *, struct PCFOP *)) { char buf[LINE_MAX], *bitr; PCFOP * ret = malloc(sizeof(PCFOP)); struct arith_op_data * data = malloc(sizeof(struct arith_op_data)); uint32_t i = 0; check_alloc(ret); check_alloc(data); bitr = buf; ret->op = op; ret->data = data; bitr[0] = '\0'; for(i = 0; i < 3; i++) { line = skip_to_colon(line); line++; bitr = buf; line = read_token(line, bitr); if(strcmp(buf, "DEST") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->dest) == 1); } else if(strcmp(buf, "OP1") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->op1) == 1); } else if(strcmp(buf, "OP2") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->op2) == 1); } else assert(0); } return ret; }
PCFOP * read_indir_copy(const char * line) { char buf[LINE_MAX], *bitr; PCFOP * ret = malloc(sizeof(PCFOP)); struct copy_op_data * data = malloc(sizeof(struct copy_op_data)); uint32_t i = 0; check_alloc(ret); check_alloc(data); bitr = buf; ret->op = indir_copy_op; ret->data = data; bitr[0] = '\0'; for(i = 0; i < 3; i++) { line = skip_to_colon(line); line++; bitr = buf; line = read_token(line, bitr); if(strcmp(buf, "DEST") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->dest) == 1); } else if(strcmp(buf, "OP1") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->source) == 1); } else if(strcmp(buf, "OP2") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->width) == 1); } else assert(0); } return ret; }
PCFOP * read_clear(const char * line) { char buf[LINE_MAX], *bitr; PCFOP * ret = (PCFOP*)malloc(sizeof(PCFOP)); struct clear_op_data * data = (struct clear_op_data*)malloc(sizeof(struct clear_op_data)); check_alloc(ret); check_alloc(data); ret->op = clear_op; bitr = buf; bitr[0] = '\0'; line = skip_to_colon(line); line++; line = assert_token(line, buf, bitr, "LOCALSIZE"); sscanf(line, "%d\n", &data->localsize); assert(data->localsize >= 0); ret->data = data; return ret; }
PCFOP * read_join(const char * line) { char buf[LINE_MAX], *bitr; PCFOP * ret = (PCFOP*)malloc(sizeof(PCFOP)); uint32_t i = 0; struct join_op_data * data = (struct join_op_data*)malloc(sizeof(struct join_op_data)); check_alloc(ret); check_alloc(data); ret->op = join_op; ret->data = data; bitr = buf; for(i = 0; i < 2; i++) { line = skip_to_colon(line); line++; bitr = buf; line = read_token(line, bitr); if(strcmp(buf, "OP1") == 0) { int cnt = count_tokens_to_close_paren(line); data->nsources = cnt; char buf2[10]; int i, j, k; data->sources = (uint32_t*)malloc(cnt * sizeof(uint32_t)); check_alloc(data->sources); i = 0; j = 0; k = 0; while(line[i] == '(') i++; while(line[i] == ' ') i++; while(line[i] == '(') i++; while(line[i] != ')') { assert(line[i] != '\0'); while((line[i] != ' ') && (line[i] != ')')) { assert(line[i] != '\0'); buf2[j] = line[i]; i++; j++; } buf2[j] = '\0'; assert(sscanf(buf2, "%d", &data->sources[k]) == 1); k++; while(line[i] == ' ') { assert(line[i] != '\0'); i++; } j = 0; } assert(k == cnt); line += i; } else if(strcmp(buf, "DEST") == 0) { bitr = buf; line = read_token(line, bitr); assert(sscanf(buf, "%d", &data->dest) == 1); } else { assert(0); } } return ret; }