OSDN Git Service

fix related check to support that if-else statement include lock/unlock statement...
[bytom/equity.git] / compiler / compile.go
1 package compiler
2
3 import (
4         "encoding/json"
5         "fmt"
6         "io"
7         "io/ioutil"
8         "strings"
9
10         chainjson "github.com/bytom/encoding/json"
11         "github.com/bytom/errors"
12         "github.com/bytom/protocol/vm"
13         "github.com/bytom/protocol/vm/vmutil"
14 )
15
16 // ValueInfo describes how a blockchain value is used in a contract
17 // clause.
18 type ValueInfo struct {
19         // Name is the clause's name for this value.
20         Name string `json:"name"`
21
22         // Program is the program expression used to the lock the value, if
23         // the value is locked with "lock." If it's unlocked with "unlock"
24         // instead, this is empty.
25         Program string `json:"program,omitempty"`
26
27         // Asset is the expression describing the asset type the value must
28         // have, as it appears in a clause's "requires" section. If this is
29         // the contract value instead, this is empty.
30         Asset string `json:"asset,omitempty"`
31
32         // Amount is the expression describing the amount the value must
33         // have, as it appears in a clause's "requires" section. If this is
34         // the contract value instead, this is empty.
35         Amount string `json:"amount,omitempty"`
36 }
37
38 // ContractArg is an argument with which to instantiate a contract as
39 // a program. Exactly one of B, I, and S should be supplied.
40 type ContractArg struct {
41         B *bool               `json:"boolean,omitempty"`
42         I *int64              `json:"integer,omitempty"`
43         S *chainjson.HexBytes `json:"string,omitempty"`
44 }
45
46 // Compile parses a sequence of Equity contracts from the supplied reader
47 // and produces Contract objects containing the compiled bytecode and
48 // other analysis. If argMap is non-nil, it maps contract names to
49 // lists of arguments with which to instantiate them as programs, with
50 // the results placed in the contract's Program field. A contract
51 // named in argMap but not found in the input is silently ignored.
52 func Compile(r io.Reader) ([]*Contract, error) {
53         inp, err := ioutil.ReadAll(r)
54         if err != nil {
55                 return nil, errors.Wrap(err, "reading input")
56         }
57         contracts, err := parse(inp)
58         if err != nil {
59                 return nil, errors.Wrap(err, "parse error")
60         }
61
62         globalEnv := newEnviron(nil)
63         for _, k := range keywords {
64                 globalEnv.add(k, nilType, roleKeyword)
65         }
66         for _, b := range builtins {
67                 globalEnv.add(b.name, nilType, roleBuiltin)
68         }
69
70         // All contracts must be checked for recursiveness before any are
71         // compiled.
72         for _, contract := range contracts {
73                 contract.Recursive = checkRecursive(contract)
74         }
75
76         for _, contract := range contracts {
77                 err = globalEnv.addContract(contract)
78                 if err != nil {
79                         return nil, err
80                 }
81         }
82
83         for _, contract := range contracts {
84                 err = compileContract(contract, globalEnv)
85                 if err != nil {
86                         return nil, errors.Wrap(err, "compiling contract")
87                 }
88                 for _, clause := range contract.Clauses {
89                         for _, stmt := range clause.statements {
90                                 switch s := stmt.(type) {
91                                 case *lockStatement:
92                                         valueInfo := ValueInfo{
93                                                 Amount:  s.lockedAmount.String(),
94                                                 Asset:   s.lockedAsset.String(),
95                                                 Program: s.program.String(),
96                                         }
97
98                                         clause.Values = append(clause.Values, valueInfo)
99                                 case *unlockStatement:
100                                         valueInfo := ValueInfo{
101                                                 Amount: contract.Value.Amount,
102                                                 Asset:  contract.Value.Asset,
103                                         }
104                                         clause.Values = append(clause.Values, valueInfo)
105                                 }
106                         }
107                 }
108         }
109
110         return contracts, nil
111 }
112
113 func Instantiate(body []byte, params []*Param, recursive bool, args []ContractArg) ([]byte, error) {
114         if len(args) != len(params) {
115                 return nil, fmt.Errorf("got %d argument(s), want %d", len(args), len(params))
116         }
117
118         // typecheck args against param types
119         for i, param := range params {
120                 arg := args[i]
121                 switch param.Type {
122                 case amountType, intType:
123                         if arg.I == nil {
124                                 return nil, fmt.Errorf("type mismatch in arg %d (want integer)", i)
125                         }
126                 case assetType, hashType, progType, pubkeyType, sigType, strType:
127                         if arg.S == nil {
128                                 return nil, fmt.Errorf("type mismatch in arg %d (want string)", i)
129                         }
130                 case boolType:
131                         if arg.B == nil {
132                                 return nil, fmt.Errorf("type mismatch in arg %d (want boolean)", i)
133                         }
134                 }
135         }
136
137         b := vmutil.NewBuilder()
138
139         for i := len(args) - 1; i >= 0; i-- {
140                 a := args[i]
141                 switch {
142                 case a.B != nil:
143                         var n int64
144                         if *a.B {
145                                 n = 1
146                         }
147                         b.AddInt64(n)
148                 case a.I != nil:
149                         b.AddInt64(*a.I)
150                 case a.S != nil:
151                         b.AddData(*a.S)
152                 }
153         }
154
155         if recursive {
156                 // <argN> <argN-1> ... <arg1> <body> DEPTH OVER 0 CHECKPREDICATE
157                 b.AddData(body)
158                 b.AddOp(vm.OP_DEPTH).AddOp(vm.OP_OVER)
159         } else {
160                 // <argN> <argN-1> ... <arg1> DEPTH <body> 0 CHECKPREDICATE
161                 b.AddOp(vm.OP_DEPTH)
162                 b.AddData(body)
163         }
164         b.AddInt64(0)
165         b.AddOp(vm.OP_CHECKPREDICATE)
166         return b.Build()
167 }
168
169 func compileContract(contract *Contract, globalEnv *environ) error {
170         var err error
171
172         if len(contract.Clauses) == 0 {
173                 return fmt.Errorf("empty contract")
174         }
175         env := newEnviron(globalEnv)
176         for _, p := range contract.Params {
177                 err = env.add(p.Name, p.Type, roleContractParam)
178                 if err != nil {
179                         return err
180                 }
181         }
182
183         // value is spilt with valueAmount and valueAsset
184         if err = env.add(contract.Value.Amount, amountType, roleContractValue); err != nil {
185                 return err
186         }
187         if err = env.add(contract.Value.Asset, assetType, roleContractValue); err != nil {
188                 return err
189         }
190
191         for _, c := range contract.Clauses {
192                 err = env.add(c.Name, nilType, roleClause)
193                 if err != nil {
194                         return err
195                 }
196         }
197
198         err = prohibitSigParams(contract)
199         if err != nil {
200                 return err
201         }
202         err = requireAllParamsUsedInClauses(contract.Params, contract.Clauses)
203         if err != nil {
204                 return err
205         }
206
207         var stk stack
208
209         if len(contract.Clauses) > 1 {
210                 stk = stk.add("<clause selector>")
211         }
212
213         for i := len(contract.Params) - 1; i >= 0; i-- {
214                 p := contract.Params[i]
215                 stk = stk.add(p.Name)
216         }
217
218         if contract.Recursive {
219                 stk = stk.add(contract.Name)
220         }
221
222         b := &builder{}
223         sequence := 0 // sequence is used to count the number of ifStatements
224
225         if len(contract.Clauses) == 1 {
226                 err = compileClause(b, stk, contract, env, contract.Clauses[0], &sequence)
227                 if err != nil {
228                         return err
229                 }
230         } else {
231                 if len(contract.Params) > 0 {
232                         // A clause selector is at the bottom of the stack. Roll it to the
233                         // top.
234                         n := len(contract.Params)
235                         if contract.Recursive {
236                                 n++
237                         }
238                         stk = b.addRoll(stk, n) // stack: [<clause params> <contract params> [<maybe contract body>] <clause selector>]
239                 }
240
241                 var stk2 stack
242
243                 // clauses 2..N-1
244                 for i := len(contract.Clauses) - 1; i >= 2; i-- {
245                         stk = b.addDup(stk)                                                   // stack: [... <clause selector> <clause selector>]
246                         stk = b.addInt64(stk, int64(i))                                       // stack: [... <clause selector> <clause selector> <i>]
247                         stk = b.addNumEqual(stk, fmt.Sprintf("(<clause selector> == %d)", i)) // stack: [... <clause selector> <i == clause selector>]
248                         stk = b.addJumpIf(stk, contract.Clauses[i].Name)                      // stack: [... <clause selector>]
249                         stk2 = stk                                                            // stack starts here for clauses 2 through N-1
250                 }
251
252                 // clause 1
253                 stk = b.addJumpIf(stk, contract.Clauses[1].Name) // consumes the clause selector
254
255                 // no jump needed for clause 0
256
257                 for i, clause := range contract.Clauses {
258                         if i > 1 {
259                                 // Clauses 0 and 1 have no clause selector on top of the
260                                 // stack. Clauses 2 and later do.
261                                 stk = stk2
262                         }
263
264                         b.addJumpTarget(stk, clause.Name)
265
266                         if i > 1 {
267                                 stk = b.addDrop(stk)
268                         }
269
270                         err = compileClause(b, stk, contract, env, clause, &sequence)
271                         if err != nil {
272                                 return errors.Wrapf(err, "compiling clause \"%s\"", clause.Name)
273                         }
274                         b.forgetPendingVerify()
275                         if i < len(contract.Clauses)-1 {
276                                 b.addJump(stk, "_end")
277                         }
278                 }
279                 b.addJumpTarget(stk, "_end")
280         }
281
282         opcodes := optimize(b.opcodes())
283         prog, err := vm.Assemble(opcodes)
284         if err != nil {
285                 return err
286         }
287
288         contract.Body = prog
289         contract.Opcodes = opcodes
290
291         contract.Steps = b.steps()
292
293         return nil
294 }
295
296 func compileClause(b *builder, contractStk stack, contract *Contract, env *environ, clause *Clause, sequence *int) error {
297         var err error
298
299         // copy env to leave outerEnv unchanged
300         env = newEnviron(env)
301         for _, p := range clause.Params {
302                 err = env.add(p.Name, p.Type, roleClauseParam)
303                 if err != nil {
304                         return err
305                 }
306         }
307
308         if err = assignIndexes(clause); err != nil {
309                 return err
310         }
311
312         var stk stack
313         for _, p := range clause.Params {
314                 // NOTE: the order of clause params is not reversed, unlike
315                 // contract params (and also unlike the arguments to Equity
316                 // function-calls).
317                 stk = stk.add(p.Name)
318         }
319         stk = stk.addFromStack(contractStk)
320
321         // a count of the number of times each variable is referenced
322         counts := make(map[string]int)
323         for _, s := range clause.statements {
324                 s.countVarRefs(counts)
325                 if stmt, ok := s.(*ifStatement); ok {
326                         for _, trueStmt := range stmt.body.trueBody {
327                                 trueStmt.countVarRefs(counts)
328                         }
329
330                         for _, falseStmt := range stmt.body.falseBody {
331                                 falseStmt.countVarRefs(counts)
332                         }
333                 }
334         }
335
336         for _, stat := range clause.statements {
337                 if stk, err = compileStatement(b, stk, contract, env, clause, counts, stat, sequence); err != nil {
338                         return err
339                 }
340         }
341
342         err = requireAllValuesDisposedOnce(contract, clause)
343         if err != nil {
344                 return err
345         }
346         err = typeCheckClause(contract, clause, env)
347         if err != nil {
348                 return err
349         }
350         err = requireAllParamsUsedInClause(clause.Params, clause)
351         if err != nil {
352                 return err
353         }
354
355         return nil
356 }
357
358 func compileStatement(b *builder, stk stack, contract *Contract, env *environ, clause *Clause, counts map[string]int, stat statement, sequence *int) (stack, error) {
359         var err error
360         switch stmt := stat.(type) {
361         case *ifStatement:
362                 // sequence add 1 when the statement is ifStatement
363                 *sequence++
364                 strSequence := fmt.Sprintf("%d", *sequence)
365
366                 // compile condition expression
367                 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.condition)
368                 if err != nil {
369                         return stk, errors.Wrapf(err, "in check condition of ifStatement in clause \"%s\"", clause.Name)
370                 }
371
372                 // jump to falseBody when condition is false, while the JUMPIF instruction will be run success when
373                 // the value of dataStack is true, therefore add this check
374                 conditionExpr := stk.str
375                 stk = b.addBoolean(stk, false)
376                 stk = b.addEqual(stk, fmt.Sprintf("(%s == false)", conditionExpr)) // stack: [... <condition_result == false>]
377
378                 // add label
379                 var label string
380                 if len(stmt.body.falseBody) != 0 {
381                         label = "else_" + strSequence
382                 } else {
383                         label = "endif_" + strSequence
384                 }
385                 stk = b.addJumpIf(stk, label)
386                 b.addJumpTarget(stk, "if_"+strSequence)
387
388                 // temporary store stack and counts for falseBody
389                 condStk := stk
390                 elseCounts := make(map[string]int)
391                 for k, v := range counts {
392                         elseCounts[k] = v
393                 }
394
395                 // compile trueBody statements
396                 if len(stmt.body.trueBody) != 0 {
397                         for _, st := range stmt.body.trueBody {
398                                 st.countVarRefs(counts)
399                         }
400
401                         // modify value amount because of using only once
402                         if counts[contract.Value.Amount] > 1 {
403                                 counts[contract.Value.Amount] = 1
404                         }
405
406                         // modify value asset because of using only once
407                         if counts[contract.Value.Asset] > 1 {
408                                 counts[contract.Value.Asset] = 1
409                         }
410
411                         for _, st := range stmt.body.trueBody {
412                                 if stk, err = compileStatement(b, stk, contract, env, clause, counts, st, sequence); err != nil {
413                                         return stk, err
414                                 }
415                         }
416                 }
417
418                 // compile falseBody statements
419                 if len(stmt.body.falseBody) != 0 {
420                         counts := make(map[string]int)
421                         for k, v := range elseCounts {
422                                 counts[k] = v
423                         }
424
425                         for _, st := range stmt.body.falseBody {
426                                 st.countVarRefs(counts)
427                         }
428
429                         // modify value amount because of using only once
430                         if counts[contract.Value.Amount] > 1 {
431                                 counts[contract.Value.Amount] = 1
432                         }
433
434                         // modify value asset because of using only once
435                         if counts[contract.Value.Asset] > 1 {
436                                 counts[contract.Value.Asset] = 1
437                         }
438
439                         stk = condStk
440                         b.addJump(stk, "endif_"+strSequence)
441                         b.addJumpTarget(stk, "else_"+strSequence)
442
443                         for _, st := range stmt.body.falseBody {
444                                 if stk, err = compileStatement(b, stk, contract, env, clause, counts, st, sequence); err != nil {
445                                         return stk, err
446                                 }
447                         }
448                 }
449                 b.addJumpTarget(stk, "endif_"+strSequence)
450
451         case *defineStatement:
452                 // variable
453                 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.expr)
454                 if err != nil {
455                         return stk, errors.Wrapf(err, "in define statement in clause \"%s\"", clause.Name)
456                 }
457
458                 // modify stack name
459                 stk.str = stmt.varName.Name
460
461                 // add environ for define variable
462                 if err = env.add(stmt.varName.Name, stmt.varName.Type, roleClauseVariable); err != nil {
463                         return stk, err
464                 }
465
466         case *verifyStatement:
467                 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.expr)
468                 if err != nil {
469                         return stk, errors.Wrapf(err, "in verify statement in clause \"%s\"", clause.Name)
470                 }
471                 stk = b.addVerify(stk)
472
473                 // special-case reporting of certain function calls
474                 if c, ok := stmt.expr.(*callExpr); ok && len(c.args) == 1 {
475                         if b := referencedBuiltin(c.fn); b != nil {
476                                 switch b.name {
477                                 case "below":
478                                         clause.BlockHeight = append(clause.BlockHeight, c.args[0].String())
479                                 case "above":
480                                         clause.BlockHeight = append(clause.BlockHeight, c.args[0].String())
481                                 }
482                         }
483                 }
484
485         case *lockStatement:
486                 // index
487                 stk = b.addInt64(stk, stmt.index)
488
489                 // TODO: permit more complex expressions for locked,
490                 // like "lock x+y with foo" (?)
491
492                 if stmt.lockedAmount.String() == contract.Value.Amount && stmt.lockedAsset.String() == contract.Value.Asset {
493                         stk = b.addAmount(stk, contract.Value.Amount)
494                         stk = b.addAsset(stk, contract.Value.Asset)
495                 } else {
496                         // amount
497                         if stmt.lockedAmount.String() == contract.Value.Amount {
498                                 stk = b.addAmount(stk, contract.Value.Amount)
499                         } else if strings.Contains(stmt.lockedAmount.String(), contract.Value.Amount) {
500                                 stk = b.addAmount(stk, contract.Value.Amount)
501                                 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.lockedAmount)
502                                 if err != nil {
503                                         return stk, errors.Wrapf(err, "in lock statement in clause \"%s\"", clause.Name)
504                                 }
505                         } else {
506                                 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.lockedAmount)
507                                 if err != nil {
508                                         return stk, errors.Wrapf(err, "in lock statement in clause \"%s\"", clause.Name)
509                                 }
510                         }
511
512                         // asset
513                         if stmt.lockedAsset.String() == contract.Value.Asset {
514                                 stk = b.addAsset(stk, contract.Value.Asset)
515                         } else if strings.Contains(stmt.lockedAsset.String(), contract.Value.Asset) {
516                                 stk = b.addAsset(stk, contract.Value.Asset)
517                                 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.lockedAsset)
518                                 if err != nil {
519                                         return stk, errors.Wrapf(err, "in lock statement in clause \"%s\"", clause.Name)
520                                 }
521                         } else {
522                                 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.lockedAsset)
523                                 if err != nil {
524                                         return stk, errors.Wrapf(err, "in lock statement in clause \"%s\"", clause.Name)
525                                 }
526                         }
527                 }
528
529                 // version
530                 stk = b.addInt64(stk, 1)
531
532                 // prog
533                 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.program)
534                 if err != nil {
535                         return stk, errors.Wrapf(err, "in lock statement in clause \"%s\"", clause.Name)
536                 }
537
538                 stk = b.addCheckOutput(stk, fmt.Sprintf("checkOutput(%s, %s, %s)",
539                         stmt.lockedAmount.String(), stmt.lockedAsset.String(), stmt.program))
540                 stk = b.addVerify(stk)
541
542         case *unlockStatement:
543                 if len(clause.statements) == 1 {
544                         // This is the only statement in the clause, make sure TRUE is
545                         // on the stack.
546                         stk = b.addBoolean(stk, true)
547                 }
548         }
549
550         return stk, nil
551 }
552
553 func compileExpr(b *builder, stk stack, contract *Contract, clause *Clause, env *environ, counts map[string]int, expr expression) (stack, error) {
554         var err error
555
556         switch e := expr.(type) {
557         case *binaryExpr:
558                 // Do typechecking after compiling subexpressions (because other
559                 // compilation errors are more interesting than type mismatch
560                 // errors).
561
562                 stk, err = compileExpr(b, stk, contract, clause, env, counts, e.left)
563                 if err != nil {
564                         return stk, errors.Wrapf(err, "in left operand of \"%s\" expression", e.op.op)
565                 }
566                 stk, err = compileExpr(b, stk, contract, clause, env, counts, e.right)
567                 if err != nil {
568                         return stk, errors.Wrapf(err, "in right operand of \"%s\" expression", e.op.op)
569                 }
570
571                 lType := e.left.typ(env)
572                 if e.op.left != "" && !(lType == e.op.left || lType == amountType) {
573                         return stk, fmt.Errorf("in \"%s\", left operand has type \"%s\", must be \"%s\"", e, lType, e.op.left)
574                 }
575
576                 rType := e.right.typ(env)
577                 if e.op.right != "" && !(rType == e.op.right || rType == amountType) {
578                         return stk, fmt.Errorf("in \"%s\", right operand has type \"%s\", must be \"%s\"", e, rType, e.op.right)
579                 }
580
581                 switch e.op.op {
582                 case "==", "!=":
583                         if lType != rType {
584                                 // Maybe one is Hash and the other is (more-specific-Hash subtype).
585                                 // TODO(bobg): generalize this mechanism
586                                 if lType == hashType && isHashSubtype(rType) {
587                                         propagateType(contract, clause, env, rType, e.left)
588                                 } else if rType == hashType && isHashSubtype(lType) {
589                                         propagateType(contract, clause, env, lType, e.right)
590                                 } else {
591                                         return stk, fmt.Errorf("type mismatch in \"%s\": left operand has type \"%s\", right operand has type \"%s\"", e, lType, rType)
592                                 }
593                         }
594                         if lType == "Boolean" {
595                                 return stk, fmt.Errorf("in \"%s\": using \"%s\" on Boolean values not allowed", e, e.op.op)
596                         }
597                 }
598
599                 stk = b.addOps(stk.dropN(2), e.op.opcodes, e.String())
600
601         case *unaryExpr:
602                 // Do typechecking after compiling subexpression (because other
603                 // compilation errors are more interesting than type mismatch
604                 // errors).
605
606                 var err error
607                 stk, err = compileExpr(b, stk, contract, clause, env, counts, e.expr)
608                 if err != nil {
609                         return stk, errors.Wrapf(err, "in \"%s\" expression", e.op.op)
610                 }
611
612                 if e.op.operand != "" && e.expr.typ(env) != e.op.operand {
613                         return stk, fmt.Errorf("in \"%s\", operand has type \"%s\", must be \"%s\"", e, e.expr.typ(env), e.op.operand)
614                 }
615                 b.addOps(stk.drop(), e.op.opcodes, e.String())
616
617         case *callExpr:
618                 bi := referencedBuiltin(e.fn)
619                 if bi == nil {
620                         if v, ok := e.fn.(varRef); ok {
621                                 if entry := env.lookup(string(v)); entry != nil && entry.t == contractType {
622                                         clause.Contracts = append(clause.Contracts, entry.c.Name)
623
624                                         partialName := fmt.Sprintf("%s(...)", v)
625                                         stk = b.addData(stk, nil)
626
627                                         if len(e.args) != len(entry.c.Params) {
628                                                 return stk, fmt.Errorf("contract \"%s\" expects %d argument(s), got %d", entry.c.Name, len(entry.c.Params), len(e.args))
629                                         }
630
631                                         for i := len(e.args) - 1; i >= 0; i-- {
632                                                 arg := e.args[i]
633                                                 if entry.c.Params[i].Type != "" && arg.typ(env) != entry.c.Params[i].Type {
634                                                         return stk, fmt.Errorf("argument %d to contract \"%s\" has type \"%s\", must be \"%s\"", i, entry.c.Name, arg.typ(env), entry.c.Params[i].Type)
635                                                 }
636                                                 stk, err = compileExpr(b, stk, contract, clause, env, counts, arg)
637                                                 if err != nil {
638                                                         return stk, err
639                                                 }
640                                                 stk = b.addCatPushdata(stk, partialName)
641                                         }
642
643                                         switch {
644                                         case entry.c == contract:
645                                                 // Recursive call - cannot use entry.c.Body
646                                                 // <argN> <argN-1> ... <arg1> <body> DEPTH OVER 0 CHECKPREDICATE
647                                                 stk, err = compileRef(b, stk, counts, varRef(contract.Name))
648                                                 if err != nil {
649                                                         return stk, errors.Wrap(err, "compiling contract call")
650                                                 }
651                                                 stk = b.addCatPushdata(stk, partialName)
652                                                 stk = b.addData(stk, []byte{byte(vm.OP_DEPTH), byte(vm.OP_OVER)})
653                                                 stk = b.addCat(stk, partialName)
654
655                                         case entry.c.Recursive:
656                                                 // Non-recursive call to a (different) recursive contract
657                                                 // <argN> <argN-1> ... <arg1> <body> DEPTH OVER 0 CHECKPREDICATE
658                                                 if len(entry.c.Body) == 0 {
659                                                         // TODO(bobg): sort input contracts topologically to permit forward calling
660                                                         return stk, fmt.Errorf("contract \"%s\" not defined", entry.c.Name)
661                                                 }
662                                                 stk = b.addData(stk, entry.c.Body)
663                                                 stk = b.addCatPushdata(stk, partialName)
664                                                 stk = b.addData(stk, []byte{byte(vm.OP_DEPTH), byte(vm.OP_OVER)})
665                                                 stk = b.addCat(stk, partialName)
666
667                                         default:
668                                                 // Non-recursive call to non-recursive contract
669                                                 // <argN> <argN-1> ... <arg1> DEPTH <body> 0 CHECKPREDICATE
670                                                 stk = b.addData(stk, []byte{byte(vm.OP_DEPTH)})
671                                                 stk = b.addCat(stk, partialName)
672                                                 if len(entry.c.Body) == 0 {
673                                                         // TODO(bobg): sort input contracts topologically to permit forward calling
674                                                         return stk, fmt.Errorf("contract \"%s\" not defined", entry.c.Name)
675                                                 }
676                                                 stk = b.addData(stk, entry.c.Body)
677                                                 stk = b.addCatPushdata(stk, partialName)
678                                         }
679                                         stk = b.addData(stk, vm.Int64Bytes(0))
680                                         stk = b.addCatPushdata(stk, partialName)
681                                         stk = b.addData(stk, []byte{byte(vm.OP_CHECKPREDICATE)})
682                                         stk = b.addCat(stk, e.String())
683
684                                         return stk, nil
685                                 }
686                         }
687                         return stk, fmt.Errorf("unknown function \"%s\"", e.fn)
688                 }
689
690                 if len(e.args) != len(bi.args) {
691                         return stk, fmt.Errorf("wrong number of args for \"%s\": have %d, want %d", bi.name, len(e.args), len(bi.args))
692                 }
693
694                 // WARNING WARNING WOOP WOOP
695                 // special-case hack
696                 // WARNING WARNING WOOP WOOP
697                 if bi.name == "checkTxMultiSig" {
698                         if _, ok := e.args[0].(listExpr); !ok {
699                                 return stk, fmt.Errorf("checkTxMultiSig expects list literals, got %T for argument 0", e.args[0])
700                         }
701                         if _, ok := e.args[1].(listExpr); !ok {
702                                 return stk, fmt.Errorf("checkTxMultiSig expects list literals, got %T for argument 1", e.args[1])
703                         }
704
705                         var k1, k2 int
706
707                         stk, k1, err = compileArg(b, stk, contract, clause, env, counts, e.args[1])
708                         if err != nil {
709                                 return stk, err
710                         }
711
712                         // stack: [... sigM ... sig1 M]
713
714                         var altEntry string
715                         stk, altEntry = b.addToAltStack(stk) // stack: [... sigM ... sig1]
716                         stk = b.addTxSigHash(stk)            // stack: [... sigM ... sig1 txsighash]
717
718                         stk, k2, err = compileArg(b, stk, contract, clause, env, counts, e.args[0])
719                         if err != nil {
720                                 return stk, err
721                         }
722
723                         // stack: [... sigM ... sig1 txsighash pubkeyN ... pubkey1 N]
724
725                         stk = b.addFromAltStack(stk, altEntry) // stack: [... sigM ... sig1 txsighash pubkeyN ... pubkey1 N M]
726                         stk = b.addSwap(stk)                   // stack: [... sigM ... sig1 txsighash pubkeyN ... pubkey1 M N]
727                         stk = b.addCheckMultisig(stk, k1+k2, e.String())
728
729                         return stk, nil
730                 }
731
732                 var k int
733
734                 for i := len(e.args) - 1; i >= 0; i-- {
735                         a := e.args[i]
736                         var k2 int
737                         var err error
738                         stk, k2, err = compileArg(b, stk, contract, clause, env, counts, a)
739                         if err != nil {
740                                 return stk, errors.Wrapf(err, "compiling argument %d in call expression", i)
741                         }
742                         k += k2
743                 }
744
745                 // Do typechecking after compiling subexpressions (because other
746                 // compilation errors are more interesting than type mismatch
747                 // errors).
748                 for i, actual := range e.args {
749                         if bi.args[i] != "" && actual.typ(env) != bi.args[i] {
750                                 return stk, fmt.Errorf("argument %d to \"%s\" has type \"%s\", must be \"%s\"", i, bi.name, actual.typ(env), bi.args[i])
751                         }
752                 }
753
754                 stk = b.addOps(stk.dropN(k), bi.opcodes, e.String())
755
756                 // special-case reporting
757                 switch bi.name {
758                 case "sha3", "sha256":
759                         clause.HashCalls = append(clause.HashCalls, HashCall{bi.name, e.args[0].String(), string(e.args[0].typ(env))})
760                 }
761
762         case varRef:
763                 return compileRef(b, stk, counts, e)
764
765         case integerLiteral:
766                 stk = b.addInt64(stk, int64(e))
767
768         case bytesLiteral:
769                 stk = b.addData(stk, []byte(e))
770
771         case booleanLiteral:
772                 stk = b.addBoolean(stk, bool(e))
773
774         case listExpr:
775                 // Lists are excluded here because they disobey the invariant of
776                 // this function: namely, that it increases the stack size by
777                 // exactly one. (A list pushes its items and its length on the
778                 // stack.) But they're OK as function-call arguments because the
779                 // function (presumably) consumes all the stack items added.
780                 return stk, fmt.Errorf("encountered list outside of function-call context")
781         }
782         return stk, nil
783 }
784
785 func compileArg(b *builder, stk stack, contract *Contract, clause *Clause, env *environ, counts map[string]int, expr expression) (stack, int, error) {
786         var n int
787         if list, ok := expr.(listExpr); ok {
788                 for i := 0; i < len(list); i++ {
789                         elt := list[len(list)-i-1]
790                         var err error
791                         stk, err = compileExpr(b, stk, contract, clause, env, counts, elt)
792                         if err != nil {
793                                 return stk, 0, err
794                         }
795                         n++
796                 }
797                 stk = b.addInt64(stk, int64(len(list)))
798                 n++
799                 return stk, n, nil
800         }
801         var err error
802         stk, err = compileExpr(b, stk, contract, clause, env, counts, expr)
803         return stk, 1, err
804 }
805
806 func compileRef(b *builder, stk stack, counts map[string]int, ref varRef) (stack, error) {
807         depth := stk.find(string(ref))
808         if depth < 0 {
809                 return stk, fmt.Errorf("undefined reference: \"%s\"", ref)
810         }
811
812         var isFinal bool
813         if count, ok := counts[string(ref)]; ok && count > 0 {
814                 count--
815                 counts[string(ref)] = count
816                 isFinal = count == 0
817         }
818
819         switch depth {
820         case 0:
821                 if !isFinal {
822                         stk = b.addDup(stk)
823                 }
824         case 1:
825                 if isFinal {
826                         stk = b.addSwap(stk)
827                 } else {
828                         stk = b.addOver(stk)
829                 }
830         default:
831                 if isFinal {
832                         stk = b.addRoll(stk, depth)
833                 } else {
834                         stk = b.addPick(stk, depth)
835                 }
836         }
837         return stk, nil
838 }
839
840 func (a *ContractArg) UnmarshalJSON(b []byte) error {
841         var m map[string]json.RawMessage
842         err := json.Unmarshal(b, &m)
843         if err != nil {
844                 return err
845         }
846         if r, ok := m["boolean"]; ok {
847                 var bval bool
848                 err = json.Unmarshal(r, &bval)
849                 if err != nil {
850                         return err
851                 }
852                 a.B = &bval
853                 return nil
854         }
855         if r, ok := m["integer"]; ok {
856                 var ival int64
857                 err = json.Unmarshal(r, &ival)
858                 if err != nil {
859                         return err
860                 }
861                 a.I = &ival
862                 return nil
863         }
864         r, ok := m["string"]
865         if !ok {
866                 return fmt.Errorf("contract arg must define one of boolean, integer, string")
867         }
868         var sval chainjson.HexBytes
869         err = json.Unmarshal(r, &sval)
870         if err != nil {
871                 return err
872         }
873         a.S = &sval
874         return nil
875 }