| 1 |
/* |
| 2 |
Copyright (C) 2008-2012 Jeroen Frijters |
| 3 |
|
| 4 |
This software is provided 'as-is', without any express or implied |
| 5 |
warranty. In no event will the authors be held liable for any damages |
| 6 |
arising from the use of this software. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely, subject to the following restrictions: |
| 11 |
|
| 12 |
1. The origin of this software must not be misrepresented; you must not |
| 13 |
claim that you wrote the original software. If you use this software |
| 14 |
in a product, an acknowledgment in the product documentation would be |
| 15 |
appreciated but is not required. |
| 16 |
2. Altered source versions must be plainly marked as such, and must not be |
| 17 |
misrepresented as being the original software. |
| 18 |
3. This notice may not be removed or altered from any source distribution. |
| 19 |
|
| 20 |
Jeroen Frijters |
| 21 |
[email protected] |
| 22 |
|
| 23 |
*/ |
| 24 |
using System; |
| 25 |
using System.Runtime.InteropServices; |
| 26 |
using System.Collections.Generic; |
| 27 |
#if !NO_SYMBOL_WRITER |
| 28 |
using System.Diagnostics.SymbolStore; |
| 29 |
#endif |
| 30 |
using System.Diagnostics; |
| 31 |
using IKVM.Reflection.Writer; |
| 32 |
|
| 33 |
namespace IKVM.Reflection.Emit |
| 34 |
{ |
| 35 |
public struct Label |
| 36 |
{ |
| 37 |
// 1-based here, to make sure that an uninitialized Label isn't valid |
| 38 |
private readonly int index1; |
| 39 |
|
| 40 |
internal Label(int index) |
| 41 |
{ |
| 42 |
this.index1 = index + 1; |
| 43 |
} |
| 44 |
|
| 45 |
internal int Index |
| 46 |
{ |
| 47 |
get { return index1 - 1; } |
| 48 |
} |
| 49 |
|
| 50 |
public bool Equals(Label other) |
| 51 |
{ |
| 52 |
return other.index1 == index1; |
| 53 |
} |
| 54 |
|
| 55 |
public override bool Equals(object obj) |
| 56 |
{ |
| 57 |
return this == obj as Label?; |
| 58 |
} |
| 59 |
|
| 60 |
public override int GetHashCode() |
| 61 |
{ |
| 62 |
return index1; |
| 63 |
} |
| 64 |
|
| 65 |
public static bool operator ==(Label arg1, Label arg2) |
| 66 |
{ |
| 67 |
return arg1.index1 == arg2.index1; |
| 68 |
} |
| 69 |
|
| 70 |
public static bool operator !=(Label arg1, Label arg2) |
| 71 |
{ |
| 72 |
return !(arg1 == arg2); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public sealed class LocalBuilder : LocalVariableInfo |
| 77 |
{ |
| 78 |
internal string name; |
| 79 |
internal int startOffset; |
| 80 |
internal int endOffset; |
| 81 |
|
| 82 |
internal LocalBuilder(Type localType, int index, bool pinned) |
| 83 |
: base(index, localType, pinned) |
| 84 |
{ |
| 85 |
} |
| 86 |
|
| 87 |
internal LocalBuilder(Type localType, int index, bool pinned, CustomModifiers customModifiers) |
| 88 |
: base(index, localType, pinned, customModifiers) |
| 89 |
{ |
| 90 |
} |
| 91 |
|
| 92 |
public void SetLocalSymInfo(string name) |
| 93 |
{ |
| 94 |
this.name = name; |
| 95 |
} |
| 96 |
|
| 97 |
public void SetLocalSymInfo(string name, int startOffset, int endOffset) |
| 98 |
{ |
| 99 |
this.name = name; |
| 100 |
this.startOffset = startOffset; |
| 101 |
this.endOffset = endOffset; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public sealed class ILGenerator |
| 106 |
{ |
| 107 |
private readonly ModuleBuilder moduleBuilder; |
| 108 |
private readonly ByteBuffer code; |
| 109 |
private readonly SignatureHelper locals; |
| 110 |
private int localsCount; |
| 111 |
private readonly List<int> tokenFixups = new List<int>(); |
| 112 |
private readonly List<int> labels = new List<int>(); |
| 113 |
private readonly List<int> labelStackHeight = new List<int>(); |
| 114 |
private readonly List<LabelFixup> labelFixups = new List<LabelFixup>(); |
| 115 |
#if !NO_SYMBOL_WRITER |
| 116 |
private readonly List<SequencePoint> sequencePoints = new List<SequencePoint>(); |
| 117 |
#endif |
| 118 |
private readonly List<ExceptionBlock> exceptions = new List<ExceptionBlock>(); |
| 119 |
private readonly Stack<ExceptionBlock> exceptionStack = new Stack<ExceptionBlock>(); |
| 120 |
private ushort maxStack; |
| 121 |
private bool fatHeader; |
| 122 |
private int stackHeight; |
| 123 |
private Scope scope; |
| 124 |
private byte exceptionBlockAssistanceMode = EBAM_COMPAT; |
| 125 |
private const byte EBAM_COMPAT = 0; |
| 126 |
private const byte EBAM_DISABLE = 1; |
| 127 |
private const byte EBAM_CLEVER = 2; |
| 128 |
|
| 129 |
private struct LabelFixup |
| 130 |
{ |
| 131 |
internal int label; |
| 132 |
internal int offset; |
| 133 |
} |
| 134 |
|
| 135 |
internal sealed class ExceptionBlock : IComparer<ExceptionBlock> |
| 136 |
{ |
| 137 |
internal readonly int ordinal; |
| 138 |
internal Label labelEnd; |
| 139 |
internal int tryOffset; |
| 140 |
internal int tryLength; |
| 141 |
internal int handlerOffset; |
| 142 |
internal int handlerLength; |
| 143 |
internal int filterOffsetOrExceptionTypeToken; |
| 144 |
internal ExceptionHandlingClauseOptions kind; |
| 145 |
|
| 146 |
internal ExceptionBlock(int ordinal) |
| 147 |
{ |
| 148 |
this.ordinal = ordinal; |
| 149 |
} |
| 150 |
|
| 151 |
internal ExceptionBlock(ExceptionHandler h) |
| 152 |
{ |
| 153 |
this.ordinal = -1; |
| 154 |
this.tryOffset = h.TryOffset; |
| 155 |
this.tryLength = h.TryLength; |
| 156 |
this.handlerOffset = h.HandlerOffset; |
| 157 |
this.handlerLength = h.HandlerLength; |
| 158 |
this.kind = h.Kind; |
| 159 |
this.filterOffsetOrExceptionTypeToken = kind == ExceptionHandlingClauseOptions.Filter ? h.FilterOffset : h.ExceptionTypeToken; |
| 160 |
} |
| 161 |
|
| 162 |
int IComparer<ExceptionBlock>.Compare(ExceptionBlock x, ExceptionBlock y) |
| 163 |
{ |
| 164 |
// Mono's sort insists on doing unnecessary comparisons |
| 165 |
if (x == y) |
| 166 |
{ |
| 167 |
return 0; |
| 168 |
} |
| 169 |
else if (x.tryOffset == y.tryOffset && x.tryLength == y.tryLength) |
| 170 |
{ |
| 171 |
return x.ordinal < y.ordinal ? -1 : 1; |
| 172 |
} |
| 173 |
else if (x.tryOffset >= y.tryOffset && x.handlerOffset + x.handlerLength <= y.handlerOffset + y.handlerLength) |
| 174 |
{ |
| 175 |
return -1; |
| 176 |
} |
| 177 |
else if (y.tryOffset >= x.tryOffset && y.handlerOffset + y.handlerLength <= x.handlerOffset + x.handlerLength) |
| 178 |
{ |
| 179 |
return 1; |
| 180 |
} |
| 181 |
else |
| 182 |
{ |
| 183 |
return x.ordinal < y.ordinal ? -1 : 1; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
private struct SequencePoint |
| 189 |
{ |
| 190 |
#if !NO_SYMBOL_WRITER |
| 191 |
internal ISymbolDocumentWriter document; |
| 192 |
internal int offset; |
| 193 |
internal int startLine; |
| 194 |
internal int startColumn; |
| 195 |
internal int endLine; |
| 196 |
internal int endColumn; |
| 197 |
#endif |
| 198 |
} |
| 199 |
|
| 200 |
private sealed class Scope |
| 201 |
{ |
| 202 |
internal readonly Scope parent; |
| 203 |
internal readonly List<Scope> children = new List<Scope>(); |
| 204 |
internal readonly List<LocalBuilder> locals = new List<LocalBuilder>(); |
| 205 |
internal int startOffset; |
| 206 |
internal int endOffset; |
| 207 |
|
| 208 |
internal Scope(Scope parent) |
| 209 |
{ |
| 210 |
this.parent = parent; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
internal ILGenerator(ModuleBuilder moduleBuilder, int initialCapacity) |
| 215 |
{ |
| 216 |
this.code = new ByteBuffer(initialCapacity); |
| 217 |
this.moduleBuilder = moduleBuilder; |
| 218 |
this.locals = SignatureHelper.GetLocalVarSigHelper(moduleBuilder); |
| 219 |
if (moduleBuilder.symbolWriter != null) |
| 220 |
{ |
| 221 |
scope = new Scope(null); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
// non-standard API |
| 226 |
public void __DisableExceptionBlockAssistance() |
| 227 |
{ |
| 228 |
exceptionBlockAssistanceMode = EBAM_DISABLE; |
| 229 |
} |
| 230 |
|
| 231 |
// non-standard API |
| 232 |
public void __CleverExceptionBlockAssistance() |
| 233 |
{ |
| 234 |
exceptionBlockAssistanceMode = EBAM_CLEVER; |
| 235 |
} |
| 236 |
|
| 237 |
// non-standard API |
| 238 |
public int __MaxStackSize |
| 239 |
{ |
| 240 |
get { return maxStack; } |
| 241 |
set |
| 242 |
{ |
| 243 |
maxStack = (ushort)value; |
| 244 |
fatHeader = true; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
// non-standard API |
| 249 |
// returns -1 if the current position is currently unreachable |
| 250 |
public int __StackHeight |
| 251 |
{ |
| 252 |
get { return stackHeight; } |
| 253 |
} |
| 254 |
|
| 255 |
// new in .NET 4.0 |
| 256 |
public int ILOffset |
| 257 |
{ |
| 258 |
get { return code.Position; } |
| 259 |
} |
| 260 |
|
| 261 |
public void BeginCatchBlock(Type exceptionType) |
| 262 |
{ |
| 263 |
if (exceptionType == null) |
| 264 |
{ |
| 265 |
// this must be a catch block after a filter |
| 266 |
ExceptionBlock block = exceptionStack.Peek(); |
| 267 |
if (block.kind != ExceptionHandlingClauseOptions.Filter || block.handlerOffset != 0) |
| 268 |
{ |
| 269 |
throw new ArgumentNullException("exceptionType"); |
| 270 |
} |
| 271 |
if (exceptionBlockAssistanceMode == EBAM_COMPAT || (exceptionBlockAssistanceMode == EBAM_CLEVER && stackHeight != -1)) |
| 272 |
{ |
| 273 |
Emit(OpCodes.Endfilter); |
| 274 |
} |
| 275 |
stackHeight = 0; |
| 276 |
UpdateStack(1); |
| 277 |
block.handlerOffset = code.Position; |
| 278 |
} |
| 279 |
else |
| 280 |
{ |
| 281 |
ExceptionBlock block = BeginCatchOrFilterBlock(); |
| 282 |
block.kind = ExceptionHandlingClauseOptions.Clause; |
| 283 |
block.filterOffsetOrExceptionTypeToken = moduleBuilder.GetTypeTokenForMemberRef(exceptionType); |
| 284 |
block.handlerOffset = code.Position; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
private ExceptionBlock BeginCatchOrFilterBlock() |
| 289 |
{ |
| 290 |
ExceptionBlock block = exceptionStack.Peek(); |
| 291 |
if (exceptionBlockAssistanceMode == EBAM_COMPAT || (exceptionBlockAssistanceMode == EBAM_CLEVER && stackHeight != -1)) |
| 292 |
{ |
| 293 |
Emit(OpCodes.Leave, block.labelEnd); |
| 294 |
} |
| 295 |
stackHeight = 0; |
| 296 |
UpdateStack(1); |
| 297 |
if (block.tryLength == 0) |
| 298 |
{ |
| 299 |
block.tryLength = code.Position - block.tryOffset; |
| 300 |
} |
| 301 |
else |
| 302 |
{ |
| 303 |
block.handlerLength = code.Position - block.handlerOffset; |
| 304 |
exceptionStack.Pop(); |
| 305 |
ExceptionBlock newBlock = new ExceptionBlock(exceptions.Count); |
| 306 |
newBlock.labelEnd = block.labelEnd; |
| 307 |
newBlock.tryOffset = block.tryOffset; |
| 308 |
newBlock.tryLength = block.tryLength; |
| 309 |
block = newBlock; |
| 310 |
exceptions.Add(block); |
| 311 |
exceptionStack.Push(block); |
| 312 |
} |
| 313 |
return block; |
| 314 |
} |
| 315 |
|
| 316 |
public Label BeginExceptionBlock() |
| 317 |
{ |
| 318 |
ExceptionBlock block = new ExceptionBlock(exceptions.Count); |
| 319 |
block.labelEnd = DefineLabel(); |
| 320 |
block.tryOffset = code.Position; |
| 321 |
exceptionStack.Push(block); |
| 322 |
exceptions.Add(block); |
| 323 |
stackHeight = 0; |
| 324 |
return block.labelEnd; |
| 325 |
} |
| 326 |
|
| 327 |
public void BeginExceptFilterBlock() |
| 328 |
{ |
| 329 |
ExceptionBlock block = BeginCatchOrFilterBlock(); |
| 330 |
block.kind = ExceptionHandlingClauseOptions.Filter; |
| 331 |
block.filterOffsetOrExceptionTypeToken = code.Position; |
| 332 |
} |
| 333 |
|
| 334 |
public void BeginFaultBlock() |
| 335 |
{ |
| 336 |
BeginFinallyFaultBlock(ExceptionHandlingClauseOptions.Fault); |
| 337 |
} |
| 338 |
|
| 339 |
public void BeginFinallyBlock() |
| 340 |
{ |
| 341 |
BeginFinallyFaultBlock(ExceptionHandlingClauseOptions.Finally); |
| 342 |
} |
| 343 |
|
| 344 |
private void BeginFinallyFaultBlock(ExceptionHandlingClauseOptions kind) |
| 345 |
{ |
| 346 |
ExceptionBlock block = exceptionStack.Peek(); |
| 347 |
if (exceptionBlockAssistanceMode == EBAM_COMPAT || (exceptionBlockAssistanceMode == EBAM_CLEVER && stackHeight != -1)) |
| 348 |
{ |
| 349 |
Emit(OpCodes.Leave, block.labelEnd); |
| 350 |
} |
| 351 |
if (block.handlerOffset == 0) |
| 352 |
{ |
| 353 |
block.tryLength = code.Position - block.tryOffset; |
| 354 |
} |
| 355 |
else |
| 356 |
{ |
| 357 |
block.handlerLength = code.Position - block.handlerOffset; |
| 358 |
Label labelEnd; |
| 359 |
if (exceptionBlockAssistanceMode != EBAM_COMPAT) |
| 360 |
{ |
| 361 |
labelEnd = block.labelEnd; |
| 362 |
} |
| 363 |
else |
| 364 |
{ |
| 365 |
MarkLabel(block.labelEnd); |
| 366 |
labelEnd = DefineLabel(); |
| 367 |
Emit(OpCodes.Leave, labelEnd); |
| 368 |
} |
| 369 |
exceptionStack.Pop(); |
| 370 |
ExceptionBlock newBlock = new ExceptionBlock(exceptions.Count); |
| 371 |
newBlock.labelEnd = labelEnd; |
| 372 |
newBlock.tryOffset = block.tryOffset; |
| 373 |
newBlock.tryLength = code.Position - block.tryOffset; |
| 374 |
block = newBlock; |
| 375 |
exceptions.Add(block); |
| 376 |
exceptionStack.Push(block); |
| 377 |
} |
| 378 |
block.handlerOffset = code.Position; |
| 379 |
block.kind = kind; |
| 380 |
stackHeight = 0; |
| 381 |
} |
| 382 |
|
| 383 |
public void EndExceptionBlock() |
| 384 |
{ |
| 385 |
ExceptionBlock block = exceptionStack.Pop(); |
| 386 |
if (exceptionBlockAssistanceMode == EBAM_COMPAT || (exceptionBlockAssistanceMode == EBAM_CLEVER && stackHeight != -1)) |
| 387 |
{ |
| 388 |
if (block.kind != ExceptionHandlingClauseOptions.Finally && block.kind != ExceptionHandlingClauseOptions.Fault) |
| 389 |
{ |
| 390 |
Emit(OpCodes.Leave, block.labelEnd); |
| 391 |
} |
| 392 |
else |
| 393 |
{ |
| 394 |
Emit(OpCodes.Endfinally); |
| 395 |
} |
| 396 |
} |
| 397 |
MarkLabel(block.labelEnd); |
| 398 |
block.handlerLength = code.Position - block.handlerOffset; |
| 399 |
} |
| 400 |
|
| 401 |
public void BeginScope() |
| 402 |
{ |
| 403 |
Scope newScope = new Scope(scope); |
| 404 |
scope.children.Add(newScope); |
| 405 |
scope = newScope; |
| 406 |
scope.startOffset = code.Position; |
| 407 |
} |
| 408 |
|
| 409 |
public void UsingNamespace(string usingNamespace) |
| 410 |
{ |
| 411 |
#if !NO_SYMBOL_WRITER |
| 412 |
if (moduleBuilder.symbolWriter != null) |
| 413 |
{ |
| 414 |
moduleBuilder.symbolWriter.UsingNamespace(usingNamespace); |
| 415 |
} |
| 416 |
#endif |
| 417 |
} |
| 418 |
|
| 419 |
public LocalBuilder DeclareLocal(Type localType) |
| 420 |
{ |
| 421 |
return DeclareLocal(localType, false); |
| 422 |
} |
| 423 |
|
| 424 |
public LocalBuilder DeclareLocal(Type localType, bool pinned) |
| 425 |
{ |
| 426 |
LocalBuilder local = new LocalBuilder(localType, localsCount++, pinned); |
| 427 |
locals.AddArgument(localType, pinned); |
| 428 |
if (scope != null) |
| 429 |
{ |
| 430 |
scope.locals.Add(local); |
| 431 |
} |
| 432 |
return local; |
| 433 |
} |
| 434 |
|
| 435 |
public LocalBuilder __DeclareLocal(Type localType, bool pinned, CustomModifiers customModifiers) |
| 436 |
{ |
| 437 |
LocalBuilder local = new LocalBuilder(localType, localsCount++, pinned, customModifiers); |
| 438 |
locals.__AddArgument(localType, pinned, customModifiers); |
| 439 |
if (scope != null) |
| 440 |
{ |
| 441 |
scope.locals.Add(local); |
| 442 |
} |
| 443 |
return local; |
| 444 |
} |
| 445 |
|
| 446 |
public Label DefineLabel() |
| 447 |
{ |
| 448 |
Label label = new Label(labels.Count); |
| 449 |
labels.Add(-1); |
| 450 |
labelStackHeight.Add(-1); |
| 451 |
return label; |
| 452 |
} |
| 453 |
|
| 454 |
public void Emit(OpCode opc) |
| 455 |
{ |
| 456 |
Debug.Assert(opc != OpCodes.Ret || (opc == OpCodes.Ret && stackHeight <= 1)); |
| 457 |
if (opc.Value < 0) |
| 458 |
{ |
| 459 |
code.Write((byte)(opc.Value >> 8)); |
| 460 |
} |
| 461 |
code.Write((byte)opc.Value); |
| 462 |
switch (opc.FlowControl) |
| 463 |
{ |
| 464 |
case FlowControl.Branch: |
| 465 |
case FlowControl.Break: |
| 466 |
case FlowControl.Return: |
| 467 |
case FlowControl.Throw: |
| 468 |
stackHeight = -1; |
| 469 |
break; |
| 470 |
default: |
| 471 |
UpdateStack(opc.StackDiff); |
| 472 |
break; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
private void UpdateStack(int stackdiff) |
| 477 |
{ |
| 478 |
if (stackHeight == -1) |
| 479 |
{ |
| 480 |
// we're about to emit code that is either unreachable or reachable only via a backward branch |
| 481 |
stackHeight = 0; |
| 482 |
} |
| 483 |
Debug.Assert(stackHeight >= 0 && stackHeight <= ushort.MaxValue); |
| 484 |
stackHeight += stackdiff; |
| 485 |
Debug.Assert(stackHeight >= 0 && stackHeight <= ushort.MaxValue); |
| 486 |
maxStack = Math.Max(maxStack, (ushort)stackHeight); |
| 487 |
} |
| 488 |
|
| 489 |
public void Emit(OpCode opc, byte arg) |
| 490 |
{ |
| 491 |
Emit(opc); |
| 492 |
code.Write(arg); |
| 493 |
} |
| 494 |
|
| 495 |
public void Emit(OpCode opc, double arg) |
| 496 |
{ |
| 497 |
Emit(opc); |
| 498 |
code.Write(arg); |
| 499 |
} |
| 500 |
|
| 501 |
public void Emit(OpCode opc, FieldInfo field) |
| 502 |
{ |
| 503 |
Emit(opc); |
| 504 |
WriteToken(moduleBuilder.GetFieldToken(field).Token); |
| 505 |
} |
| 506 |
|
| 507 |
public void Emit(OpCode opc, short arg) |
| 508 |
{ |
| 509 |
Emit(opc); |
| 510 |
code.Write(arg); |
| 511 |
} |
| 512 |
|
| 513 |
public void Emit(OpCode opc, int arg) |
| 514 |
{ |
| 515 |
Emit(opc); |
| 516 |
code.Write(arg); |
| 517 |
} |
| 518 |
|
| 519 |
public void Emit(OpCode opc, long arg) |
| 520 |
{ |
| 521 |
Emit(opc); |
| 522 |
code.Write(arg); |
| 523 |
} |
| 524 |
|
| 525 |
public void Emit(OpCode opc, Label label) |
| 526 |
{ |
| 527 |
// We need special stackHeight handling for unconditional branches, |
| 528 |
// because the branch and next flows have differing stack heights. |
| 529 |
// Note that this assumes that unconditional branches do not push/pop. |
| 530 |
int flowStackHeight = this.stackHeight; |
| 531 |
Emit(opc); |
| 532 |
if (opc == OpCodes.Leave || opc == OpCodes.Leave_S) |
| 533 |
{ |
| 534 |
flowStackHeight = 0; |
| 535 |
} |
| 536 |
else if (opc.FlowControl != FlowControl.Branch) |
| 537 |
{ |
| 538 |
flowStackHeight = this.stackHeight; |
| 539 |
} |
| 540 |
// if the label has already been marked, we can emit the branch offset directly |
| 541 |
if (labels[label.Index] != -1) |
| 542 |
{ |
| 543 |
if (labelStackHeight[label.Index] != flowStackHeight && (labelStackHeight[label.Index] != 0 || flowStackHeight != -1)) |
| 544 |
{ |
| 545 |
// the "backward branch constraint" prohibits this, so we don't need to support it |
| 546 |
throw new NotSupportedException("'Backward branch constraints' violated"); |
| 547 |
} |
| 548 |
if (opc.OperandType == OperandType.ShortInlineBrTarget) |
| 549 |
{ |
| 550 |
WriteByteBranchOffset(labels[label.Index] - (code.Position + 1)); |
| 551 |
} |
| 552 |
else |
| 553 |
{ |
| 554 |
code.Write(labels[label.Index] - (code.Position + 4)); |
| 555 |
} |
| 556 |
} |
| 557 |
else |
| 558 |
{ |
| 559 |
Debug.Assert(labelStackHeight[label.Index] == -1 || labelStackHeight[label.Index] == flowStackHeight || (flowStackHeight == -1 && labelStackHeight[label.Index] == 0)); |
| 560 |
labelStackHeight[label.Index] = flowStackHeight; |
| 561 |
LabelFixup fix = new LabelFixup(); |
| 562 |
fix.label = label.Index; |
| 563 |
fix.offset = code.Position; |
| 564 |
labelFixups.Add(fix); |
| 565 |
if (opc.OperandType == OperandType.ShortInlineBrTarget) |
| 566 |
{ |
| 567 |
code.Write((byte)1); |
| 568 |
} |
| 569 |
else |
| 570 |
{ |
| 571 |
code.Write(4); |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
private void WriteByteBranchOffset(int offset) |
| 577 |
{ |
| 578 |
if (offset < -128 || offset > 127) |
| 579 |
{ |
| 580 |
throw new NotSupportedException("Branch offset of " + offset + " does not fit in one-byte branch target at position " + code.Position); |
| 581 |
} |
| 582 |
code.Write((byte)offset); |
| 583 |
} |
| 584 |
|
| 585 |
public void Emit(OpCode opc, Label[] labels) |
| 586 |
{ |
| 587 |
Emit(opc); |
| 588 |
LabelFixup fix = new LabelFixup(); |
| 589 |
fix.label = -1; |
| 590 |
fix.offset = code.Position; |
| 591 |
labelFixups.Add(fix); |
| 592 |
code.Write(labels.Length); |
| 593 |
foreach (Label label in labels) |
| 594 |
{ |
| 595 |
code.Write(label.Index); |
| 596 |
if (this.labels[label.Index] != -1) |
| 597 |
{ |
| 598 |
if (labelStackHeight[label.Index] != stackHeight) |
| 599 |
{ |
| 600 |
// the "backward branch constraint" prohibits this, so we don't need to support it |
| 601 |
throw new NotSupportedException(); |
| 602 |
} |
| 603 |
} |
| 604 |
else |
| 605 |
{ |
| 606 |
Debug.Assert(labelStackHeight[label.Index] == -1 || labelStackHeight[label.Index] == stackHeight); |
| 607 |
labelStackHeight[label.Index] = stackHeight; |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
public void Emit(OpCode opc, LocalBuilder local) |
| 613 |
{ |
| 614 |
if ((opc == OpCodes.Ldloc || opc == OpCodes.Ldloca || opc == OpCodes.Stloc) && local.LocalIndex < 256) |
| 615 |
{ |
| 616 |
if (opc == OpCodes.Ldloc) |
| 617 |
{ |
| 618 |
switch (local.LocalIndex) |
| 619 |
{ |
| 620 |
case 0: |
| 621 |
Emit(OpCodes.Ldloc_0); |
| 622 |
break; |
| 623 |
case 1: |
| 624 |
Emit(OpCodes.Ldloc_1); |
| 625 |
break; |
| 626 |
case 2: |
| 627 |
Emit(OpCodes.Ldloc_2); |
| 628 |
break; |
| 629 |
case 3: |
| 630 |
Emit(OpCodes.Ldloc_3); |
| 631 |
break; |
| 632 |
default: |
| 633 |
Emit(OpCodes.Ldloc_S); |
| 634 |
code.Write((byte)local.LocalIndex); |
| 635 |
break; |
| 636 |
} |
| 637 |
} |
| 638 |
else if (opc == OpCodes.Ldloca) |
| 639 |
{ |
| 640 |
Emit(OpCodes.Ldloca_S); |
| 641 |
code.Write((byte)local.LocalIndex); |
| 642 |
} |
| 643 |
else if (opc == OpCodes.Stloc) |
| 644 |
{ |
| 645 |
switch (local.LocalIndex) |
| 646 |
{ |
| 647 |
case 0: |
| 648 |
Emit(OpCodes.Stloc_0); |
| 649 |
break; |
| 650 |
case 1: |
| 651 |
Emit(OpCodes.Stloc_1); |
| 652 |
break; |
| 653 |
case 2: |
| 654 |
Emit(OpCodes.Stloc_2); |
| 655 |
break; |
| 656 |
case 3: |
| 657 |
Emit(OpCodes.Stloc_3); |
| 658 |
break; |
| 659 |
default: |
| 660 |
Emit(OpCodes.Stloc_S); |
| 661 |
code.Write((byte)local.LocalIndex); |
| 662 |
break; |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
else |
| 667 |
{ |
| 668 |
Emit(opc); |
| 669 |
switch (opc.OperandType) |
| 670 |
{ |
| 671 |
case OperandType.InlineVar: |
| 672 |
code.Write((ushort)local.LocalIndex); |
| 673 |
break; |
| 674 |
case OperandType.ShortInlineVar: |
| 675 |
code.Write((byte)local.LocalIndex); |
| 676 |
break; |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
private void WriteToken(int token) |
| 682 |
{ |
| 683 |
if (ModuleBuilder.IsPseudoToken(token)) |
| 684 |
{ |
| 685 |
tokenFixups.Add(code.Position); |
| 686 |
} |
| 687 |
code.Write(token); |
| 688 |
} |
| 689 |
|
| 690 |
private void UpdateStack(OpCode opc, bool hasthis, Type returnType, int parameterCount) |
| 691 |
{ |
| 692 |
if (opc == OpCodes.Jmp) |
| 693 |
{ |
| 694 |
stackHeight = -1; |
| 695 |
} |
| 696 |
else if (opc.FlowControl == FlowControl.Call) |
| 697 |
{ |
| 698 |
int stackdiff = 0; |
| 699 |
if ((hasthis && opc != OpCodes.Newobj) || opc == OpCodes.Calli) |
| 700 |
{ |
| 701 |
// pop this |
| 702 |
stackdiff--; |
| 703 |
} |
| 704 |
// pop parameters |
| 705 |
stackdiff -= parameterCount; |
| 706 |
if (returnType != moduleBuilder.universe.System_Void) |
| 707 |
{ |
| 708 |
// push return value |
| 709 |
stackdiff++; |
| 710 |
} |
| 711 |
UpdateStack(stackdiff); |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
public void Emit(OpCode opc, MethodInfo method) |
| 716 |
{ |
| 717 |
UpdateStack(opc, method.HasThis, method.ReturnType, method.ParameterCount); |
| 718 |
Emit(opc); |
| 719 |
WriteToken(moduleBuilder.GetMethodTokenForIL(method).Token); |
| 720 |
} |
| 721 |
|
| 722 |
public void Emit(OpCode opc, ConstructorInfo constructor) |
| 723 |
{ |
| 724 |
Emit(opc, constructor.GetMethodInfo()); |
| 725 |
} |
| 726 |
|
| 727 |
public void Emit(OpCode opc, sbyte arg) |
| 728 |
{ |
| 729 |
Emit(opc); |
| 730 |
code.Write(arg); |
| 731 |
} |
| 732 |
|
| 733 |
public void Emit(OpCode opc, float arg) |
| 734 |
{ |
| 735 |
Emit(opc); |
| 736 |
code.Write(arg); |
| 737 |
} |
| 738 |
|
| 739 |
public void Emit(OpCode opc, string str) |
| 740 |
{ |
| 741 |
Emit(opc); |
| 742 |
code.Write(moduleBuilder.GetStringConstant(str).Token); |
| 743 |
} |
| 744 |
|
| 745 |
public void Emit(OpCode opc, Type type) |
| 746 |
{ |
| 747 |
Emit(opc); |
| 748 |
if (opc == OpCodes.Ldtoken) |
| 749 |
{ |
| 750 |
code.Write(moduleBuilder.GetTypeToken(type).Token); |
| 751 |
} |
| 752 |
else |
| 753 |
{ |
| 754 |
code.Write(moduleBuilder.GetTypeTokenForMemberRef(type)); |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
public void Emit(OpCode opcode, SignatureHelper signature) |
| 759 |
{ |
| 760 |
Emit(opcode); |
| 761 |
UpdateStack(opcode, signature.HasThis, signature.ReturnType, signature.ParameterCount); |
| 762 |
code.Write(moduleBuilder.GetSignatureToken(signature).Token); |
| 763 |
} |
| 764 |
|
| 765 |
public void EmitCall(OpCode opc, MethodInfo method, Type[] optionalParameterTypes) |
| 766 |
{ |
| 767 |
__EmitCall(opc, method, optionalParameterTypes, null); |
| 768 |
} |
| 769 |
|
| 770 |
public void __EmitCall(OpCode opc, MethodInfo method, Type[] optionalParameterTypes, CustomModifiers[] customModifiers) |
| 771 |
{ |
| 772 |
if (optionalParameterTypes == null || optionalParameterTypes.Length == 0) |
| 773 |
{ |
| 774 |
Emit(opc, method); |
| 775 |
} |
| 776 |
else |
| 777 |
{ |
| 778 |
Emit(opc); |
| 779 |
UpdateStack(opc, method.HasThis, method.ReturnType, method.ParameterCount + optionalParameterTypes.Length); |
| 780 |
code.Write(moduleBuilder.__GetMethodToken(method, optionalParameterTypes, customModifiers).Token); |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
public void __EmitCall(OpCode opc, ConstructorInfo constructor, Type[] optionalParameterTypes) |
| 785 |
{ |
| 786 |
EmitCall(opc, constructor.GetMethodInfo(), optionalParameterTypes); |
| 787 |
} |
| 788 |
|
| 789 |
public void __EmitCall(OpCode opc, ConstructorInfo constructor, Type[] optionalParameterTypes, CustomModifiers[] customModifiers) |
| 790 |
{ |
| 791 |
__EmitCall(opc, constructor.GetMethodInfo(), optionalParameterTypes, customModifiers); |
| 792 |
} |
| 793 |
|
| 794 |
public void EmitCalli(OpCode opc, CallingConvention callingConvention, Type returnType, Type[] parameterTypes) |
| 795 |
{ |
| 796 |
SignatureHelper sig = SignatureHelper.GetMethodSigHelper(moduleBuilder, callingConvention, returnType); |
| 797 |
sig.AddArguments(parameterTypes, null, null); |
| 798 |
Emit(opc, sig); |
| 799 |
} |
| 800 |
|
| 801 |
public void EmitCalli(OpCode opc, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type[] optionalParameterTypes) |
| 802 |
{ |
| 803 |
SignatureHelper sig = SignatureHelper.GetMethodSigHelper(moduleBuilder, callingConvention, returnType); |
| 804 |
sig.AddArguments(parameterTypes, null, null); |
| 805 |
if (optionalParameterTypes != null && optionalParameterTypes.Length != 0) |
| 806 |
{ |
| 807 |
sig.AddSentinel(); |
| 808 |
sig.AddArguments(optionalParameterTypes, null, null); |
| 809 |
} |
| 810 |
Emit(opc, sig); |
| 811 |
} |
| 812 |
|
| 813 |
public void __EmitCalli(OpCode opc, __StandAloneMethodSig sig) |
| 814 |
{ |
| 815 |
Emit(opc); |
| 816 |
if (sig.IsUnmanaged) |
| 817 |
{ |
| 818 |
UpdateStack(opc, false, sig.ReturnType, sig.ParameterCount); |
| 819 |
} |
| 820 |
else |
| 821 |
{ |
| 822 |
CallingConventions callingConvention = sig.CallingConvention; |
| 823 |
UpdateStack(opc, (callingConvention & CallingConventions.HasThis | CallingConventions.ExplicitThis) == CallingConventions.HasThis, sig.ReturnType, sig.ParameterCount); |
| 824 |
} |
| 825 |
ByteBuffer bb = new ByteBuffer(16); |
| 826 |
Signature.WriteStandAloneMethodSig(moduleBuilder, bb, sig); |
| 827 |
code.Write(0x11000000 | moduleBuilder.StandAloneSig.FindOrAddRecord(moduleBuilder.Blobs.Add(bb))); |
| 828 |
} |
| 829 |
|
| 830 |
public void EmitWriteLine(string text) |
| 831 |
{ |
| 832 |
Universe u = moduleBuilder.universe; |
| 833 |
Emit(OpCodes.Ldstr, text); |
| 834 |
Emit(OpCodes.Call, u.Import(typeof(Console)).GetMethod("WriteLine", new Type[] { u.System_String })); |
| 835 |
} |
| 836 |
|
| 837 |
public void EmitWriteLine(FieldInfo field) |
| 838 |
{ |
| 839 |
Universe u = moduleBuilder.universe; |
| 840 |
Emit(OpCodes.Call, u.Import(typeof(Console)).GetMethod("get_Out")); |
| 841 |
if (field.IsStatic) |
| 842 |
{ |
| 843 |
Emit(OpCodes.Ldsfld, field); |
| 844 |
} |
| 845 |
else |
| 846 |
{ |
| 847 |
Emit(OpCodes.Ldarg_0); |
| 848 |
Emit(OpCodes.Ldfld, field); |
| 849 |
} |
| 850 |
Emit(OpCodes.Callvirt, u.Import(typeof(System.IO.TextWriter)).GetMethod("WriteLine", new Type[] { field.FieldType })); |
| 851 |
} |
| 852 |
|
| 853 |
public void EmitWriteLine(LocalBuilder local) |
| 854 |
{ |
| 855 |
Universe u = moduleBuilder.universe; |
| 856 |
Emit(OpCodes.Call, u.Import(typeof(Console)).GetMethod("get_Out")); |
| 857 |
Emit(OpCodes.Ldloc, local); |
| 858 |
Emit(OpCodes.Callvirt, u.Import(typeof(System.IO.TextWriter)).GetMethod("WriteLine", new Type[] { local.LocalType })); |
| 859 |
} |
| 860 |
|
| 861 |
public void EndScope() |
| 862 |
{ |
| 863 |
scope.endOffset = code.Position; |
| 864 |
scope = scope.parent; |
| 865 |
} |
| 866 |
|
| 867 |
public void MarkLabel(Label loc) |
| 868 |
{ |
| 869 |
Debug.Assert(stackHeight == -1 || labelStackHeight[loc.Index] == -1 || stackHeight == labelStackHeight[loc.Index]); |
| 870 |
labels[loc.Index] = code.Position; |
| 871 |
if (labelStackHeight[loc.Index] == -1) |
| 872 |
{ |
| 873 |
if (stackHeight == -1) |
| 874 |
{ |
| 875 |
// We're at a location that can only be reached by a backward branch, |
| 876 |
// so according to the "backward branch constraint" that must mean the stack is empty, |
| 877 |
// but note that this may be an unused label followed by another label that is used and |
| 878 |
// that does have a non-zero stack height, so we don't yet set stackHeight here. |
| 879 |
labelStackHeight[loc.Index] = 0; |
| 880 |
} |
| 881 |
else |
| 882 |
{ |
| 883 |
labelStackHeight[loc.Index] = stackHeight; |
| 884 |
} |
| 885 |
} |
| 886 |
else |
| 887 |
{ |
| 888 |
Debug.Assert(stackHeight == -1 || stackHeight == labelStackHeight[loc.Index]); |
| 889 |
stackHeight = labelStackHeight[loc.Index]; |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
#if !NO_SYMBOL_WRITER |
| 894 |
public void MarkSequencePoint(ISymbolDocumentWriter document, int startLine, int startColumn, int endLine, int endColumn) |
| 895 |
{ |
| 896 |
SequencePoint sp = new SequencePoint(); |
| 897 |
sp.document = document; |
| 898 |
sp.offset = code.Position; |
| 899 |
sp.startLine = startLine; |
| 900 |
sp.startColumn = startColumn; |
| 901 |
sp.endLine = endLine; |
| 902 |
sp.endColumn = endColumn; |
| 903 |
sequencePoints.Add(sp); |
| 904 |
} |
| 905 |
#endif |
| 906 |
|
| 907 |
public void ThrowException(Type excType) |
| 908 |
{ |
| 909 |
Emit(OpCodes.Newobj, excType.GetConstructor(Type.EmptyTypes)); |
| 910 |
Emit(OpCodes.Throw); |
| 911 |
} |
| 912 |
|
| 913 |
internal int WriteBody(bool initLocals) |
| 914 |
{ |
| 915 |
if (moduleBuilder.symbolWriter != null) |
| 916 |
{ |
| 917 |
Debug.Assert(scope != null && scope.parent == null); |
| 918 |
scope.endOffset = code.Position; |
| 919 |
} |
| 920 |
|
| 921 |
ResolveBranches(); |
| 922 |
|
| 923 |
ByteBuffer bb = moduleBuilder.methodBodies; |
| 924 |
|
| 925 |
int localVarSigTok = 0; |
| 926 |
|
| 927 |
int rva; |
| 928 |
if (localsCount == 0 && exceptions.Count == 0 && maxStack <= 8 && code.Length < 64 && !fatHeader) |
| 929 |
{ |
| 930 |
rva = WriteTinyHeaderAndCode(bb); |
| 931 |
} |
| 932 |
else |
| 933 |
{ |
| 934 |
if (localsCount != 0) |
| 935 |
{ |
| 936 |
localVarSigTok = moduleBuilder.GetSignatureToken(locals).Token; |
| 937 |
} |
| 938 |
rva = WriteFatHeaderAndCode(bb, localVarSigTok, initLocals); |
| 939 |
} |
| 940 |
|
| 941 |
#if !NO_SYMBOL_WRITER |
| 942 |
if (moduleBuilder.symbolWriter != null) |
| 943 |
{ |
| 944 |
if (sequencePoints.Count != 0) |
| 945 |
{ |
| 946 |
ISymbolDocumentWriter document = sequencePoints[0].document; |
| 947 |
int[] offsets = new int[sequencePoints.Count]; |
| 948 |
int[] lines = new int[sequencePoints.Count]; |
| 949 |
int[] columns = new int[sequencePoints.Count]; |
| 950 |
int[] endLines = new int[sequencePoints.Count]; |
| 951 |
int[] endColumns = new int[sequencePoints.Count]; |
| 952 |
for (int i = 0; i < sequencePoints.Count; i++) |
| 953 |
{ |
| 954 |
if (sequencePoints[i].document != document) |
| 955 |
{ |
| 956 |
throw new NotImplementedException(); |
| 957 |
} |
| 958 |
offsets[i] = sequencePoints[i].offset; |
| 959 |
lines[i] = sequencePoints[i].startLine; |
| 960 |
columns[i] = sequencePoints[i].startColumn; |
| 961 |
endLines[i] = sequencePoints[i].endLine; |
| 962 |
endColumns[i] = sequencePoints[i].endColumn; |
| 963 |
} |
| 964 |
moduleBuilder.symbolWriter.DefineSequencePoints(document, offsets, lines, columns, endLines, endColumns); |
| 965 |
} |
| 966 |
|
| 967 |
WriteScope(scope, localVarSigTok); |
| 968 |
} |
| 969 |
#endif |
| 970 |
return rva; |
| 971 |
} |
| 972 |
|
| 973 |
private void ResolveBranches() |
| 974 |
{ |
| 975 |
foreach (LabelFixup fixup in labelFixups) |
| 976 |
{ |
| 977 |
// is it a switch? |
| 978 |
if (fixup.label == -1) |
| 979 |
{ |
| 980 |
code.Position = fixup.offset; |
| 981 |
int count = code.GetInt32AtCurrentPosition(); |
| 982 |
int offset = fixup.offset + 4 + 4 * count; |
| 983 |
code.Position += 4; |
| 984 |
for (int i = 0; i < count; i++) |
| 985 |
{ |
| 986 |
int index = code.GetInt32AtCurrentPosition(); |
| 987 |
code.Write(labels[index] - offset); |
| 988 |
} |
| 989 |
} |
| 990 |
else |
| 991 |
{ |
| 992 |
code.Position = fixup.offset; |
| 993 |
byte size = code.GetByteAtCurrentPosition(); |
| 994 |
int branchOffset = labels[fixup.label] - (code.Position + size); |
| 995 |
if (size == 1) |
| 996 |
{ |
| 997 |
WriteByteBranchOffset(branchOffset); |
| 998 |
} |
| 999 |
else |
| 1000 |
{ |
| 1001 |
code.Write(branchOffset); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
internal static void WriteTinyHeader(ByteBuffer bb, int length) |
| 1008 |
{ |
| 1009 |
const byte CorILMethod_TinyFormat = 0x2; |
| 1010 |
bb.Write((byte)(CorILMethod_TinyFormat | (length << 2))); |
| 1011 |
} |
| 1012 |
|
| 1013 |
private int WriteTinyHeaderAndCode(ByteBuffer bb) |
| 1014 |
{ |
| 1015 |
int rva = bb.Position; |
| 1016 |
WriteTinyHeader(bb, code.Length); |
| 1017 |
AddTokenFixups(bb.Position, moduleBuilder.tokenFixupOffsets, tokenFixups); |
| 1018 |
bb.Write(code); |
| 1019 |
return rva; |
| 1020 |
} |
| 1021 |
|
| 1022 |
internal static void WriteFatHeader(ByteBuffer bb, bool initLocals, bool exceptions, ushort maxStack, int codeLength, int localVarSigTok) |
| 1023 |
{ |
| 1024 |
const byte CorILMethod_FatFormat = 0x03; |
| 1025 |
const byte CorILMethod_MoreSects = 0x08; |
| 1026 |
const byte CorILMethod_InitLocals = 0x10; |
| 1027 |
|
| 1028 |
short flagsAndSize = (short)(CorILMethod_FatFormat | (3 << 12)); |
| 1029 |
if (initLocals) |
| 1030 |
{ |
| 1031 |
flagsAndSize |= CorILMethod_InitLocals; |
| 1032 |
} |
| 1033 |
|
| 1034 |
if (exceptions) |
| 1035 |
{ |
| 1036 |
flagsAndSize |= CorILMethod_MoreSects; |
| 1037 |
} |
| 1038 |
|
| 1039 |
bb.Write(flagsAndSize); |
| 1040 |
bb.Write(maxStack); |
| 1041 |
bb.Write(codeLength); |
| 1042 |
bb.Write(localVarSigTok); |
| 1043 |
} |
| 1044 |
|
| 1045 |
private int WriteFatHeaderAndCode(ByteBuffer bb, int localVarSigTok, bool initLocals) |
| 1046 |
{ |
| 1047 |
// fat headers require 4-byte alignment |
| 1048 |
bb.Align(4); |
| 1049 |
int rva = bb.Position; |
| 1050 |
WriteFatHeader(bb, initLocals, exceptions.Count > 0, maxStack, code.Length, localVarSigTok); |
| 1051 |
AddTokenFixups(bb.Position, moduleBuilder.tokenFixupOffsets, tokenFixups); |
| 1052 |
bb.Write(code); |
| 1053 |
if (exceptions.Count > 0) |
| 1054 |
{ |
| 1055 |
exceptions.Sort(exceptions[0]); |
| 1056 |
WriteExceptionHandlers(bb, exceptions); |
| 1057 |
} |
| 1058 |
return rva; |
| 1059 |
} |
| 1060 |
|
| 1061 |
internal static void WriteExceptionHandlers(ByteBuffer bb, List<ExceptionBlock> exceptions) |
| 1062 |
{ |
| 1063 |
bb.Align(4); |
| 1064 |
|
| 1065 |
bool fat = false; |
| 1066 |
if (exceptions.Count * 12 + 4 > 255) |
| 1067 |
{ |
| 1068 |
fat = true; |
| 1069 |
} |
| 1070 |
else |
| 1071 |
{ |
| 1072 |
foreach (ExceptionBlock block in exceptions) |
| 1073 |
{ |
| 1074 |
if (block.tryOffset > 65535 || block.tryLength > 255 || block.handlerOffset > 65535 || block.handlerLength > 255) |
| 1075 |
{ |
| 1076 |
fat = true; |
| 1077 |
break; |
| 1078 |
} |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
const byte CorILMethod_Sect_EHTable = 0x1; |
| 1083 |
const byte CorILMethod_Sect_FatFormat = 0x40; |
| 1084 |
|
| 1085 |
if (fat) |
| 1086 |
{ |
| 1087 |
bb.Write((byte)(CorILMethod_Sect_EHTable | CorILMethod_Sect_FatFormat)); |
| 1088 |
int dataSize = exceptions.Count * 24 + 4; |
| 1089 |
bb.Write((byte)dataSize); |
| 1090 |
bb.Write((short)(dataSize >> 8)); |
| 1091 |
foreach (ExceptionBlock block in exceptions) |
| 1092 |
{ |
| 1093 |
bb.Write((int)block.kind); |
| 1094 |
bb.Write(block.tryOffset); |
| 1095 |
bb.Write(block.tryLength); |
| 1096 |
bb.Write(block.handlerOffset); |
| 1097 |
bb.Write(block.handlerLength); |
| 1098 |
bb.Write(block.filterOffsetOrExceptionTypeToken); |
| 1099 |
} |
| 1100 |
} |
| 1101 |
else |
| 1102 |
{ |
| 1103 |
bb.Write(CorILMethod_Sect_EHTable); |
| 1104 |
bb.Write((byte)(exceptions.Count * 12 + 4)); |
| 1105 |
bb.Write((short)0); |
| 1106 |
foreach (ExceptionBlock block in exceptions) |
| 1107 |
{ |
| 1108 |
bb.Write((short)block.kind); |
| 1109 |
bb.Write((short)block.tryOffset); |
| 1110 |
bb.Write((byte)block.tryLength); |
| 1111 |
bb.Write((short)block.handlerOffset); |
| 1112 |
bb.Write((byte)block.handlerLength); |
| 1113 |
bb.Write(block.filterOffsetOrExceptionTypeToken); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
internal static void AddTokenFixups(int codeOffset, List<int> tokenFixupOffsets, IEnumerable<int> tokenFixups) |
| 1119 |
{ |
| 1120 |
foreach (int fixup in tokenFixups) |
| 1121 |
{ |
| 1122 |
tokenFixupOffsets.Add(fixup + codeOffset); |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
#if !NO_SYMBOL_WRITER |
| 1127 |
private void WriteScope(Scope scope, int localVarSigTok) |
| 1128 |
{ |
| 1129 |
moduleBuilder.symbolWriter.OpenScope(scope.startOffset); |
| 1130 |
foreach (LocalBuilder local in scope.locals) |
| 1131 |
{ |
| 1132 |
if (local.name != null) |
| 1133 |
{ |
| 1134 |
int startOffset = local.startOffset; |
| 1135 |
int endOffset = local.endOffset; |
| 1136 |
if (startOffset == 0 && endOffset == 0) |
| 1137 |
{ |
| 1138 |
startOffset = scope.startOffset; |
| 1139 |
endOffset = scope.endOffset; |
| 1140 |
} |
| 1141 |
moduleBuilder.symbolWriter.DefineLocalVariable2(local.name, 0, localVarSigTok, SymAddressKind.ILOffset, local.LocalIndex, 0, 0, startOffset, endOffset); |
| 1142 |
} |
| 1143 |
} |
| 1144 |
foreach (Scope child in scope.children) |
| 1145 |
{ |
| 1146 |
WriteScope(child, localVarSigTok); |
| 1147 |
} |
| 1148 |
moduleBuilder.symbolWriter.CloseScope(scope.endOffset); |
| 1149 |
} |
| 1150 |
#endif |
| 1151 |
} |
| 1152 |
} |