Skip to content

Commit

Permalink
feat: improve layout for records in llvm backend
Browse files Browse the repository at this point in the history
  • Loading branch information
aboeglin committed Apr 20, 2024
1 parent e1d2bea commit 34d7860
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 118 deletions.
12 changes: 6 additions & 6 deletions compiler/main/Generate/LLVM/LLVM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -945,10 +945,10 @@ generateExp env symbolTable exp = case exp of
let index = fromIntegral $ Maybe.fromMaybe 0 (List.elemIndex fieldName (Map.keys fields))
fieldsOperand <- gep recordOperand' [i32ConstOp 0, i32ConstOp 1] -- i8**
fieldsOperand' <- load fieldsOperand 0 -- i8*
fieldsOperand'' <- safeBitcast fieldsOperand' (Type.ptr (Type.ptr fieldType))
fieldsOperand'' <- safeBitcast fieldsOperand' (Type.ptr fieldType)
field <- gep fieldsOperand'' [i32ConstOp index]
field' <- load field 0
valuePtr <- gep field' [i32ConstOp 0, i32ConstOp 1]
-- field' <- load field 0
valuePtr <- gep field [i32ConstOp 0, i32ConstOp 1]
store valuePtr 0 exp''
let unit = Operand.ConstantOperand $ Constant.Null (Type.ptr Type.i1)
return (symbolTable, unit, Nothing)
Expand Down Expand Up @@ -1815,6 +1815,7 @@ generateExp env symbolTable exp = case exp of
field <- generateExp env { isLast = False } symbolTable value
fields <- retrieveArgs [Core.getMetadata value] [field]

