Skip to content

Commit

Permalink
Adding structure generation (#4)
Browse files Browse the repository at this point in the history
Adding support for basic structure type.

Signed-off-by: vsoch <vsoch@users.noreply.github.com>
Co-authored-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch and vsoch committed Oct 5, 2021
1 parent 202d0df commit 0fce618
Show file tree
Hide file tree
Showing 14 changed files with 369 additions and 219 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ to a function, we use a pipe. Thus, you need to know the functions that are supp
|function| DeclareArgs | Make multi-line declarations of parameters to pass into the function |
|function| GetFunctionName | Just print the name of the function |
|function| PrintArgs | just do a println of each named param, usually for debugging |
|function| DeclareStructs | write declarations for structures |
You'll also notice there is a Makefile in the folder - likely when we generate many of these
and save the renderings somewhere, we will copy over all the content here (including the Makefile, which
Expand Down Expand Up @@ -216,8 +217,8 @@ originally produce for testing cases:
## TODO
- I'm not sure how _Complex works (need help initializing them)
- need to be able to print an int128 without an error
- double pointers seem to give me trouble (need help here too)
- pointers on type double seem to give me trouble (need help here too)
- how to include int128/pointers in structs
### License
Expand Down
5 changes: 3 additions & 2 deletions examples/cpp/simple/1/foo.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#include <iostream>
#include "foo.h"

void Function(unsigned long long fpIntLnxssnwknplnukqo) {
void Function(unsigned long long fpIntUhasbzebyxv, double fpFloatKmeaonqssokgultzsf) {

std::cout << fpIntLnxssnwknplnukqo << std::endl;
std::cout << "fpIntUhasbzebyxv " << fpIntUhasbzebyxv << std::endl;
std::cout << "fpFloatKmeaonqssokgultzsf " << fpFloatKmeaonqssokgultzsf << std::endl;

}
5 changes: 4 additions & 1 deletion examples/cpp/simple/1/foo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

#include <cstdint>

void Function(unsigned long long fpIntLnxssnwknplnukqo);
// Structs used in the function should be declared first


void Function(unsigned long long fpIntUhasbzebyxv, double fpFloatKmeaonqssokgultzsf);
Binary file modified examples/cpp/simple/1/libfoo.so
Binary file not shown.
5 changes: 3 additions & 2 deletions examples/cpp/simple/1/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
int main() {

// Initialize each formal param
unsigned long long fpIntLnxssnwknplnukqo = 13432713847415853222;
unsigned long long fpIntUhasbzebyxv = 9850645379854796567;
double fpFloatKmeaonqssokgultzsf = 14059.994265585735417;

// bigcall(1, 2, 3, 4, 5, bigthing);
Function(fpIntLnxssnwknplnukqo);
Function(fpIntUhasbzebyxv, fpFloatKmeaonqssokgultzsf);
}
Binary file modified examples/cpp/simple/1/v
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/cpp/simple/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 -o v main.c -L. -lfoo -Wl,-rpath,`pwd`
clang++ -O3 -o foo-exec main.c -L. -lfoo -Wl,-rpath,`pwd`
3 changes: 3 additions & 0 deletions examples/cpp/simple/foo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

#include <cstdint>

// Structs used in the function should be declared first
{{ .Function | DeclareStructs }}

void {{ .Function | GetFunctionName }}({{ .Function | AsFormalParams }});
88 changes: 88 additions & 0 deletions generate/cpp/float.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cpp

func GetFloatTypes() []string {
return []string{"float", "double", "long double"}
}

// Integral Types
type FloatFormalParam struct {
Name string
Type string
IsComplex bool
IsPointer bool
Value string
}

// Declaration of a formal param
func (p FloatFormalParam) DeclareFormalParam() string {
return p.Type + " " + p.Reference()
}

// DeclareValues includes the value
func (p FloatFormalParam) DeclareValue() string {
return p.Prefix() + p.Type + " " + p.Name + " = " + p.Value
}

// Declaration of a float
func (p FloatFormalParam) Declaration() string {
// This is a declaration for formal params (we need the * for pointer)
return p.Type + " " + p.Reference()
}

// TODO the above returns a & but for struct we don't want that?
// also look up ow to define struct with pointer...

// Reference of an integral formal param
func (p FloatFormalParam) Reference() string {
if p.IsPointer {
return "&" + p.Name
}
return p.Name
}

// Prefix of an integral formal param
func (p FloatFormalParam) Prefix() string {

// TODO need to add complex back
// if p.IsComplex {
// return "_Complex "
// }
return ""
}

// Type of an integral formal param
func (p FloatFormalParam) GetType() string {
if p.IsPointer {
return p.Type + " *"
}
return p.Type
}

// Raw type
func (p FloatFormalParam) GetRawType() string {
return p.Type
}

// Value returns the string representation of the value
func (p FloatFormalParam) GetValue() string {
return p.Value
}

// GetName returns the name
func (p FloatFormalParam) GetName() string {
if p.IsPointer {
return "*" + p.Name
}
return p.Name
}

// GetFieldName returns the name without decoration
func (p FloatFormalParam) GetFieldName() string {
return p.Name
}

// Print prints an float formal param
func (p FloatFormalParam) Print() string {
// TODO we will want more custom formatting based on the type here
return "std::cout << \"" + p.Name + " \" << " + p.Reference() + " << std::endl;"
}
52 changes: 41 additions & 11 deletions generate/cpp/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ 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)
function.FormalParams = params
return function
}

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

params := []FormalParam{}

// How many to generate? First choice goes to exact
Expand All @@ -132,30 +140,52 @@ func GenerateFunction(name string, entry config.Render) Function {
num = utils.RandomIntRange(entry.Parameters.Min, entry.Parameters.Max)
}
for i := 0; i < num; i++ {
params = append(params, NewFormalParam())
params = append(params, NewFormalParam(entry, nestingCount, withinStruct))
}

function.FormalParams = params
return function
return params
}

