--- a/src/dataBuffer.h Sat Jul 12 23:04:46 2014 +0300 +++ b/src/dataBuffer.h Sun Jul 20 17:25:36 2014 +0300 @@ -34,124 +34,64 @@ #include "main.h" #include "stringTable.h" -/** - * @class DataBuffer - * @brief Stores a buffer of bytecode - * - * The DataBuffer class stores a section of bytecode. Buffers are allocated on - * the heap and written to using the @c write* functions. Buffers can be cut and - * pasted together with @c mergeAndDestroy, note that this function destroys the - * parameter buffer in the process - * - * A mark is a "pointer" to a particular position in the bytecode. The actual - * permanent position cannot be predicted in any way or form, thus these things - * are used to "bookmark" a position like that for future use. - * - * A reference acts as a pointer to a mark. The reference is four bytes in the - * bytecode which will be replaced with its mark's position when the bytecode - * is written to the output file. - * - * This mark/reference system is used to know bytecode offset values when - * compiling, even though actual final positions cannot be known. - */ +// ------------------------------------------------------------------------------------------------ +// +// The DataBuffer class stores a section of bytecode. Buffers are allocated on +// the heap and written to using the @c write* functions. Buffers can be cut and +// pasted together with @c mergeAndDestroy, note that this function destroys the +// parameter buffer in the process +// +// A mark is a "pointer" to a particular position in the bytecode. The actual +// permanent position cannot be predicted in any way or form, thus these things +// are used to "bookmark" a position like that for future use. +// +// A reference acts as a pointer to a mark. The reference is four bytes in the +// bytecode which will be replaced with its mark's position when the bytecode +// is written to the output file. +// +// This mark/reference system is used to know bytecode offset values when +// compiling, even though actual final positions cannot be known. +// class DataBuffer { - //! @ PROPERTY (private, char*, buffer, setBuffer, STOCK_WRITE) PROPERTY (private, int, allocatedSize, setAllocatedSize, STOCK_WRITE) PROPERTY (private, char*, position, setPosition, STOCK_WRITE) PROPERTY (private, List<ByteMark*>, marks, setMarks, STOCK_WRITE) PROPERTY (private, List<MarkReference*>, references, setReferences, STOCK_WRITE) - public: - //! Constructs a new databuffer with @c size bytes. - DataBuffer (int size = 128); - - //! Destructs the databuffer. - ~DataBuffer(); - - //! Adds a new mark to the current position with the name @c name. - //! @param name the name of the new mark - //! @return a pointer to the new mark - ByteMark* addMark (const String& name); - - //! Adds a new reference to @c mark at the current position. This - //! function will write 4 bytes to the buffer whose value will - //! be determined at final output writing. - //! @param mark the mark which the new reference will attach to - //! @return a pointer to the new reference - MarkReference* addReference (ByteMark* mark); - - //! Moves @c mark to the current bytecode position. - //! @param mark the mark to adjust - void adjustMark (ByteMark* mark); - - //! Ensures there's at least @c bytes left in the buffer. Will resize - //! if necessary, no-op if not. On resize, 512 extra bytes are allocated - //! to reduce the amount of resizes. - //! @param bytes the amount of space in bytes to ensure allocated - void checkSpace (int bytes); - - //! Creates a clone of this data buffer. - //! @note All marks will be moved into the new databuffer as marks are - //! @note never duplicated. - //! @return The newly cloned databuffer. - DataBuffer* clone(); - - //! Prints the buffer to stdout. Useful for debugging. - void dump(); - - //! Finds the given mark by name. - //! @param name the name of the mark to find - ByteMark* findMarkByName (const String& name); +public: + DataBuffer (int size = 128); + ~DataBuffer(); - //! Merge another data buffer into this one. - //! Note: @c other is destroyed in the process. - //! @param other the buffer to merge in - void mergeAndDestroy (DataBuffer* other); - - //! Moves @c mark to the given bytecode position. - //! @param mark the mark to adjust - //! @param position where to adjust the mark - void offsetMark (ByteMark* mark, int position); - - //! Transfers all marks of this buffer to @c other. - //! @param other the data buffer to transfer marks to - void transferMarksTo (DataBuffer* other); - - //! Writes the index of the given string to the databuffer. - //! 4 bytes will be written to the bytecode. - //! @param a the string whose index to write - void writeStringIndex (const String& a); - - //! Writes the given string as-is into the databuffer. - //! @c a.length + 4 bytes will be written to the buffer. - //! @param a the string to write - void writeString (const String& a); + ByteMark* addMark (const String& name); + MarkReference* addReference (ByteMark* mark); + void adjustMark (ByteMark* mark); + void checkSpace (int bytes); + DataBuffer* clone(); + void dump(); + ByteMark* findMarkByName (const String& name); + void mergeAndDestroy (DataBuffer* other); + void offsetMark (ByteMark* mark, int position); + void transferMarksTo (DataBuffer* other); + void writeStringIndex (const String& a); + void writeString (const String& a); + void writeByte (int8_t data); + void writeWord (int16_t data); + void writeDWord (int32_t data); + inline int writtenSize() const; - //! Writes the given byte to the buffer. 1 byte will be written. - //! @c data the byte to write - void writeByte (int8_t data); - - //! Writes the given word to the buffer. 2 byte will be written. - //! @c data the word to write - void writeWord (int16_t data); - - //! Writes the given double word to the buffer. 4 bytes will be written. - //! @c data the double word to write - void writeDWord (int32_t data); - - //! @return the amount of bytes written to this buffer. - inline int writtenSize() const - { - return position() - buffer(); - } - - protected: - //! Writes the buffer's contents from @c buf. - //! @c buf.writtenSize() bytes will be written. - //! @param buf the buffer to copy - void copyBuffer (const DataBuffer* buf); +private: + void copyBuffer (const DataBuffer* buf); }; +// ------------------------------------------------------------------------------------------------ +// +// Returns the amount of bytes written into the buffer. +// +inline int DataBuffer::writtenSize() const +{ + return position() - buffer(); +} + #endif // BOTC_DATABUFFER_H