-- TODO: use alloca instead
fieldPtr <- callWithMetadata (makeDILocation env area) gcMalloc [(Operand.ConstantOperand $ sizeof' fieldType, [])]
fieldPtr' <- safeBitcast fieldPtr (Type.ptr fieldType)

Expand Down Expand Up @@ -1867,10 +1868,9 @@ generateExp env symbolTable exp = case exp of
let index = fromIntegral $ Maybe.fromMaybe 0 (List.elemIndex fieldName (Map.keys fields))
fieldsOperand <- gep recordOperand' [i32ConstOp 0, i32ConstOp 1] -- i8**
fieldsOperand' <- load fieldsOperand 0 -- i8*
fieldsOperand'' <- safeBitcast fieldsOperand' (Type.ptr (Type.ptr fieldType))
fieldsOperand'' <- safeBitcast fieldsOperand' (Type.ptr fieldType)
field <- gep fieldsOperand'' [i32ConstOp index]
field' <- load field 0
value <- gep field' [i32ConstOp 0, i32ConstOp 1]
value <- gep field [i32ConstOp 0, i32ConstOp 1]
load value 0

_ -> do
Expand Down
5 changes: 2 additions & 3 deletions compiler/main/Run/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,14 @@ runModule target input args watchMode = do
, optParseOnly = False
}

let args' = map (("\"" <>) . (<> "\"")) args
let runner = \_ ->
if target == TNode then do
(_, _, _, handle) <-
createProcess (proc "node" (jsOutputPath : args'))
createProcess (proc "node" (jsOutputPath : args))
return handle
else do
(_, _, _, handle) <-
createProcess (proc llvmOutputPath args')
createProcess (proc llvmOutputPath args)
return handle

state <- Driver.initialState
Expand Down
2 changes: 1 addition & 1 deletion madlib.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 2.0
-- see: https://github.com/sol/hpack

name: madlib
version: 0.23.10
version: 0.23.11
description: Please see the README on GitHub at <https://github.com/madlib-lang/madlib#readme>
homepage: https://github.com/madlib-lang/madlib#readme
bug-reports: https://github.com/madlib-lang/madlib/issues
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: madlib
version: 0.23.10
version: 0.23.11
github: "madlib-lang/madlib"
license: BSD3
author: "Arnaud Boeglin, Brekk Bockrath"
Expand Down
2 changes: 1 addition & 1 deletion pkg/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@madlib-lang/madlib",
"version": "0.23.10",
"version": "0.23.11",
"main": "./src/run.js",
"bin": {
"madlib": "src/run.js"
Expand Down
89 changes: 48 additions & 41 deletions runtime/src/date.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
#include "date.hpp"

#include <gc.h>
#include <sys/time.h>
#include <ctime>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>

#include <ctime>

#ifdef __MINGW32__
#define timegm _mkgmtime
#define timegm _mkgmtime
#endif


#ifdef __cplusplus
extern "C" {
#endif

int64_t madlib__date__now() {
struct timeval time_now{};
struct timeval time_now {};
gettimeofday(&time_now, nullptr);
time_t msecs_time = (time_now.tv_sec * 1000) + (time_now.tv_usec / 1000);
return msecs_time;
}


// YYYY-MM-DDTHH:mm:ss.sssZ
char *madlib__date__toISOString(int64_t epochMilliseconds) {
time_t epochSeconds = trunc(epochMilliseconds / 1000);
Expand All @@ -37,13 +36,12 @@ char *madlib__date__toISOString(int64_t epochMilliseconds) {
int month = timeInfo.tm_mon + 1;
int year = timeInfo.tm_year + 1900;

char *result = (char*) GC_MALLOC(25);
char *result = (char *)GC_MALLOC(25);
sprintf(result, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", year, month, day, hours, minutes, seconds, milliseconds);

return result;
}


madlib__record__Record_t *madlib__date__toDateInfo(int64_t epochMilliseconds) {
time_t epochSeconds = trunc(epochMilliseconds / 1000);
struct tm timeInfo = *gmtime(&epochSeconds);
Expand All @@ -56,37 +54,45 @@ madlib__record__Record_t *madlib__date__toDateInfo(int64_t epochMilliseconds) {
int64_t month = timeInfo.tm_mon + 1;
int64_t year = timeInfo.tm_year + 1900;

madlib__record__Field_t *millisecondField = (madlib__record__Field_t *) GC_MALLOC(sizeof(madlib__record__Field_t));
millisecondField->name = (char*) "milliseconds";
millisecondField->value = (int64_t*) milliseconds;
madlib__record__Field_t millisecondField = {
.name = (char *)"milliseconds",
.value = (int64_t *)milliseconds,
};

madlib__record__Field_t secondsField = {
.name = (char *)"seconds",
.value = (int64_t *)seconds,
};

madlib__record__Field_t *secondsField = (madlib__record__Field_t *) GC_MALLOC(sizeof(madlib__record__Field_t));
secondsField->name = (char*) "seconds";
secondsField->value = (int64_t*) seconds;
madlib__record__Field_t minutesField = {
.name = (char *)"minutes",
.value = (int64_t *)minutes,
};

madlib__record__Field_t *minutesField = (madlib__record__Field_t *) GC_MALLOC(sizeof(madlib__record__Field_t));
minutesField->name = (char*) "minutes";
minutesField->value = (int64_t*) minutes;
madlib__record__Field_t hoursField = {

madlib__record__Field_t *hoursField = (madlib__record__Field_t *) GC_MALLOC(sizeof(madlib__record__Field_t));
hoursField->name = (char*) "hours";
hoursField->value = (int64_t*) hours;
.name = (char *)"hours",
.value = (int64_t *)hours,
};

madlib__record__Field_t *dayField = (madlib__record__Field_t *) GC_MALLOC(sizeof(madlib__record__Field_t));
dayField->name = (char*) "day";
dayField->value = (int64_t*) day;
madlib__record__Field_t dayField = {
.name = (char *)"day",
.value = (int64_t *)day,
};

madlib__record__Field_t *monthField = (madlib__record__Field_t *) GC_MALLOC(sizeof(madlib__record__Field_t));
monthField->name = (char*) "month";
monthField->value = (int64_t*) month;
madlib__record__Field_t monthField = {
.name = (char *)"month",
.value = (int64_t *)month,
};

madlib__record__Field_t *yearField = (madlib__record__Field_t *) GC_MALLOC(sizeof(madlib__record__Field_t));
yearField->name = (char*) "year";
yearField->value = (int64_t*) year;
madlib__record__Field_t yearField = {
.name = (char *)"year",
.value = (int64_t *)year,
};

madlib__record__Record_t *result = (madlib__record__Record_t*) GC_MALLOC(sizeof(madlib__record__Record_t));
madlib__record__Record_t *result = (madlib__record__Record_t *)GC_MALLOC(sizeof(madlib__record__Record_t));
result->fieldCount = 7;
result->fields = (madlib__record__Field_t**) GC_MALLOC(sizeof(madlib__record__Field_t*) * 7);
result->fields = (madlib__record__Field_t *)GC_MALLOC(sizeof(madlib__record__Field_t) * 7);
result->fields[0] = dayField;
result->fields[1] = hoursField;
result->fields[2] = millisecondField;
Expand All @@ -99,15 +105,16 @@ madlib__record__Record_t *madlib__date__toDateInfo(int64_t epochMilliseconds) {
}

int64_t madlib__date__fromDateInfo(madlib__record__Record_t *dateInfo) {
int64_t year = (int64_t) madlib__record__internal__selectField((char*)"year", dateInfo);
int64_t month = (int64_t) madlib__record__internal__selectField((char*)"month", dateInfo);
int64_t day = (int64_t) madlib__record__internal__selectField((char*)"day", dateInfo);
int64_t hours = (int64_t) madlib__record__internal__selectField((char*)"hours", dateInfo);
int64_t minutes = (int64_t) madlib__record__internal__selectField((char*)"minutes", dateInfo);
int64_t seconds = (int64_t) madlib__record__internal__selectField((char*)"seconds", dateInfo);
int64_t milliseconds = (int64_t) madlib__record__internal__selectField((char*)"milliseconds", dateInfo);

struct tm timeInfo = { (int) seconds, (int) minutes, (int) hours, (int) day, (int) month - 1, (int) year - 1900 };
// TODO: rewrite this without selectField
int64_t year = (int64_t)madlib__record__internal__selectField((char *)"year", dateInfo);
int64_t month = (int64_t)madlib__record__internal__selectField((char *)"month", dateInfo);
int64_t day = (int64_t)madlib__record__internal__selectField((char *)"day", dateInfo);
int64_t hours = (int64_t)madlib__record__internal__selectField((char *)"hours", dateInfo);
int64_t minutes = (int64_t)madlib__record__internal__selectField((char *)"minutes", dateInfo);
int64_t seconds = (int64_t)madlib__record__internal__selectField((char *)"seconds", dateInfo);
int64_t milliseconds = (int64_t)madlib__record__internal__selectField((char *)"milliseconds", dateInfo);

struct tm timeInfo = {(int)seconds, (int)minutes, (int)hours, (int)day, (int)month - 1, (int)year - 1900};

time_t rawTime = timegm(&timeInfo);
return rawTime * 1000 + milliseconds;
Expand Down
38 changes: 21 additions & 17 deletions runtime/src/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,26 @@ const char *methodToString(madlib__http__Method_t *method) {


madlib__record__Record_t *buildResponse(void *boxedBody, madlib__list__Node_t *boxedHeaders, int64_t *boxedStatus) {
madlib__record__Field_t *bodyField = (madlib__record__Field_t *)GC_MALLOC(sizeof(madlib__record__Field_t));
madlib__record__Field_t *headerField = (madlib__record__Field_t *)GC_MALLOC(sizeof(madlib__record__Field_t));
madlib__record__Field_t *statusField = (madlib__record__Field_t *)GC_MALLOC(sizeof(madlib__record__Field_t));

bodyField->name = (char *)GC_MALLOC_ATOMIC(sizeof(char) * 5);
strcpy(bodyField->name, "body");
bodyField->value = boxedBody;

headerField->name = (char *)GC_MALLOC_ATOMIC(sizeof(char) * 8);
strcpy(headerField->name, "headers");
headerField->value = boxedHeaders;

statusField->name = (char *)GC_MALLOC_ATOMIC(sizeof(char) * 7);
strcpy(statusField->name, "status");
statusField->value = boxedStatus;

return madlib__record__internal__buildRecord(3, NULL, bodyField, headerField, statusField);
madlib__record__Record_t *response = (madlib__record__Record_t *) GC_MALLOC(sizeof(madlib__record__Record_t));
response->fieldCount = 3;
response->fields = (madlib__record__Field_t*) GC_MALLOC(sizeof(madlib__record__Field_t) * 3);

response->fields[0] = {
.name = "body",
.value = boxedBody,
};

response->fields[1] = {
.name = "headers",
.value = boxedHeaders,
};

response->fields[2] = {
.name = "status",
.value = boxedStatus,
};

return response;
}

void callCallback(RequestData_t *requestData, CURLcode curlCode) {
Expand Down Expand Up @@ -409,6 +412,7 @@ curl_slist *buildLibCurlHeaders(madlib__list__Node_t *headers) {
RequestData_t *makeRequest(madlib__record__Record_t *request, PAP_t *badCallback, PAP_t *goodCallback, bool asBytes) {
char *url = (char *)madlib__record__internal__selectField((char *)"url", request);

// TODO: rewrite these without selectField
madlib__http__Method_t *boxedMethod =
(madlib__http__Method_t *)madlib__record__internal__selectField((char *)"method", request);
const char *methodString = methodToString(boxedMethod);
Expand Down
57 changes: 29 additions & 28 deletions runtime/src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,46 @@ madlib__list__Node_t *madlib__network__readNetworkInterfaces() {

while (i--) {
madlib__record__Record_t *networkInterface = (madlib__record__Record_t*) GC_MALLOC(sizeof(madlib__record__Record_t));
madlib__record__Field_t *ipv4Field = (madlib__record__Field_t *) GC_MALLOC(sizeof(madlib__record__Field_t));
ipv4Field->name = (char *) "ipv4";
madlib__record__Field_t *ipv4MaskField = (madlib__record__Field_t *) GC_MALLOC(sizeof(madlib__record__Field_t));
ipv4MaskField->name = (char *) "ipv4Mask";
madlib__record__Field_t *ipv6Field = (madlib__record__Field_t *) GC_MALLOC(sizeof(madlib__record__Field_t));
ipv6Field->name = (char *) "ipv6";
madlib__record__Field_t *ipv6MaskField = (madlib__record__Field_t *) GC_MALLOC(sizeof(madlib__record__Field_t));
ipv6MaskField->name = (char *) "ipv6Mask";
madlib__record__Field_t *isInternalField = (madlib__record__Field_t *) GC_MALLOC(sizeof(madlib__record__Field_t));
isInternalField->name = (char *) "isInternal";
madlib__record__Field_t *nameField = (madlib__record__Field_t *) GC_MALLOC(sizeof(madlib__record__Field_t));
nameField->name = (char *) "name";

networkInterface->fieldCount = 6;
networkInterface->fields = (madlib__record__Field_t**) GC_MALLOC(sizeof(madlib__record__Field_t*) * 6);
networkInterface->fields[0] = ipv4Field;
networkInterface->fields[1] = ipv4MaskField;
networkInterface->fields[2] = ipv6Field;
networkInterface->fields[3] = ipv6MaskField;
networkInterface->fields[4] = isInternalField;
networkInterface->fields[5] = nameField;

uv_interface_address_t interface_a = info[i];
networkInterface->fields = (madlib__record__Field_t*) GC_MALLOC(sizeof(madlib__record__Field_t) * 6);

nameField->value = copyString(interface_a.name);
isInternalField->value = (void*) interface_a.is_internal;
uv_interface_address_t interface_a = info[i];

madlib__maybe__Maybe_t *ipv4Maybe = (madlib__maybe__Maybe_t*) GC_MALLOC(sizeof(madlib__maybe__Maybe_t));
ipv4Maybe->index = madlib__maybe__Maybe_NOTHING_INDEX;
ipv4Field->value = ipv4Maybe;
networkInterface->fields[0] = {
.name = "ipv4",
.value = ipv4Maybe,
};

madlib__maybe__Maybe_t *ipv4MaskMaybe = (madlib__maybe__Maybe_t*) GC_MALLOC(sizeof(madlib__maybe__Maybe_t));
ipv4MaskMaybe->index = madlib__maybe__Maybe_NOTHING_INDEX;
ipv4MaskField->value = ipv4MaskMaybe;
networkInterface->fields[1] = {
.name = "ipv4Mask",
.value = ipv4MaskMaybe,
};

madlib__maybe__Maybe_t *ipv6Maybe = (madlib__maybe__Maybe_t*) GC_MALLOC(sizeof(madlib__maybe__Maybe_t));
ipv6Maybe->index = madlib__maybe__Maybe_NOTHING_INDEX;
ipv6Field->value = ipv6Maybe;
networkInterface->fields[2] = {
.name = "ipv6",
.value = ipv6Maybe,
};

madlib__maybe__Maybe_t *ipv6MaskMaybe = (madlib__maybe__Maybe_t*) GC_MALLOC(sizeof(madlib__maybe__Maybe_t));
ipv6MaskMaybe->index = madlib__maybe__Maybe_NOTHING_INDEX;
ipv6MaskField->value = ipv6MaskMaybe;
networkInterface->fields[3] = {
.name = "ipv6Mask",
.value = ipv6MaskMaybe,
};
networkInterface->fields[4] = {
.name = "isInternal",
.value = (void*) interface_a.is_internal,
};
networkInterface->fields[5] = {
.name = "name",
.value = copyString(interface_a.name),
};

if (interface_a.address.address4.sin_family == AF_INET) {
uv_ip4_name(&interface_a.address.address4, buf, sizeof(buf));
Expand Down
Loading

0 comments on commit 34d7860

Please sign in to comment.