Skip to content

Commit

Permalink
Add instructions cmovl, cmovle, cmovg
Browse files Browse the repository at this point in the history
  • Loading branch information
v420v committed Jul 7, 2023
1 parent 5ec0a97 commit df2ef54
Show file tree
Hide file tree
Showing 6 changed files with 8,052 additions and 7,849 deletions.
9 changes: 9 additions & 0 deletions doc/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ VAS uses AT&T assembly syntax. Some key differences between AT&T syntax and Inte
- [x] `cmovgeq`
- [x] `cmovgel`
- [x] `cmovgew`
- [x] `cmovlq`
- [x] `cmovll`
- [x] `cmovlw`
- [x] `cmovleq`
- [x] `cmovlel`
- [x] `cmovlew`
- [x] `cmovgq`
- [x] `cmovgl`
- [x] `cmovgw`
- [x] `ret`
- [x] `syscall`
- [x] `nop`
Expand Down
9 changes: 9 additions & 0 deletions doc/ドキュメント.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ VAS は AT&T アセンブリ構文を使用しています。AT&T 構文と他
- [x] `cmovgeq`
- [x] `cmovgel`
- [x] `cmovgew`
- [x] `cmovlq`
- [x] `cmovll`
- [x] `cmovlw`
- [x] `cmovleq`
- [x] `cmovlel`
- [x] `cmovlew`
- [x] `cmovgq`
- [x] `cmovgl`
- [x] `cmovgw`
- [x] `ret`
- [x] `syscall`
- [x] `nop`
Expand Down
33 changes: 22 additions & 11 deletions encoder/addr.v
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn (mut e Encoder) change_symbol_visibility(instr Instr, visibility u8) {
fn (mut e Encoder) fix_same_section_relocations() {
for mut rela in e.rela_text_users {
if symbol := e.user_defined_symbols[rela.uses] {
if symbol.section != rela.instr.section {
if symbol.section_name != rela.instr.section_name {
continue
}
if symbol.binding == encoder.stb_global {
Expand All @@ -64,10 +64,15 @@ fn (mut e Encoder) fix_same_section_relocations() {

mut hex := [u8(0), 0, 0, 0]
binary.little_endian_put_u32(mut &hex, u32(num))
e.user_defined_sections[rela.instr.section].code[rela.instr.addr + rela.offset] = hex[0]
e.user_defined_sections[rela.instr.section].code[rela.instr.addr + rela.offset+1] = hex[1]
e.user_defined_sections[rela.instr.section].code[rela.instr.addr + rela.offset+2] = hex[2]
e.user_defined_sections[rela.instr.section].code[rela.instr.addr + rela.offset+3] = hex[3]

mut section := e.user_defined_sections[rela.instr.section_name] or {
panic('this should not happen')
}

section.code[rela.instr.addr + rela.offset] = hex[0]
section.code[rela.instr.addr + rela.offset+1] = hex[1]
section.code[rela.instr.addr + rela.offset+2] = hex[2]
section.code[rela.instr.addr + rela.offset+3] = hex[3]

rela.is_already_resolved = true
}
Expand All @@ -76,30 +81,36 @@ fn (mut e Encoder) fix_same_section_relocations() {

pub fn (mut e Encoder) assign_addresses() {
for mut instr in e.instrs {
if instr.section !in e.user_defined_sections {
e.user_defined_sections[instr.section] = &UserDefinedSection{}
if instr.kind == .section && instr.section_name !in e.user_defined_sections {
e.user_defined_sections[instr.section_name] = &UserDefinedSection{
flags: section_flags(instr.flags)
}
}
mut section := e.user_defined_sections[instr.section] or {

mut section := e.user_defined_sections[instr.section_name] or {
panic('this should not happen')
}

match instr.kind {
.section {
section.flags = section_flags(instr.flags)
}
.global {
e.change_symbol_binding(*instr, encoder.stb_global)
continue
}
.local {
e.change_symbol_binding(*instr, encoder.stb_local)
continue
}
.hidden {
e.change_symbol_visibility(*instr, encoder.stv_hidden)
continue
}
.internal {
e.change_symbol_visibility(*instr, encoder.stv_internal)
continue
}
.protected {
e.change_symbol_visibility(*instr, encoder.stv_protected)
continue
} else {}
}

Expand Down
12 changes: 6 additions & 6 deletions encoder/directive.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import token
import error
import encoding.binary

fn (mut e Encoder) add_section(name string, flag string, pos token.Position) {
e.current_section = name
fn (mut e Encoder) add_section(section_name string, flag string, pos token.Position) {
e.current_section_name = section_name

instr := Instr{kind: .section, pos: pos, section: name, symbol_type: encoder.stt_section, flags: flag}
instr := Instr{kind: .section, pos: pos, section_name: section_name, symbol_type: encoder.stt_section, flags: flag}
e.instrs << &instr

if s := e.user_defined_symbols[name] {
if s := e.user_defined_symbols[section_name] {
if s.kind == .label {
error.print(pos, 'symbol `$name` is already defined')
error.print(pos, 'symbol `$section_name` is already defined')
exit(1)
}
} else {
e.user_defined_symbols[name] = &instr
e.user_defined_symbols[section_name] = &instr
}
}

Expand Down
52 changes: 32 additions & 20 deletions encoder/encoder.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ mut:
tok token.Token // current token
l lexer.Lexer // lexer
pub mut:
current_section string
current_section_name string
current_instr &Instr
instrs []&Instr
instrs []&Instr // All instructions, sections, symbols, directives
rela_text_users []Rela
user_defined_symbols map[string]&Instr
user_defined_sections map[string]&UserDefinedSection
Expand Down Expand Up @@ -100,7 +100,10 @@ pub enum InstrKind {
leave
cmovs
cmovns
cmovg
cmovge
cmovl
cmovle
cvttss2sil
cvtsi2ssq
cvtsi2sdq
Expand Down Expand Up @@ -198,7 +201,7 @@ pub mut:
binding u8
visibility u8 // STV_DEFAULT, STV_INTERNAL, STV_HIDDEN, STV_PROTECTED
symbol_type u8
section string [required]
section_name string [required]
is_jmp_or_call bool
pos token.Position [required]
}
Expand Down Expand Up @@ -331,7 +334,7 @@ pub fn new(mut l lexer.Lexer, file_name string) &Encoder {
mut e := &Encoder{
tok: tok
l: l
current_section: '.text'
current_section_name: '.text'
instrs: []&Instr{cap: 1500000}
current_instr: unsafe { nil }
}
Expand All @@ -345,7 +348,7 @@ fn (mut e Encoder) set_current_instr(kind InstrKind) {
instr := &Instr{
pos: e.tok.pos
kind: kind
section: e.current_section
section_name: e.current_section_name
}
e.current_instr = instr
e.instrs << instr
Expand Down Expand Up @@ -651,7 +654,7 @@ fn (mut e Encoder) encode_instr() {
instr := Instr{
kind: .label
pos: pos
section: e.current_section
section_name: e.current_section_name
symbol_name: instr_name
}
e.expect(.colon)
Expand Down Expand Up @@ -683,7 +686,7 @@ fn (mut e Encoder) encode_instr() {
e.instrs << &Instr{
kind: .global
pos: pos
section: e.current_section
section_name: e.current_section_name
symbol_name: e.tok.lit
}
e.next()
Expand All @@ -692,7 +695,7 @@ fn (mut e Encoder) encode_instr() {
e.instrs << &Instr{
kind: .local
pos: pos
section: e.current_section
section_name: e.current_section_name
symbol_name: e.tok.lit
}
e.next()
Expand All @@ -701,7 +704,7 @@ fn (mut e Encoder) encode_instr() {
e.instrs << &Instr{
kind: .hidden
pos: pos
section: e.current_section
section_name: e.current_section_name
symbol_name: e.tok.lit
}
e.next()
Expand All @@ -710,7 +713,7 @@ fn (mut e Encoder) encode_instr() {
e.instrs << &Instr{
kind: .internal
pos: pos
section: e.current_section
section_name: e.current_section_name
symbol_name: e.tok.lit
}
e.next()
Expand All @@ -719,7 +722,7 @@ fn (mut e Encoder) encode_instr() {
e.instrs << &Instr{
kind: .protected
pos: pos
section: e.current_section
section_name: e.current_section_name
symbol_name: e.tok.lit
}
e.next()
Expand Down Expand Up @@ -1021,78 +1024,87 @@ fn (mut e Encoder) encode_instr() {
'CMOVNSQ', 'CMOVNSL', 'CMOVNSW' {
e.cmov(.cmovns, [u8(0x0F), 0x49], get_size_by_suffix(instr_name_upper))
}
'CMOVLQ', 'CMOVLL', 'CMOVLW' {
e.cmov(.cmovl, [u8(0x0F), 0x4C], get_size_by_suffix(instr_name_upper))
}
'CMOVGEQ', 'CMOVGEL', 'CMOVGEW' {
e.cmov(.cmovge, [u8(0x0F), 0x4D], get_size_by_suffix(instr_name_upper))
}
'CMOVLEQ', 'CMOVLEL', 'CMOVLEW' {
e.cmov(.cmovle, [u8(0x0F), 0x4E], get_size_by_suffix(instr_name_upper))
}
'CMOVGQ', 'CMOVGL', 'CMOVGW' {
e.cmov(.cmovg, [u8(0x0F), 0x4F], get_size_by_suffix(instr_name_upper))
}
'RETQ', 'RET' {
e.instrs << &Instr{
kind: .ret
pos: pos
section: e.current_section
section_name: e.current_section_name
code: [u8(0xc3)]
}
}
'SYSCALL' {
e.instrs << &Instr{
kind: .syscall
pos: pos
section: e.current_section
section_name: e.current_section_name
code: [u8(0x0f), 0x05]
}
}
'NOPQ', 'NOP' {
e.instrs << &Instr{
kind: .nop
pos: pos
section: e.current_section
section_name: e.current_section_name
code: [u8(0x90)]
}
}
'HLT' {
e.instrs << &Instr{
kind: .hlt
pos: pos
section: e.current_section
section_name: e.current_section_name
code: [u8(0xf4)]
}
}
'LEAVE' {
e.instrs << &Instr{
kind: .leave
pos: pos
section: e.current_section
section_name: e.current_section_name
code: [u8(0xc9)]
}
}
'CLTQ' {
e.instrs << &Instr{
kind: .cltq
pos: pos
section: e.current_section
section_name: e.current_section_name
code: [u8(0x48), 0x98]
}
}
'CLTD' {
e.instrs << &Instr{
kind: .cltd
pos: pos
section: e.current_section
section_name: e.current_section_name
code: [u8(0x99)]
}
}
'CQTO' {
e.instrs << &Instr{
kind: .cqto
pos: pos
section: e.current_section
section_name: e.current_section_name
code: [u8(0x48), 0x99]
}
}
'CWTL' {
e.instrs << &Instr{
kind: .cwtl
pos: pos
section: e.current_section
section_name: e.current_section_name
code: [u8(0x98)]
}
}
Expand Down
Loading

0 comments on commit df2ef54

Please sign in to comment.