Skip to content

[RISCV] Add CORE-V headers

Eclipse Webmaster requested to merge github/fork/melonedo/add-headers into development

Created by: melonedo

Add header files for all CORE-V extensions that have C builtin functions following D155647. The definitions of existing builtin functions are also adjusted accordingly.

Note that D155647 defines new C functions without prefix __builtin (see below, also refer to RISCV-C-API Specification#Intrinsic Functions), while this PR simply re-declares existing builtins.

static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__))
__riscv_rol_32(uint32_t __x, uint32_t __y) {
  return __builtin_rotateleft32(__x, __y);
}

The header files are extracted from the specification with this script:

script
import re
from pathlib import Path
from textwrap import dedent

# identify target-independent builtin functions
NAME_TO_EXTENSION_MAP = dict(abs="alu")

with open("corev-builtin-spec.md", "rt") as file:
    spec = file.read()
exts = dict()

# rely on "####" as an indication of a builtin
for f in re.finditer(r"#### `(.+)`", spec):
    r = re.match(
        r"\w+ __builtin_((?:riscv_cv_([^_]+))?\w+)\s*\(.+\)",
        f[1],
    )
    if r is None:
        print("Failed to parse:", f[1])
        continue
    name = r[1]
    ext = NAME_TO_EXTENSION_MAP.get(name, r[2])
    # print(name, ext)
    exts.setdefault(ext, []).append(f[1])

include = Path("include")
include.mkdir(exist_ok=True)

for e, funcs in exts.items():
    prologue = dedent(f"""\
    #ifndef __RISCV_COREV_{e.upper()}_H
    #define __RISCV_COREV_{e.upper()}_H

    #include <stdint.h>

    #if defined(__cplusplus)
    extern "C" {{
    #endif

    #if defined(__riscv_xcv{e})
    """)
    epilogue = dedent(f"""\
    #endif // defined(__riscv_xcv{e})

    #if defined(__cplusplus)
    }}
    #endif

    #endif // define __RISCV_COREV_{e.upper()}_H
    """)
    ext_header = include / f"riscv_corev_{e}.h"
    with ext_header.open("wt") as file:
        file.write(prologue)
        for func in funcs:
            # file.write(
            #     "\nstatic __inline__ uint64_t __attribute__((__always_inline__, __nodebug__))\n"
            # )
            file.write(func)
            file.write(";\n")
        file.write(epilogue)

Fix #74 (closed). CC @MaryBennett, @PaoloS02.

Merge request reports

Loading