src/dataBuffer.h

changeset 134
eca2fc0acaa2
parent 133
dbbdb870c835
child 135
8b9132fea327
equal deleted inserted replaced
133:dbbdb870c835 134:eca2fc0acaa2
32 #include <stdio.h> 32 #include <stdio.h>
33 #include <string.h> 33 #include <string.h>
34 #include "main.h" 34 #include "main.h"
35 #include "stringTable.h" 35 #include "stringTable.h"
36 36
37 /** 37 // ------------------------------------------------------------------------------------------------
38 * @class DataBuffer 38 //
39 * @brief Stores a buffer of bytecode 39 // The DataBuffer class stores a section of bytecode. Buffers are allocated on
40 * 40 // the heap and written to using the @c write* functions. Buffers can be cut and
41 * The DataBuffer class stores a section of bytecode. Buffers are allocated on 41 // pasted together with @c mergeAndDestroy, note that this function destroys the
42 * the heap and written to using the @c write* functions. Buffers can be cut and 42 // parameter buffer in the process
43 * pasted together with @c mergeAndDestroy, note that this function destroys the 43 //
44 * parameter buffer in the process 44 // A mark is a "pointer" to a particular position in the bytecode. The actual
45 * 45 // permanent position cannot be predicted in any way or form, thus these things
46 * A mark is a "pointer" to a particular position in the bytecode. The actual 46 // are used to "bookmark" a position like that for future use.
47 * permanent position cannot be predicted in any way or form, thus these things 47 //
48 * are used to "bookmark" a position like that for future use. 48 // A reference acts as a pointer to a mark. The reference is four bytes in the
49 * 49 // bytecode which will be replaced with its mark's position when the bytecode
50 * A reference acts as a pointer to a mark. The reference is four bytes in the 50 // is written to the output file.
51 * bytecode which will be replaced with its mark's position when the bytecode 51 //
52 * is written to the output file. 52 // This mark/reference system is used to know bytecode offset values when
53 * 53 // compiling, even though actual final positions cannot be known.
54 * This mark/reference system is used to know bytecode offset values when 54 //
55 * compiling, even though actual final positions cannot be known.
56 */
57 class DataBuffer 55 class DataBuffer
58 { 56 {
59 //! @
60 PROPERTY (private, char*, buffer, setBuffer, STOCK_WRITE) 57 PROPERTY (private, char*, buffer, setBuffer, STOCK_WRITE)
61 PROPERTY (private, int, allocatedSize, setAllocatedSize, STOCK_WRITE) 58 PROPERTY (private, int, allocatedSize, setAllocatedSize, STOCK_WRITE)
62 PROPERTY (private, char*, position, setPosition, STOCK_WRITE) 59 PROPERTY (private, char*, position, setPosition, STOCK_WRITE)
63 PROPERTY (private, List<ByteMark*>, marks, setMarks, STOCK_WRITE) 60 PROPERTY (private, List<ByteMark*>, marks, setMarks, STOCK_WRITE)
64 PROPERTY (private, List<MarkReference*>, references, setReferences, STOCK_WRITE) 61 PROPERTY (private, List<MarkReference*>, references, setReferences, STOCK_WRITE)
65 62
66 public: 63 public:
67 //! Constructs a new databuffer with @c size bytes. 64 DataBuffer (int size = 128);
68 DataBuffer (int size = 128); 65 ~DataBuffer();
69 66
70 //! Destructs the databuffer. 67 ByteMark* addMark (const String& name);
71 ~DataBuffer(); 68 MarkReference* addReference (ByteMark* mark);
69 void adjustMark (ByteMark* mark);
70 void checkSpace (int bytes);
71 DataBuffer* clone();
72 void dump();
73 ByteMark* findMarkByName (const String& name);
74 void mergeAndDestroy (DataBuffer* other);
75 void offsetMark (ByteMark* mark, int position);
76 void transferMarksTo (DataBuffer* other);
77 void writeStringIndex (const String& a);
78 void writeString (const String& a);
79 void writeByte (int8_t data);
80 void writeWord (int16_t data);
81 void writeDWord (int32_t data);
82 inline int writtenSize() const;
72 83
73 //! Adds a new mark to the current position with the name @c name. 84 private:
74 //! @param name the name of the new mark 85 void copyBuffer (const DataBuffer* buf);
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 }; 86 };
156 87
88 // ------------------------------------------------------------------------------------------------
89 //
90 // Returns the amount of bytes written into the buffer.
91 //
92 inline int DataBuffer::writtenSize() const
93 {
94 return position() - buffer();
95 }
96
157 #endif // BOTC_DATABUFFER_H 97 #endif // BOTC_DATABUFFER_H

mercurial