1 /*
2  * Copyright 2019 The Hafnium Authors.
3  *
4  * Use of this source code is governed by a BSD-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/BSD-3-Clause.
7  */
8 
9 /**
10  * This header file is intended for use by files compiled with the
11  * 'offset_size_header' build rule. See overview in 'offset_size_header.gni'.
12  */
13 
14 #pragma once
15 
16 #if (defined GENERATE_BINARY) && (defined VERIFY_HEADER)
17 #error Only one action can be specified
18 
19 #elif defined GENERATE_BINARY
20 
21 /**
22  * File being compiled to generate binaries with definitions of constants.
23  */
24 
25 /**
26  * Emit a function with an embedded string in the format:
27  *     <HAFNIUM_DEFINE name value />
28  * These will be recognized by a script that generates the header file.
29  */
30 #define DEFINE(sym, val)                                    \
31 	void gen_header__##sym(void)                        \
32 	{                                                   \
33 		__asm__ volatile(                           \
34 			"\n"                                \
35 			".ascii \"\\n<HAFNIUM_DEFINE " #sym \
36 			" %0 />\\n\"\n"                     \
37 			".align 8\n" /* Align epilogue */   \
38 			:                                   \
39 			: "i"(val));                        \
40 	}
41 
42 #define DEFINE_SIZEOF(sym, type) DEFINE(sym, sizeof(type))
43 #define DEFINE_OFFSETOF(sym, type, field) DEFINE(sym, offsetof(type, field))
44 
45 #elif defined VERIFY_HEADER
46 
47 /**
48  * File being compiled as part of the main build to check the values in
49  * the auto-generated header file (included using a command-line flag).
50  */
51 
52 #include "hf/static_assert.h"
53 
54 #define DEFINE_SIZEOF(sym, type)                                 \
55 	void gen_header__##sym(void)                             \
56 	{                                                        \
57 		static_assert(sizeof(type) == sym,               \
58 			      "Generated struct size mismatch"); \
59 	}
60 
61 #define DEFINE_OFFSETOF(sym, type, field)                          \
62 	void gen_header__##sym(void)                               \
63 	{                                                          \
64 		static_assert(offsetof(type, field) == sym,        \
65 			      "Generated struct offset mismatch"); \
66 	}
67 
68 #else
69 #error No action specified
70 #endif
71