Skip to content

Commit 145d8ac

Browse files
authored
Minor flow improvements (AssemblyScript#924)
1 parent 93a5045 commit 145d8ac

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

src/compiler.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ export class Compiler extends DiagnosticEmitter {
12881288
)
12891289
)
12901290
);
1291-
flow.setLocalFlag(index, LocalFlags.RETAINED);
1291+
flow.setLocalFlag(index, LocalFlags.RETAINED | LocalFlags.PARAMETER);
12921292
}
12931293
}
12941294

@@ -2252,8 +2252,10 @@ export class Compiler extends DiagnosticEmitter {
22522252
if (!this.skippedAutoreleases.has(expr)) {
22532253
if (returnType.isManaged) {
22542254
if (getExpressionId(expr) == ExpressionId.LocalGet) {
2255-
if (flow.isAnyLocalFlag(getLocalGetIndex(expr), LocalFlags.ANY_RETAINED)) {
2256-
flow.unsetLocalFlag(getLocalGetIndex(expr), LocalFlags.ANY_RETAINED);
2255+
let index = getLocalGetIndex(expr);
2256+
if (flow.isAnyLocalFlag(index, LocalFlags.ANY_RETAINED)) {
2257+
flow.unsetLocalFlag(index, LocalFlags.ANY_RETAINED);
2258+
flow.setLocalFlag(index, LocalFlags.RETURNED);
22572259
this.skippedAutoreleases.add(expr);
22582260
}
22592261
}
@@ -6248,23 +6250,18 @@ export class Compiler extends DiagnosticEmitter {
62486250
let initExpr = this.compileExpression(
62496251
assert(instance.prototype.functionTypeNode.parameters[i].initializer),
62506252
initType,
6251-
Constraints.CONV_IMPLICIT
6253+
Constraints.CONV_IMPLICIT | Constraints.WILL_RETAIN
62526254
);
62536255
let argumentLocal = flow.addScopedLocal(signature.getParameterName(i), initType);
62546256
if (!flow.canOverflow(initExpr, initType)) flow.setLocalFlag(argumentLocal.index, LocalFlags.WRAPPED);
62556257
if (flow.isNonnull(initExpr, initType)) flow.setLocalFlag(argumentLocal.index, LocalFlags.NONNULL);
62566258
if (initType.isManaged) {
62576259
flow.setLocalFlag(argumentLocal.index, LocalFlags.RETAINED);
6258-
body.push(
6259-
module.local_set(argumentLocal.index,
6260-
this.makeRetain(initExpr)
6261-
)
6262-
);
6263-
} else {
6264-
body.push(
6265-
module.local_set(argumentLocal.index, initExpr)
6266-
);
6260+
if (!this.skippedAutoreleases.has(initExpr)) initExpr = this.makeRetain(initExpr);
62676261
}
6262+
body.push(
6263+
module.local_set(argumentLocal.index, initExpr)
6264+
);
62686265
}
62696266

62706267
// Compile the called function's body in the scope of the inlined flow

src/flow.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,41 +144,58 @@ export enum LocalFlags {
144144

145145
/** Local is constant. */
146146
CONSTANT = 1 << 0,
147+
/** Local is a function parameter. */
148+
PARAMETER = 1 << 1,
147149
/** Local is properly wrapped. Relevant for small integers. */
148-
WRAPPED = 1 << 1,
150+
WRAPPED = 1 << 2,
149151
/** Local is non-null. */
150-
NONNULL = 1 << 2,
152+
NONNULL = 1 << 3,
151153
/** Local is read from. */
152-
READFROM = 1 << 3,
154+
READFROM = 1 << 4,
153155
/** Local is written to. */
154-
WRITTENTO = 1 << 4,
156+
WRITTENTO = 1 << 5,
155157
/** Local is retained. */
156-
RETAINED = 1 << 5,
158+
RETAINED = 1 << 6,
159+
/** Local is returned. */
160+
RETURNED = 1 << 7,
157161

158162
/** Local is conditionally read from. */
159-
CONDITIONALLY_READFROM = 1 << 6,
163+
CONDITIONALLY_READFROM = 1 << 8,
160164
/** Local is conditionally written to. */
161-
CONDITIONALLY_WRITTENTO = 1 << 7,
165+
CONDITIONALLY_WRITTENTO = 1 << 9,
162166
/** Local must be conditionally retained. */
163-
CONDITIONALLY_RETAINED = 1 << 8,
167+
CONDITIONALLY_RETAINED = 1 << 10,
168+
/** Local is conditionally returned. */
169+
CONDITIONALLY_RETURNED = 1 << 11,
164170

165171
/** Any categorical flag. */
166172
ANY_CATEGORICAL = CONSTANT
173+
| PARAMETER
167174
| WRAPPED
168175
| NONNULL
169176
| READFROM
170177
| WRITTENTO
171-
| RETAINED,
178+
| RETAINED
179+
| RETURNED,
172180

173181
/** Any conditional flag. */
174182
ANY_CONDITIONAL = RETAINED
175183
| CONDITIONALLY_READFROM
176184
| CONDITIONALLY_WRITTENTO
177-
| CONDITIONALLY_RETAINED,
185+
| CONDITIONALLY_RETAINED
186+
| CONDITIONALLY_RETURNED,
187+
188+
/** Any written to flag. */
189+
ANY_WRITTENTO = WRITTENTO
190+
| CONDITIONALLY_WRITTENTO,
178191

179192
/** Any retained flag. */
180193
ANY_RETAINED = RETAINED
181-
| CONDITIONALLY_RETAINED
194+
| CONDITIONALLY_RETAINED,
195+
196+
/** Any returned flag. */
197+
ANY_RETURNED = RETURNED
198+
| CONDITIONALLY_RETURNED
182199
}
183200
export namespace LocalFlags {
184201
export function join(left: LocalFlags, right: LocalFlags): LocalFlags {
@@ -565,6 +582,7 @@ export class Flow {
565582
if (flags & LocalFlags.RETAINED) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_RETAINED);
566583
if (flags & LocalFlags.READFROM) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_READFROM);
567584
if (flags & LocalFlags.WRITTENTO) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_WRITTENTO);
585+
if (flags & LocalFlags.RETURNED) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_RETURNED);
568586
}
569587
}
570588

0 commit comments

Comments
 (0)