Skip to content

Commit

Permalink
Merge pull request #8 from buildsi/add/unions
Browse files Browse the repository at this point in the history
Adding union type
  • Loading branch information
vsoch committed Oct 20, 2021
2 parents 4b1cbe1 + 65ac3aa commit dd21e6a
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 80 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,6 @@ This example generates only numeric types, and does addition with a return value
$ go run main.go gen examples/cpp/addition/codegen.yaml
```
### Writing a Template
A template is a folder with a codegen.yaml file and one or more files that are to be filled in (templates).
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/simple/1/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
all:
clang++ -fPIC -shared -O3 -g -o libfoo.so foo.c
clang++ -O3 -g -o foo-exec main.c -L. -lfoo -Wl,-rpath,`pwd`
clang++ -O3 -o foo-exec main.c -L. -lfoo -Wl,-rpath,`pwd`
142 changes: 104 additions & 38 deletions examples/cpp/simple/1/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,128 @@
"Function": {
"name": "Function",
"parameters": [
{
"name": "fpIntNirpvxxjljseoeq",
"type": "std::string",
"is_signed": false,
"is_pointer": false,
"value": "\"rijpkyzbtlx\""
},
{
"name": "fpFloatPfsdlnaiffqe",
"type": "long double",
"is_complex": false,
"is_pointer": false,
"value": "27105.92540864471443"
},
{
"name": "fpIntNauekcseuyuls",
"type": "short",
"is_signed": true,
"is_pointer": false,
"value": "21157"
},
{
"type": "struct",
"value": "",
"name": "structHsqlmtcfjxgalrdce",
"is_pointer": true,
"name": "structEjknkgadfa",
"is_pointer": false,
"fields": [
{
"name": "fpIntVztmefdjczvtfppzxgu",
"name": "fpFloatLuzkhkoxhyjkn",
"type": "float",
"is_complex": false,
"is_pointer": false,
"value": "64316.06133035861504"
},
{
"name": "fpIntVraaodeeavanyhdz",
"type": "int",
"is_signed": false,
"is_pointer": true,
"value": "1144873560"
},
{
"name": "fpIntXvzdloucfqfaazabgf",
"type": "long long",
"is_signed": true,
"is_pointer": true,
"value": "-2676140095681027988"
"value": "9217545440300276469"
},
{
"name": "fpFloatCsnvyrxala",
"type": "float",
"is_complex": true,
"is_pointer": false,
"value": "9361592366087721.203"
},
{
"name": "fpIntJfntojpcbnobhyzlu",
"type": "char",
"is_signed": true,
"is_pointer": true,
"value": "'v'"
},
{
"name": "fpFloatQythcelcmpldgugdnz",
"type": "double",
"is_complex": false,
"is_pointer": false,
"value": "2609453.9312241003"
}
]
],
"is_union": false
},
{
"name": "fpFloatWbxnrgynsukt",
"type": "double",
"is_complex": false,
"name": "fpIntJpqjsedhnpvnvtjfxm",
"type": "long",
"is_signed": true,
"is_pointer": true,
"value": "-1195503477"
},
{
"name": "fpFloatOkxtgxtfbgmvoezfcmz",
"type": "float",
"is_complex": true,
"is_pointer": false,
"value": "35637.78088195861942"
"value": "2848223.570775133493"
},
{
"name": "fpIntZitcfqtgcwgg",
"type": "std::size_t",
"is_signed": false,
"is_pointer": true,
"value": "27173"
"name": "fpIntYoiwjlejeux",
"type": "long",
"is_signed": true,
"is_pointer": false,
"value": "-1206782476"
},
{
"name": "fpFloatCpeqyqqtqlwrpp",
"type": "long double",
"name": "fpFloatYtpwgmomqgnq",
"type": "float",
"is_complex": true,
"is_pointer": false,
"value": "13697830667.942846202"
"value": "124838372252039654.38"
},
{
"type": "union",
"value": "",
"name": "unionGplgcipqwtlgfz",
"is_pointer": true,
"fields": [
{
"name": "fpIntLlbkcrdenfsqxvhjyqy",
"type": "short",
"is_signed": true,
"is_pointer": false,
"value": "-22420"
},
{
"name": "fpIntIeijlyuyho",
"type": "char",
"is_signed": true,
"is_pointer": true,
"value": "'Q'"
},
{
"name": "fpFloatUkvgmvngoibibz",
"type": "double",
"is_complex": false,
"is_pointer": false,
"value": "262822.4884931115707"
},
{
"name": "fpIntEkjetkvotspayy",
"type": "short",
"is_signed": true,
"is_pointer": false,
"value": "-12165"
},
{
"name": "fpFloatTxtyuzacgvurrwek",
"type": "float",
"is_complex": false,
"is_pointer": false,
"value": "1351860.2488992992004"
}
],
"is_union": true
}
]
}
Expand Down
25 changes: 17 additions & 8 deletions examples/cpp/simple/1/foo.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@
#include <string>
#include "foo.h"

void Function( std::string fpIntNirpvxxjljseoeq, long double fpFloatPfsdlnaiffqe, signed short fpIntNauekcseuyuls, STRUCTstructHsqlmtcfjxgalrdce * structHsqlmtcfjxgalrdce, double fpFloatWbxnrgynsukt, std::size_t * fpIntZitcfqtgcwgg, long double fpFloatCpeqyqqtqlwrpp) {
void Function(STRUCTstructEjknkgadfa structEjknkgadfa, signed long * fpIntJpqjsedhnpvnvtjfxm, float fpFloatOkxtgxtfbgmvoezfcmz, signed long fpIntYoiwjlejeux, float fpFloatYtpwgmomqgnq, UNIONunionGplgcipqwtlgfz * unionGplgcipqwtlgfz) {

std::cout << "fpIntNirpvxxjljseoeq" << fpIntNirpvxxjljseoeq << std::endl;
std::cout << "fpFloatPfsdlnaiffqe " << fpFloatPfsdlnaiffqe << std::endl;
std::cout << "fpIntNauekcseuyuls" << fpIntNauekcseuyuls << std::endl;
std::cout << "structHsqlmtcfjxgalrdce" << structHsqlmtcfjxgalrdce->fpIntVztmefdjczvtfppzxgu << std::endl;
std::cout << "structEjknkgadfa" << structEjknkgadfa.fpFloatLuzkhkoxhyjkn << std::endl;
std::cout << "structEjknkgadfa" << structEjknkgadfa.fpIntVraaodeeavanyhdz << std::endl;
std::cout << "structEjknkgadfa" << structEjknkgadfa.fpIntXvzdloucfqfaazabgf << std::endl;
std::cout << "structEjknkgadfa" << structEjknkgadfa.fpFloatCsnvyrxala << std::endl;
std::cout << "structEjknkgadfa" << structEjknkgadfa.fpIntJfntojpcbnobhyzlu << std::endl;
std::cout << "structEjknkgadfa" << structEjknkgadfa.fpFloatQythcelcmpldgugdnz << std::endl;

std::cout << "fpIntJpqjsedhnpvnvtjfxm" << &fpIntJpqjsedhnpvnvtjfxm << std::endl;
std::cout << "fpFloatOkxtgxtfbgmvoezfcmz " << fpFloatOkxtgxtfbgmvoezfcmz << std::endl;
std::cout << "fpIntYoiwjlejeux" << fpIntYoiwjlejeux << std::endl;
std::cout << "fpFloatYtpwgmomqgnq " << fpFloatYtpwgmomqgnq << std::endl;
std::cout << "unionGplgcipqwtlgfz" << unionGplgcipqwtlgfz->fpIntLlbkcrdenfsqxvhjyqy << std::endl;
std::cout << "unionGplgcipqwtlgfz" << unionGplgcipqwtlgfz->fpIntIeijlyuyho << std::endl;
std::cout << "unionGplgcipqwtlgfz" << unionGplgcipqwtlgfz->fpFloatUkvgmvngoibibz << std::endl;
std::cout << "unionGplgcipqwtlgfz" << unionGplgcipqwtlgfz->fpIntEkjetkvotspayy << std::endl;
std::cout << "unionGplgcipqwtlgfz" << unionGplgcipqwtlgfz->fpFloatTxtyuzacgvurrwek << std::endl;

std::cout << "fpFloatWbxnrgynsukt " << fpFloatWbxnrgynsukt << std::endl;
std::cout << "fpIntZitcfqtgcwgg" << &fpIntZitcfqtgcwgg << std::endl;
std::cout << "fpFloatCpeqyqqtqlwrpp " << fpFloatCpeqyqqtqlwrpp << std::endl;

}
18 changes: 15 additions & 3 deletions examples/cpp/simple/1/foo.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@

// Structs used in the function should be declared first

struct STRUCTstructHsqlmtcfjxgalrdce {
signed long long fpIntVztmefdjczvtfppzxgu = -2676140095681027988;
struct STRUCTstructEjknkgadfa {
float fpFloatLuzkhkoxhyjkn = 64316.06133035861504;
unsigned int fpIntVraaodeeavanyhdz = 1144873560;
signed long long fpIntXvzdloucfqfaazabgf = 9217545440300276469;
float fpFloatCsnvyrxala = 9361592366087721.203;
signed char fpIntJfntojpcbnobhyzlu = 'v';
double fpFloatQythcelcmpldgugdnz = 2609453.9312241003;
};
union UNIONunionGplgcipqwtlgfz {
signed short fpIntLlbkcrdenfsqxvhjyqy;
signed char fpIntIeijlyuyho;
double fpFloatUkvgmvngoibibz;
signed short fpIntEkjetkvotspayy;
float fpFloatTxtyuzacgvurrwek = 1351860.2488992992004;
};

void Function( std::string fpIntNirpvxxjljseoeq, long double fpFloatPfsdlnaiffqe, signed short fpIntNauekcseuyuls, STRUCTstructHsqlmtcfjxgalrdce * structHsqlmtcfjxgalrdce, double fpFloatWbxnrgynsukt, std::size_t * fpIntZitcfqtgcwgg, long double fpFloatCpeqyqqtqlwrpp);
void Function(STRUCTstructEjknkgadfa structEjknkgadfa, signed long * fpIntJpqjsedhnpvnvtjfxm, float fpFloatOkxtgxtfbgmvoezfcmz, signed long fpIntYoiwjlejeux, float fpFloatYtpwgmomqgnq, UNIONunionGplgcipqwtlgfz * unionGplgcipqwtlgfz);
Binary file modified examples/cpp/simple/1/libfoo.so
Binary file not shown.
15 changes: 7 additions & 8 deletions examples/cpp/simple/1/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
int main() {

// Initialize each formal param
std::string fpIntNirpvxxjljseoeq = "rijpkyzbtlx";
long double fpFloatPfsdlnaiffqe = 27105.92540864471443;
signed short fpIntNauekcseuyuls = 21157;
STRUCTstructHsqlmtcfjxgalrdce structHsqlmtcfjxgalrdce;;
double fpFloatWbxnrgynsukt = 35637.78088195861942;
std::size_t fpIntZitcfqtgcwgg = 27173;
long double fpFloatCpeqyqqtqlwrpp = 13697830667.942846202;
STRUCTstructEjknkgadfa structEjknkgadfa;;
signed long fpIntJpqjsedhnpvnvtjfxm = -1195503477;
float fpFloatOkxtgxtfbgmvoezfcmz = 2848223.570775133493;
signed long fpIntYoiwjlejeux = -1206782476;
float fpFloatYtpwgmomqgnq = 124838372252039654.38;
UNIONunionGplgcipqwtlgfz unionGplgcipqwtlgfz;;

// bigcall(1, 2, 3, 4, 5, bigthing);
Function(fpIntNirpvxxjljseoeq, fpFloatPfsdlnaiffqe, fpIntNauekcseuyuls, &structHsqlmtcfjxgalrdce, fpFloatWbxnrgynsukt, &fpIntZitcfqtgcwgg, fpFloatCpeqyqqtqlwrpp);
Function(structEjknkgadfa, &fpIntJpqjsedhnpvnvtjfxm, fpFloatOkxtgxtfbgmvoezfcmz, fpIntYoiwjlejeux, fpFloatYtpwgmomqgnq, &unionGplgcipqwtlgfz);
}
29 changes: 18 additions & 11 deletions generate/cpp/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ func WriteTemplate(path string, t *template.Template, funcs *map[string]Function
func GenerateFunction(name string, entry config.Render) Function {

function := Function{Name: name}
params := GenerateFormalParams(entry, 0, false)
params := GenerateFormalParams(entry, 0, false, false)
function.FormalParams = params
return function
}

// Generate formal params, either for a function or structure
func GenerateFormalParams(entry config.Render, nestingCount int, withinStruct bool) []FormalParam {
func GenerateFormalParams(entry config.Render, nestingCount int, withinStruct bool, withinUnion bool) []FormalParam {

params := []FormalParam{}

Expand All @@ -147,13 +147,13 @@ func GenerateFormalParams(entry config.Render, nestingCount int, withinStruct bo
num = utils.RandomIntRange(entry.Parameters.Min, entry.Parameters.Max)
}
for i := 0; i < num; i++ {
params = append(params, NewFormalParam(entry, nestingCount, withinStruct))
params = append(params, NewFormalParam(entry, nestingCount, withinStruct, withinUnion))
}
return params
}

// Functions to create new Formal Parameters (all random)
func NewFormalParam(entry config.Render, nestingCount int, withinStruct bool) FormalParam {
func NewFormalParam(entry config.Render, nestingCount int, withinStruct bool, withinUnion bool) FormalParam {

// Only allow 1 level of structs (and make float, integral more likely)
choices := []string{"integral", "float", "struct", "integral", "float"}
Expand All @@ -170,36 +170,43 @@ func NewFormalParam(entry config.Render, nestingCount int, withinStruct bool) Fo
case "numeric":
return NewIntegralNumeric()
case "integral":
return NewIntegral(withinStruct)
return NewIntegral(withinStruct, withinUnion)
case "float":
return NewFloat()
case "struct":
return NewStruct(entry, nestingCount)
}
return NewIntegral(withinStruct)
return NewIntegral(withinStruct, withinUnion)
}

// NewStruct returns a new struct type, which also includes its own set of fields
func NewStruct(entry config.Render, nestingCount int) FormalParam {

isUnion := utils.RandomBool()
typeName := "struct"
if isUnion {
typeName = "union"
}

// A struct has its own list of fields
nestingCount += 1
fields := GenerateFormalParams(entry, nestingCount, false)
fields := GenerateFormalParams(entry, nestingCount, false, isUnion)

// Get the type beforehand to derive a random value for it
name := "struct" + strings.Title(utils.RandomName())
name := typeName + strings.Title(utils.RandomName())
return StructureParam{Name: name,
Type: "struct",
Type: typeName,
IsUnion: isUnion,
IsPointer: utils.RandomBool(),
Fields: fields}
}

// NewIntegral returns a new integral type
func NewIntegral(withinStruct bool) FormalParam {
func NewIntegral(withinStruct bool, withinUnion bool) FormalParam {

// Get the type beforehand to derive a random value for it
name := "fpInt" + strings.Title(utils.RandomName())
integralType := utils.RandomChoice(GetIntegralTypes(withinStruct))
integralType := utils.RandomChoice(GetIntegralTypes(withinStruct, withinUnion))
isSigned := utils.RandomBool()
value := GetIntegralValue(integralType, isSigned, name)

Expand Down
4 changes: 3 additions & 1 deletion generate/cpp/integral.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
)

// INTEGRAL Possible integral types NOTE __int128 removed for now, need help to define!
func GetIntegralTypes(withinStruct bool) []string {
func GetIntegralTypes(withinStruct bool, withinUnion bool) []string {
if withinStruct {
return []string{"char", "short", "int", "long", "long long", "std::string", "bool"}
} else if withinUnion {
return []string{"char", "short", "int", "long", "long long", "bool"}
}
return []string{"char", "short", "int", "std::size_t", "long", "long long", "std::string", "bool"}
}
Expand Down

0 comments on commit dd21e6a

Please sign in to comment.