Skip to content

Commit 5033b93

Browse files
committed
Got more tests working with new binder.
1 parent 2a1e57a commit 5033b93

26 files changed

+82
-53
lines changed

TODO

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Add LLVM compiler hints and optmizations as described at
3737
Support named parameters, like Python
3838

3939
## Medium Term
40+
Write tests to verify that constant time processing of secrets is achieved. E.g. if (issecret(foo)...
4041
Add support for fixed-sized arrays for improved speed.
4142
Make modular inverse constant time.
4243
Upgrade from lineNum to an object specifying the text of the line, position, and file name.

database/util.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ char *deBytesToHex(void *bytes, uint32 len, bool littleEndian) {
509509
// Determine if the identifier conforms to: [-a-zA-Z$._][-a-zA-Z$._0-9]*
510510
bool deIsLegalIdentifier(char *identifier) {
511511
uint8 *p = (uint8*)identifier;
512-
uint8 len = strlen(identifier);
512+
uint8 *end = p + strlen(identifier);
513513
uint8 c = *p++;
514514
if (!isalpha(c) && c != '_' && c != '$' && c < 0xc0) {
515515
return false;
@@ -518,7 +518,7 @@ bool deIsLegalIdentifier(char *identifier) {
518518
do {
519519
c = *p++;
520520
} while (c >= 0x80 && c <= 0xbf);
521-
for (uint32 i = 1; i < len; i++) {
521+
while (p < end) {
522522
if (!isalnum(c) && !isdigit(c) && c != '_' && c != '$' && c < 0xc0) {
523523
return false;
524524
}

include/de.h

+13-17
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,28 @@ static inline deVariable deBlockIndexVariable(deBlock block, uint32 index) {
110110
utExit("Indexed past end of block variables");
111111
return deVariableNull;
112112
}
113-
static inline uint32 deBlockFindVariableIndex(deBlock block, deVariable variable) {
113+
static inline uint32 deBlockFindVariableIndex(deBlock block,
114+
deVariable variable) {
114115
deVariable var;
115116
uint32 i = 0;
116117
deForeachBlockVariable(block, var) {
117118
if (var == variable) {
118119
return i;
119120
}
120121
i++;
121-
} deEndBlockVariable;
122+
}
123+
deEndBlockVariable;
122124
utExit("Varaible not found on block");
123125
return 0; // Dummy return.
124126
}
125127

126128
// Function methods.
127129
deFunction deFunctionCreate(deFilepath filepath, deBlock block,
128-
deFunctionType type, utSym name, deLinkage linkage,
129-
deLine line);
130+
deFunctionType type, utSym name, deLinkage linkage, deLine line);
130131
deFunction deIteratorFunctionCreate(deBlock block, utSym name, utSym selfName,
131-
deLinkage linkage, deLine line);
132+
deLinkage linkage, deLine line);
132133
deFunction deOperatorFunctionCreate(deBlock block, deExpressionType opType,
133-
deLine line);
134+
deLine line);
134135
char *deGetFunctionTypeName(deFunctionType type);
135136
void deDumpFunction(deFunction function);
136137
void deDumpFunctionStr(deString string, deFunction function);
@@ -192,8 +193,7 @@ static inline deBlock deGeneratorGetSubBlock(deGenerator generator) {
192193

193194
// Variable methods.
194195
deVariable deVariableCreate(deBlock block, deVariableType type, bool isConst,
195-
utSym name, deExpression initializer,
196-
bool generated, deLine line);
196+
utSym name, deExpression initializer, bool generated, deLine line);
197197
deVariable deCopyVariable(deVariable variable, deBlock destBlock);
198198
void deDumpVariable(deVariable variable);
199199
void deDumpVariableStr(deString str, deVariable variable);
@@ -227,10 +227,8 @@ deExpression deStringExpressionCreate(deString, deLine line);
227227
deExpression deCStringExpressionCreate(char *text, deLine line);
228228
deExpression deBoolExpressionCreate(bool value, deLine line);
229229
deExpression deBinaryExpressionCreate(deExpressionType type,
230-
deExpression leftExpr,
231-
deExpression rightExpr, deLine line);
232-
deExpression deUnaryExpressionCreate(deExpressionType type, deExpression expr,
233-
deLine line);
230+
deExpression leftExpr, deExpression rightExpr, deLine line);
231+
deExpression deUnaryExpressionCreate(deExpressionType type, deExpression expr, deLine line);
234232
void deDumpExpression(deExpression expression);
235233
void deDumpExpressionStr(deString string, deExpression expression);
236234
deStatement deFindExpressionStatement(deExpression expression);
@@ -332,8 +330,7 @@ deDatatype deNullDatatypeCreate(deTclass tclass);
332330
deDatatype deFunctionDatatypeCreate(deFunction function);
333331
deDatatype deFuncptrDatatypeCreate(deDatatype returnType, deDatatypeArray parameterTypes);
334332
deDatatype deTupleDatatypeCreate(deDatatypeArray types);
335-
deDatatype deStructDatatypeCreate(deFunction structFunction,
336-
deDatatypeArray types, deLine line);
333+
deDatatype deStructDatatypeCreate(deFunction structFunction, deDatatypeArray types, deLine line);
337334
deDatatype deGetStructTupleDatatype(deDatatype structDatatype);
338335
deDatatype deEnumClassDatatypeCreate(deFunction enumFunction);
339336
deDatatype deEnumDatatypeCreate(deFunction enumFunction);
@@ -374,7 +371,7 @@ static inline bool deDatatypeIsNumber(deDatatype datatype) {
374371
// Signature, SignatureBin, and Paramspec methods.
375372
deSignature deLookupSignature(deFunction function, deDatatypeArray parameterTypes);
376373
deSignature deSignatureCreate(deFunction function,
377-
deDatatypeArray parameterTypes, deLine line);
374+
deDatatypeArray parameterTypes, deLine line);
378375
deSignature deResolveConstructorSignature(deSignature signature);
379376
bool deSignatureIsConstructor(deSignature signature);
380377
bool deSignatureIsMethod(deSignature signature);
@@ -517,8 +514,7 @@ bool deIsLegalIdentifier(char *identifier);
517514
char *deSnakeCase(char *camelCase);
518515
char *deUpperSnakeCase(char *camelCase);
519516
void deGenerateDummyLLFileAndExit(void);
520-
char *deGetOldVsNewDatatypeStrings(deDatatype oldDatatype,
521-
deDatatype newDatatype);
517+
char *deGetOldVsNewDatatypeStrings(deDatatype oldDatatype, deDatatype newDatatype);
522518
static inline uint32 deBitsToBytes(uint32 bits) {
523519
return (bits + 7) / 8;
524520
}

llvm/genllvm.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -2327,9 +2327,9 @@ static void generateCallExpression(deExpression expression) {
23272327
deSignature signature = deExpressionGetSignature(expression);
23282328
deDatatype returnType = deExpressionGetDatatype(expression);
23292329
uint32 savedStackPos = llStackPos;
2330+
bool isMethodCall = deExpressionIsMethodCall(accessExpression);
23302331
if (signature != deSignatureNull) {
2331-
evaluateParameters(signature, deDatatypeNull, parameters,
2332-
deExpressionIsMethodCall(accessExpression));
2332+
evaluateParameters(signature, deDatatypeNull, parameters, isMethodCall);
23332333
} else {
23342334
evaluateIndirectCallParameters(parameters);
23352335
}
@@ -2339,9 +2339,13 @@ static void generateCallExpression(deExpression expression) {
23392339
deDatatypeType accessType = deDatatypeGetType(accessDatatype);
23402340
if (llElementIsDelegate(element)) {
23412341
// If this is a delegate, the self expression is still on the stack, and
2342-
// needs to be derefed
2343-
llElement *selfElement = topOfStack();
2344-
derefElement(selfElement);
2342+
// needs to be derefed or popped if not used.
2343+
if (!deSignatureParamInstantiated(signature, 0)) {
2344+
popElement(false);
2345+
} else {
2346+
llElement *selfElement = topOfStack();
2347+
derefElement(selfElement);
2348+
}
23452349
}
23462350
llElement returnElement;
23472351
bool returnsValuePassedByReference = llDatatypePassedByReference(returnType);

tests/exteuc.rn newtests/exteuc.rn

File renamed without changes.

newtests/exteuc.stdout

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[5i64, 1i64, -5i64]
2+
true
3+
[1i64, 1i64, 0i64]
4+
[1i64, 34i64, -9i64]
File renamed without changes.
File renamed without changes.

tests/gf2.rn newtests/gf2.rn

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/rebind.rn newtests/rebind.rn

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/tuple.rn newtests/tuple.rn

File renamed without changes.

newtests/tuple.stdout

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
23 34

tests/Γ.rn newtests/Γ.rn

File renamed without changes.

newtests/Γ.stdout

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3628800

prettyprint.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
#!/bin/bash
2+
# Copyright 2022 Google LLC.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
116
import gdb.printing
217

318

src/bind2.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ static bool instantiateSubExpressions(deExpressionType type) {
185185
case DE_EXPR_FUNCADDR:
186186
case DE_EXPR_ARRAYOF:
187187
case DE_EXPR_TYPEOF:
188-
case DE_EXPR_UNSIGNED:
189-
case DE_EXPR_SIGNED:
190188
case DE_EXPR_WIDTHOF:
191189
return false;
192190
default:
@@ -234,6 +232,14 @@ static void postProcessDotExpression(deBinding binding) {
234232
deStateBindingRemoveBinding(deBindingGetStateBinding(binding), identBinding);
235233
}
236234

235+
// Remove the identifier to the left of the named parameter expression from the
236+
// binding queue. The handler for binding named parameter expressions must bind
237+
// this once the right hand side is bound.
238+
static void postProcessNamedParameterExpression(deBinding binding) {
239+
deBinding identBinding = deBindingGetFirstBinding(binding);
240+
deStateBindingRemoveBinding(deBindingGetStateBinding(binding), identBinding);
241+
}
242+
237243
// Queue the expression for binding.
238244
deBinding deQueueExpression(deSignature scopeSig, deStateBinding statebinding,
239245
deBinding owningBinding, deExpression expression, bool instantiating) {
@@ -253,6 +259,8 @@ deBinding deQueueExpression(deSignature scopeSig, deStateBinding statebinding,
253259
postProcessAssignment(binding);
254260
} else if (type == DE_EXPR_DOT) {
255261
postProcessDotExpression(binding);
262+
} else if (type == DE_EXPR_NAMEDPARAM) {
263+
postProcessNamedParameterExpression(binding);
256264
}
257265
return binding;
258266
}

src/bindexpr.c

+21-6
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,10 @@ static void checkBinaryExpression(deSignature scopeSig, deBinding binding,
8484
if (compareTypes && !typesAreEquivalent(*leftType, *rightType)) {
8585
// Try auto-cast.
8686
if (deBindingAutocast(left) && !deBindingAutocast(right)) {
87-
// TODO: Comment in when we test autocast.
88-
// coautocastExpression(left, *rightType);
87+
autocastExpression(left, *rightType);
8988
*leftType = deBindingGetDatatype(left);
9089
} else if (deBindingAutocast(right) && !deBindingAutocast(left)) {
91-
// autocastExpression(right, *leftType);
90+
autocastExpression(right, *leftType);
9291
*rightType = deBindingGetDatatype(right);
9392
}
9493
}
@@ -152,11 +151,10 @@ static void bindBinaryExpression(deSignature scopeSig, deBinding binding,
152151
if (compareTypes && !typesAreEquivalent(*leftType, *rightType)) {
153152
// Try auto-cast.
154153
if (deBindingAutocast(left) && !deBindingAutocast(right)) {
155-
// TODO: Comment in when we test autocast.
156-
// autocastExpression(left, *rightType);
154+
autocastExpression(left, *rightType);
157155
*leftType = deBindingGetDatatype(left);
158156
} else if (deBindingAutocast(right) && !deBindingAutocast(left)) {
159-
// autocastExpression(right, *leftType);
157+
autocastExpression(right, *leftType);
160158
*rightType = deBindingGetDatatype(right);
161159
}
162160
}
@@ -1293,6 +1291,23 @@ static void bindNamedParameter(deSignature scopeSig, deBinding binding) {
12931291
deBinding right = deBindingGetLastBinding(binding);
12941292
deBindingSetDatatype(binding, deBindingGetDatatype(right));
12951293
deBindingSetIsType(binding, deBindingIsType(right));
1294+
// Find the variable in the called function so we can bind to it.
1295+
deBinding call = deBindingGetBinding(deBindingGetBinding(binding));
1296+
utAssert(deBindingGetType(call) == DE_EXPR_CALL);
1297+
deBinding callAccess = deBindingGetFirstBinding(call);
1298+
deFunction function = findCalledFunction(callAccess);
1299+
deBlock block = deFunctionGetSubBlock(function);
1300+
deBinding paramNameBinding = deBindingGetFirstBinding(binding);
1301+
utSym paramName = deExpressionGetName(deBindingGetExpression(paramNameBinding));
1302+
deIdent ident = deBlockFindIdent(block, paramName);
1303+
if (ident == deIdentNull || deIdentGetType(ident) != DE_IDENT_VARIABLE) {
1304+
error(binding, "No parameter named %s found", utSymGetName(paramName));
1305+
}
1306+
deVariable var = deIdentGetVariable(ident);
1307+
if (deVariableGetType(var) != DE_VAR_PARAMETER) {
1308+
error(binding, "Variable %s is a local variable, not a parameter", utSymGetName(paramName));
1309+
}
1310+
deIdentAppendBinding(ident, paramNameBinding);
12961311
}
12971312

12981313
// Bind the binding's expression.

tests/classheapsort.rn

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Element(self, name, age) {
2727
}
2828
}
2929

30-
relation HeapList Root Element cascade
30+
relation HeapqList Root Element cascade
3131

3232
root = Root()
3333
root.pushElement(Element("Fred", 44))

tests/classheapsort.stdout

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{name = Alice, age = 32}
2+
{name = Bob, age = 32}
3+
{name = Fred, age = 44}
4+
{name = Dave, age = 56}

tests/xorstrings.rn

-20
This file was deleted.

0 commit comments

Comments
 (0)