Tue, 22 Jul 2014 12:57:46 +0300
- even more rework
119
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | /* |
133
dbbdb870c835
- changed all source files to use my correct legal name instead of my calling name
Teemu Piippo <crimsondusk64@gmail.com>
parents:
119
diff
changeset
|
2 | Copyright 2012-2014 Teemu Piippo |
119
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
3 | All rights reserved. |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | Redistribution and use in source and binary forms, with or without |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | modification, are permitted provided that the following conditions |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | are met: |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | 1. Redistributions of source code must retain the above copyright |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | notice, this list of conditions and the following disclaimer. |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | 2. Redistributions in binary form must reproduce the above copyright |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | notice, this list of conditions and the following disclaimer in the |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | documentation and/or other materials provided with the distribution. |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | 3. The name of the author may not be used to endorse or promote products |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | derived from this software without specific prior written permission. |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
26 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
27 | */ |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | #ifndef BOTC_DATABUFFER_H |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | #define BOTC_DATABUFFER_H |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | #include "main.h" |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | #include "stringTable.h" |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | |
138 | 35 | // _________________________________________________________________________________________________ |
134
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
36 | // |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
37 | // The DataBuffer class stores a section of bytecode. Buffers are allocated on |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
38 | // the heap and written to using the @c write* functions. Buffers can be cut and |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
39 | // pasted together with @c mergeAndDestroy, note that this function destroys the |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
40 | // parameter buffer in the process |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
41 | // |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
42 | // A mark is a "pointer" to a particular position in the bytecode. The actual |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
43 | // permanent position cannot be predicted in any way or form, thus these things |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
44 | // are used to "bookmark" a position like that for future use. |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
45 | // |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
46 | // A reference acts as a pointer to a mark. The reference is four bytes in the |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
47 | // bytecode which will be replaced with its mark's position when the bytecode |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
48 | // is written to the output file. |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
49 | // |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
50 | // This mark/reference system is used to know bytecode offset values when |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
51 | // compiling, even though actual final positions cannot be known. |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
52 | // |
119
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | class DataBuffer |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | { |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | PROPERTY (private, char*, buffer, setBuffer, STOCK_WRITE) |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
56 | PROPERTY (private, int, allocatedSize, setAllocatedSize, STOCK_WRITE) |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | PROPERTY (private, char*, position, setPosition, STOCK_WRITE) |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | PROPERTY (private, List<ByteMark*>, marks, setMarks, STOCK_WRITE) |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | PROPERTY (private, List<MarkReference*>, references, setReferences, STOCK_WRITE) |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
60 | |
134
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
61 | public: |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
62 | DataBuffer (int size = 128); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
63 | ~DataBuffer(); |
119
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
64 | |
134
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
65 | ByteMark* addMark (const String& name); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
66 | MarkReference* addReference (ByteMark* mark); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
67 | void adjustMark (ByteMark* mark); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
68 | void checkSpace (int bytes); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
69 | DataBuffer* clone(); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
70 | void dump(); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
71 | ByteMark* findMarkByName (const String& name); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
72 | void mergeAndDestroy (DataBuffer* other); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
73 | void offsetMark (ByteMark* mark, int position); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
74 | void transferMarksTo (DataBuffer* other); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
75 | void writeStringIndex (const String& a); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
76 | void writeString (const String& a); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
77 | void writeByte (int8_t data); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
78 | void writeWord (int16_t data); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
79 | void writeDWord (int32_t data); |
135
8b9132fea327
- massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
134
diff
changeset
|
80 | void writeHeader (DataHeader data); |
134
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
81 | inline int writtenSize() const; |
119
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | |
134
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
83 | private: |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
84 | void copyBuffer (const DataBuffer* buf); |
119
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
85 | }; |
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
86 | |
138 | 87 | // _________________________________________________________________________________________________ |
134
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
88 | // |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
89 | // Returns the amount of bytes written into the buffer. |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
90 | // |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
91 | inline int DataBuffer::writtenSize() const |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
92 | { |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
93 | return position() - buffer(); |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
94 | } |
eca2fc0acaa2
- massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents:
133
diff
changeset
|
95 | |
119
bdf8d46c145f
- renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | #endif // BOTC_DATABUFFER_H |