2004-10-27 00:54:26 +02:00
|
|
|
#! /usr/bin/perl -w
|
|
|
|
|
2004-10-29 13:22:49 +02:00
|
|
|
# This program generates C/C++ code for efficiently manipulating
|
|
|
|
# ATerms. It generates functions to build and match ATerms according
|
|
|
|
# to a set of constructor definitions defined in a file read from
|
|
|
|
# standard input. A constructor is defined by a line with the
|
|
|
|
# following format:
|
|
|
|
#
|
|
|
|
# SYM | ARGS | TYPE | FUN?
|
|
|
|
#
|
|
|
|
# where SYM is the name of the constructor, ARGS is a
|
|
|
|
# whitespace-separated list of argument types, TYPE is the type of the
|
|
|
|
# resulting ATerm (which should be `ATerm' or a type synonym for
|
|
|
|
# `ATerm'), and the optional FUN is used to construct the names of the
|
|
|
|
# build and match functions (it defaults to SYM; overriding it is
|
|
|
|
# useful if there are overloaded constructors, e.g., with different
|
|
|
|
# arities). Note that SYM may be empty.
|
|
|
|
#
|
|
|
|
# A line of the form
|
|
|
|
#
|
|
|
|
# VAR = EXPR
|
|
|
|
#
|
|
|
|
# causes a ATerm variable to be generated that is initialised to the
|
|
|
|
# value EXPR.
|
|
|
|
#
|
|
|
|
# Finally, a line of the form
|
|
|
|
#
|
|
|
|
# init NAME
|
|
|
|
#
|
|
|
|
# causes the initialisation function to be called `NAME'. This
|
|
|
|
# function must be called before any of the build/match functions or
|
|
|
|
# the generated variables are used.
|
|
|
|
|
2004-10-27 00:54:26 +02:00
|
|
|
die if scalar @ARGV != 2;
|
|
|
|
|
|
|
|
my $syms = "";
|
|
|
|
my $init = "";
|
2004-10-29 13:22:49 +02:00
|
|
|
my $initFun = "init";
|
2004-10-27 00:54:26 +02:00
|
|
|
|
|
|
|
open HEADER, ">$ARGV[0]";
|
|
|
|
open IMPL, ">$ARGV[1]";
|
|
|
|
|
2006-09-05 10:54:48 +02:00
|
|
|
print HEADER "#include <aterm2.h>\n";
|
2006-09-04 23:06:23 +02:00
|
|
|
print HEADER "#ifdef __cplusplus\n";
|
|
|
|
print HEADER "namespace nix {\n";
|
|
|
|
print HEADER "#endif\n\n\n";
|
|
|
|
print IMPL "namespace nix {\n";
|
|
|
|
|
2004-10-27 00:54:26 +02:00
|
|
|
while (<STDIN>) {
|
2006-10-16 17:55:34 +02:00
|
|
|
s/\#.*//;
|
2004-10-27 00:54:26 +02:00
|
|
|
next if (/^\s*$/);
|
|
|
|
|
2004-10-29 13:22:49 +02:00
|
|
|
if (/^\s*(\w*)\s*\|([^\|]*)\|\s*(\w+)\s*\|\s*(\w+)?/) {
|
2004-10-27 00:54:26 +02:00
|
|
|
my $const = $1;
|
|
|
|
my @types = split ' ', $2;
|
|
|
|
my $result = $3;
|
|
|
|
my $funname = $4;
|
|
|
|
$funname = $const unless defined $funname;
|
|
|
|
|
|
|
|
my $formals = "";
|
|
|
|
my $formals2 = "";
|
|
|
|
my $args = "";
|
|
|
|
my $unpack = "";
|
|
|
|
my $n = 1;
|
|
|
|
foreach my $type (@types) {
|
* Removed the `id' attribute hack.
* Formalise the notion of fixed-output derivations, i.e., derivations
for which a cryptographic hash of the output is known in advance.
Changes to such derivations should not propagate upwards through the
dependency graph. Previously this was done by specifying the hash
component of the output path through the `id' attribute, but this is
insecure since you can lie about it (i.e., you can specify any hash
and then produce a completely different output). Now the
responsibility for checking the output is moved from the builder to
Nix itself.
A fixed-output derivation can be created by specifying the
`outputHash' and `outputHashAlgo' attributes, the latter taking
values `md5', `sha1', and `sha256', and the former specifying the
actual hash in hexadecimal or in base-32 (auto-detected by looking
at the length of the attribute value). MD5 is included for
compatibility but should be considered deprecated.
* Removed the `drvPath' pseudo-attribute in derivation results. It's
no longer necessary.
* Cleaned up the support for multiple output paths in derivation store
expressions. Each output now has a unique identifier (e.g., `out',
`devel', `docs'). Previously there was no way to tell output paths
apart at the store expression level.
* `nix-hash' now has a flag `--base32' to specify that the hash should
be printed in base-32 notation.
* `fetchurl' accepts parameters `sha256' and `sha1' in addition to
`md5'.
* `nix-prefetch-url' now prints out a SHA-1 hash in base-32. (TODO: a
flag to specify the hash.)
2005-01-17 17:55:19 +01:00
|
|
|
my $realType = $type;
|
2004-10-27 00:54:26 +02:00
|
|
|
$args .= ", ";
|
|
|
|
if ($type eq "string") {
|
|
|
|
# $args .= "(ATerm) ATmakeAppl0(ATmakeAFun((char *) e$n, 0, ATtrue))";
|
|
|
|
# $type = "const char *";
|
|
|
|
$type = "ATerm";
|
|
|
|
$args .= "e$n";
|
2004-10-29 13:22:49 +02:00
|
|
|
# !!! in the matcher, we should check that the
|
|
|
|
# argument is a string (i.e., a nullary application).
|
2004-10-27 00:54:26 +02:00
|
|
|
} elsif ($type eq "int") {
|
|
|
|
$args .= "(ATerm) ATmakeInt(e$n)";
|
|
|
|
} elsif ($type eq "ATermList" || $type eq "ATermBlob") {
|
|
|
|
$args .= "(ATerm) e$n";
|
|
|
|
} else {
|
|
|
|
$args .= "e$n";
|
|
|
|
}
|
|
|
|
$formals .= ", " if $formals ne "";
|
|
|
|
$formals .= "$type e$n";
|
|
|
|
$formals2 .= ", ";
|
|
|
|
$formals2 .= "$type & e$n";
|
|
|
|
my $m = $n - 1;
|
2004-10-29 13:22:49 +02:00
|
|
|
# !!! more checks here
|
2004-10-27 00:54:26 +02:00
|
|
|
if ($type eq "int") {
|
|
|
|
$unpack .= " e$n = ATgetInt((ATermInt) ATgetArgument(e, $m));\n";
|
|
|
|
} elsif ($type eq "ATermList") {
|
|
|
|
$unpack .= " e$n = (ATermList) ATgetArgument(e, $m);\n";
|
|
|
|
} elsif ($type eq "ATermBlob") {
|
|
|
|
$unpack .= " e$n = (ATermBlob) ATgetArgument(e, $m);\n";
|
* Removed the `id' attribute hack.
* Formalise the notion of fixed-output derivations, i.e., derivations
for which a cryptographic hash of the output is known in advance.
Changes to such derivations should not propagate upwards through the
dependency graph. Previously this was done by specifying the hash
component of the output path through the `id' attribute, but this is
insecure since you can lie about it (i.e., you can specify any hash
and then produce a completely different output). Now the
responsibility for checking the output is moved from the builder to
Nix itself.
A fixed-output derivation can be created by specifying the
`outputHash' and `outputHashAlgo' attributes, the latter taking
values `md5', `sha1', and `sha256', and the former specifying the
actual hash in hexadecimal or in base-32 (auto-detected by looking
at the length of the attribute value). MD5 is included for
compatibility but should be considered deprecated.
* Removed the `drvPath' pseudo-attribute in derivation results. It's
no longer necessary.
* Cleaned up the support for multiple output paths in derivation store
expressions. Each output now has a unique identifier (e.g., `out',
`devel', `docs'). Previously there was no way to tell output paths
apart at the store expression level.
* `nix-hash' now has a flag `--base32' to specify that the hash should
be printed in base-32 notation.
* `fetchurl' accepts parameters `sha256' and `sha1' in addition to
`md5'.
* `nix-prefetch-url' now prints out a SHA-1 hash in base-32. (TODO: a
flag to specify the hash.)
2005-01-17 17:55:19 +01:00
|
|
|
} elsif ($realType eq "string") {
|
|
|
|
$unpack .= " e$n = ATgetArgument(e, $m);\n";
|
|
|
|
$unpack .= " if (ATgetType(e$n) != AT_APPL) return false;\n";
|
2004-10-27 00:54:26 +02:00
|
|
|
} else {
|
|
|
|
$unpack .= " e$n = ATgetArgument(e, $m);\n";
|
|
|
|
}
|
|
|
|
$n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $arity = scalar @types;
|
|
|
|
|
|
|
|
print HEADER "extern AFun sym$funname;\n\n";
|
|
|
|
|
|
|
|
print IMPL "AFun sym$funname = 0;\n";
|
|
|
|
|
2006-05-02 23:58:46 +02:00
|
|
|
if ($arity == 0) {
|
|
|
|
print HEADER "extern ATerm const$funname;\n\n";
|
|
|
|
print IMPL "ATerm const$funname = 0;\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
print HEADER "static inline $result make$funname($formals) __attribute__ ((pure, nothrow));\n";
|
2004-10-27 00:54:26 +02:00
|
|
|
print HEADER "static inline $result make$funname($formals) {\n";
|
2006-05-02 23:58:46 +02:00
|
|
|
if ($arity == 0) {
|
|
|
|
print HEADER " return const$funname;\n";
|
|
|
|
}
|
|
|
|
elsif ($arity <= 6) {
|
2005-01-19 12:04:24 +01:00
|
|
|
print HEADER " return (ATerm) ATmakeAppl$arity(sym$funname$args);\n";
|
|
|
|
} else {
|
|
|
|
$args =~ s/^,//;
|
|
|
|
print HEADER " ATerm array[$arity] = {$args};\n";
|
|
|
|
print HEADER " return (ATerm) ATmakeApplArray(sym$funname, array);\n";
|
|
|
|
}
|
2004-10-27 00:54:26 +02:00
|
|
|
print HEADER "}\n\n";
|
|
|
|
|
|
|
|
print HEADER "#ifdef __cplusplus\n";
|
|
|
|
print HEADER "static inline bool match$funname(ATerm e$formals2) {\n";
|
2005-02-09 10:50:29 +01:00
|
|
|
print HEADER " if (ATgetType(e) != AT_APPL || (AFun) ATgetAFun(e) != sym$funname) return false;\n";
|
2004-10-27 00:54:26 +02:00
|
|
|
print HEADER "$unpack";
|
|
|
|
print HEADER " return true;\n";
|
|
|
|
print HEADER "}\n";
|
|
|
|
print HEADER "#endif\n\n\n";
|
|
|
|
|
|
|
|
$init .= " sym$funname = ATmakeAFun(\"$const\", $arity, ATfalse);\n";
|
|
|
|
$init .= " ATprotectAFun(sym$funname);\n";
|
2006-05-02 23:58:46 +02:00
|
|
|
if ($arity == 0) {
|
|
|
|
$init .= " const$funname = (ATerm) ATmakeAppl0(sym$funname);\n";
|
|
|
|
$init .= " ATprotect(&const$funname);\n";
|
|
|
|
}
|
2004-10-27 00:54:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
elsif (/^\s*(\w+)\s*=\s*(.*)$/) {
|
|
|
|
my $name = $1;
|
|
|
|
my $value = $2;
|
|
|
|
print HEADER "extern ATerm $name;\n";
|
|
|
|
print IMPL "ATerm $name = 0;\n";
|
|
|
|
$init .= " $name = $value;\n";
|
|
|
|
}
|
|
|
|
|
2004-10-29 13:22:49 +02:00
|
|
|
elsif (/^\s*init\s+(\w+)\s*$/) {
|
|
|
|
$initFun = $1;
|
|
|
|
}
|
|
|
|
|
2004-10-27 00:54:26 +02:00
|
|
|
else {
|
|
|
|
die "bad line: `$_'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-29 13:22:49 +02:00
|
|
|
print HEADER "void $initFun();\n\n";
|
2004-10-27 00:54:26 +02:00
|
|
|
|
|
|
|
print HEADER "static inline const char * aterm2String(ATerm t) {\n";
|
|
|
|
print HEADER " return (const char *) ATgetName(ATgetAFun(t));\n";
|
|
|
|
print HEADER "}\n\n";
|
|
|
|
|
|
|
|
print IMPL "\n";
|
2004-10-29 13:22:49 +02:00
|
|
|
print IMPL "void $initFun() {\n";
|
2004-10-27 00:54:26 +02:00
|
|
|
print IMPL "$init";
|
|
|
|
print IMPL "}\n";
|
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
print HEADER "#ifdef __cplusplus\n";
|
|
|
|
print HEADER "}\n";
|
|
|
|
print HEADER "#endif\n\n\n";
|
|
|
|
print IMPL "}\n";
|
|
|
|
|
2004-10-27 00:54:26 +02:00
|
|
|
close HEADER;
|
|
|
|
close IMPL;
|