Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: src/IceAssembler.h

Issue 1311653003: Add UBSAN build option and fix undefined behaviour errors. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Fix after native_client pull Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Makefile.standalone ('k') | src/IceConverter.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- subzero/src/IceAssembler.h - Integrated assembler --------*- C++ -*-===// 1 //===- subzero/src/IceAssembler.h - Integrated assembler --------*- C++ -*-===//
2 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 2 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
3 // for details. All rights reserved. Use of this source code is governed by a 3 // for details. All rights reserved. Use of this source code is governed by a
4 // BSD-style license that can be found in the LICENSE file. 4 // BSD-style license that can be found in the LICENSE file.
5 // 5 //
6 // Modified by the Subzero authors. 6 // Modified by the Subzero authors.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // The Subzero Code Generator 10 // The Subzero Code Generator
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 90
91 /// Assembler buffers are used to emit binary code. They grow on demand. 91 /// Assembler buffers are used to emit binary code. They grow on demand.
92 class AssemblerBuffer { 92 class AssemblerBuffer {
93 AssemblerBuffer(const AssemblerBuffer &) = delete; 93 AssemblerBuffer(const AssemblerBuffer &) = delete;
94 AssemblerBuffer &operator=(const AssemblerBuffer &) = delete; 94 AssemblerBuffer &operator=(const AssemblerBuffer &) = delete;
95 95
96 public: 96 public:
97 AssemblerBuffer(Assembler &); 97 AssemblerBuffer(Assembler &);
98 ~AssemblerBuffer(); 98 ~AssemblerBuffer();
99 99
100 /// Basic support for emitting, loading, and storing. 100 /// \name Basic support for emitting, loading, and storing.
101 /// @{
102 // These use memcpy instead of assignment to avoid undefined behaviour of
103 // assigning to unaligned addresses. Since the size of the copy is known the
104 // compiler can inline the memcpy with simple moves.
101 template <typename T> void emit(T Value) { 105 template <typename T> void emit(T Value) {
102 assert(hasEnsuredCapacity()); 106 assert(hasEnsuredCapacity());
103 *reinterpret_cast<T *>(Cursor) = Value; 107 memcpy(reinterpret_cast<void *>(Cursor), &Value, sizeof(T));
104 Cursor += sizeof(T); 108 Cursor += sizeof(T);
105 } 109 }
106 110
107 template <typename T> T load(intptr_t Position) const { 111 template <typename T> T load(intptr_t Position) const {
108 assert(Position >= 0 && 112 assert(Position >= 0 &&
109 Position <= (size() - static_cast<intptr_t>(sizeof(T)))); 113 Position <= (size() - static_cast<intptr_t>(sizeof(T))));
110 return *reinterpret_cast<T *>(Contents + Position); 114 T Value;
115 memcpy(&Value, reinterpret_cast<void *>(Contents + Position), sizeof(T));
116 return Value;
111 } 117 }
112 118
113 template <typename T> void store(intptr_t Position, T Value) { 119 template <typename T> void store(intptr_t Position, T Value) {
114 assert(Position >= 0 && 120 assert(Position >= 0 &&
115 Position <= (size() - static_cast<intptr_t>(sizeof(T)))); 121 Position <= (size() - static_cast<intptr_t>(sizeof(T))));
116 *reinterpret_cast<T *>(Contents + Position) = Value; 122 memcpy(reinterpret_cast<void *>(Contents + Position), &Value, sizeof(T));
117 } 123 }
124 /// @{
118 125
119 /// Emit a fixup at the current location. 126 /// Emit a fixup at the current location.
120 void emitFixup(AssemblerFixup *Fixup) { Fixup->set_position(size()); } 127 void emitFixup(AssemblerFixup *Fixup) { Fixup->set_position(size()); }
121 128
122 /// Get the size of the emitted code. 129 /// Get the size of the emitted code.
123 intptr_t size() const { return Cursor - Contents; } 130 intptr_t size() const { return Cursor - Contents; }
124 uintptr_t contents() const { return Contents; } 131 uintptr_t contents() const { return Contents; }
125 132
126 /// To emit an instruction to the assembler buffer, the EnsureCapacity helper 133 /// To emit an instruction to the assembler buffer, the EnsureCapacity helper
127 /// must be used to guarantee that the underlying data area is big enough to 134 /// must be used to guarantee that the underlying data area is big enough to
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 protected: 307 protected:
301 // Buffer's constructor uses the Allocator, so it needs to appear after it. 308 // Buffer's constructor uses the Allocator, so it needs to appear after it.
302 // TODO(jpp): dependencies on construction order are a nice way of shooting 309 // TODO(jpp): dependencies on construction order are a nice way of shooting
303 // yourself in the foot. Fix this. 310 // yourself in the foot. Fix this.
304 AssemblerBuffer Buffer; 311 AssemblerBuffer Buffer;
305 }; 312 };
306 313
307 } // end of namespace Ice 314 } // end of namespace Ice
308 315
309 #endif // SUBZERO_SRC_ICEASSEMBLER_H_ 316 #endif // SUBZERO_SRC_ICEASSEMBLER_H_
OLDNEW
« no previous file with comments | « Makefile.standalone ('k') | src/IceConverter.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698