<commit_before>/**
 * @author kos
 */
package compiler.phases.finalphase;

import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.util.Vector;

import compiler.Main;
import compiler.common.report.Report;
import compiler.data.asmcode.AsmInstr;
import compiler.data.asmcode.AsmLABEL;
import compiler.data.asmcode.AsmOPER;
import compiler.data.asmcode.Code;
import compiler.data.chunk.DataChunk;
import compiler.data.layout.Frame;
import compiler.data.layout.Label;
import compiler.data.layout.Temp;
import compiler.phases.*;
import compiler.phases.asmcode.AsmGen;
import compiler.phases.chunks.Chunks;

public class FinalPhase extends Phase {

	private final String kJump = "JMP ";
	private final String kSetConstPrePendix = "SET $0,";
	private final String kINCLow = "INCML $0,";
	private final String kINCMidHigh = "INCMH $0,";
	private final String kINCHigh = "INCH $0,";
	private final String kFP = "FP";// "$253";
	private final String kSP = "SP";// "$254";

	public FinalPhase() {
		super("final");
	}

	private Vector<String> AsmInstructionToString(Vector<AsmInstr> instructions) {
		Vector<String> strings = new Vector<>();

		for (AsmInstr instr : instructions) {
			if (instr instanceof AsmLABEL) {
				strings.add(instr.toAsemblerCode(null));
			} else {
				strings.add(instr.toAsemblerCode(null) + '\n');
			}
		}

		return strings;
	}

	private Vector<String> CreatePutChar() {
		Vector<String> putCharCode = new Vector<>();
		Label putCharLabel = new Label("putChar");
		Label entryLabel = new Label();
		Label exitLabel = new Label();
		Code code = new Code(new Frame(putCharLabel, 0, 0, 8), entryLabel, exitLabel, new Vector<>(), null, 0);

		putCharCode.add("% Code for function: _putChar\n");
		putCharCode.addAll(AsmInstructionToString(CreateProlog(code)));

		putCharCode.add(entryLabel.name + "\tSET\t$0,14\n");
		putCharCode.add("\tADD\t$0,FP,$0\n");

		putCharCode.add("\t%Putting char one position in front\n");
		putCharCode.add("\t%so that we put end char at the end\n");
		putCharCode.add("\tLDB\t$1,$0,1\n");
		putCharCode.add("\tSTB\t$1,$0,0\n");

		putCharCode.add("\tSET\t$1,0\n");
		putCharCode.add("\tSTB\t$1,$0,1\n");

		putCharCode.add("\tSET\t$255,$0\n");
		putCharCode.add("\tTRAP\t0,Fputs,StdOut\n");

		putCharCode.addAll(AsmInstructionToString(CreateEpilogue(code)));

		return putCharCode;
	}

	private Vector<String> CreatePutString() {
		Vector<String> putStringCode = new Vector<>();
		Label putStringLabel = new Label("putString");
		Label entryLabel = new Label();
		Label exitLabel = new Label();
		Code code = new Code(new Frame(putStringLabel, 0, 0, 8), entryLabel, exitLabel, new Vector<>(), null, 0);

		putStringCode.add("% Code for function: _putString\n");
		putStringCode.addAll(AsmInstructionToString(CreateProlog(code)));

		putStringCode.add(entryLabel.name + "\tSET\t$0,8\n");
		putStringCode.add("\tADD\t$0,FP,$0\n");
		putStringCode.add("\tLDO\t$1,$0,0\n");
		putStringCode.add("\tSET\t$255,$1\n");
		putStringCode.add("\tTRAP\t0,Fputs,StdOut\n");

		putStringCode.addAll(AsmInstructionToString(CreateEpilogue(code)));

		return putStringCode;
	}

	private Vector<String> GetFunctionLocalVariableAddress(int offset) {
		Vector<String> varCode = new Vector<>();

		varCode.add("\tSET\t$0,FP\n");
		varCode.add("\tSET\t$1," + offset + "\n");
		varCode.add("\tNEG\t$1,0,$1\n");
		varCode.add("\tADD\t$0,$0,$1\n");

		return varCode;
	}

	private Vector<String> GetFunctionArgVariableAddress(int offset) {
		Vector<String> varCode = new Vector<>();

		varCode.add("\tSET\t$0,FP\n");
		varCode.add("\tSET\t$1," + offset + "\n");
		varCode.add("\tADD\t$0,$0,$1\n");

		return varCode;
	}

	private Vector<String> CompareWith(int comparator) {
		Vector<String> varCode = new Vector<>();
		varCode.add("\tSET\t$1," + comparator + "\n");
		varCode.add("\tCMP\t$0,$0,$1\n");
		varCode.add("\tZSP\t$0,$0,1\n");

		return varCode;

	}

