1 /* |
|
2 Copyright 2012-2014 Santeri Piippo |
|
3 All rights reserved. |
|
4 |
|
5 Redistribution and use in source and binary forms, with or without |
|
6 modification, are permitted provided that the following conditions |
|
7 are met: |
|
8 |
|
9 1. Redistributions of source code must retain the above copyright |
|
10 notice, this list of conditions and the following disclaimer. |
|
11 2. Redistributions in binary form must reproduce the above copyright |
|
12 notice, this list of conditions and the following disclaimer in the |
|
13 documentation and/or other materials provided with the distribution. |
|
14 3. The name of the author may not be used to endorse or promote products |
|
15 derived from this software without specific prior written permission. |
|
16 |
|
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #ifndef BOTC_DATABUFFER_H |
|
30 #define BOTC_DATABUFFER_H |
|
31 |
|
32 #include <stdio.h> |
|
33 #include <string.h> |
|
34 #include "Main.h" |
|
35 #include "StringTable.h" |
|
36 |
|
37 /** |
|
38 * @class DataBuffer |
|
39 * @brief Stores a buffer of bytecode |
|
40 * |
|
41 * The DataBuffer class stores a section of bytecode. Buffers are allocated on |
|
42 * the heap and written to using the @c write* functions. Buffers can be cut and |
|
43 * pasted together with @c mergeAndDestroy, note that this function destroys the |
|
44 * parameter buffer in the process |
|
45 * |
|
46 * A mark is a "pointer" to a particular position in the bytecode. The actual |
|
47 * permanent position cannot be predicted in any way or form, thus these things |
|
48 * are used to "bookmark" a position like that for future use. |
|
49 * |
|
50 * A reference acts as a pointer to a mark. The reference is four bytes in the |
|
51 * bytecode which will be replaced with its mark's position when the bytecode |
|
52 * is written to the output file. |
|
53 * |
|
54 * This mark/reference system is used to know bytecode offset values when |
|
55 * compiling, even though actual final positions cannot be known. |
|
56 */ |
|
57 class DataBuffer |
|
58 { |
|
59 //! @ |
|
60 PROPERTY (private, char*, buffer, setBuffer, STOCK_WRITE) |
|
61 PROPERTY (private, int, allocatedSize, setAllocatedSize, STOCK_WRITE) |
|
62 PROPERTY (private, char*, position, setPosition, STOCK_WRITE) |
|
63 PROPERTY (private, List<ByteMark*>, marks, setMarks, STOCK_WRITE) |
|
64 PROPERTY (private, List<MarkReference*>, references, setReferences, STOCK_WRITE) |
|
65 |
|
66 public: |
|
67 //! Constructs a new databuffer with @c size bytes. |
|
68 DataBuffer (int size = 128); |
|
69 |
|
70 //! Destructs the databuffer. |
|
71 ~DataBuffer(); |
|
72 |
|
73 //! Adds a new mark to the current position with the name @c name. |
|
74 //! @param name the name of the new mark |
|
75 //! @return a pointer to the new mark |
|
76 ByteMark* addMark (const String& name); |
|
77 |
|
78 //! Adds a new reference to @c mark at the current position. This |
|
79 //! function will write 4 bytes to the buffer whose value will |
|
80 //! be determined at final output writing. |
|
81 //! @param mark the mark which the new reference will attach to |
|
82 //! @return a pointer to the new reference |
|
83 MarkReference* addReference (ByteMark* mark); |
|
84 |
|
85 //! Moves @c mark to the current bytecode position. |
|
86 //! @param mark the mark to adjust |
|
87 void adjustMark (ByteMark* mark); |
|
88 |
|
89 //! Ensures there's at least @c bytes left in the buffer. Will resize |
|
90 //! if necessary, no-op if not. On resize, 512 extra bytes are allocated |
|
91 //! to reduce the amount of resizes. |
|
92 //! @param bytes the amount of space in bytes to ensure allocated |
|
93 void checkSpace (int bytes); |
|
94 |
|
95 //! Creates a clone of this data buffer. |
|
96 //! @note All marks will be moved into the new databuffer as marks are |
|
97 //! @note never duplicated. |
|
98 //! @return The newly cloned databuffer. |
|
99 DataBuffer* clone(); |
|
100 |
|
101 //! Prints the buffer to stdout. Useful for debugging. |
|
102 void dump(); |
|
103 |
|
104 //! Finds the given mark by name. |
|
105 //! @param name the name of the mark to find |
|
106 ByteMark* findMarkByName (const String& name); |
|
107 |
|
108 //! Merge another data buffer into this one. |
|
109 //! Note: @c other is destroyed in the process. |
|
110 //! @param other the buffer to merge in |
|
111 void mergeAndDestroy (DataBuffer* other); |
|
112 |
|
113 //! Moves @c mark to the given bytecode position. |
|
114 //! @param mark the mark to adjust |
|
115 //! @param position where to adjust the mark |
|
116 void offsetMark (ByteMark* mark, int position); |
|
117 |
|
118 //! Transfers all marks of this buffer to @c other. |
|
119 //! @param other the data buffer to transfer marks to |
|
120 void transferMarksTo (DataBuffer* other); |
|
121 |
|
122 //! Writes the index of the given string to the databuffer. |
|
123 //! 4 bytes will be written to the bytecode. |
|
124 //! @param a the string whose index to write |
|
125 void writeStringIndex (const String& a); |
|
126 |
|
127 //! Writes the given string as-is into the databuffer. |
|
128 //! @c a.length + 4 bytes will be written to the buffer. |
|
129 //! @param a the string to write |
|
130 void writeString (const String& a); |
|
131 |
|
132 //! Writes the given byte to the buffer. 1 byte will be written. |
|
133 //! @c data the byte to write |
|
134 void writeByte (int8_t data); |
|
135 |
|
136 //! Writes the given word to the buffer. 2 byte will be written. |
|
137 //! @c data the word to write |
|
138 void writeWord (int16_t data); |
|
139 |
|
140 //! Writes the given double word to the buffer. 4 bytes will be written. |
|
141 //! @c data the double word to write |
|
142 void writeDWord (int32_t data); |
|
143 |
|
144 //! @return the amount of bytes written to this buffer. |
|
145 inline int writtenSize() const |
|
146 { |
|
147 return position() - buffer(); |
|
148 } |
|
149 |
|
150 protected: |
|
151 //! Writes the buffer's contents from @c buf. |
|
152 //! @c buf.writtenSize() bytes will be written. |
|
153 //! @param buf the buffer to copy |
|
154 void copyBuffer (const DataBuffer* buf); |
|
155 }; |
|
156 |
|
157 #endif // BOTC_DATABUFFER_H |
|