1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // This defines BufferSerializer that can be used with the Assembler to assemble
16 : // into a memory buffer.
17 :
18 : #ifndef SYZYGY_ASSM_BUFFER_SERIALIZER_H_
19 : #define SYZYGY_ASSM_BUFFER_SERIALIZER_H_
20 :
21 : #include "syzygy/assm/assembler.h"
22 :
23 : namespace assm {
24 :
25 : // An InstructionSerializer class that assembles instructions into a memory
26 : // buffer.
27 : // The assembler should also be created using the desired target location to
28 : // make correct references. The buffer passed in the constructor arguments is
29 : // used for bounds checking in DCHECK mode.
30 : class BufferSerializer : public AssemblerImpl::InstructionSerializer {
31 : public:
32 : // Creates a BufferSerializer object.
33 : // @param buffer A pointer to the buffer.
34 : // @param size The size of the buffer.
35 E : BufferSerializer(uint8* buffer, size_t size)
36 : : buffer_(buffer), size_(size) { }
37 :
38 : // @name Accessors.
39 : // @{
40 : uint8* buffer() const { return buffer_; }
41 : size_t size() const { return size_; }
42 : // @}
43 :
44 : // @name Implementation of the InstructionSerializer interface.
45 : // @{
46 : typedef assm::AssemblerImpl::ReferenceInfo ReferenceInfo;
47 : void AppendInstruction(uint32 location,
48 : const uint8* bytes,
49 : size_t num_bytes,
50 : const ReferenceInfo* refs,
51 E : size_t num_refs) override {
52 E : uint8* write_location = static_cast<uint8*>(0) + location;
53 E : DCHECK_GE(write_location, buffer_);
54 E : DCHECK_LE(write_location + num_bytes, buffer_ + size_);
55 :
56 E : ::memcpy(write_location, bytes, num_bytes);
57 E : }
58 : bool FinalizeLabel(uint32 location,
59 : const uint8* bytes,
60 i : size_t num_bytes) override {
61 i : return false; // No label support.
62 i : }
63 : // @}
64 :
65 : private:
66 : uint8* buffer_;
67 : size_t size_;
68 : };
69 :
70 : } // namespace assm
71 :
72 : #endif // SYZYGY_ASSM_BUFFER_SERIALIZER_H_
|