-
Notifications
You must be signed in to change notification settings - Fork 15
/
CgOp.pm
182 lines (143 loc) · 3.6 KB
/
CgOp.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use 5.010;
use strict;
use warnings;
# for transition only!
{
package CgOp::NIL;
use Moose;
has ops => (isa => 'ArrayRef', is => 'ro');
sub var_cg {
my ($self, $cg) = @_;
for (@{ $self->ops }) {
if (blessed $_) {
$_->var_cg($cg);
} else {
my ($c, @o) = @$_;
$cg->$c(@o);
}
}
}
no Moose;
__PACKAGE__->meta->make_immutable;
}
{
package CgOp::Ternary;
use Moose;
has check => (is => 'ro');
has true => (is => 'ro');
has false => (is => 'ro');
sub var_cg {
my ($self, $cg) = @_;
my $l1 = $cg->label;
my $l2 = $cg->label;
$self->check->var_cg($cg);
$cg->ncgoto($l1);
$self->true->var_cg($cg);
$cg->goto($l2);
$cg->labelhere($l1);
$self->false->var_cg($cg);
$cg->labelhere($l2);
}
no Moose;
__PACKAGE__->meta->make_immutable;
}
{
package CgOp::While;
use Moose;
has check => (is => 'ro');
has body => (is => 'ro');
has once => (is => 'ro', isa => 'Bool');
has until => (is => 'ro', isa => 'Bool');
sub var_cg {
my ($self, $cg) = @_;
my $lagain = $cg->label;
my $lcheck = $self->once ? 0 : $cg->label;
$cg->goto($lcheck) unless $self->once;
$cg->labelhere($lagain);
$self->body->var_cg($cg);
$cg->labelhere($lcheck) unless $self->once;
$self->check->var_cg($cg);
if ($self->until) {
$cg->ncgoto($lagain);
} else {
$cg->cgoto($lagain);
}
}
no Moose;
__PACKAGE__->meta->make_immutable;
}
# just a bunch of smart constructors
{
package CgOp;
sub nil {
CgOp::NIL->new(ops => [ @_ ]);
}
sub null {
CgOp::NIL->new(ops => [[ push_null => $_[0] ]]);
}
sub prog {
CgOp::NIL->new(ops => [ @_ ]);
}
sub wrap {
CgOp::NIL->new(ops => [ $_[0], [ 'clr_wrap' ] ]);
}
sub sink {
CgOp::NIL->new(ops => [ $_[0], [ 'drop' ] ]);
}
sub fetch {
CgOp::NIL->new(ops => [ $_[0], [ 'fetch' ] ]);
}
sub how {
CgOp::NIL->new(ops => [ $_[0], [ 'how' ] ]);
}
sub getfield {
CgOp::NIL->new(ops => [ $_[1], [ 'clr_field_get', $_[0] ] ]);
}
sub cast {
CgOp::NIL->new(ops => [ $_[1], [ 'cast', $_[0] ] ]);
}
sub newscalar {
CgOp::NIL->new(ops => [ $_[0], [ 'newscalar' ] ]);
}
sub string_var {
CgOp::NIL->new(ops => [ [ 'string_var', $_[0] ] ]);
}
sub double {
CgOp::NIL->new(ops => [ [ 'clr_double', $_[0] ] ]);
}
sub unbox {
CgOp::NIL->new(ops => [ $_[1], [ 'unbox', $_[0] ] ]);
}
sub box {
CgOp::NIL->new(ops => [ $_[1], [ 'box', $_[0] ] ]);
}
sub bind {
CgOp::NIL->new(ops => [ $_[1], $_[2], [ 'bind', $_[0] ] ]);
}
sub scopedlex {
CgOp::NIL->new(ops => [[ scopelexget => $_[0] ]]);
}
sub subcall {
my ($sub, @args) = @_;
CgOp::NIL->new(ops => [ $sub, @args, [ 'call_sub', 1, scalar @args ] ]);
}
sub methodcall {
my ($obj, $name, @args) = @_;
CgOp::NIL->new(ops => [ $obj, [ 'dup_fetch' ], @args,
[ 'call_method', 1, $name, scalar @args ] ]);
}
sub ternary {
CgOp::Ternary->new(
check => $_[0],
true => $_[1],
false => $_[2]);
}
sub whileloop {
CgOp::While->new(
until => $_[0],
once => $_[1],
check => $_[2],
body => $_[3]);
}
}
1;