/* ###
 * IP: GHIDRA
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ghidra.app.util.bin.format.dwarf;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jdom.*;
import org.jdom.input.SAXBuilder;

import generic.jar.ResourceFile;
import ghidra.program.model.lang.*;
import ghidra.util.Msg;
import ghidra.util.xml.XmlUtilities;

/**
 * Factory class to instantiate and cache {@link DWARFRegisterMappings} objects.
 */
public class DWARFRegisterMappingsManager {
	private static final String DWARF_REGISTER_MAPPING_NAME = "DWARF.register.mapping.file";

	private static Map<LanguageID, DWARFRegisterMappings> cache = new HashMap<>();

	/**
	 * Returns true if the specified {@link Language} has DWARF register
	 * mappings.
	 *
	 * @param lang The {@link Language} to test
	 * @return true if the language has a DWARF register mapping specified
	 * @throws IOException if there was an error in the language LDEF file.
	 */
	public static boolean hasDWARFRegisterMapping(Language lang) throws IOException {
		return DWARFUtil.getLanguageExternalFile(lang, DWARF_REGISTER_MAPPING_NAME) != null;
	}

	/**
	 * Returns a possibly cached {@link DWARFRegisterMappings} object for the
	 * specified language,
	 * 
	 * @param lang {@link Language} to get the matching DWARF register mappings
	 *            for
	 * @return {@link DWARFRegisterMappings} instance, never null
	 * @throws IOException if mapping not found or invalid
	 */
	public static synchronized DWARFRegisterMappings getMappingForLang(Language lang)
			throws IOException {
		DWARFRegisterMappings result = cache.get(lang.getLanguageID());
		if (result == null) {
			result = readMappingForLang(lang);
			cache.put(lang.getLanguageID(), result);
		}
		return result;
	}

	/**
	 * Finds the DWARF register mapping information file specified in the
	 * specified language's LDEF file and returns a new
	 * {@link DWARFRegisterMappings} object containing the data read from that
	 * file.
	 * <p>
	 * Throws {@link IOException} if the lang does not have a mapping or it is
	 * invalid.
	 * 
	 * @param lang {@link Language} to read the matching DWARF register mappings
	 *            for
	 * @return a new {@link DWARFRegisterMappings} instance, created from
	 *         information read from the {@link #DWARF_REGISTER_MAPPING_NAME}
	 *         xml file referenced in the language's LDEF, never null.
	 * @throws IOException if there is no DWARF register mapping file associated
	 *             with the specified {@link Language} or if there was an error
	 *             in the register mapping data.
	 */
	public static DWARFRegisterMappings readMappingForLang(Language lang) throws IOException {
		ResourceFile dwarfFile =
			DWARFUtil.getLanguageExternalFile(lang, DWARF_REGISTER_MAPPING_NAME);
		if (dwarfFile == null) {
			throw new IOException("Missing DWARF register mapping file for language " +
				lang.getLanguageID().getIdAsString());
		}
		if (!dwarfFile.exists()) {
			throw new IOException("Missing DWARF register mapping file " + dwarfFile +
				" for language " + lang.getLanguageID().getIdAsString());
		}

		SAXBuilder sax = XmlUtilities.createSecureSAXBuilder(false, false);
		try (InputStream fis = dwarfFile.getInputStream()) {
			Document doc = sax.build(fis);
			Element rootElem = doc.getRootElement();
			return readMappingFrom(rootElem, lang);
		}
		catch (JDOMException | IOException e) {
			Msg.error(DWARFRegisterMappingsManager.class,
				"Bad DWARF register mapping file " + dwarfFile, e);
			throw new IOException("Failed to read DWARF register mapping file " + dwarfFile, e);
		}
	}

