+void code_write(TAG*tag, abc_code_t*code, pool_t*pool, abc_file_t*file)
+{
+ int pos = 0;
+ int length = 0;
+ abc_code_t*c = code;
+ while(c) {
+ c->pos = pos;
+ pos += opcode_write(0, c, pool, file);
+ c = c->next;
+ }
+ length = pos;
+ swf_SetU30(tag, pos);
+ int start = tag->len;
+ c = code;
+ pos = 0;
+ while(c) {
+ opcode_t*op = opcode_get(code->opcode);
+ if(op->flags&(OP_BRANCH|OP_JUMP)) {
+ int skip = 0;
+ if(c->branch)
+ skip = (c->branch->pos) - pos - 4;
+ else
+ skip = length - pos - 4;
+ c->data[0] = (void*)(ptroff_t)skip;
+ }
+ pos += opcode_write(tag, c, pool, file);
+ c = c->next;
+ }
+ assert(tag->len - start == pos);
+}
+
+
+typedef struct {
+ int stackpos;
+ int scopepos;
+ abc_code_t*code;
+ char seen;
+} stackpos_t;
+
+typedef struct {
+ stackpos_t*stack;
+ int num;
+ int maxlocal;
+ int maxstack;
+ int maxscope;
+} currentstats_t;
+
+static int stack_minus(abc_code_t*c)
+{
+ opcode_t*op = opcode_get(c->opcode);
+ assert(op->stack_minus<=0);
+ int stack = op->stack_minus;
+ if(op->flags&OP_STACK_NS) {
+ multiname_t*m = (multiname_t*)c->data[0];
+ stack--; //read namespace
+ if(m->type == RTQNAMEL || m->type == RTQNAMELA) {
+ stack--; //read name
+ }
+ }
+ if(op->flags&OP_STACK_ARGS || op->flags&OP_STACK_ARGS2) {
+ assert(strchr(op->params, 'n'));
+ int nr = (ptroff_t)(op->params[0]=='n'?c->data[0]:c->data[1]);
+ stack-=nr;
+ if(op->flags&OP_STACK_ARGS2)
+ stack-=nr;
+ }
+ return stack;
+}
+static void handleregister(currentstats_t*stats, int reg)
+{
+ if(reg > stats->maxlocal)
+ stats->maxlocal = reg;
+}
+
+static void callcode(currentstats_t*stats, int pos, int stack, int scope)
+{
+ while(pos<stats->num) {
+ if(stats->stack[pos].seen) {
+ if(stats->stack[pos].stackpos != stack ||
+ stats->stack[pos].scopepos != scope) {
+ fprintf(stderr, "Stack mismatch at pos %d\n", pos);
+ }
+ return;
+ }
+ stats->stack[pos].seen = 1;
+ stats->stack[pos].stackpos = stack;
+ stats->stack[pos].scopepos = scope;
+
+ abc_code_t*c = stats->stack[pos].code;
+ opcode_t*op = opcode_get(c->opcode);
+
+ stack += stack_minus(c);
+
+ if(stack<0) {
+ fprintf(stderr, "error: stack underflow at %d (%s)\n", pos, op->name);
+ return;
+ }
+
+ stack += op->stack_plus;
+ scope += op->scope_stack_plus;
+
+ if(stack > stats->maxstack)
+ stats->maxstack = stack;
+ if(scope > stats->maxscope)
+ stats->maxscope = scope;
+
+ if(op->flags & OP_REGISTER) {
+ char*p = op->params;
+ int pos = 0;
+ char ok=0;
+ while(*p) {
+ if(*p=='r') {
+ handleregister(stats, (ptroff_t)c->data[pos]);
+ ok = 1;
+ }
+ p++;
+ }
+ if(!ok) {
+ handleregister(stats, c->opcode&3);
+ }
+ }
+ if(op->flags & (OP_THROW|OP_RETURN))
+ return;
+ if(op->flags & OP_JUMP) {
+ c = c->branch;
+ pos = c->pos;
+ continue;
+ }
+ if(op->flags & OP_BRANCH) {
+ int newpos = c->branch->pos;
+ callcode(stats, newpos, stack, scope);
+ }
+
+ pos++;
+ if(pos<stats->num) {
+ assert(c->next == stats->stack[pos].code);
+ }
+ }
+}
+
+codestats_t code_get_statistics(abc_code_t*code)
+{
+ int num = 0;
+ abc_code_t*c = code;
+ while(c) {
+ num++;
+ c = c->next;
+ }
+ currentstats_t current;
+ current.stack = rfx_calloc(sizeof(stackpos_t)*num);
+ current.maxlocal = 0;
+ current.maxstack = 0;
+ current.maxscope = 0;
+ current.num = num;
+
+ num = 0;
+ c = code;
+ while(c) {
+ //crosslink
+ current.stack[num].code = c;
+ c->pos = num;
+ num++;
+ c = c->next;
+ }
+ callcode(¤t, 0, 0, 0);
+
+ free(current.stack);
+
+ codestats_t stats;
+ stats.local_count = current.maxlocal;
+ stats.max_stack = current.maxstack;
+ stats.max_scope_depth = current.maxscope;
+ return stats;
+}