	private Vector<String> CreatePutInt() {
		Vector<String> putIntCode = new Vector<>();
		Label putIntLabel = new Label("putInt");
		Label entryLabel = new Label();
		Label exitLabel = new Label();
		Code code = new Code(new Frame(putIntLabel, 1, 16, 16), entryLabel, exitLabel, new Vector<>(), null, 0);

		putIntCode.add("% Code for function: _putInt\n");
		putIntCode.addAll(AsmInstructionToString(CreateProlog(code)));

		putIntCode.add("% Storing inverse number\n");
		putIntCode.add(entryLabel.name);
		putIntCode.addAll(GetFunctionLocalVariableAddress(16));
		putIntCode.add("\tSET\t$1,0\n");
		putIntCode.add("\tSTO\t$1,$0,0\n");
<commit_msg>PREV - Final: Optimization when getting local and argument variables<commit_after>/**
 * @author kos
 */
package compiler.phases.finalphase;

import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.util.Vector;

import compiler.Main;
import compiler.common.report.Report;
import compiler.data.asmcode.AsmInstr;
import compiler.data.asmcode.AsmLABEL;
import compiler.data.asmcode.AsmOPER;
import compiler.data.asmcode.Code;
import compiler.data.chunk.DataChunk;
import compiler.data.layout.Frame;
import compiler.data.layout.Label;
import compiler.data.layout.Temp;
import compiler.phases.*;
import compiler.phases.asmcode.AsmGen;
import compiler.phases.chunks.Chunks;

public class FinalPhase extends Phase {

	private final String kJump = "JMP ";
	private final String kSetConstPrePendix = "SET $0,";
	private final String kINCLow = "INCML $0,";
	private final String kINCMidHigh = "INCMH $0,";
	private final String kINCHigh = "INCH $0,";
	private final String kFP = "FP";// "$253";
	private final String kSP = "SP";// "$254";

	public FinalPhase() {
		super("final");
	}

	private Vector<String> AsmInstructionToString(Vector<AsmInstr> instructions) {
		Vector<String> strings = new Vector<>();

		for (AsmInstr instr : instructions) {
			if (instr instanceof AsmLABEL) {
				strings.add(instr.toAsemblerCode(null));
			} else {
				strings.add(instr.toAsemblerCode(null) + '\n');
			}
		}

		return strings;
	}

	private Vector<String> CreatePutChar() {
		Vector<String> putCharCode = new Vector<>();
		Label putCharLabel = new Label("putChar");
		Label entryLabel = new Label();
		Label exitLabel = new Label();
		Code code = new Code(new Frame(putCharLabel, 0, 0, 8), entryLabel, exitLabel, new Vector<>(), null, 0);

		putCharCode.add("% Code for function: _putChar\n");
		putCharCode.addAll(AsmInstructionToString(CreateProlog(code)));

		putCharCode.add(entryLabel.name + "\tSET\t$0,14\n");
		putCharCode.add("\tADD\t$0,FP,$0\n");

		putCharCode.add("\t%Putting char one position in front\n");
		putCharCode.add("\t%so that we put end char at the end\n");
		putCharCode.add("\tLDB\t$1,$0,1\n");
		putCharCode.add("\tSTB\t$1,$0,0\n");

		putCharCode.add("\tSET\t$1,0\n");
		putCharCode.add("\tSTB\t$1,$0,1\n");

		putCharCode.add("\tSET\t$255,$0\n");
		putCharCode.add("\tTRAP\t0,Fputs,StdOut\n");

		putCharCode.addAll(AsmInstructionToString(CreateEpilogue(code)));

		return putCharCode;
	}

	private Vector<String> CreatePutString() {
		Vector<String> putStringCode = new Vector<>();
		Label putStringLabel = new Label("putString");
		Label entryLabel = new Label();
		Label exitLabel = new Label();
		Code code = new Code(new Frame(putStringLabel, 0, 0, 8), entryLabel, exitLabel, new Vector<>(), null, 0);

		putStringCode.add("% Code for function: _putString\n");
		putStringCode.addAll(AsmInstructionToString(CreateProlog(code)));

		putStringCode.add(entryLabel.name + "\tSET\t$0,8\n");
		putStringCode.add("\tADD\t$0,FP,$0\n");
		putStringCode.add("\tLDO\t$1,$0,0\n");
		putStringCode.add("\tSET\t$255,$1\n");
		putStringCode.add("\tTRAP\t0,Fputs,StdOut\n");

		putStringCode.addAll(AsmInstructionToString(CreateEpilogue(code)));

		return putStringCode;
	}

	private Vector<String> GetFunctionLocalVariableAddress(int offset) {
		Vector<String> varCode = new Vector<>();

		varCode.add("\tSET\t$0," + offset + "\n");
		varCode.add("\tSUB\t$0,FP,$0\n");

		return varCode;
	}

	private Vector<String> GetFunctionArgVariableAddress(int offset) {
		Vector<String> varCode = new Vector<>();

		varCode.add("\tSET\t$0," + offset + "\n");
		varCode.add("\tADD\t$0,$0,FP\n");

		return varCode;
	}

	private Vector<String> CompareWith(int comparator) {
		Vector<String> varCode = new Vector<>();
		varCode.add("\tSET\t$1," + comparator + "\n");
		varCode.add("\tCMP\t$0,$0,$1\n");
		varCode.add("\tZSP\t$0,$0,1\n");

		return varCode;

	}

	private Vector<String> CreatePutInt() {
		Vector<String> putIntCode = new Vector<>();
		Label putIntLabel = new Label("putInt");
		Label entryLabel = new Label();
		Label exitLabel = new Label();
		Code code = new Code(new Frame(putIntLabel, 1, 16, 16), entryLabel, exitLabel, new Vector<>(), null, 0);

		putIntCode.add("% Code for function: _putInt\n");
		putIntCode.addAll(AsmInstructionToString(CreateProlog(code)));

		putIntCode.add("% Storing inverse number\n");
		putIntCode.add(entryLabel.name);
		putIntCode.addAll(GetFunctionLocalVariableAddress(16));
		putIntCode.add("\tSET\t$1,0\n");
		putIntCode.add("\tSTO\t$1,$0,0\n");

{'fraction_non_alphanumeric': 0.16874180865006552, 'fraction_numerical': 0.017584097859327217, 'mean_word_length': 4.992801047120419, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4329769', 'n_tokens_mistral': 3501, 'n_tokens_neox': 3235, 'n_words': 643}