use of suite.primitive.Bytes in project suite by stupidsing.
the class Assembler method assemble.
public Bytes assemble(String in0) {
Set<Character> whitespaces = Collections.singleton('\n');
Fun<String, List<Run>> gct = CommentPreprocessor.groupCommentPreprocessor(whitespaces);
Fun<String, List<Run>> lct = CommentPreprocessor.lineCommentPreprocessor(whitespaces);
String in1 = Preprocess.transform(List.of(gct, lct), in0).t0;
Generalizer generalizer = new Generalizer();
List<String> lines = List.of(in1.split("\n"));
Pair<String, String> pe;
int start = 0;
while (!(pe = String_.split2(lines.get(start), "=")).t1.isEmpty()) {
generalizer.getVariable(Atom.of(pe.t0)).bound(Suite.parse(pe.t1));
start++;
}
List<Pair<Reference, Node>> lnis = //
Read.from(//
List_.right(lines, start)).map(line -> {
Pair<String, String> pt = String_.split2(line, "\t");
String label = pt.t0;
String command = pt.t1;
Reference reference = String_.isNotBlank(label) ? generalizer.getVariable(Atom.of(label)) : null;
Node instruction = generalizer.generalize(Suite.parse(command));
return Pair.of(reference, instruction);
}).toList();
return assemble(generalizer, lnis);
}
use of suite.primitive.Bytes in project suite by stupidsing.
the class Amd64Interpret method interpret.
public int interpret(List<Instruction> instructions, Bytes code, Bytes input) {
mem.position(positionCode0);
mem.put(code.bs);
eip = positionCode0;
regs[esp] = baseStackx - 16;
IntIntMap labels = new IntIntMap();
for (int i = 0; i < instructions.size(); i++) {
int i_ = i;
Instruction instruction = instructions.get(i_);
if (instruction.insn == Insn.LABEL)
labels.update((int) ((OpImm) instruction.op0).imm, i0 -> i_ + 1);
}
while (true) {
Instruction instruction = instructions.get(eip++);
if (Boolean.FALSE)
LogUtil.info(state(instruction));
try {
Operand op0 = instruction.op0;
Operand op1 = instruction.op1;
int source0, source1;
IntSink assign;
if (op0 instanceof OpImm) {
source0 = (int) ((OpImm) op0).imm;
assign = null;
} else if (op0 instanceof OpMem) {
int index = index(address((OpMem) op0));
source0 = mem.getInt(index);
assign = i -> mem.putInt(index, i);
} else if (op0 instanceof OpReg) {
int reg = ((OpReg) op0).reg;
source0 = regs[reg];
assign = i -> {
regs[reg] = i;
};
} else {
source0 = 0;
assign = null;
}
if (op1 instanceof OpImm)
source1 = (int) ((OpImm) op1).imm;
else if (op1 instanceof OpMem)
source1 = mem.getInt(index(address((OpMem) op1)));
else if (op1 instanceof OpReg)
source1 = regs[((OpReg) op1).reg];
else
source1 = 0;
switch(instruction.insn) {
case ADD:
assign.sink(source0 + source1);
break;
case CALL:
push(eip);
eip = labels.get(source0);
break;
case CMP:
c = Integer.compare(source0, source1);
break;
case DEC:
assign.sink(source0 - 1);
break;
case INC:
assign.sink(source0 + 1);
break;
case INT:
if (source0 == -128)
if (regs[eax] == 1)
return regs[ebx];
else if (regs[eax] == 3) {
int length = min(regs[edx], input.size());
int di = index(regs[ecx]);
for (int i = 0; i < length; i++) mem.put(di++, input.get(i));
input = input.range(length);
regs[eax] = length;
} else if (regs[eax] == 4) {
int length = regs[edx];
int si = index(regs[ecx]);
byte[] bs = new byte[length];
for (int i = 0; i < length; i++) bs[i] = mem.get(si++);
output.sink(Bytes.of(bs));
} else
Fail.t();
else
Fail.t();
break;
case JE:
if (c == 0)
eip = labels.get(source0);
break;
case JMP:
eip = labels.get(source0);
break;
case JG:
if (0 < c)
eip = labels.get(source0);
break;
case JGE:
if (0 <= c)
eip = labels.get(source0);
break;
case JL:
if (c < 0)
eip = labels.get(source0);
break;
case JLE:
if (c <= 0)
eip = labels.get(source0);
break;
case JNE:
if (c != 0)
eip = labels.get(source0);
break;
case LABEL:
break;
case LEA:
assign.sink(address((OpMem) op1));
break;
case MOV:
assign.sink(source1);
break;
case POP:
assign.sink(pop());
break;
case PUSH:
push(source0);
break;
case RET:
eip = pop();
break;
case SUB:
assign.sink(source0 - source1);
break;
case XOR:
assign.sink(source0 ^ source1);
break;
default:
Fail.t();
}
} catch (Exception ex) {
LogUtil.info(state(instruction));
throw ex;
}
}
}
use of suite.primitive.Bytes in project suite by stupidsing.
the class B_TreeBuilder method build.
private B_Tree<Key, Value> build(Comparator<Key> comparator, PageFile alf0, PageFile sbf0, PageFile pf0) {
B_TreeImpl<Key, Value> b_tree = new B_TreeImpl<>(Object_.nullsFirst(comparator));
Serializer<Bytes> als = serialize.bytes(pageSize);
B_TreeSuperblockSerializer sbs = new B_TreeSuperblockSerializer(b_tree);
Serializer<Bytes> pys = serialize.bytes(pageSize);
B_TreePageSerializer ps = new B_TreePageSerializer(b_tree);
SerializedPageFile<Bytes> alf = SerializedFileFactory.serialized(alf0, als);
SerializedPageFile<B_TreeImpl<Key, Value>.Superblock> sbf = SerializedFileFactory.serialized(sbf0, sbs);
SerializedPageFile<Bytes> pyf = SerializedFileFactory.serialized(pf0, pys);
SerializedPageFile<B_TreeImpl<Key, Value>.Page> pf = SerializedFileFactory.serialized(pf0, ps);
b_tree.setAllocator(new AllocatorImpl(alf));
b_tree.setSuperblockPageFile(sbf);
b_tree.setPayloadFile(pyf);
b_tree.setPageFile(pf);
b_tree.setBranchFactor(16);
return b_tree;
}
use of suite.primitive.Bytes in project suite by stupidsing.
the class JournalledFileFactory method journalled.
private static //
JournalledPageFile journalled(//
PageFile df, //
PageFile jpf, //
PageFile ppf, int pageSize) {
Serializer<Bytes> bytesSerializer = serialize.bytes(pageSize);
Serializer<JournalEntry> journalEntrySerializer = new Serializer<>() {
public JournalEntry read(DataInput_ dataInput) throws IOException {
int pointer = dataInput.readInt();
Bytes bytes = bytesSerializer.read(dataInput);
return new JournalEntry(pointer, bytes);
}
public void write(DataOutput_ dataOutput, JournalEntry journalEntry) throws IOException {
dataOutput.writeInt(journalEntry.pointer);
bytesSerializer.write(dataOutput, journalEntry.bytes);
}
};
PageFile dataFile = df;
SerializedPageFile<JournalEntry> journalPageFile = SerializedFileFactory.serialized(jpf, journalEntrySerializer);
SerializedPageFile<Integer> pointerPageFile = SerializedFileFactory.serialized(ppf, serialize.int_);
int nCommittedJournalEntries0 = pointerPageFile.load(0);
List<JournalEntry> journalEntries = new ArrayList<>();
for (int jp = 0; jp < nCommittedJournalEntries0; jp++) journalEntries.add(journalPageFile.load(jp));
return new JournalledPageFile() {
private int nCommittedJournalEntries = nCommittedJournalEntries0;
public void close() throws IOException {
dataFile.close();
journalPageFile.close();
pointerPageFile.close();
}
public synchronized Bytes load(int pointer) {
IntObjPair<JournalEntry> pair = findPageInJournal(pointer);
if (pair != null)
return pair.t1.bytes;
else
return dataFile.load(pointer);
}
public synchronized void save(int pointer, Bytes bytes) {
IntObjPair<JournalEntry> pair = findDirtyPageInJournal(pointer);
int jp;
JournalEntry journalEntry;
if (pair != null) {
jp = pair.t0;
journalEntry = pair.t1;
} else {
jp = journalEntries.size();
journalEntries.add(journalEntry = new JournalEntry(pointer, null));
}
journalEntry.bytes = bytes;
journalPageFile.save(jp, journalEntry);
}
/**
* Marks a snapshot that data can be recovered to.
*/
public synchronized void commit() {
while (nCommittedJournalEntries < journalEntries.size()) {
JournalEntry journalEntry = journalEntries.get(nCommittedJournalEntries++);
dataFile.save(journalEntry.pointer, journalEntry.bytes);
}
if (8 < nCommittedJournalEntries)
saveJournal();
}
/**
* Makes sure the current snapshot of data is saved and recoverable
* on failure, upon the return of method call.
*/
public synchronized void sync() {
journalPageFile.sync();
saveJournal();
pointerPageFile.sync();
}
private void saveJournal() {
pointerPageFile.save(0, nCommittedJournalEntries);
if (128 < nCommittedJournalEntries)
applyJournal();
}
/**
* Shortens the journal by applying them to page file.
*/
public synchronized void applyJournal() {
// make sure all changes are written to main file
dataFile.sync();
// clear all committed entries
journalEntries.subList(0, nCommittedJournalEntries).clear();
// reset committed pointer
pointerPageFile.save(0, nCommittedJournalEntries = 0);
pointerPageFile.sync();
// write back entries for next commit
for (int jp = 0; jp < journalEntries.size(); jp++) journalPageFile.save(jp, journalEntries.get(jp));
}
private IntObjPair<JournalEntry> findPageInJournal(int pointer) {
return findPageInJournal(pointer, 0);
}
private IntObjPair<JournalEntry> findDirtyPageInJournal(int pointer) {
return findPageInJournal(pointer, nCommittedJournalEntries);
}
private IntObjPair<JournalEntry> findPageInJournal(int pointer, int start) {
IntObjPair<JournalEntry> pair = null;
for (int jp = start; jp < journalEntries.size(); jp++) {
JournalEntry journalEntry = journalEntries.get(jp);
if (journalEntry.pointer == pointer)
pair = IntObjPair.of(jp, journalEntry);
}
return pair;
}
};
}
use of suite.primitive.Bytes in project suite by stupidsing.
the class FileSystemKeySet method increment.
private Bytes increment(Bytes bytes) {
if (!bytes.isEmpty()) {
Bytes bytes1 = bytes.range(0, -1);
byte b1 = (byte) (bytes.get(-1) + 1);
if (b1 != 0)
return Bytes.concat(bytes1, Bytes.of(b1));
else
return Bytes.concat(increment(bytes1), Bytes.of((byte) 0));
} else
return bytes;
}
Aggregations