Skip to content

Commit

Permalink
Merge branch 'extern' into 'master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Nov 22, 2019
2 parents f03e86f + f174603 commit b247c65
Show file tree
Hide file tree
Showing 80 changed files with 2,915 additions and 185 deletions.
1 change: 1 addition & 0 deletions compiler/core/antlr/ZserioLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ CONST : 'const' ;
DEFAULT : 'default' ;
ENUM : 'enum' ;
EXPLICIT : 'explicit' ;
EXTERN : 'extern' ;
FLOAT16 : 'float16' ;
FLOAT32 : 'float32' ;
FLOAT64 : 'float64' ;
Expand Down
7 changes: 6 additions & 1 deletion compiler/core/antlr/ZserioParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ choiceCases
;

choiceCase
: CASE expression COLON
: CASE expression COLON
;

choiceDefault
Expand Down Expand Up @@ -345,6 +345,7 @@ builtinType
| boolType
| stringType
| floatType
| externType
;

qualifiedName
Expand Down Expand Up @@ -408,3 +409,7 @@ floatType
| FLOAT32
| FLOAT64
;

externType
: EXTERN
;
26 changes: 26 additions & 0 deletions compiler/core/src/zserio/ast/ExternType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package zserio.ast;

/**
* AST node for Extern types.
*
* Extern types (Zserio keyword 'extern') are Zserio types as well.
*/
public class ExternType extends BuiltInType
{
/**
* Constructor from AST node location and the name.
*
* @param location AST node location.
* @param name Name of the AST node taken from grammar.
*/
public ExternType(AstLocation location, String name)
{
super(location, name);
}

@Override
public void accept(ZserioAstVisitor visitor)
{
visitor.visitExternType(this);
}
}
9 changes: 9 additions & 0 deletions compiler/core/src/zserio/ast/ZserioAstBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,15 @@ public FloatType visitFloatType(ZserioParser.FloatTypeContext ctx)
return new FloatType(location, token.getText(), token.getType());
}

@Override
public ExternType visitExternType(ZserioParser.ExternTypeContext ctx)
{
final Token token = ctx.getStart();
final AstLocation location = new AstLocation(token);

return new ExternType(location, token.getText());
}

