[stupid-crypto] 6 new revisions pushed by ben@links.org on 2010-06-13 18:05 GMT

3 views
Skip to first unread message

stupid...@googlecode.com

unread,
Jun 13, 2010, 2:06:48 PM6/13/10
to stupi...@googlegroups.com
6 new revisions:

Revision: eb76224d43
Author: Ben Laurie <b...@links.org>
Date: Sun Jun 13 06:44:21 2010
Log: Add another test.
http://code.google.com/p/stupid-crypto/source/detail?r=eb76224d43

Revision: 84024f83a4
Author: Ben Laurie <b...@links.org>
Date: Sun Jun 13 06:52:48 2010
Log: Make overflow32 work.
http://code.google.com/p/stupid-crypto/source/detail?r=84024f83a4

Revision: 7ad8e38592
Author: Ben Laurie <b...@links.org>
Date: Sun Jun 13 06:53:28 2010
Log: Another test.
http://code.google.com/p/stupid-crypto/source/detail?r=7ad8e38592

Revision: b7de525cab
Author: Ben Laurie <b...@links.org>
Date: Sun Jun 13 06:54:27 2010
Log: Make overflow8 work.
http://code.google.com/p/stupid-crypto/source/detail?r=b7de525cab

Revision: 0e577566a0
Author: Ben Laurie <b...@links.org>
Date: Sun Jun 13 06:55:14 2010
Log: Another test.
http://code.google.com/p/stupid-crypto/source/detail?r=0e577566a0

Revision: a236109127
Author: Ben Laurie <b...@links.org>
Date: Sun Jun 13 11:05:33 2010
Log: Structures working.
http://code.google.com/p/stupid-crypto/source/detail?r=a236109127

==============================================================================
Revision: eb76224d43
Author: Ben Laurie <b...@links.org>
Date: Sun Jun 13 06:44:21 2010
Log: Add another test.
http://code.google.com/p/stupid-crypto/source/detail?r=eb76224d43

Added:
/test2/overflow32.stupid

=======================================
--- /dev/null
+++ /test2/overflow32.stupid Sun Jun 13 06:44:21 2010
@@ -0,0 +1,10 @@
+"EXPECT-RUN-FAIL:305419896 plus32 4275878552 overflows
+";
+
+function(ostream o) test() {
+ uint32 a = 0x12345678;
+ uint32 b = 0xfedcba98;
+ uint32 c = 0;
+
+ c = a plus32 b;
+}

==============================================================================
Revision: 84024f83a4
Author: Ben Laurie <b...@links.org>
Date: Sun Jun 13 06:52:48 2010
Log: Make overflow32 work.
http://code.google.com/p/stupid-crypto/source/detail?r=84024f83a4

Modified:
/src/Stupid2/C.pm
/src/stupid2.pl
/test2/overflow32.stupid

=======================================
--- /src/Stupid2/C.pm Sat Jun 5 10:05:49 2010
+++ /src/Stupid2/C.pm Sun Jun 13 06:52:48 2010
@@ -315,6 +315,12 @@

$self->emitCode();
}
+
+sub Stupid2::HexValue::emitCode {
+ my $self = shift;
+
+ print $self->{value}->as_hex(), 'U';
+}

sub Stupid2::Eq::emitCode {
my $self = shift;
=======================================
--- /src/stupid2.pl Sat Jun 5 10:05:49 2010
+++ /src/stupid2.pl Sun Jun 13 06:52:48 2010
@@ -91,7 +91,7 @@
# hex VALUE
} elsif($code =~ /^0x([0-9a-f]+)(.*)$/s) {
$type = 'VALUE';
- $value = new Stupid::HexValue($1);
+ $value = new Stupid2::HexValue($1);
$code = $2;
# decimal UVALUE
} elsif($code =~ /^([0-9]+)u(.*)$/s) {
@@ -1740,9 +1740,10 @@
$self->{type}->setWidth($self->{width});
}

-package Stupid::HexValue;
+package Stupid2::HexValue;

use strict;
+use base qw(Stupid2::HasWidthWithoutDeduction);
use Math::BigInt;

# Unsigned hex value, any length
@@ -1750,11 +1751,13 @@
sub new {
my $class = shift;
my $value = shift;
+ my $width = shift;

my $self = {};
bless $self, $class;

$self->{value} = new Math::BigInt("0x$value");
+ $self->setWidth($width);

return $self;
}
=======================================
--- /test2/overflow32.stupid Sun Jun 13 06:44:21 2010
+++ /test2/overflow32.stupid Sun Jun 13 06:52:48 2010
@@ -2,9 +2,9 @@
";

