Initial commit

This commit is contained in:
Dimitri Lajou
2025-03-21 17:26:31 +01:00
commit 7114c0e978
115 changed files with 1760 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
package TP2.llvm;
public record LLVMString(String data, int size) {
}

View File

@@ -0,0 +1,81 @@
package TP2.llvm;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Utils {
private static int tmp = 0;
private static int lab = 0;
private static int glob = 0;
private final static Pattern re = Pattern.compile("\\\\n");
/**
* Generate a new unique local identifier (starting with %tmp)
*
* @return the identifier
*/
public static String newtmp() {
return newVar("tmp");
}
/**
* Generate a new unique local identifier (starting with %) for a variable
*
* @return the identifier
*/
public static String newVar(String prefix) {
tmp++;
return "%" + prefix + tmp;
}
/**
* <p>
* generate a new unique label starting with str
* </p>
*
* ex: newlab("if") may return "if24"
*
* @param str the start of the label
* @return the label name
*/
public static String newlab(String str) {
lab++;
return str + lab;
}
/**
* generate a new unique global identifier (starting with @)
*
* @param str the start of the name of the identifier
* @return the name of the identifier
*/
public static String newglob(String str) {
glob++;
return "@" + str + glob;
}
/**
* Transform escaped newlines ('\' 'n') into newline form suitable for LLVM and
* append the NUL character (end of string)
*
* @param str the input string
* @return a pair: the new String, and its size (according to LLVM)
*/
public static LLVMString stringTransform(String str) {
Matcher m = re.matcher(str);
StringBuffer res = new StringBuffer();
int count = 0;
while (m.find()) {
m.appendReplacement(res, "\\\\0A");
count++;
}
m.appendTail(res).append("\\00");
// + 1 for \00
// - 1 by \n because each ('\' '\n') is transformed into one char
return new LLVMString(res.toString(), 1 + str.length() - count);
}
}