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);
			}
		}
		protected override void AddDefaultImports(CodeNamespace n)
		{
			n.Imports.Add(new CodeNamespaceImport("std.conv"));
			n.Imports.Add(new CodeNamespaceImport("std.string"));
			n.Imports.Add(new CodeNamespaceImport(RootNamespace + ".Core"));
			if (n.Name != RootNamespace)
			{
				n.Imports.Add(new CodeNamespaceImport(RootNamespace + ".Instructions.Core"));
			}
			string cun = n.Name;
			string filNam;
			if (cun != RootNamespace)
			{
				int li = cun.LastIndexOf('.');
				filNam = cun.Substring(li + 1);
				cun = cun.Substring(0, li) + ".Instructions";
			}
			else
			{
				cun += ".Instructions";
				filNam = "Core";
			}
			n.Name = cun + "." + filNam;
			GeneratedNamespaces.Add(n.Name);
		}
		protected override void AddDefaultImports(CodeNamespace n)
		{
			n.Imports.Add(new CodeNamespaceImport("System"));
			if (n.Name != RootNamespace)
			{
				n.Imports.Add(new CodeNamespaceImport(RootNamespace));
			}
		}
		public void AddRange(CodeNamespace[] value)
		{
			if (value == null)
			{
				throw new ArgumentNullException("value");
			}

			for (int i = 0; i < value.Length; i++)
			{
				Add(value[i]);
			}
		}
		public virtual CodeNamespace GetNamespace(string name)
		{
			CodeNamespace nm;
			if (!Namespaces.TryGetValue(name, out nm))
			{
				CodeCompileUnit cu = new CodeCompileUnit();
				CodeNamespace n = new CodeNamespace(name);
				AddDefaultImports(n);
				cu.Namespaces.Add(n);
				CompileUnits.Add(cu);
				Namespaces[name] = n;
				nm = n;
			}
			return nm;
		}
Beispiel #6
0
		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 GenerateNamespaceStart(CodeNamespace ns)
		{
			base.Output.Write("module ");
			base.Output.Write(ns.Name);
			base.Output.WriteLine(";");
			base.Output.WriteLine();
		}
		protected override void GenerateNamespaceEnd(CodeNamespace ns) { }
		public void Insert(int index, CodeNamespace value)
		{
			List.Insert(index, value);
		}
		public int IndexOf(CodeNamespace value)
		{
			return List.IndexOf(value);
		}
		protected override void GenerateNamespaceStart(CodeNamespace ns)
		{
			TextWriter output = base.Output;
			string name = ns.Name;
			if (name != null && name.Length != 0)
			{
				output.Write("namespace ");
				output.Write(this.GetSafeName(name));
				this.OutputStartBrace();
				base.Indent++;
			}
		}
		protected abstract void AddDefaultImports(CodeNamespace n);
		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);
			}
		}
		protected override void GenerateNamespaceEnd(CodeNamespace ns)
		{
			string name = ns.Name;
			if (name != null && name.Length != 0)
			{
				base.Indent--;
				base.Output.WriteLine("}");
			}
		}
		public void CopyTo(CodeNamespace[] array, int index)
		{
			List.CopyTo(array, index);
		}
		public void Remove(CodeNamespace value)
		{
			List.Remove(value);
		}
Beispiel #17
0
		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);
		}
		public CodeNamespaceCollection(CodeNamespace[] value)
		{
			AddRange(value);
		}
		//
		// Methods
		//
		public int Add(CodeNamespace value)
		{
			return List.Add(value); 
		}
Beispiel #20
0
		public static void WriteEnums(CodeNamespace n)
		{
			foreach (var e in mEntries)
			{
				e.Write(n);
			}
		}
		public bool Contains(CodeNamespace value)
		{
			return List.Contains(value);
		}