function(ostream o) test() {
- uint32 a = 0x12345678;
- uint32 b = 0xfedcba98;
- uint32 c = 0;
-
- c = a plus32 b;
-}
+ int_32u a = 0x12345678;
+ int_32u b = 0xfedcba98;
+ int_32u c = 0;
+
+ c = a + b;
+}

==============================================================================
Revision: 7ad8e38592
Author: Ben Laurie <b...@links.org>
Date: Sun Jun 13 06:53:28 2010
Log: Another test.
http://code.google.com/p/stupid-crypto/source/detail?r=7ad8e38592

Added:
/test2/overflow8.stupid

=======================================
--- /dev/null
+++ /test2/overflow8.stupid Sun Jun 13 06:53:28 2010
@@ -0,0 +1,10 @@
+"EXPECT-RUN-FAIL:129 plus8 128 overflows
+";
+
+function(ostream o) test() {
+ uint8 a = 129;
+ uint8 b = 128;
+ uint8 c = 0;
+
+ c = a plus8 b;
+}

==============================================================================
Revision: b7de525cab
Author: Ben Laurie <b...@links.org>
Date: Sun Jun 13 06:54:27 2010
Log: Make overflow8 work.
http://code.google.com/p/stupid-crypto/source/detail?r=b7de525cab

Modified:
/test2/overflow8.stupid

=======================================
--- /test2/overflow8.stupid Sun Jun 13 06:53:28 2010
+++ /test2/overflow8.stupid Sun Jun 13 06:54:27 2010
@@ -2,9 +2,9 @@
";

function(ostream o) test() {
- uint8 a = 129;
- uint8 b = 128;
- uint8 c = 0;
-
- c = a plus8 b;
-}
+ int_8u a = 129;
+ int_8u b = 128;
+ int_8u c = 0;
+
+ c = a + b;
+}

==============================================================================
Revision: 0e577566a0
Author: Ben Laurie <b...@links.org>
Date: Sun Jun 13 06:55:14 2010
Log: Another test.
http://code.google.com/p/stupid-crypto/source/detail?r=0e577566a0

Added:
/test2/struct-returns-init.stupid

=======================================
--- /dev/null
+++ /test2/struct-returns-init.stupid Sun Jun 13 06:55:14 2010
@@ -0,0 +1,19 @@
+"EXPECT:";
+
+struct test (
+ uint32 a,
+ uint32 b
+);
+
+function (struct test t) inner() {
+ t.a = 5;
+}
+
+function () test() {
+ struct test c = [0,0];
+
+ c = inner();
+ c.a = 1;
+ c.b = 23;
+}
+

==============================================================================
Revision: a236109127
Author: Ben Laurie <b...@links.org>
Date: Sun Jun 13 11:05:33 2010
Log: Structures working.
http://code.google.com/p/stupid-crypto/source/detail?r=a236109127

Modified:
/src/Stupid2/C.pm
/src/grammar2.y
/src/stupid2.pl
/test2/struct-returns-init.stupid

=======================================
--- /src/Stupid2/C.pm Sun Jun 13 06:52:48 2010
+++ /src/Stupid2/C.pm Sun Jun 13 11:05:33 2010
@@ -150,6 +150,56 @@
print $self->{type}->typeName(), ' ', $name,
'[', $self->{size}->value(), ']';
}
+
+sub Stupid2::Type::Struct::emitCode {
+ my $self = shift;
+
+ print "struct $self->{name} {\n";
+ $self->{decls}->emitCode();
+ print "};\n";
+}
+
+sub Stupid2::Type::StructInstance::emitReturnDecl {
+ my $self = shift;
+ my $name = shift;
+
+ print "struct $self->{struct}->{name} *$name";
+}
+
+sub Stupid2::Type::StructInstance::dereference {
+ my $self = shift;
+
+ print '*';
+}
+
+sub Stupid2::Type::StructInstance::emitDeclaration {
+ my $self = shift;
+ my $name = shift;
+
+ print "struct $self->{struct}->{name} $name";
+}
+
+sub Stupid2::Type::StructInstance::emitPointer {
+ my $self = shift;
+
+ print '&';
+}
+
+sub Stupid2::AbstractDeclList::emitCode {
+ my $self = shift;
+
+ foreach my $decl (@{$self->{decls}}) {
+ $decl->emitCode();
+ }
+}
+
+sub Stupid2::AbstractDeclare::emitCode {
+ my $self = shift;
+
+ print ' ';
+ $self->{type}->emitDeclaration($self->{name});
+ print ";\n";
+}

