Skip to content

Commit 44c7e14

Browse files
committed
Fixes some bugs to enable library compilation by gcc
1 parent 948bc3e commit 44c7e14

15 files changed

+148
-30
lines changed

libc-hls/libc-hls.h

-8
This file was deleted.

libc-hls/libc_hls.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef _LIBC_HLS_H_
2+
#define _LIBC_HLS_H_
3+
4+
#include "libc_hls_types.h"
5+
#include "libc_hls_synthesizable.h"
6+
#include "libc_hls_reimplemented.h"
7+
#include "libc_hls_async_kernel.h"
8+
#endif

libc-hls/libc-hls-async-kernel.c libc-hls/libc_hls_async_kernel.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "libc-hls-async-kernel.h"
2-
void hls_abort(char* buf, async_kernel_info* info, bool is_last) {
1+
#include "libc_hls_async_kernel.h"
2+
void hls_abort(char* buf, hls_async_info* info, bool is_last) {
33
if(info->idx == -1) {
44
info->idx = 0;
55
}
@@ -8,7 +8,7 @@ void hls_abort(char* buf, async_kernel_info* info, bool is_last) {
88
}
99
}
1010

11-
void hls_assert(char* buf, async_kernel_info* info, bool is_last, int expression) {
11+
void hls_assert(char* buf, hls_async_info* info, bool is_last, int expression) {
1212
if(info->idx == -1) {
1313
info->idx = 0;
1414
}

libc-hls/libc-hls-async-kernel.h libc-hls/libc_hls_async_kernel.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef _LIBC_HLS_ASYNC_KERNEL_H_
22
#define _LIBC_HLS_ASYNC_KERNEL_H_
33

4-
#include <features-time64.h>
54
#include <math.h>
65
#include <nl_types.h>
76
#include <regex.h>
@@ -16,7 +15,7 @@
1615
#include <time.h>
1716
#include <wchar.h>
1817
#include <wctype.h>
19-
#include "libc-hls-types.h"
20-
void hls_abort(char* buf, async_kernel_info* info, bool is_last);
21-
void hls_assert(char* buf, async_kernel_info* info, bool is_last, int expression);
18+
#include "libc_hls_types.h"
19+
void hls_abort(char* buf, hls_async_info* info, bool is_last);
20+
void hls_assert(char* buf, hls_async_info* info, bool is_last, int expression);
2221
#endif
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "libc-hls-reimplemented.h"
1+
#include "libc_hls_reimplemented.h"
22
double hls_atof(char const * string) {
33
//TODO: Implement this function
44
}

libc-hls/libc-hls-reimplemented.h libc-hls/libc_hls_reimplemented.h

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef _LIBC_HLS_REIMPLEMENTED_H_
22
#define _LIBC_HLS_REIMPLEMENTED_H_
33

4-
#include <features-time64.h>
54
#include <math.h>
65
#include <nl_types.h>
76
#include <regex.h>

libc-hls/libc-hls-synthesizable.c libc-hls/libc_hls_synthesizable.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "libc-hls-synthesizable.h"
1+
#include "libc_hls_synthesizable.h"
22
int hls_abs(int n) {
33

44
return abs(n);

libc-hls/libc-hls-synthesizable.h libc-hls/libc_hls_synthesizable.h

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef _LIBC_HLS_SYNTHESIZABLE_H_
22
#define _LIBC_HLS_SYNTHESIZABLE_H_
33

4-
#include <features-time64.h>
54
#include <math.h>
65
#include <nl_types.h>
76
#include <regex.h>
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
double atof(char const* string)
2+
{
3+
if (string == (char const*)0)
4+
{
5+
return 0.0;
6+
}
7+
8+
double result = 0.0;
9+
double fraction = 0.0;
10+
double divisor = 10.0;
11+
int sign = 1;
12+
int exponent_sign = 1;
13+
int exponent = 0;
14+
int has_fraction = 0;
15+
16+
while (*string == ' ' || *string == '\t' || *string == '\n' || *string == '\r')
17+
{
18+
string++;
19+
}
20+
21+
if (*string == '-')
22+
{
23+
sign = -1;
24+
string++;
25+
}
26+
else if (*string == '+')
27+
{
28+
string++;
29+
}
30+
31+
while (*string >= '0' && *string <= '9')
32+
{
33+
result = result * 10.0 + (*string - '0');
34+
string++;
35+
}
36+
37+
if (*string == '.')
38+
{
39+
has_fraction = 1;
40+
string++;
41+
while (*string >= '0' && *string <= '9')
42+
{
43+
fraction += (*string - '0') / divisor;
44+
divisor *= 10.0;
45+
string++;
46+
}
47+
}
48+
result += fraction;
49+
50+
if (*string == 'e' || *string == 'E')
51+
{
52+
string++;
53+
if (*string == '-')
54+
{
55+
exponent_sign = -1;
56+
string++;
57+
}
58+
else if (*string == '+')
59+
{
60+
string++;
61+
}
62+
63+
while (*string >= '0' && *string <= '9')
64+
{
65+
exponent = exponent * 10 + (*string - '0');
66+
string++;
67+
}
68+
69+
if (exponent != 0)
70+
{
71+
double power = 1.0;
72+
for (int i = 0; i < exponent; i++)
73+
{
74+
power *= 10.0;
75+
}
76+
if (exponent_sign == -1)
77+
{
78+
result /= power;
79+
}
80+
else
81+
{
82+
result *= power;
83+
}
84+
}
85+
}
86+
87+
return sign * result;
88+
}

library-generator/json-to-c/src/AHandler.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export abstract class AHandler {
1010
protected fileName: string;
1111

1212
constructor(name: string, libName: string) {
13-
this.fileName = `${libName}-${name}`;
13+
this.fileName = `${libName}_${name}`;
1414

1515
this.header = ClavaJoinPoints.file(this.fileName + ".h", ".");
1616
this.source = ClavaJoinPoints.file(this.fileName + ".c", ".");
@@ -22,7 +22,6 @@ export abstract class AHandler {
2222

2323
this.source.addInclude(this.fileName + ".h", false);
2424
const defaultIncludes = [
25-
"features-time64.h",
2625
"math.h",
2726
"nl_types.h",
2827
"regex.h",

library-generator/json-to-c/src/AsyncKernelHandler.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import ClavaJoinPoints from "@specs-feup/clava/api/clava/ClavaJoinPoints.js";
44

55
export class AsyncKernelHandler extends AHandler {
66
constructor(libraryPrefix: string) {
7-
super("async-kernel", libraryPrefix);
7+
super("async_kernel", libraryPrefix);
88
}
99

1010
protected applyHeaderPrologue(): void {
11-
this.header.addInclude(`${this.libName}-types.h`, false);
11+
this.header.addInclude(`${this.libName}_types.h`, false);
1212
return;
1313
}
1414

1515
protected buildSignature(name: string, returnType: string, parameters: Record<string, string>[]): FunctionJp {
1616
const newParams: Decl[] = [
1717
ClavaJoinPoints.param("buf", ClavaJoinPoints.type("char*")),
18-
ClavaJoinPoints.param("info", ClavaJoinPoints.type("async_kernel_info*")),
18+
ClavaJoinPoints.param("info", ClavaJoinPoints.type("hls_async_info*")),
1919
ClavaJoinPoints.param("is_last", ClavaJoinPoints.type("bool"))
2020
];
2121

library-generator/json-to-c/src/Entrypoint.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import Io from "@specs-feup/lara/api/lara/Io.js";
44
const json = Io.readJson("../intermediate-data/libc-annotated.json");
55
const existingHeaders = ["../intermediate-data/types.h"];
66
const existingSources = ["../intermediate-data/reimplementations.c"];
7-
const outputDirs = ["./output", "../../libc-hls"];
7+
const outputDirs = ["../../libc-hls"];
88

99
const converter = new JsonToCConverter();
10-
const success = converter.convert(json, "libc-hls", existingHeaders, existingSources, outputDirs);
10+
const success = converter.convert(json, "libc_hls", existingHeaders, existingSources, outputDirs);
1111
console.log(success ? "Conversion successful" : "Conversion failed");

library-generator/json-to-c/src/JsonToCConverter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class JsonToCConverter {
3939
const oldHeader = ClavaJoinPoints.file(header);
4040
Clava.addFile(oldHeader);
4141

42-
const headerName = `${libName}-${oldHeader.name}`;
42+
const headerName = `${libName}_${oldHeader.name}`;
4343
headerNames.push(headerName);
4444

4545
const newHeader = ClavaJoinPoints.file(headerName, ".");
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { FunctionJp } from "@specs-feup/clava/api/Joinpoints.js";
1+
import { FunctionJp, Joinpoint, Statement } from "@specs-feup/clava/api/Joinpoints.js";
22
import { AHandler } from "./AHandler.js";
33
import ClavaJoinPoints from "@specs-feup/clava/api/clava/ClavaJoinPoints.js";
44
import Clava from "@specs-feup/clava/api/clava/Clava.js";
5+
import Query from "@specs-feup/lara/api/weaver/Query.js";
56

67
export class ReimplementableHandler extends AHandler {
78
constructor(libraryPrefix: string, additionalSources: string[]) {
@@ -10,16 +11,49 @@ export class ReimplementableHandler extends AHandler {
1011
for (const source of additionalSources) {
1112
const file = ClavaJoinPoints.file(source);
1213
Clava.addFile(file);
14+
//Clava.rebuild();
1315
}
1416
}
1517

1618
protected buildFunctionImpl(signature: Record<string, any>, newSig: FunctionJp): FunctionJp {
1719
const newFun = newSig.copy() as FunctionJp;
1820

19-
const comment = ClavaJoinPoints.comment("TODO: Implement this function");
21+
const stmts: Joinpoint[] = [];
22+
const impl = this.findExitstingImpl(signature);
2023

21-
const scope = ClavaJoinPoints.scope(comment);
24+
if (impl.length > 0) {
25+
stmts.push(...impl);
26+
}
27+
else {
28+
const comment = ClavaJoinPoints.comment("TODO: Implement this function");
29+
stmts.push(comment);
30+
}
31+
32+
const scope = ClavaJoinPoints.scope(...stmts);
2233
newFun.setBody(scope);
2334
return newFun;
2435
}
36+
37+
private findExitstingImpl(signature: Record<string, any>): Joinpoint[] {
38+
const jps: Joinpoint[] = [];
39+
40+
for (const fun of Query.search(FunctionJp, { name: signature.name })) {
41+
console.log(fun);
42+
if (!fun.isImplementation) {
43+
continue;
44+
}
45+
if (fun.params.length !== signature.parameters.length) {
46+
continue;
47+
}
48+
if (fun.returnType.code !== signature.returnType) {
49+
continue;
50+
}
51+
for (const child of fun.body.children) {
52+
const stmt = ClavaJoinPoints.stmtLiteral(child.code);
53+
jps.push(stmt);
54+
return jps;
55+
}
56+
}
57+
return jps;
58+
}
2559
}

0 commit comments

Comments
 (0)