private PackageName createPackageName(List<ZserioParser.IdContext> ids)
{
final PackageName.Builder packageNameBuilder = new PackageName.Builder();
Expand Down
4 changes: 4 additions & 0 deletions compiler/core/src/zserio/ast/ZserioAstDefaultVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ public void visitStringType(StringType stringType)
public void visitFloatType(FloatType floatType)
{}

@Override
public void visitExternType(ExternType externType)
{}

@Override
public void visitTemplateParameter(TemplateParameter templateParameter)
{}
Expand Down
7 changes: 7 additions & 0 deletions compiler/core/src/zserio/ast/ZserioAstVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ public interface ZserioAstVisitor
*/
void visitFloatType(FloatType floatType);

/**
* Visits reference to built-in extern type.
*
* @param externType Extern type AST node.
*/
void visitExternType(ExternType externType);

/**
* Visits template parameter.
*
Expand Down
6 changes: 6 additions & 0 deletions compiler/core/src/zserio/ast/ZserioAstWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ public void visitFloatType(FloatType floatType)
floatType.visitChildren(this);
}

@Override
public void visitExternType(ExternType externType)
{
externType.visitChildren(this);
}

@Override
public void visitTemplateParameter(TemplateParameter templateParameter)
{
Expand Down
2 changes: 2 additions & 0 deletions compiler/extensions/cpp/runtime/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ project(ZserioCppRuntime)

set(ZSERIO_CPP_RUNTIME_LIB_SRCS
zserio/Arrays.h
zserio/BitBuffer.h
zserio/BitBuffer.cpp
zserio/BitFieldUtil.cpp
zserio/BitFieldUtil.h
zserio/BitPositionUtil.cpp
Expand Down
59 changes: 59 additions & 0 deletions compiler/extensions/cpp/runtime/src/zserio/Arrays.h
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,65 @@ struct StringArrayTraits
static const bool IS_BITSIZEOF_CONSTANT = false;
};

/**
* Array traits for Zserio extern bit buffer type.
*/
struct BitBufferArrayTraits
{
/** Type of the single array element. */
typedef zserio::BitBuffer type;

/**
* Calculates bit size of the array element.
*
* \param value Element's value.
*
* \return Bit size of the array element.
*/
static size_t bitSizeOf(size_t, const type& value)
{
return zserio::bitSizeOfBitBuffer(value);
}

/**
* Initializes indexed offsets of the single array element.
*
* \param bitPosition Current bit position.
* \param value Element's value.
*
* \return Updated bit position which points to the first bit after the array element.
*/
static size_t initializeOffsets(size_t bitPosition, const type& value)
{
return bitPosition + bitSizeOf(bitPosition, value);
}

/**
* Reads the single array element.
*
* \param array Array to read the element to.
* \param in Bit stream reader.
*/
static void read(std::vector<type>& array, BitStreamReader& in, size_t)
{
array.push_back(in.readBitBuffer());
}

/**
* Writes the single array element.
*
* \param out Bit stream writer.
* \param value Element's value to write.
*/
static void write(BitStreamWriter& out, type value)
{
out.writeBitBuffer(value);
}

/** Determines whether the bit size of the single element is constant. */
static const bool IS_BITSIZEOF_CONSTANT = false;
};

/**
* Array traits for Zserio enumeration type.
*/
Expand Down
126 changes: 126 additions & 0 deletions compiler/extensions/cpp/runtime/src/zserio/BitBuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#include <utility>
#include <cstring>

#include "zserio/CppRuntimeException.h"
#include "zserio/StringConvertUtil.h"
#include "zserio/HashCodeUtil.h"
#include "zserio/BitBuffer.h"

namespace zserio
{

BitBuffer::BitBuffer() : m_bitSize(0)
{
}

BitBuffer::BitBuffer(size_t bitSize) : m_buffer((bitSize + 7) / 8), m_bitSize(bitSize)
{
}

BitBuffer::BitBuffer(const std::vector<uint8_t>& buffer) :
m_buffer(buffer), m_bitSize(8 * buffer.size())
{
}

BitBuffer::BitBuffer(const std::vector<uint8_t>& buffer, size_t bitSize) :
m_bitSize(bitSize)
{
const size_t byteSize = (bitSize + 7) / 8;
if (buffer.size() < byteSize)
throw CppRuntimeException("BitBuffer: Bit size " + convertToString(bitSize) +
" out of range for given vector byte size " + convertToString(buffer.size()) + "!");

m_buffer.assign(buffer.data(), buffer.data() + byteSize);
}

BitBuffer::BitBuffer(std::vector<uint8_t>&& buffer) :
m_buffer(std::move(buffer)), m_bitSize(8 * m_buffer.size())
{
}

BitBuffer::BitBuffer(std::vector<uint8_t>&& buffer, size_t bitSize) :
m_buffer(std::move(buffer)), m_bitSize(bitSize)
{
const size_t byteSize = (bitSize + 7) / 8;
if (m_buffer.size() < byteSize)
throw CppRuntimeException("BitBuffer: Bit size " + convertToString(bitSize) +
" out of range for given vector byte size " + convertToString(buffer.size()) + "!");
}

BitBuffer::BitBuffer(const uint8_t* buffer, size_t bitSize) :
m_buffer(buffer, buffer + (bitSize + 7) / 8), m_bitSize(bitSize)
{
}

bool BitBuffer::operator==(const BitBuffer& other) const
{
if (this != &other)
{
if (m_bitSize != other.m_bitSize)
return false;

const size_t byteSize = getByteSize();
if (byteSize > 0)
{
if (byteSize > 1)
{
if (memcmp(getBuffer(), other.getBuffer(), byteSize - 1) != 0)
return false;
}

if (getMaskedLastByte() != other.getMaskedLastByte())
return false;
}
}

return true;
}

int BitBuffer::hashCode() const
{
int result = ::zserio::HASH_SEED;
const size_t byteSize = getByteSize();
if (byteSize > 0)
{
if (byteSize > 1)
{
const uint8_t* lastElement = getBuffer() + byteSize - 1;
for (const uint8_t* p = getBuffer(); p < lastElement; p++)
result = calcHashCode(result, *p);
}
result = ::zserio::calcHashCode(result, getMaskedLastByte());
}

return result;
}

const uint8_t* BitBuffer::getBuffer() const
{
return m_buffer.data();
}

uint8_t* BitBuffer::getBuffer()
{
return m_buffer.data();
}

size_t BitBuffer::getBitSize() const
{
return m_bitSize;
}

size_t BitBuffer::getByteSize() const
{
return (m_bitSize + 7) / 8;
}

uint8_t BitBuffer::getMaskedLastByte() const
{
const size_t roundedByteSize = m_bitSize / 8;
const uint8_t lastByteBits = static_cast<uint8_t>(m_bitSize - 8 * roundedByteSize);

return (lastByteBits == 0) ? m_buffer[roundedByteSize - 1] :
(m_buffer[roundedByteSize] & (0xFF >> (8 - lastByteBits)));
}

} // namespace zserio
Loading

0 comments on commit b247c65

Please sign in to comment.