public static void WriteFormEnum(CodeNamespace n) { // For those architectures that don't have // instruction forms. if (RegisteredForms.Count > 0) { CodeTypeDeclaration decl = new CodeTypeDeclaration(StaticTypeReferences.InstructionFormClassName); decl.Attributes = MemberAttributes.Assembly; decl.IsEnum = true; CodeTypeReference enumType = null; if (RegisteredForms.Count < ushort.MaxValue) { if (RegisteredForms.Count < byte.MaxValue) { enumType = StaticTypeReferences.Byte; } else { enumType = StaticTypeReferences.UShort; } } else { throw new Exception("Yikes that IS a lot of forms!"); } decl.BaseTypes.Add(enumType); foreach (string f in RegisteredForms.Keys) { decl.Members.Add(new CodeMemberField(enumType, f)); } n.Types.Add(decl); } }
public void AddRange(CodeTypeDeclaration[] value) { if (value == null) { throw new ArgumentNullException("value"); } for (int i = 0; i < value.Length; i++) { Add(value[i]); } }
public void Write(CodeNamespace n) { CodeTypeDeclaration decl = new CodeTypeDeclaration(Name); decl.TypeAttributes = TypeAttributes.Public; decl.Attributes = MemberAttributes.Public; decl.IsEnum = true; if (BaseTypeID != -1) { decl.BaseTypes.Add(FieldTypeRegistry.Fields[BaseTypeID].CodeType); } if (Documentation != null) { decl.Documentation.Add(new CodeDocumentationSummaryNode(Documentation)); } foreach (var m in Members) { CodeMemberField fld = new CodeMemberField(decl.Name, m.Name); if (m.Documentation != null) fld.Documentation.Add(new CodeDocumentationSummaryNode(m.Documentation)); if (m.Value != null) { if (m.ShowValueAsHex) { fld.InitExpression = new CodePrimitiveExpression(m.Value.Value); ((CodePrimitiveExpression)fld.InitExpression).PrintAsHex = true; } else { fld.InitExpression = new CodePrimitiveExpression((int)m.Value.Value); } } decl.Members.Add(fld); } n.Types.Add(decl); }
protected override void GenerateConstructor(CodeConstructor constructor, CodeTypeDeclaration declaration) { if (base.IsCurrentDelegate || base.IsCurrentEnum || base.IsCurrentInterface) { return; } this.OutputMemberAccessModifier(constructor.Attributes); base.Output.Write("this("); this.OutputParameters(constructor.Parameters); base.Output.Write(")"); base.OutputStartBrace(); base.Indent++; if (constructor.BaseConstructorArgs.Count > 0) { base.Output.Write("super("); this.OutputExpressionList(constructor.BaseConstructorArgs); base.Output.WriteLine(");"); } if (constructor.ChainedConstructorArgs.Count > 0) { base.Output.Write("this("); this.OutputExpressionList(constructor.ChainedConstructorArgs); base.Output.WriteLine(");"); } base.GenerateStatements(constructor.Statements); base.Indent--; base.Output.WriteLine('}'); }
protected override void GenerateEntryPointMethod(CodeEntryPointMethod method, CodeTypeDeclaration declaration) { base.Output.Write("public static "); this.OutputType(method.ReturnType); base.Output.Write(" Main()"); this.OutputStartBrace(); base.Indent++; base.GenerateStatements(method.Statements); base.Indent--; base.Output.WriteLine("}"); }
protected override void GenerateMethod(CodeMemberMethod method, CodeTypeDeclaration declaration) { if (base.IsCurrentDelegate || base.IsCurrentEnum) { return; } TextWriter output = base.Output; MemberAttributes attributes = method.Attributes; if (!base.IsCurrentInterface) { if (method.PrivateImplementationType == null) { this.OutputMemberAccessModifier(attributes); this.OutputVTableModifier(attributes); this.OutputMemberScopeModifier(attributes); } } else { this.OutputVTableModifier(attributes); } this.OutputType(method.ReturnType); output.Write(' '); CodeTypeReference privateImplementationType = method.PrivateImplementationType; if (privateImplementationType != null) { output.Write(privateImplementationType.BaseType); output.Write('.'); } output.Write(this.GetSafeName(method.Name)); this.GenerateGenericsParameters(method.TypeParameters); output.Write('('); this.OutputParameters(method.Parameters); output.Write(')'); this.GenerateGenericsConstraints(method.TypeParameters); if (CSharpCodeGenerator.IsAbstract(attributes) || declaration.IsInterface) { output.WriteLine(';'); } else { this.OutputStartBrace(); base.Indent++; base.GenerateStatements(method.Statements); base.Indent--; output.WriteLine('}'); } }
protected override void GenerateTypeEnd(CodeTypeDeclaration declaration) { if (!base.IsCurrentDelegate) { base.Indent--; base.Output.WriteLine("}"); } }
protected override void GenerateTypeStart(CodeTypeDeclaration declaration) { TextWriter output = base.Output; if (!base.IsCurrentDelegate) { this.OutputTypeAttributes(declaration); output.Write(this.GetSafeName(declaration.Name)); this.GenerateGenericsParameters(declaration.TypeParameters); IEnumerator enumerator = declaration.BaseTypes.GetEnumerator(); if (enumerator.MoveNext()) { CodeTypeReference t = (CodeTypeReference)enumerator.Current; output.Write(" : "); this.OutputType(t); while (enumerator.MoveNext()) { t = (CodeTypeReference)enumerator.Current; output.Write(", "); this.OutputType(t); } } this.GenerateGenericsConstraints(declaration.TypeParameters); this.OutputStartBrace(); base.Indent++; } else { if ((declaration.TypeAttributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public) { output.Write("public "); } CodeTypeDelegate codeTypeDelegate = (CodeTypeDelegate)declaration; output.Write("delegate "); this.OutputType(codeTypeDelegate.ReturnType); output.Write(" "); output.Write(this.GetSafeName(declaration.Name)); output.Write("("); this.OutputParameters(codeTypeDelegate.Parameters); output.WriteLine(");"); } }
public void Insert(int index, CodeTypeDeclaration value) { List.Insert(index, value); }
protected override void GenerateProperty(CodeMemberProperty property, CodeTypeDeclaration declaration) { if (base.IsCurrentDelegate || base.IsCurrentEnum) { return; } TextWriter output = base.Output; MemberAttributes attributes = property.Attributes; if (!base.IsCurrentInterface) { if (property.PrivateImplementationType == null) { this.OutputMemberAccessModifier(attributes); this.OutputVTableModifier(attributes); this.OutputMemberScopeModifier(attributes); } } else { this.OutputVTableModifier(attributes); } this.OutputType(property.Type); output.Write(' '); if (!base.IsCurrentInterface && property.PrivateImplementationType != null) { output.Write(property.PrivateImplementationType.BaseType); output.Write('.'); } if (string.Compare(property.Name, "Item", true, CultureInfo.InvariantCulture) == 0 && property.Parameters.Count > 0) { output.Write("this["); this.OutputParameters(property.Parameters); output.Write(']'); } else { output.Write(this.GetSafeName(property.Name)); } this.OutputStartBrace(); base.Indent++; if (declaration.IsInterface || CSharpCodeGenerator.IsAbstract(property.Attributes)) { if (property.HasGet) { output.WriteLine("get;"); } if (property.HasSet) { output.WriteLine("set;"); } } else { if (property.HasGet) { output.Write("get"); this.OutputStartBrace(); base.Indent++; base.GenerateStatements(property.GetStatements); base.Indent--; output.WriteLine('}'); } if (property.HasSet) { output.Write("set"); this.OutputStartBrace(); base.Indent++; base.GenerateStatements(property.SetStatements); base.Indent--; output.WriteLine('}'); } } base.Indent--; output.WriteLine('}'); }
public bool Contains(CodeTypeDeclaration value) { return List.Contains(value); }
public void Write(CodeTypeDeclaration td) { CodeMemberMethod mthd = new CodeMemberMethod(); mthd.Name = Name; mthd.Attributes = MemberAttributes.Public; mthd.Attributes |= MemberAttributes.Static; mthd.Parameters.Add(new CodeParameterDeclarationExpression(StaticTypeReferences.Stream, StaticTypeReferences.Emit_StreamArgName)); foreach (BitPatternArgument arg in Args) { CodeTypeReference pt = null; if (arg.Size <= 8) pt = StaticTypeReferences.Byte; else if (arg.Size <= 16) pt = StaticTypeReferences.UShort; else if (arg.Size <= 32) pt = StaticTypeReferences.UInt; else if (arg.Size <= 64) pt = StaticTypeReferences.ULong; else throw new Exception("Geeze, that's a giant argument!"); mthd.Parameters.Add(new CodeParameterDeclarationExpression(pt, arg.Name)); } foreach (BitPatternPiece pc in Pieces) { pc.Write(mthd); } td.Members.Add(mthd); }
public void CopyTo(CodeTypeDeclaration[] array, int index) { List.CopyTo(array, index); }
public void Write(string outDir, CodeNamespace nmspc) { ExpandForms(); EvaluateOverrides(); bool lonely = FinalForms.Count == 1; CodeTypeDeclaration td = new CodeTypeDeclaration(FileName); td.Attributes = MemberAttributes.Final; td.Attributes |= MemberAttributes.Public; td.IsClass = true; td.BaseTypes.Add(StaticTypeReferences.Instruction); td.Documentation.Add(new CodeDocumentationSummaryNode(LinesOfDocumentation)); foreach (InstructionForm f in FinalForms) { f.RequestFields(); } foreach (var v in FieldDeclarations) { td.Members.Add(v); } foreach (InstructionForm f in FinalForms) { f.WriteConstructors(td, FileName); } CodeMemberMethod mth = new CodeMemberMethod(); mth.Documentation.AddRange( new CodeDocumentationSummaryNode("Write this instruction to a stream."), new CodeDocumentationParameterNode(StaticTypeReferences.Emit_StreamArgName, "The stream to write to.") ); mth.Name = "Emit"; mth.Parameters.Add(new CodeParameterDeclarationExpression(StaticTypeReferences.Stream, StaticTypeReferences.Emit_StreamArgName)); mth.Attributes = MemberAttributes.Public; mth.Attributes |= MemberAttributes.Override; mth.Attributes |= MemberAttributes.Sealed; mth.ReturnType = StaticTypeReferences.Void; CodeSwitchStatement instructionFormSwitch = null; if (!lonely) { instructionFormSwitch = new CodeSwitchStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "InstructionForm")); mth.Statements.Add(instructionFormSwitch); } foreach (InstructionForm f in FinalForms) { if (NeedsEmission(f)) { CodeScopeStatement con = new CodeScopeStatement(); if (lonely) { mth.Statements.Add(con); } else { CodeCaseStatement cas = new CodeCaseStatement( new CodeFieldReferenceExpression(StaticTypeReferences.InstructionFormExpression, f.GetInstructionCaseString()) ); cas.Statements.Add(con); cas.Statements.Add(new CodeBreakStatement()); instructionFormSwitch.Cases.Add(cas); } f.WriteEmit(con, false); } } if (!lonely) { CodeDefaultCaseStatement defStat = new CodeDefaultCaseStatement(); defStat.Statements.Add( new CodeThrowExceptionStatement( new CodeObjectCreateExpression( StaticTypeReferences.Exception, new CodePrimitiveExpression("Unknown Instruction Form!") ) ) ); instructionFormSwitch.Cases.Add(defStat); } td.Members.Add(mth); instructionFormSwitch = null; FormsEmitted = new Dictionary<InstructionArgSet, bool>(); mth = new CodeMemberMethod(); bool hasSyntax = StaticTypeReferences.AssemblySyntaxClassName != null; if (hasSyntax) { mth.Documentation.AddRange( new CodeDocumentationSummaryNode( "Get a string representation of this instruction in the", "specified assembly syntax." ), new CodeDocumentationParameterNode(StaticTypeReferences.ToString_SyntaxArgName, "The syntax to get the string representation in.") ); } else { mth.Documentation.Add(new CodeDocumentationSummaryNode("Get a string representation of this instruction.")); } mth.Name = "ToString"; mth.Attributes = MemberAttributes.Public; mth.Attributes |= MemberAttributes.Override; mth.Attributes |= MemberAttributes.Sealed; mth.ReturnType = StaticTypeReferences.String; if (!hasSyntax) { if (!lonely) { instructionFormSwitch = new CodeSwitchStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "InstructionForm")); mth.Statements.Add(instructionFormSwitch); } foreach (InstructionForm f in FinalForms) { if (NeedsEmission(f)) { CodeScopeStatement con = new CodeScopeStatement(); if (lonely) { mth.Statements.Add(con); } else { CodeCaseStatement cas = new CodeCaseStatement( new CodeFieldReferenceExpression( StaticTypeReferences.InstructionFormExpression, f.GetInstructionCaseString() ) ); cas.Statements.Add(con); instructionFormSwitch.Cases.Add(cas); } f.WriteToString(con); } } if (!lonely) { CodeDefaultCaseStatement defStat = new CodeDefaultCaseStatement(); defStat.Statements.Add( new CodeThrowExceptionStatement( new CodeObjectCreateExpression( StaticTypeReferences.Exception, new CodePrimitiveExpression("Unknown Instruction Form!") ) ) ); instructionFormSwitch.Cases.Add(defStat); } } else { mth.Parameters.Add(new CodeParameterDeclarationExpression(StaticTypeReferences.AssemblySyntax, StaticTypeReferences.ToString_SyntaxArgName)); CodeSwitchStatement sw = new CodeSwitchStatement(StaticTypeReferences.ToString_SyntaxArg); mth.Statements.Add(sw); CodeCaseStatement cs = new CodeCaseStatement( new CodeFieldReferenceExpression( StaticTypeReferences.AssemblySyntaxExpression, "NASM" ) ); if (!lonely) { instructionFormSwitch = new CodeSwitchStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "InstructionForm")); cs.Statements.Add(instructionFormSwitch); } foreach (InstructionForm f in FinalForms) { if (NeedsEmission(f)) { CodeScopeStatement con = new CodeScopeStatement(); if (lonely) { cs.Statements.Add(con); } else { CodeCaseStatement cas = new CodeCaseStatement( new CodeFieldReferenceExpression(StaticTypeReferences.InstructionFormExpression, f.GetInstructionCaseString()) ); cas.Statements.Add(con); instructionFormSwitch.Cases.Add(cas); } f.WriteToString(con); } } if (!lonely) { CodeDefaultCaseStatement defStat = new CodeDefaultCaseStatement(); defStat.Statements.Add( new CodeThrowExceptionStatement( new CodeObjectCreateExpression( StaticTypeReferences.Exception, new CodePrimitiveExpression("Unknown Instruction Form!") ) ) ); instructionFormSwitch.Cases.Add(defStat); } sw.Cases.Add(cs); cs = new CodeCaseStatement( new CodeFieldReferenceExpression( StaticTypeReferences.AssemblySyntaxExpression, "GAS" ) ); sw.Cases.Add(cs); CodeDefaultCaseStatement def = new CodeDefaultCaseStatement(); def.Statements.Add( new CodeThrowExceptionStatement( new CodeObjectCreateExpression( StaticTypeReferences.Exception, new CodePrimitiveExpression("Unknown Instruction Form!") ) ) ); sw.Cases.Add(def); } td.Members.Add(mth); nmspc.Types.Add(td); }
// // Methods // public int Add(CodeTypeDeclaration value) { return List.Add(value); }
public CodeTypeDeclarationCollection(CodeTypeDeclaration[] value) { AddRange(value); }
public void Remove(CodeTypeDeclaration value) { List.Remove(value); }
protected override void OutputTypeAttributes(CodeTypeDeclaration declaration) { TextWriter output = base.Output; TypeAttributes typeAttributes = declaration.TypeAttributes; switch (typeAttributes & TypeAttributes.VisibilityMask) { case TypeAttributes.NotPublic: case TypeAttributes.NestedAssembly: case TypeAttributes.NestedFamANDAssem: output.Write("internal "); break; case TypeAttributes.Public: case TypeAttributes.NestedPublic: output.Write("public "); break; case TypeAttributes.NestedPrivate: output.Write("private "); break; case TypeAttributes.NestedFamily: output.Write("protected "); break; case TypeAttributes.VisibilityMask: output.Write("protected internal "); break; } if ((declaration.Attributes & MemberAttributes.New) != (MemberAttributes)0) { output.Write("new "); } // BUGFIX: This previously was not checked, producing a non-sealed class when // a sealed one was expected. The same goes for static. bool wasFinal = false; if ((declaration.Attributes & MemberAttributes.ScopeMask) == MemberAttributes.Final) { wasFinal = true; output.Write("final "); } else if ((declaration.Attributes & MemberAttributes.ScopeMask) == MemberAttributes.Static) { output.Write("static "); } if (declaration.IsStruct) { if (declaration.IsPartial) { output.Write("partial "); } output.Write("struct "); } else { if (declaration.IsEnum) { output.Write("enum "); } else { if ((typeAttributes & TypeAttributes.ClassSemanticsMask) != TypeAttributes.NotPublic) { if (declaration.IsPartial) { output.Write("partial "); } output.Write("interface "); } else { if ((typeAttributes & TypeAttributes.Sealed) == TypeAttributes.Sealed && !wasFinal) { output.Write("final "); } if ((typeAttributes & TypeAttributes.Abstract) != TypeAttributes.NotPublic) { output.Write("abstract "); } if (declaration.IsPartial) { output.Write("partial "); } output.Write("class "); } } } }
public static void WritePatterns(CodeNamespace nmspc) { if (Patterns.Count > 0) { CodeTypeDeclaration td = new CodeTypeDeclaration("BitPatterns"); td.Attributes = MemberAttributes.Static; td.Attributes |= MemberAttributes.Public; td.TypeAttributes = TypeAttributes.Class; td.TypeAttributes |= TypeAttributes.Public; // Deal with aliases. Dictionary<BitPattern, bool> pats = new Dictionary<BitPattern, bool>(); foreach (BitPattern pat in Patterns.Values) { if (!pats.ContainsKey(pat)) { pat.Write(td); pats[pat] = true; } } nmspc.Types.Add(td); } }
public void WriteConstructors(CodeTypeDeclaration tdecl, string className) { if (ParentInstruction.LinesOfDocumentation.Count == 0) { ParentInstruction.LinesOfDocumentation.Add("Creates a new instance of the <see cref=\"" + ParentInstruction.FileName + "\" /> class."); } if (Arg1.ArgType == InstructionArgType.None) { CodeConstructor c = new CodeConstructor(); c.Attributes = MemberAttributes.Public; c.Documentation.AddRange( new CodeDocumentationSummaryNode(ParentInstruction.LinesOfDocumentation), new CodeDocumentationParameterNode("parentAssembler", "The assembler to add this instruction to.") ); c.Parameters.Add(new CodeParameterDeclarationExpression(StaticTypeReferences.Assembler, "parentAssembler")); c.BaseConstructorArgs.Add(new CodeArgumentReferenceExpression("parentAssembler")); tdecl.Members.Add(c); } else { List<byte> sizesNeeded = new List<byte>(4); List<string> formsNeeded = new List<string>(4); bool constructorNeeded; bool needsSizeArg = ParentInstruction.NeedsSizeArgument(Arg1.ArgType, Arg2.ArgType, Arg3.ArgType, ref sizesNeeded, out constructorNeeded, ref formsNeeded); if (constructorNeeded) { int segArgIdx; bool needsSegArg = NeedsSegment(out segArgIdx); CodeConstructor c = new CodeConstructor(); c.Attributes = MemberAttributes.Public; c.Parameters.Add(new CodeParameterDeclarationExpression(StaticTypeReferences.Assembler, "parentAssembler")); c.BaseConstructorArgs.Add(new CodeArgumentReferenceExpression("parentAssembler")); Arg1.ArgType.RequestParameters(Arg1, needsSizeArg, c.Parameters); Arg2.ArgType.RequestParameters(Arg2, needsSizeArg, c.Parameters); Arg3.ArgType.RequestParameters(Arg3, needsSizeArg, c.Parameters); if (needsSizeArg) c.Parameters.Add(new CodeParameterDeclarationExpression(StaticTypeReferences.Byte, "size")); if (needsSegArg) { CodeParameterDeclarationExpression cpd = new CodeParameterDeclarationExpression(StaticTypeReferences.Segment, "segment"); cpd.DefaultValueExpression = new CodeFieldReferenceExpression(StaticTypeReferences.SegmentExpression, DefaultSegment.ToString()); c.Parameters.Add(cpd); } CodeDocumentationNodeCollection docs = new CodeDocumentationNodeCollection(); docs.Add(new CodeDocumentationParameterNode("parentAssembler", "The assembler to add this instruction to.")); Arg1.ArgType.AddDocumentationLines(Arg1.Name, docs); Arg2.ArgType.AddDocumentationLines(Arg2.Name, docs); Arg3.ArgType.AddDocumentationLines(Arg3.Name, docs); if (needsSizeArg) docs.Add(new CodeDocumentationParameterNode("size", "The size of the operands for this instruction.")); if (needsSegArg) docs.Add(new CodeDocumentationParameterNode("segment", "The segment to use for memory access within this instruction.")); c.Documentation.Add(new CodeDocumentationSummaryNode(ParentInstruction.LinesOfDocumentation)); c.Documentation.AddRange(docs); WriteConstructorBodyInstructionFormDetermination(c, sizesNeeded, formsNeeded); Arg1.ArgType.WriteConstructorBodyArgProcessing(this, c, 0); Arg2.ArgType.WriteConstructorBodyArgProcessing(this, c, 1); Arg3.ArgType.WriteConstructorBodyArgProcessing(this, c, 2); if (needsSegArg) { c.Statements.Add( new CodeAssignStatement( new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), GetArgName(FieldTypeRegistry.Segment.ID, 1, segArgIdx)), new CodeArgumentReferenceExpression("segment") ) ); } tdecl.Members.Add(c); } } }
public int IndexOf(CodeTypeDeclaration value) { return List.IndexOf(value); }