// Functions to create new Formal Parameters (all random)
func NewFormalParam() FormalParam {
switch utils.RandomChoice([]string{"integral", "float"}) {
func NewFormalParam(entry config.Render, nestingCount int, withinStruct bool) FormalParam {

// Only allow 1 level of structs (and make float, integral more likely)
choices := []string{"integral", "float", "struct", "integral", "float"}
if nestingCount > 0 {
choices = []string{"integral", "float"}
}

switch utils.RandomChoice(choices) {
case "integral":
return NewIntegral()
return NewIntegral(withinStruct)
case "float":
return NewFloat()
case "struct":
return NewStruct(entry, nestingCount)
}
return NewIntegral()
return NewIntegral(withinStruct)
}

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

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

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

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

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

Expand All @@ -178,6 +208,6 @@ func NewFloat() FormalParam {
Type: floatType,
Value: value,
IsComplex: isComplex,
IsPointer: utils.RandomBool(),
IsPointer: false,
}
}
110 changes: 110 additions & 0 deletions generate/cpp/integral.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package cpp

// INTEGRAL Possible integral types
func GetIntegralTypes(withinStruct bool) []string {
if withinStruct {
return []string{"char", "short", "int", "long", "long long"}
}
return []string{"char", "short", "int", "std::size_t", "long", "long long", "__int128"}
}

// Integral Types
type IntegralFormalParam struct {
Name string
Type string
IsSigned bool
IsPointer bool
Value string // since we are printing to a template, this can be a string
}

// Declaration of a formal param
func (p IntegralFormalParam) DeclareFormalParam() string {
if p.IsPointer {
return p.Prefix() + " " + p.Type + " * " + p.Name
}
return p.Prefix() + " " + p.GetType() + " " + p.GetName()
}

// DeclareValues includes the value
func (p IntegralFormalParam) DeclareValue() string {
var result string
if p.GetType() == "__int128" {
result = p.Prefix() + p.GetType() + " " + p.Name + ";"
result += p.GetValue()
return result
}
return p.Prefix() + p.Type + " " + p.Name + " = " + p.Value
}

// Declaration of an integral formal param
func (p IntegralFormalParam) Declaration() string {
result := p.Prefix() + p.Type + " " + p.Name
if p.GetRawType() == "__int128" {
return p.Prefix() + p.GetType() + " " + p.Name
}
return result
}

// GetValue returns the string representation of the value
func (p IntegralFormalParam) GetValue() string {
return p.Value
}

// GetName returns the string representation of the value
func (p IntegralFormalParam) GetName() string {
if p.IsPointer {
return "* " + p.Name
}
return p.Name
}
func (p IntegralFormalParam) GetFieldName() string {
return p.Name
}

// Reference of an integral formal param
func (p IntegralFormalParam) Reference() string {

// TODO need help creating these types
if p.Type == "double" {
return p.Name
}
if p.IsPointer {
return "&" + p.Name
}
return p.Name
}

// Prefix of an integral formal param
func (p IntegralFormalParam) Prefix() string {

// size T always unsigned
if p.Type == "std::size_t" {
return ""
}
if p.IsSigned {
return "signed "
}
return "unsigned "
}

// GetType of an integral formal param
func (p IntegralFormalParam) GetType() string {
if p.IsPointer {
return p.Type + " *"
}
return p.Type
}

func (p IntegralFormalParam) GetRawType() string {
return p.Type
}

// Print prints an integral formal param
func (p IntegralFormalParam) Print() string {
// TODO not sure how to do this one
if p.Type == "__int128" {
return ""
}
// TODO we will want custom printing based on the type here
return "std::cout << \"" + p.Name + " \" << " + p.Reference() + " << std::endl;"
}

0 comments on commit 0fce618

Please sign in to comment.