	/**
	 * Creates a new {@link DWARFRegisterMappings} from the data present in the
	 * xml element.
	 * 
	 * @param rootElem JDom XML element containing the &lt;dwarf&gt; root
	 *            element of the mapping file.
	 * @param lang The Ghidra {@link Language} that the DWARF register mapping
	 *            applies to
	 * @return a new {@link DWARFRegisterMappings} instance, never null.
	 * @throws IOException if missing or invalid data found in xml
	 */
	public static DWARFRegisterMappings readMappingFrom(Element rootElem, Language lang)
			throws IOException {

		Element regMappingsElem = rootElem.getChild("register_mappings");
		if (regMappingsElem == null) {
			throw new IOException("Missing required DWARF <register_mappings> element");
		}

		Map<Integer, Register> regmap = new HashMap<>();
		int spi;
		Integer cfa = null; // null == not set
		Register stackFrameRegister = null;
		int stackFrameRegisterOffset = 0;
		boolean useFPS;
		try {
			spi = readMappingsElem(regMappingsElem, lang, regmap);
			Element callFrameElem = rootElem.getChild("call_frame_cfa");
			if (callFrameElem != null) {
				cfa = XmlUtilities.parseOptionalBoundedIntAttr(callFrameElem, "value", 0, 0,
					Integer.MAX_VALUE);
			}

			Element stackFrameElem = rootElem.getChild("stack_frame");
			if (stackFrameElem != null) {
				String stackFrameRegisterName =
					stackFrameElem.getAttributeValue("register");
				stackFrameRegister =
					stackFrameRegisterName != null && !stackFrameRegisterName.isEmpty()
							? lang.getRegister(stackFrameRegisterName)
							: null;
				if (stackFrameRegister != null) {
					stackFrameRegisterOffset = XmlUtilities.parseBoundedIntAttr(stackFrameElem,
						"offset", Integer.MIN_VALUE, Integer.MAX_VALUE);
				}
			}

			Element useFormalParameterStorageElem =
				rootElem.getChild("use_formal_parameter_storage");
			useFPS = (useFormalParameterStorageElem != null);
		}
		catch (NumberFormatException nfe) {
			throw new IOException("Failed to parse DWARF register mappings: " + nfe.getMessage(),
				nfe);
		}

		return new DWARFRegisterMappings(regmap, cfa, spi, stackFrameRegister,
			stackFrameRegisterOffset, useFPS);
	}

	/*
	 * Reads and populates map of dwarf reg numbers to ghidra register objects, and returns
	 * the index of the dwarf stack pointer register.
	 */
	@SuppressWarnings("unchecked")
	private static int readMappingsElem(Element regMappingsElem, Language lang,
			Map<Integer, Register> dwarfRegisterMap) throws IOException {

		int stackPointerIndex = -1;

		for (Element regMappingElem : (List<Element>) regMappingsElem.getChildren(
			"register_mapping")) {

			int dwarfRegNum =
				XmlUtilities.parseBoundedIntAttr(regMappingElem, "dwarf", 0, Integer.MAX_VALUE);
			String ghidraRegName = XmlUtilities.requireStringAttr(regMappingElem, "ghidra");
			boolean stackPointer =
				XmlUtilities.parseOptionalBooleanAttr(regMappingElem, "stackpointer", false);
			int autoincCount = XmlUtilities.parseOptionalBoundedIntAttr(regMappingElem,
				"auto_count", -1, 0, Integer.MAX_VALUE);
			if (autoincCount > 0) {
				Pattern regNamePattern = Pattern.compile("([a-zA-Z]+)([0-9]+)");
				Matcher m = regNamePattern.matcher(ghidraRegName);
				if (!m.matches()) {
					throw new IOException("Unsupported register name for auto-increment: " +
						ghidraRegName + ", " + XmlUtilities.toString(regMappingElem));
				}
				String baseGhidraRegName = m.group(1);
				int baseGhidraRegNum = Integer.parseInt(m.group(2));
				for (int autoIncNum = 0; autoIncNum < autoincCount; autoIncNum++) {
					String autoIncRegName =
						baseGhidraRegName + Integer.toString(baseGhidraRegNum + autoIncNum);
					Register autoIncReg = lang.getRegister(autoIncRegName);
					if (autoIncReg == null) {
						throw new IOException("Unknown Ghidra auto-increment register: " +
							autoIncRegName + ", " + XmlUtilities.toString(regMappingElem) +
							" for " + lang.getLanguageID());
					}
					int actualDwarfRegNum = dwarfRegNum + autoIncNum;
					if (dwarfRegisterMap.containsKey(actualDwarfRegNum)) {
						throw new IOException("Duplicate mapping for DWARF register " +
							actualDwarfRegNum + ": " + XmlUtilities.toString(regMappingElem));
					}
					dwarfRegisterMap.put(actualDwarfRegNum, autoIncReg);
				}
			}
			else {
				Register reg = lang.getRegister(ghidraRegName);
				if (reg == null) {
					throw new IOException("Unknown Ghidra register: " + ghidraRegName + ", " +
						XmlUtilities.toString(regMappingElem));
				}
				if (dwarfRegisterMap.containsKey(dwarfRegNum)) {
					throw new IOException("Duplicate mapping for DWARF register " + dwarfRegNum +
						": " + XmlUtilities.toString(regMappingElem));
				}

				dwarfRegisterMap.put(dwarfRegNum, reg);
				if (stackPointer) {
					stackPointerIndex = dwarfRegNum;
				}
			}
		}

		if (stackPointerIndex < 0) {
			throw new IOException("Missing stackpointer specification in registermappings");
		}

		return stackPointerIndex;
	}

}
