Skip to content
Tcl Source Code Artifact [8dd538caf7]
Login

Artifact [8dd538caf7]

Artifact 8dd538caf76390b9c301647955b37360d093ccbf7c0493aa2ad9e19ea2f3ad64:


--- a/generic/tclCompExpr.c
+++ b/generic/tclCompExpr.c
@@ -14,6 +14,7 @@
 #include "tclInt.h"
 #include "tclCompile.h"		/* CompileEnv */
 #include "tclParse.h"
+#include "tclTypes.h"		/* v18l : TclMapFuncKnown */
 
 /*
  * Expression parsing takes place in the routine ParseExpr(). It takes a
@@ -3895,3 +3896,448 @@
  * fill-column: 78
  * End:
  */
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclExprProfile -- (v18l)
+ *
+ *	expr diagnoses its own vectorisability. The expression is parsed by
+ *	ParseExpr, the same parse that compiles it, and its tree is walked in
+ *	the order of evaluation (the order CompileExprTree consumes literals,
+ *	functions and tokens). The profile says whether the expression is in
+ *	the subset the type map evaluator compiles to raw numbers, and, if
+ *	not, why; which variables it reads and writes; and, among the written
+ *	ones, which are carried from one evaluation to the next (read before
+ *	written: accumulators), which are temporaries (written before read),
+ *	and which accumulators are additive folds (s = $s + E, s read nowhere
+ *	else), the ones the block evaluator can sum by columns.
+ *
+ * Results:
+ *	A dict: pure 0|1, why TEXT, statements N, reads, writes, carried,
+ *	temps, folds, conditional (written under ?:, && or ||), funcs.
+ *
+ *----------------------------------------------------------------------
+ */
+
+typedef struct {
+    Tcl_Obj *name;
+    int readFirst;		/* first access in an evaluation: a read */
+    int written;
+    int condWrite;		/* a write under ?:, &&, || */
+    int foldWrites;		/* writes of the form name = $name + E */
+    int otherWrites;
+    int readsOutside;		/* reads not the $name of a fold write */
+} ProfVar;
+
+typedef struct {
+    OpNode *nodes;
+    Tcl_Obj *const *lit;	/* next literal */
+    Tcl_Obj *const *func;	/* next function name */
+    Tcl_Token *tok;		/* next token */
+    ProfVar *vars;
+    int nvars, maxVars;
+    const char *why;		/* first reason the expression is impure */
+    int statements;
+    int cond;			/* depth under ?:, &&, || */
+    Tcl_Obj *funcs;
+    /* the fold candidate being evaluated: its target and its $target */
+    int foldVar, foldLeaf, foldLeafSeen, foldTargetReads;
+} ExprProf;
+
+static ProfVar *
+ProfVarOf(
+    ExprProf *p,
+    const char *name,
+    Tcl_Size len)
+{
+    int i;
+
+    for (i = 0; i < p->nvars; i++) {
+	Tcl_Size l;
+	const char *s = TclGetStringFromObj(p->vars[i].name, &l);
+
+	if (l == len && memcmp(s, name, len) == 0) {
+	    return p->vars + i;
+	}
+    }
+    if (p->nvars == p->maxVars) {
+	p->maxVars = p->maxVars ? 2 * p->maxVars : 8;
+	p->vars = (ProfVar *) Tcl_Realloc(p->vars,
+		sizeof(ProfVar) * (size_t) p->maxVars);
+    }
+    memset(p->vars + p->nvars, 0, sizeof(ProfVar));
+    p->vars[p->nvars].name = Tcl_NewStringObj(name, len);
+    Tcl_IncrRefCount(p->vars[p->nvars].name);
+    return p->vars + p->nvars++;
+}
+
+static void
+ProfImpure(
+    ExprProf *p,
+    const char *why)
+{
+    if (p->why == NULL) {
+	p->why = why;
+    }
+}
+
+/* a simple scalar name: no namespace, no array element, not empty */
+static int
+ProfSimpleName(
+    const char *s,
+    Tcl_Size len)
+{
+    Tcl_Size k;
+
+    if (len == 0) {
+	return 0;
+    }
+    for (k = 0; k < len; k++) {
+	if (s[k] == ':' || s[k] == '(' || s[k] == ')') {
+	    return 0;
+	}
+    }
+    return 1;
+}
+
+static void ProfWalk(ExprProf *p, int index);
+
+/* an operand that is not a node: a literal, tokens, or nothing */
+static void
+ProfLeaf(
+    ExprProf *p,
+    int kind)
+{
+    switch (kind) {
+    case OT_EMPTY:
+	return;
+    case OT_LITERAL: {
+	Tcl_Obj *lit = *p->lit++;
+	void *num;
+	int type;
+
+	if (Tcl_GetNumberFromObj(NULL, lit, &num, &type) != TCL_OK
+		|| type == TCL_NUMBER_BIG || type == TCL_NUMBER_NAN) {
+	    ProfImpure(p, "a literal that is not a number");
+	}
+	return;
+    }
+    case OT_TOKENS: {
+	Tcl_Token *t = p->tok, *w = p->tok;
+
+	p->tok += t->numComponents + 1;
+	if (w->type == TCL_TOKEN_WORD && w->numComponents >= 1) {
+	    /* an operand: a word whose components say what it is; $x is a
+	     * word of one variable token (and its name) */
+	    t = w + 1;
+	    if (t->numComponents + 1 != w->numComponents) {
+		ProfImpure(p, "a string operand");
+		if (p->foldVar >= 0) {
+		    p->foldLeafSeen = 1;
+		}
+		return;
+	    }
+	}
+	if (t->type == TCL_TOKEN_VARIABLE && t->numComponents == 1
+		&& ProfSimpleName(t[1].start, t[1].size)) {
+	    ProfVar *v = ProfVarOf(p, t[1].start, t[1].size);
+	    int isFoldLeaf = 0;
+
+	    if (!v->written) {
+		v->readFirst = 1;
+	    }
+	    if (p->foldVar >= 0 && v == p->vars + p->foldVar) {
+		p->foldTargetReads++;
+		if (!p->foldLeafSeen) {
+		    /* the first leaf read in "s = $s + E" */
+		    isFoldLeaf = 1;
+		    p->foldLeafSeen = 1;
+		}
+	    } else if (p->foldVar >= 0) {
+		p->foldLeafSeen = 1;
+	    }
+	    if (!isFoldLeaf) {
+		v->readsOutside++;
+	    }
+	    return;
+	}
+	if (p->foldVar >= 0) {
+	    p->foldLeafSeen = 1;
+	}
+	switch (t->type) {
+	case TCL_TOKEN_VARIABLE:
+	    ProfImpure(p, "a variable that is not a simple scalar");
+	    break;
+	case TCL_TOKEN_COMMAND:
+	    ProfImpure(p, "a command substitution [...]");
+	    break;
+	case TCL_TOKEN_CMD_IN_EXPR:
+	    ProfImpure(p, "a command in the expression");
+	    break;
+	case TCL_TOKEN_SUB_EXPR:
+	    ProfImpure(p, "a nested subexpression");
+	    break;
+	default:
+	    ProfImpure(p, "a string operand");
+	    break;
+	}
+	return;
+    }
+    default:
+	ProfWalk(p, kind);
+    }
+}
+
+static void
+ProfWalk(
+    ExprProf *p,
+    int index)
+{
+    OpNode *n = p->nodes + index;
+    int lexeme = n->lexeme;
+
+    switch (lexeme) {
+    case START: case OPEN_PAREN: case NULL_FUNC: case UNARY_PLUS:
+    case UNARY_MINUS:
+    case NOT: case BINARY_PLUS: case BINARY_MINUS: case MULT: case DIVIDE:
+    case MOD: case EXPON: case LESS: case GREATER: case LEQ: case GEQ:
+    case EQUAL: case NEQ: case COMMA:
+	break;
+    case SEPARATOR:
+	p->statements++;
+	break;
+    case QUESTION: case COLON: case AND: case OR:
+	/* the right operand is evaluated only sometimes */
+	ProfLeaf(p, n->left);
+	p->cond++;
+	ProfLeaf(p, n->right);
+	p->cond--;
+	return;
+    case FUNCTION: {
+	Tcl_Size len;
+	const char *name = TclGetStringFromObj(*p->func, &len);
+
+	p->func++;
+	if (!TclMapFuncKnown(name, len)) {
+	    ProfImpure(p, "a function outside the compiled set");
+	}
+	Tcl_ListObjAppendElement(NULL, p->funcs,
+		Tcl_NewStringObj(name, len));
+	ProfLeaf(p, n->right);
+	return;
+    }
+    case ASSIGN: {
+	Tcl_Obj *nameObj;
+	const char *name;
+	Tcl_Size len;
+	int rhs = n->right, savedVar, savedLeaf, savedSeen, savedReads;
+	int fold, vi;
+	ProfVar *v;
+
+	if (n->left != OT_LITERAL) {
+	    ProfImpure(p, "an assignment to a tuple or a declaration");
+	    ProfLeaf(p, n->left);
+	    ProfLeaf(p, n->right);
+	    return;
+	}
+	nameObj = *p->lit++;
+	name = TclGetStringFromObj(nameObj, &len);
+	if (!ProfSimpleName(name, len)) {
+	    ProfImpure(p, "an assignment to a name that is not a scalar");
+	    ProfLeaf(p, rhs);
+	    return;
+	}
+	v = ProfVarOf(p, name, len);
+	vi = (int) (v - p->vars);
+	/* "s = $s + E": the right operand is BINARY_PLUS whose left
+	 * operand is the first leaf read, $s */
+	savedVar = p->foldVar;
+	savedLeaf = p->foldLeaf;
+	savedSeen = p->foldLeafSeen;
+	savedReads = p->foldTargetReads;
+	{
+	    int r = rhs;
+
+	    /* s = ($s + E) : des parenthèses autour */
+	    while (r >= 0 && (p->nodes[r].lexeme == OPEN_PAREN
+		    || p->nodes[r].lexeme == NULL_FUNC)) {
+		r = p->nodes[r].right;
+	    }
+	    fold = (r >= 0 && p->nodes[r].lexeme == BINARY_PLUS
+		    && p->nodes[r].left == OT_TOKENS);
+	}
+	p->foldVar = fold ? vi : -1;
+	p->foldLeafSeen = 0;
+	p->foldTargetReads = 0;
+	ProfLeaf(p, rhs);
+	v = p->vars + vi;		/* (the table may have grown) */
+	if (fold && p->foldTargetReads == 1 && v->readsOutside == 0
+		/* the one read of s was the fold leaf */) {
+	    v->foldWrites++;
+	} else {
+	    if (fold && p->foldTargetReads >= 1) {
+		/* $s read as the fold leaf, and again: not a fold */
+		v->readsOutside++;
+	    }
+	    v->otherWrites++;
+	}
+	v->written = 1;
+	if (p->cond > 0) {
+	    v->condWrite = 1;
+	}
+	p->foldVar = savedVar;
+	p->foldLeaf = savedLeaf;
+	p->foldLeafSeen = savedSeen;
+	p->foldTargetReads = savedReads;
+	return;
+    }
+    case IS_TYPE:
+	ProfImpure(p, "a type test (is)");
+	break;
+    case LIST:			/* (NULL_FUNC seul : des parenthèses) */
+	ProfImpure(p, "a list or a tuple");
+	break;
+    case STREQ: case STRNEQ: case STR_LT: case STR_GT: case STR_LEQ:
+    case STR_GEQ: case IN_LIST: case NOT_IN_LIST:
+	ProfImpure(p, "a string or list operator");
+	break;
+    default:
+	ProfImpure(p, "an operator outside the compiled set");
+	break;
+    }
+    if (n->mark == MARK_LEFT) {
+	ProfLeaf(p, n->left);
+    }
+    ProfLeaf(p, n->right);
+}
+
+int
+TclExprProfile(
+    Tcl_Interp *interp,
+    const char *script,
+    Tcl_Size numBytes,
+    Tcl_Obj **profilePtr)
+{
+    OpNode *opTree = NULL;
+    Tcl_Size numBytesIn = numBytes;
+    Tcl_Obj *litList, *funcList, *d, *reads, *writes, *carried, *temps;
+    Tcl_Obj *folds, *conds;
+    Tcl_Parse *parsePtr = (Tcl_Parse *)
+	    TclStackAlloc(interp, sizeof(Tcl_Parse));
+    ExprProf p;
+    int code, i;
+
+    TclNewObj(litList);
+    TclNewObj(funcList);
+    Tcl_IncrRefCount(litList);
+    Tcl_IncrRefCount(funcList);
+    code = ParseExpr(interp, script, numBytes, &opTree, litList, funcList,
+	    parsePtr, 0 /* parseOnly */);
+    if (code != TCL_OK) {
+	goto done;
+    }
+    memset(&p, 0, sizeof(p));
+    p.nodes = opTree;
+    TclListObjGetElements(NULL, litList, &numBytes, (Tcl_Obj ***) &p.lit);
+    TclListObjGetElements(NULL, funcList, &numBytes, (Tcl_Obj ***) &p.func);
+    p.tok = parsePtr->tokenPtr;
+    p.foldVar = -1;
+    p.statements = 1;
+    p.funcs = Tcl_NewListObj(0, NULL);
+    ProfWalk(&p, 0);
+
+    TclNewObj(d);
+    reads = Tcl_NewListObj(0, NULL);
+    writes = Tcl_NewListObj(0, NULL);
+    carried = Tcl_NewListObj(0, NULL);
+    temps = Tcl_NewListObj(0, NULL);
+    folds = Tcl_NewListObj(0, NULL);
+    conds = Tcl_NewListObj(0, NULL);
+    for (i = 0; i < p.nvars; i++) {
+	ProfVar *v = p.vars + i;
+
+	if (!v->written) {
+	    Tcl_ListObjAppendElement(NULL, reads, v->name);
+	    continue;
+	}
+	Tcl_ListObjAppendElement(NULL, writes, v->name);
+	if (v->readFirst || v->condWrite) {
+	    /* its value goes from one evaluation to the next (a write
+	     * under ?: may not happen: the old value stays) */
+	    Tcl_ListObjAppendElement(NULL, carried, v->name);
+	    if (v->foldWrites > 0 && v->otherWrites == 0
+		    && v->readsOutside == 0 && !v->condWrite) {
+		Tcl_ListObjAppendElement(NULL, folds, v->name);
+	    }
+	} else {
+	    Tcl_ListObjAppendElement(NULL, temps, v->name);
+	}
+	if (v->condWrite) {
+	    Tcl_ListObjAppendElement(NULL, conds, v->name);
+	}
+    }
+    TclDictPut(NULL, d, "pure", Tcl_NewBooleanObj(p.why == NULL));
+    TclDictPut(NULL, d, "why", Tcl_NewStringObj(p.why ? p.why : "", -1));
+    TclDictPut(NULL, d, "statements", Tcl_NewWideIntObj(p.statements));
+    TclDictPut(NULL, d, "reads", reads);
+    TclDictPut(NULL, d, "writes", writes);
+    TclDictPut(NULL, d, "carried", carried);
+    TclDictPut(NULL, d, "temps", temps);
+    TclDictPut(NULL, d, "folds", folds);
+    TclDictPut(NULL, d, "conditional", conds);
+    TclDictPut(NULL, d, "funcs", p.funcs);
+    if (p.statements == 1 && writes != NULL) {
+	Tcl_Size nw;
+	Tcl_Obj *txt = Tcl_NewStringObj(script, numBytesIn);
+
+	/* the verdict of type map's own parser, for comparison (it knows
+	 * neither assignments nor sequences) */
+	Tcl_ListObjLength(NULL, writes, &nw);
+	Tcl_IncrRefCount(txt);
+	TclDictPut(NULL, d, "mapexpr", nw > 0 ? Tcl_NewStringObj("-", 1)
+		: Tcl_NewBooleanObj(TclMapExprPure(Tcl_GetString(txt))));
+	Tcl_DecrRefCount(txt);
+    }
+    for (i = 0; i < p.nvars; i++) {
+	Tcl_DecrRefCount(p.vars[i].name);
+    }
+    if (p.vars != NULL) {
+	Tcl_Free(p.vars);
+    }
+    *profilePtr = d;
+
+  done:
+    Tcl_FreeParse(parsePtr);
+    TclStackFree(interp, parsePtr);
+    Tcl_DecrRefCount(funcList);
+    Tcl_DecrRefCount(litList);
+    if (opTree != NULL) {
+	Tcl_Free(opTree);
+    }
+    return code;
+}
+
+/* ::tcl::unsupported::exprprofile EXPR (v18l) */
+int
+TclExprProfileObjCmd(
+    TCL_UNUSED(void *),
+    Tcl_Interp *interp,
+    Tcl_Size objc,
+    Tcl_Obj *const objv[])
+{
+    Tcl_Obj *d;
+    Tcl_Size len;
+    const char *s;
+
+    if (objc != 2) {
+	Tcl_WrongNumArgs(interp, 1, objv, "expression");
+	return TCL_ERROR;
+    }
+    s = TclGetStringFromObj(objv[1], &len);
+    if (TclExprProfile(interp, s, len, &d) != TCL_OK) {
+	return TCL_ERROR;
+    }
+    Tcl_SetObjResult(interp, d);
+    return TCL_OK;
+}