DEADSOFTWARE

Patched for Linux
[mp3cc.git] / MPC.3.5.LINUX / classgen / bytecode.c
1 /********************************************************************
3 bytecode.c - funcions for creating the bytecode
5 Niksa Orlic, 2004-06-11
7 ********************************************************************/
9 #include "bytecode.h"
10 #include "../util/error.h"
11 //#include "../util/message.h"
12 #include "../util/memory.h"
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdio.h>
17 //#include "../main/static_entry.h"
19 extern int linenum;
21 //extern FILE* yyin;
22 //extern int fileSize;
24 /*
25 Allocate a new bytecode
26 */
27 bytecode *bytecode_create()
28 {
29 bytecode *new_bytecode = (bytecode*) mem_alloc(sizeof(bytecode));
31 if (new_bytecode == NULL)
32 die(1);
34 new_bytecode->bytecode_allocated_size = 0;
35 new_bytecode->bytecode_pos = 0;
37 return new_bytecode;
38 }
40 /*
41 Destroy the bytecode
42 */
43 void bytecode_destroy(bytecode *code)
44 {
45 if (code->bytecode_allocated_size > 0)
46 mem_free(code->bytecode);
48 mem_free(code);
49 }
51 bytecode *bytecode_duplicate(bytecode *code)
52 {
53 bytecode *new_bytecode = bytecode_create();
54 bytecode_append_bytecode(new_bytecode, code);
56 return new_bytecode;
57 }
60 /*
61 Appends a single byte to the bytecode
62 */
63 void bytecode_append(bytecode *code, char c)
64 {
65 //if (c == swap$)
66 //{
67 // int a = 1;
68 //}
69 if (code->bytecode_pos == code->bytecode_allocated_size)
70 {
71 if (code->bytecode_allocated_size == 0)
72 code->bytecode = (char*) mem_alloc(128);
73 else
74 code->bytecode = (char*) mem_realloc(code->bytecode,
75 code->bytecode_allocated_size + 128);
77 if (code->bytecode == NULL)
78 die(1);
80 code->bytecode_allocated_size += 128;
81 }
83 code->bytecode[code->bytecode_pos] = c;
84 code->bytecode_pos ++;
85 }
87 /*
88 Append one bytecode to another.
89 */
90 void bytecode_append_bytecode(bytecode *dest, bytecode *src)
91 {
92 if (src->bytecode_pos == 0)
93 return;
95 if (dest->bytecode_allocated_size == 0)
96 {
97 dest->bytecode_allocated_size += src->bytecode_pos;
98 dest->bytecode = (char*) mem_alloc(dest->bytecode_allocated_size);
99 }
100 else
102 dest->bytecode_allocated_size += src->bytecode_pos;
103 dest->bytecode = (char*) mem_realloc(dest->bytecode, dest->bytecode_allocated_size);
106 if (dest->bytecode == NULL)
107 die(1);
109 memcpy(dest->bytecode + dest->bytecode_pos, src->bytecode, src->bytecode_pos);
110 dest->bytecode_pos += src->bytecode_pos;
113 /*
114 Appends a short int to the bytecode
115 */
116 void bytecode_append_short_int(bytecode *code, short int s)
118 char ch;
120 ch = (char) (s >> 8);
121 bytecode_append(code, ch);
123 ch = (char) s;
124 bytecode_append(code, ch);
127 /*
128 Appends a long int to the bytecode
129 */
130 void bytecode_append_long_int(bytecode *code, long int l)
132 char ch;
134 ch = (char) (l >> 24);
135 bytecode_append(code, ch);
137 ch = (char) (l >> 16);
138 bytecode_append(code, ch);
140 ch = (char) (l >> 8);
141 bytecode_append(code, ch);
143 ch = (char) l;
144 bytecode_append(code, ch);