strm_string strm_inspect(strm_value v) { if (strm_string_p(v)) { strm_string str = strm_value_str(v); return str_dump(str, str_dump_len(str)); } else if (strm_array_p(v)) { char *buf = malloc(32); strm_int i, bi = 0, capa = 32; strm_array a = strm_value_ary(v); for (i=0; i<strm_ary_len(a); i++) { strm_string str = strm_inspect(strm_ary_ptr(a)[i]); strm_string key = (strm_ary_headers(a) && strm_string_p(strm_ary_ptr(strm_ary_headers(a))[i])) ? strm_value_str(strm_ary_ptr(strm_ary_headers(a))[i]) : strm_str_null; strm_int slen = (key ? (strm_str_len(key)+1) : 0) + strm_str_len(str) + 3; if (bi+slen > capa) { capa *= 2; buf = realloc(buf, capa); } if (bi == 0) { buf[bi++] = '['; } else { buf[bi++] = ','; buf[bi++] = ' '; } if (key) { if (!str_symbol_p(key)) { key = str_dump(key, str_dump_len(key)); } memcpy(buf+bi, strm_str_ptr(key), strm_str_len(key)); bi += strm_str_len(key); buf[bi++] = ':'; } memcpy(buf+bi, strm_str_ptr(str), strm_str_len(str)); bi += strm_str_len(str); } buf[bi++] = ']'; return strm_str_new(buf, bi); } else { return strm_to_str(v); } }
static int write_syms_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) { int sym_no; char *buf_top = buf; char *char_buf; uint16_t buf_size =0; buf_size = MRB_DUMP_DEFAULT_STR_LEN; char_buf = (char *)mrb_malloc(mrb, buf_size); if (char_buf == NULL) goto error_exit; buf += uint32_dump((uint32_t)irep->slen, buf, type); /* number of symbol */ for (sym_no = 0; sym_no < irep->slen; sym_no++) { const char * name; uint16_t nlen =0; if (irep->syms[sym_no] != 0) { size_t len; name = mrb_sym2name_len(mrb, irep->syms[sym_no], &len); if (len > UINT16_MAX) goto error_exit; nlen = str_dump_len((char*)name, len, type); if ( nlen > buf_size - 1) { buf_size = nlen + 1; char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); if (char_buf == NULL) goto error_exit; } memset(char_buf, 0, buf_size); str_dump((char*)name, char_buf, len, type); buf += uint16_dump(nlen, buf, type); /* length of symbol name */ memcpy(buf, char_buf, nlen); /* symbol name */ buf += nlen; } else { buf += uint16_dump(MRB_DUMP_NULL_SYM_LEN, buf, type); /* length of symbol name */ } } error_exit: mrb_free(mrb, char_buf); return (int)(buf - buf_top); }
struct tnode *add_tree(struct tnode *p, char *w) { int cond; if (p == NULL) { p = talloc(); p->word = str_dump(w); p->count = 1; p->left = p->right = NULL; } else if ((cond = strcmp(w, p->word)) == 0) p->count++; else if (cond < 0) p->left = add_tree(p->left, w); else p->right = add_tree(p->right, w); return p; }
/* Finalization. */ void emitter_fini(void) { /* Terminate the function, just in case. */ w("ret void\n\n"); /* Emit exit basic blocks. */ callgraph_dump(); /* Terminate and close the LLVM function. */ w("\n; End of LOWL code\nret void;\n}\n\n"); /* Declare MESS strings. */ str_dump(); w("\n\n"); }
static int write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) { int pool_no; mrb_value str; char *buf_top = buf; char *char_buf; uint16_t buf_size =0; uint16_t len =0; int result; buf_size = MRB_DUMP_DEFAULT_STR_LEN; char_buf = (char *)mrb_malloc(mrb, buf_size); if (char_buf == NULL) { result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } buf += uint32_dump((uint32_t)irep->plen, buf, type); /* number of pool */ for (pool_no = 0; pool_no < irep->plen; pool_no++) { buf += uint8_dump(mrb_type(irep->pool[pool_no]), buf, type); /* data type */ memset(char_buf, 0, buf_size); switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: str = mrb_fix2str(mrb, irep->pool[pool_no], 10); memcpy(char_buf, RSTRING_PTR(str), RSTRING_LEN(str)); len = RSTRING_LEN(str); break; case MRB_TT_FLOAT: len = mrb_float_to_str(char_buf, mrb_float(irep->pool[pool_no])); break; case MRB_TT_STRING: str = irep->pool[pool_no]; len = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if (len > buf_size - 1) { buf_size = len + 1; char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); if (char_buf == NULL) { result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } memset(char_buf, 0, buf_size); } str_dump(RSTRING_PTR(str), char_buf, RSTRING_LEN(str), type); break; default: buf += uint16_dump(0, buf, type); /* data length = 0 */ continue; } buf += uint16_dump(len, buf, type); /* data length */ memcpy(buf, char_buf, len); buf += len; } result = (int)(buf - buf_top); error_exit: mrb_free(mrb, char_buf); return result; }
static int write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) { int pool_no; mrb_value str; char *buf_top = buf; char *char_buf; uint16_t buf_size =0; buf_size = MRB_DUMP_DEFAULT_STR_LEN; if ((char_buf = mrb_malloc(mrb, buf_size)) == 0) goto error_exit; buf += uint32_dump((uint32_t)irep->plen, buf, type); /* number of pool */ for (pool_no = 0; pool_no < irep->plen; pool_no++) { uint16_t nlen =0; buf += uint8_dump(irep->pool[pool_no].tt, buf, type); /* data type */ memset(char_buf, 0, buf_size); switch (irep->pool[pool_no].tt) { case MRB_TT_FIXNUM: sprintf(char_buf, "%d", irep->pool[pool_no].value.i); break; case MRB_TT_FLOAT: sprintf(char_buf, "%.16e", irep->pool[pool_no].value.f); break; case MRB_TT_STRING: str = mrb_string_value( mrb, &irep->pool[pool_no]); nlen = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if ( nlen > buf_size - 1) { buf_size = nlen + 1; if ((char_buf = mrb_realloc(mrb, char_buf, buf_size)) == 0) goto error_exit; memset(char_buf, 0, buf_size); } str_dump(RSTRING_PTR(str), char_buf, RSTRING_LEN(str), type); break; #ifdef INCLUDE_REGEXP case MRB_TT_REGEX: str = mrb_reg_to_s(mrb, irep->pool[pool_no]); nlen = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if ( nlen > buf_size - 1) { buf_size = nlen + 1; if ((char_buf = mrb_realloc(mrb, char_buf, buf_size)) == 0) goto error_exit; memset(char_buf, 0, buf_size); } str_dump(RSTRING_PTR(str), char_buf, RSTRING_LEN(str), type); break; #endif default: buf += uint16_dump(0, buf, type); /* data length = 0 */ continue; } buf += uint16_dump((uint16_t)strlen(char_buf), buf, type); /* data length */ memcpy(buf, char_buf, strlen(char_buf)); buf += strlen(char_buf); } error_exit: if (char_buf) mrb_free(mrb, char_buf); return (int)(buf - buf_top); }