1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "jit/x86-shared/AssemblerBuffer-x86-shared.h"
8 
9 #include "mozilla/Sprintf.h"
10 
11 using namespace js;
12 using namespace jit;
13 
swap(Vector<uint8_t,0,SystemAllocPolicy> & bytes)14 bool AssemblerBuffer::swap(Vector<uint8_t, 0, SystemAllocPolicy>& bytes) {
15   // For now, specialize to the one use case.
16   MOZ_ASSERT(bytes.empty());
17 
18   if (m_buffer.empty()) {
19     if (bytes.capacity() > m_buffer.capacity()) {
20       size_t newCapacity = bytes.capacity();
21       uint8_t* newBuffer = bytes.extractRawBuffer();
22       MOZ_ASSERT(newBuffer);
23       m_buffer.replaceRawBuffer((unsigned char*)newBuffer, 0, newCapacity);
24     }
25     return true;
26   }
27 
28   size_t newLength = m_buffer.length();
29   size_t newCapacity = m_buffer.capacity();
30   unsigned char* newBuffer = m_buffer.extractRawBuffer();
31 
32   // NB: extractRawBuffer() only returns null if the Vector is using
33   // inline storage and thus a malloc would be needed. In this case,
34   // just make a simple copy.
35   if (!newBuffer) {
36     return bytes.append(m_buffer.begin(), m_buffer.end());
37   }
38 
39   bytes.replaceRawBuffer((uint8_t*)newBuffer, newLength, newCapacity);
40   return true;
41 }
42 
43 #ifdef JS_JITSPEW
spew(const char * fmt,va_list va)44 void js::jit::GenericAssembler::spew(const char* fmt, va_list va) {
45   // Buffer to hold the formatted string. Note that this may contain
46   // '%' characters, so do not pass it directly to printf functions.
47   char buf[200];
48 
49   int i = VsprintfLiteral(buf, fmt, va);
50   if (i > -1) {
51     if (printer) {
52       printer->printf("%s\n", buf);
53     }
54     js::jit::JitSpew(js::jit::JitSpew_Codegen, "%s", buf);
55   }
56 }
57 #endif
58