Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package gc |
| 6 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 7 | // case OADD: |
| 8 | // if(n->right->op == OLITERAL) { |
| 9 | // v = n->right->vconst; |
| 10 | // naddr(n->left, a, canemitcode); |
| 11 | // } else |
| 12 | // if(n->left->op == OLITERAL) { |
| 13 | // v = n->left->vconst; |
| 14 | // naddr(n->right, a, canemitcode); |
| 15 | // } else |
| 16 | // goto bad; |
| 17 | // a->offset += v; |
| 18 | // break; |
| 19 | |
| 20 | /* |
| 21 | * a function named init is a special case. |
| 22 | * it is called by the initialization before |
| 23 | * main is run. to make it unique within a |
| 24 | * package and also uncallable, the name, |
Russ Cox | f6791da | 2015-02-20 13:54:45 -0500 | [diff] [blame] | 25 | * normally "pkg.init", is altered to "pkg.init.1". |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | var renameinit_initgen int |
| 29 | |
| 30 | func renameinit() *Sym { |
| 31 | renameinit_initgen++ |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 32 | return Lookupf("init.%d", renameinit_initgen) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /* |
| 36 | * hand-craft the following initialization code |
| 37 | * var initdone· uint8 (1) |
| 38 | * func init() (2) |
| 39 | * if initdone· != 0 { (3) |
| 40 | * if initdone· == 2 (4) |
| 41 | * return |
| 42 | * throw(); (5) |
| 43 | * } |
| 44 | * initdone· = 1; (6) |
| 45 | * // over all matching imported symbols |
| 46 | * <pkg>.init() (7) |
| 47 | * { <init stmts> } (8) |
Russ Cox | f6791da | 2015-02-20 13:54:45 -0500 | [diff] [blame] | 48 | * init.<n>() // if any (9) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 49 | * initdone· = 2; (10) |
| 50 | * return (11) |
| 51 | * } |
| 52 | */ |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 53 | func anyinit(n *NodeList) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 54 | // are there any interesting init statements |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 55 | for l := n; l != nil; l = l.Next { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 56 | switch l.N.Op { |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 57 | case ODCLFUNC, ODCLCONST, ODCLTYPE, OEMPTY: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 58 | break |
| 59 | |
Russ Cox | 0ad4f8b | 2015-04-17 00:25:10 -0400 | [diff] [blame] | 60 | case OAS, OASWB: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 61 | if isblank(l.N.Left) && candiscard(l.N.Right) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 62 | break |
| 63 | } |
| 64 | fallthrough |
| 65 | |
| 66 | // fall through |
| 67 | default: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 68 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | // is this main |
| 73 | if localpkg.Name == "main" { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 74 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // is there an explicit init function |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 78 | s := Lookup("init.1") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 79 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 80 | if s.Def != nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 81 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // are there any imported init functions |
Russ Cox | d0b59de | 2015-03-02 16:21:15 -0500 | [diff] [blame] | 85 | for _, s := range initSyms { |
| 86 | if s.Def != nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 87 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | // then none |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 92 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | func fninit(n *NodeList) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 96 | if Debug['A'] != 0 { |
| 97 | // sys.go or unsafe.go during compiler build |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | n = initfix(n) |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 102 | if !anyinit(n) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 103 | return |
| 104 | } |
| 105 | |
Russ Cox | 175929b | 2015-03-02 14:22:05 -0500 | [diff] [blame] | 106 | var r *NodeList |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 107 | |
| 108 | // (1) |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 109 | gatevar := newname(Lookup("initdone·")) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 110 | addvar(gatevar, Types[TUINT8], PEXTERN) |
| 111 | |
| 112 | // (2) |
| 113 | Maxarg = 0 |
| 114 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 115 | fn := Nod(ODCLFUNC, nil, nil) |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 116 | initsym := Lookup("init") |
Russ Cox | bd4fff6 | 2015-05-27 10:42:55 -0400 | [diff] [blame] | 117 | fn.Func.Nname = newname(initsym) |
| 118 | fn.Func.Nname.Name.Defn = fn |
| 119 | fn.Func.Nname.Name.Param.Ntype = Nod(OTFUNC, nil, nil) |
| 120 | declare(fn.Func.Nname, PFUNC) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 121 | funchdr(fn) |
| 122 | |
| 123 | // (3) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 124 | a := Nod(OIF, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 125 | |
Russ Cox | 66be148 | 2015-05-26 21:30:20 -0400 | [diff] [blame] | 126 | a.Left = Nod(ONE, gatevar, Nodintconst(0)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 127 | r = list(r, a) |
| 128 | |
| 129 | // (4) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 130 | b := Nod(OIF, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 131 | |
Russ Cox | 66be148 | 2015-05-26 21:30:20 -0400 | [diff] [blame] | 132 | b.Left = Nod(OEQ, gatevar, Nodintconst(2)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 133 | b.Nbody = list1(Nod(ORETURN, nil, nil)) |
| 134 | a.Nbody = list1(b) |
| 135 | |
| 136 | // (5) |
| 137 | b = syslook("throwinit", 0) |
| 138 | |
| 139 | b = Nod(OCALL, b, nil) |
| 140 | a.Nbody = list(a.Nbody, b) |
| 141 | |
| 142 | // (6) |
| 143 | a = Nod(OAS, gatevar, Nodintconst(1)) |
| 144 | |
| 145 | r = list(r, a) |
| 146 | |
| 147 | // (7) |
Russ Cox | d0b59de | 2015-03-02 16:21:15 -0500 | [diff] [blame] | 148 | for _, s := range initSyms { |
| 149 | if s.Def != nil && s != initsym { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 150 | // could check that it is fn of no args/returns |
| 151 | a = Nod(OCALL, s.Def, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 152 | r = list(r, a) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // (8) |
| 157 | r = concat(r, n) |
| 158 | |
| 159 | // (9) |
| 160 | // could check that it is fn of no args/returns |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 161 | for i := 1; ; i++ { |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 162 | s := Lookupf("init.%d", i) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 163 | if s.Def == nil { |
| 164 | break |
| 165 | } |
| 166 | a = Nod(OCALL, s.Def, nil) |
| 167 | r = list(r, a) |
| 168 | } |
| 169 | |
| 170 | // (10) |
| 171 | a = Nod(OAS, gatevar, Nodintconst(2)) |
| 172 | |
| 173 | r = list(r, a) |
| 174 | |
| 175 | // (11) |
| 176 | a = Nod(ORETURN, nil, nil) |
| 177 | |
| 178 | r = list(r, a) |
Russ Cox | bd4fff6 | 2015-05-27 10:42:55 -0400 | [diff] [blame] | 179 | exportsym(fn.Func.Nname) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 180 | |
| 181 | fn.Nbody = r |
| 182 | funcbody(fn) |
| 183 | |
| 184 | Curfn = fn |
| 185 | typecheck(&fn, Etop) |
| 186 | typechecklist(r, Etop) |
| 187 | Curfn = nil |
| 188 | funccompile(fn) |
| 189 | } |