sub Stupid2::Type::Int::typeName {
my $self = shift;
@@ -274,6 +324,20 @@
$self->{type}->emitPointer() if !$self->{isReturn};
print $self->{name};
}
+
+sub Stupid2::MemberRef::emitCode {
+ my $self = shift;
+
+ $self->{owner}->emitCode();
+ $self->{owner}->emitMemberRef($self->{member});
+}
+
+sub Stupid2::MemberRef::emitLValue {
+ my $self = shift;
+
+ $self->{owner}->emitCode();
+ $self->{owner}->emitMemberRef($self->{member});
+}

sub Stupid2::ArrayRef::emitParameter {
my $self = shift;
=======================================
--- /src/grammar2.y Sat Jun 5 09:40:17 2010
+++ /src/grammar2.y Sun Jun 13 11:05:33 2010
@@ -20,19 +20,19 @@
;

struct_decl : 'struct' WORD '(' abstract_decl_list ')' ';'
- { new Stupid::Type::Struct($::Context, $_[2], $_[4]); }
+ { new Stupid2::Type::Struct($::Context, $_[2], $_[4]); }
;

abstract_decl_list : abstract_decl_list ',' abstract_decl
{ $_[1]->appendAbstractDecl($_[3]); $_[1]; }
| abstract_decl
- { my $t = new Stupid::AbstractDeclList();
+ { my $t = new Stupid2::AbstractDeclList();
$t->appendAbstractDecl($_[1]);
$t; }
;

abstract_decl : type vardecl
- { new Stupid::AbstractDeclare($_[1], $_[2]); }
+ { new Stupid2::AbstractDeclare($_[1], $_[2]); }
;

function : 'function' '(' arglist ')' WORD '(' arglist ')'
@@ -149,7 +149,7 @@
| var '[' expr ']'
{ new Stupid2::ArrayRef($_[1], $_[3]); }
| expr '.' WORD
- { new Stupid::MemberRef($_[1], $_[3]); }
+ { new Stupid2::MemberRef($_[1], $_[3]); }
| call
;

@@ -170,7 +170,7 @@
| 'array' '(' type ',' VALUE ')'
{ new Stupid2::Type::Array($_[3], $_[5]); }
| 'struct' WORD
- { new Stupid::Type::StructInstance($::Context, $_[2]); }
+ { new Stupid2::Type::StructInstance($::Context, $_[2]); }
;

bitwidth : VALUE
=======================================
--- /src/stupid2.pl Sun Jun 13 06:52:48 2010
+++ /src/stupid2.pl Sun Jun 13 11:05:33 2010
@@ -245,9 +245,11 @@
return $self;
}

-sub getArrayElementWidth {
+sub getElementWidth {
my $self = shift;
-
+ my $member = shift;
+
+ # In the case of arrays, all members are the same size
return $self->{width};
}

@@ -266,6 +268,46 @@
return if !defined $other->{count};
croak 'Array counts differ' if $self->{count} != $other->{count};
}
+
+package Stupid2::StructWidth;
+
+use strict;
+use Carp;
+
+sub new {
+ my $class = shift;
+
+ my $self = {};
+ bless $self, $class;
+
+ $self->{width} = [];
+
+ return $self;
+}
+
+sub merge {
+ my $self = shift;
+ my $other = shift;
+
+ croak ref($other) if ref($other) ne 'Stupid2::StructWidth';
+}
+
+sub maybeSetElementWidth {
+ my $self = shift;
+ my $n = shift;
+ my $width = shift;
+
+ return if !defined $width;
+ $self->{width}->[$n] = $width if !defined $self->{width}->[$n];
+ $self->{width}->[$n]->merge($width);
+}
+
+sub getElementWidth {
+ my $self = shift;
+ my $n = shift;
+
+ return $self->{width}->[$n];
+}

package Stupid2::HasWidth;

@@ -313,7 +355,8 @@
}

