Skip to content

Commit d7f3838

Browse files
committed
Add a C generator.
1 parent 9071f56 commit d7f3838

12 files changed

+4872
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,17 @@ add_custom_target(sbe-jar
129129
)
130130

131131
set(CODEC_TARGET_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated")
132+
set(C_CODEC_TARGET_DIR "${CODEC_TARGET_DIR}/c")
132133
set(CXX_CODEC_TARGET_DIR "${CODEC_TARGET_DIR}/cpp")
133134
set(CODEC_SCHEMA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/sbe-tool/src/test/resources")
134135
set(CODEC_PERF_SCHEMA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/sbe-benchmarks/src/main/resources")
135136
set(CODEC_EXAMPLES_SCHEMA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/sbe-samples/src/main/resources")
136137

138+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/sbe-tool/src/main/c)
137139
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/sbe-tool/src/main/cpp)
138140

139141
if(SBE_TESTS)
142+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/sbe-tool/src/test/c)
140143
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/sbe-tool/src/test/cpp)
141144
endif()
142145

sbe-tool/src/main/c/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_library(c_sbe INTERFACE)
2+
target_include_directories(c_sbe INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

sbe-tool/src/main/c/sbe/sbe.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#ifndef _SBE_H_
2+
#define _SBE_H_
3+
4+
#if !defined(__STDC_LIMIT_MACROS)
5+
#define __STDC_LIMIT_MACROS 1
6+
#endif
7+
#include <string.h>
8+
#include <stdint.h>
9+
#include <limits.h>
10+
11+
#define E100 -50100 // E_BUF_SHORT
12+
#define E103 -50103 // VAL_UNKNWN_ENUM
13+
#define E104 -50104 // I_OUT_RANGE_NUM
14+
#define E105 -50105 // I_OUT_RANGE_NUM
15+
#define E106 -50106 // I_OUT_RANGE_NUM
16+
#define E107 -50107 // BUF_SHORT_FLYWEIGHT
17+
#define E108 -50108 // BUF_SHORT_NXT_GRP_IND
18+
#define E109 -50109 // STR_TOO_LONG_FOR_LEN_TYP
19+
#define E110 -50110 // CNT_OUT_RANGE
20+
21+
inline const char *sbe_strerror(const int errnum)
22+
{
23+
switch (errnum)
24+
{
25+
case E100:
26+
return "buffer too short";
27+
case E103:
28+
return "unknown value for enum";
29+
case E104:
30+
return "index out of range";
31+
case E105:
32+
return "index out of range";
33+
case E106:
34+
return "length too large";
35+
case E107:
36+
return "buffer too short for flyweight";
37+
case E108:
38+
return "buffer too short to support next group index";
39+
case E109:
40+
return "std::string too long for length type";
41+
case E110:
42+
return "count outside of allowed range";
43+
default:
44+
return "unknown error";
45+
}
46+
}
47+
48+
/*
49+
* Define some byte ordering macros
50+
*/
51+
#if defined(WIN32) || defined(_WIN32)
52+
#define SBE_BIG_ENDIAN_ENCODE_16(v) _byteswap_ushort(v)
53+
#define SBE_BIG_ENDIAN_ENCODE_32(v) _byteswap_ulong(v)
54+
#define SBE_BIG_ENDIAN_ENCODE_64(v) _byteswap_uint64(v)
55+
#define SBE_LITTLE_ENDIAN_ENCODE_16(v) (v)
56+
#define SBE_LITTLE_ENDIAN_ENCODE_32(v) (v)
57+
#define SBE_LITTLE_ENDIAN_ENCODE_64(v) (v)
58+
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
59+
#define SBE_BIG_ENDIAN_ENCODE_16(v) __builtin_bswap16(v)
60+
#define SBE_BIG_ENDIAN_ENCODE_32(v) __builtin_bswap32(v)
61+
#define SBE_BIG_ENDIAN_ENCODE_64(v) __builtin_bswap64(v)
62+
#define SBE_LITTLE_ENDIAN_ENCODE_16(v) (v)
63+
#define SBE_LITTLE_ENDIAN_ENCODE_32(v) (v)
64+
#define SBE_LITTLE_ENDIAN_ENCODE_64(v) (v)
65+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
66+
#define SBE_LITTLE_ENDIAN_ENCODE_16(v) __builtin_bswap16(v)
67+
#define SBE_LITTLE_ENDIAN_ENCODE_32(v) __builtin_bswap32(v)
68+
#define SBE_LITTLE_ENDIAN_ENCODE_64(v) __builtin_bswap64(v)
69+
#define SBE_BIG_ENDIAN_ENCODE_16(v) (v)
70+
#define SBE_BIG_ENDIAN_ENCODE_32(v) (v)
71+
#define SBE_BIG_ENDIAN_ENCODE_64(v) (v)
72+
#else
73+
#error "Byte Ordering of platform not determined. Set __BYTE_ORDER__ manually before including this file."
74+
#endif
75+
76+
#if defined(SBE_NO_BOUNDS_CHECK)
77+
#define SBE_BOUNDS_CHECK_EXPECT(exp,c) (false)
78+
#elif defined(_MSC_VER)
79+
#define SBE_BOUNDS_CHECK_EXPECT(exp,c) (exp)
80+
#else
81+
#define SBE_BOUNDS_CHECK_EXPECT(exp,c) (__builtin_expect(exp,c))
82+
#endif
83+
84+
#define SBE_NULLVALUE_INT8 INT8_MIN
85+
#define SBE_NULLVALUE_INT16 INT16_MIN
86+
#define SBE_NULLVALUE_INT32 INT32_MIN
87+
#define SBE_NULLVALUE_INT64 INT64_MIN
88+
#define SBE_NULLVALUE_UINT8 UINT8_MAX
89+
#define SBE_NULLVALUE_UINT16 UINT16_MAX
90+
#define SBE_NULLVALUE_UINT32 UINT32_MAX
91+
#define SBE_NULLVALUE_UINT64 UINT64_MAX
92+
93+
typedef union sbe_float_as_uint_u
94+
{
95+
float fp_value;
96+
uint32_t uint_value;
97+
}
98+
sbe_float_as_uint_t;
99+
100+
typedef union sbe_double_as_uint_u
101+
{
102+
double fp_value;
103+
uint64_t uint_value;
104+
}
105+
sbe_double_as_uint_t;
106+
107+
enum sbe_meta_attribute
108+
{
109+
sbe_meta_attribute_EPOCH,
110+
sbe_meta_attribute_TIME_UNIT,
111+
sbe_meta_attribute_SEMANTIC_TYPE,
112+
sbe_meta_attribute_PRESENCE
113+
};
114+
115+
#endif

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/TargetCodeGeneratorLoader.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package uk.co.real_logic.sbe.generation;
1717

1818
import org.agrona.generation.PackageOutputManager;
19+
import uk.co.real_logic.sbe.generation.c.CGenerator;
20+
import uk.co.real_logic.sbe.generation.c.COutputManager;
1921
import uk.co.real_logic.sbe.generation.cpp.CppGenerator;
2022
import uk.co.real_logic.sbe.generation.cpp.NamespaceOutputManager;
2123
import uk.co.real_logic.sbe.generation.golang.GolangGenerator;
@@ -42,6 +44,14 @@ public CodeGenerator newInstance(final Ir ir, final String outputDir)
4244
}
4345
},
4446

47+
C()
48+
{
49+
public CodeGenerator newInstance(final Ir ir, final String outputDir)
50+
{
51+
return new CGenerator(ir, new COutputManager(outputDir, ir.applicableNamespace()));
52+
}
53+
},
54+
4555
CPP()
4656
{
4757
public CodeGenerator newInstance(final Ir ir, final String outputDir)

0 commit comments

Comments
 (0)