blob: f35ba3b5163b29f67bcde27a02f2fb925b8a5987 [file] [log] [blame]
David Brazdil863b1502019-10-24 13:55:50 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * This header file is intended for use by files compiled with the
19 * 'offset_size_header' build rule. See overview in 'offset_size_header.gni'.
20 */
21
22#pragma once
23
24#if (defined GENERATE_BINARY) && (defined VERIFY_HEADER)
25#error Only one action can be specified
26
27#elif defined GENERATE_BINARY
28
29/**
30 * File being compiled to generate binaries with definitions of constants.
31 */
32
33/**
34 * Emit a function with an embedded string in the format:
Andrew Walbran4cd2b662020-04-07 12:26:12 +010035 * <HAFNIUM_DEFINE name value />
David Brazdil863b1502019-10-24 13:55:50 +010036 * These will be recognized by a script that generates the header file.
37 */
38#define DEFINE(sym, val) \
39 void gen_header__##sym(void) \
40 { \
41 __asm__ volatile( \
42 "\n" \
43 ".ascii \"\\n<HAFNIUM_DEFINE " #sym \
44 " %0 />\\n\"\n" \
45 ".align 8\n" /* Align epilogue */ \
46 : \
47 : "i"(val)); \
48 }
49
50#define DEFINE_SIZEOF(sym, type) DEFINE(sym, sizeof(type))
51#define DEFINE_OFFSETOF(sym, type, field) DEFINE(sym, offsetof(type, field))
52
53#elif defined VERIFY_HEADER
54
55/**
56 * File being compiled as part of the main build to check the values in
57 * the auto-generated header file (included using a command-line flag).
58 */
59
60#include "hf/static_assert.h"
61
62#define DEFINE_SIZEOF(sym, type) \
63 void gen_header__##sym(void) \
64 { \
65 static_assert(sizeof(type) == sym, \
66 "Generated struct size mismatch"); \
67 }
68
69#define DEFINE_OFFSETOF(sym, type, field) \
70 void gen_header__##sym(void) \
71 { \
72 static_assert(offsetof(type, field) == sym, \
73 "Generated struct offset mismatch"); \
74 }
75
76#else
77#error No action specified
78#endif