added numframes parameter
[swftools.git] / lib / action / assembler.c
1 /*
2     Ming, an SWF output library
3     Copyright (C) 2002  Opaque Industries - http://www.opaque.net/
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "assembler.h"
24 #include "compile.h"
25 #include "action.h"
26
27
28 int len;
29 Buffer asmBuffer;
30 int nLabels;
31
32 struct label
33 {
34         char *name;
35         int offset;
36 };
37
38 struct label labels[256];
39
40
41 static int
42 findLabel(char *label)
43 {
44         int i;
45
46         for ( i=0; i<nLabels; ++i )
47         {
48                 if ( strcmp(label, labels[i].name) == 0 )
49                         return i;
50         }
51
52         return -1;
53 }
54
55
56 static void
57 addLabel(char *label)
58 {
59         int i = findLabel(label);
60
61         if ( i == -1 )
62         {
63                 labels[nLabels].name = strdup(label);
64                 labels[nLabels].offset = len;
65                 ++nLabels;
66         }
67         else
68                 labels[i].offset = len;
69 }
70
71
72 int
73 bufferBranchTarget(Buffer output, char *label)
74 {
75         int i = findLabel(label);
76
77         if ( i == -1 )
78         {
79                 i = nLabels;
80                 addLabel(label);
81         }
82
83         return bufferWriteS16(output, i);
84 }
85
86
87 void
88 bufferPatchTargets(Buffer buffer)
89 {
90         int l, i = 0;
91         unsigned char *output = buffer->buffer;
92
93         while ( i < len )
94         {
95                 if ( output[i] & 0x80 ) /* then it's a multibyte instruction */
96                 {
97                         if ( output[i] == SWFACTION_BRANCHALWAYS ||
98                                          output[i] == SWFACTION_BRANCHIFTRUE )
99                         {
100                                 int target, offset;
101
102                                 i += 3; /* plus instruction plus two-byte length */
103
104                                 target = output[i];
105                                 offset = labels[target].offset - (i+2);
106                                 output[i] = offset & 0xff;
107                                 output[++i] = (offset>>8) & 0xff;
108                                 ++i;
109                         }
110                         else
111                         {
112                                 ++i;
113                                 l = output[i];
114                                 ++i;
115                                 l += output[i]<<8;
116
117                                 i += l+1;
118                         }
119                 }
120                 else
121                         ++i;
122         }
123 }
124
125
126 /*
127  * Local variables:
128  * tab-width: 2
129  * c-basic-offset: 2
130  * End:
131  */