confess ref($width) if ref($width) ne 'Stupid2::Bitwidth'
- && ref($width) ne 'Stupid2::ArrayWidth';
+ && ref($width) ne 'Stupid2::ArrayWidth'
+ && ref($width) ne 'Stupid2::StructWidth';

if(!defined $self->{width}) {
$self->{width} = $width;
@@ -543,9 +586,10 @@
}
}

-package Stupid::Type::Struct;
+package Stupid2::Type::Struct;

use strict;
+use base qw(Stupid2::HasWidth);

sub new {
my $class = shift;
@@ -558,6 +602,7 @@

$self->{name} = $name;
$self->{decls} = $decls;
+ $self->{width} = new Stupid2::StructWidth();

$context->addStruct($self);

@@ -571,9 +616,21 @@
return $self->{decls}->findDeclaration($name);
}

-package Stupid::Type::StructInstance;
+sub deduceWidth {
+ my $self = shift;
+
+ # Not sure anything below this can do anything useful, but to be
safe...
+ $self->{decls}->deduceWidth();
+ # Now populate our widths from the decls...
+ for(my $n = 0 ; my $decl = $self->{decls}->getElement($n) ; ++$n) {
+ $self->{width}->maybeSetElementWidth($n, $decl->maybeWidth());
+ }
+}
+
+package Stupid2::Type::StructInstance;

use strict;
+use base qw(Stupid2::HasWidthWithoutDeduction);

sub new {
my $class = shift;
@@ -584,6 +641,7 @@
bless $self, $class;

$self->{struct} = $context->findStruct($name);
+ $self->{width} = $self->{struct}->width();

return $self;
}
@@ -595,9 +653,10 @@
return $self->{struct}->findMember($name);
}

-package Stupid::AbstractDeclare;
+package Stupid2::AbstractDeclare;

use strict;
+use base qw(Stupid2::HasWidth);

sub new {
my $class = shift;
@@ -613,7 +672,20 @@
return $self;
}

-package Stupid::AbstractDeclList;
+sub deduceWidth {
+ my $self = shift;
+
+ $self->{type}->deduceWidth();
+ $self->maybeSetWidth($self->{type}->maybeWidth());
+}
+
+sub setChildrensWidth {
+ my $self = shift;
+
+ $self->{type}->setWidth($self->width());
+}
+
+package Stupid2::AbstractDeclList;

use strict;

@@ -646,6 +718,23 @@
}
croak "Can't find declaration of $name";
}
+
+sub deduceWidth {
+ my $self = shift;
+
+ # Not sure there's any point to this, but to be safe...
+ foreach my $decl (@{$self->{decls}}) {
+ $decl->deduceWidth();
+ }
+}
+
+sub getElement {
+ my $self = shift;
+ my $n = shift;
+
+ return undef if $n > $#{$self->{decls}};
+ return $self->{decls}->[$n];
+}

package Stupid2::ExprList;

@@ -724,9 +813,10 @@
# confess "Setting function call width" if defined $width;
}

-package Stupid::MemberRef;
+package Stupid2::MemberRef;

use strict;
+use base qw(Stupid2::HasWidth);

sub new {
my $class = shift;
@@ -741,6 +831,18 @@

return $self;
}
+
+sub deduceWidth {
+ my $self = shift;
+
+ $self->maybeSetWidth($self->{member}->maybeWidth());
+}
+
+sub setChildrensWidth {
+ my $self = shift;
+
+ $self->{member}->setWidth($self->width());
+}

package Stupid2::Comment;

@@ -1810,8 +1912,10 @@
sub setChildrensWidth {
my $self = shift;

- my $child_width = $self->{width}->getArrayElementWidth();
+ my $n = 0;
foreach my $value (@{$self->{values}}) {
+ my $child_width = $self->{width}->getElementWidth($n);
$value->setWidth($child_width);
+ ++$n;
}
}
=======================================
--- /test2/struct-returns-init.stupid Sun Jun 13 06:55:14 2010
+++ /test2/struct-returns-init.stupid Sun Jun 13 11:05:33 2010
@@ -1,8 +1,8 @@
"EXPECT:";

struct test (
- uint32 a,
- uint32 b
+ int_32u a,
+ int_32u b
);

function (struct test t) inner() {
@@ -16,4 +16,3 @@
c.a = 1;
c.b = 23;
}
-

Reply all
Reply to author
Forward
0 new messages