Skip to content

Commit

Permalink
feat: bitwise not operator
Browse files Browse the repository at this point in the history
closes #67
  • Loading branch information
MilkeeyCat committed Sep 20, 2024
1 parent 7566ca3 commit 44240f8
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/archs/amd64/amd64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ impl Architecture for Amd64 {
}
}

fn bitwise_not(&mut self, dest: &Destination) {
self.buf.push_str(&formatdoc!(
"
\tnot {dest}
"
));
}

fn cmp(&mut self, dest: &Destination, src: &Source) {
self.buf.push_str(&formatdoc!(
"
Expand Down
1 change: 1 addition & 0 deletions src/archs/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub trait Architecture: ArchitectureClone {
fn mul(&mut self, dest: &Destination, src: &Source, signed: bool) -> Result<(), ArchError>;
fn div(&mut self, dest: &Destination, src: &Source, signed: bool) -> Result<(), ArchError>;
fn bitwise(&mut self, dest: &Destination, src: &Source, op: BitwiseOp);
fn bitwise_not(&mut self, dest: &Destination);
fn cmp(&mut self, dest: &Destination, src: &Source);
fn setcc(&mut self, dest: &Destination, condition: CmpOp);
fn fn_preamble(
Expand Down
7 changes: 6 additions & 1 deletion src/codegen/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ impl CodeGen {
self.expr(*unary_expr.expr, Some(dest.clone()), state)?;
self.arch.negate(&dest);
}
UnOp::Not => {
UnOp::LogicalNot => {
let type_ = unary_expr.type_(&self.scope)?;
let r = self.arch.alloc()?;

Expand Down Expand Up @@ -599,6 +599,11 @@ impl CodeGen {
unary_expr.expr.type_(&self.scope)?.signed(),
)?;
}
UnOp::BitwiseNot => {
self.expr(*unary_expr.expr, Some(dest.clone()), state)?;

self.arch.bitwise_not(&dest);
}
};

Ok(())
Expand Down
1 change: 1 addition & 0 deletions src/lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl Lexer {
}
}
b'.' => Token::Period,
b'~' => Token::Tilde,
b'&' => {
if self.peek() == b'&' {
self.read_char();
Expand Down
2 changes: 2 additions & 0 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum Token {
Slash,
Arrow,
Period,
Tilde,
Ampersand,
Bar,
Equal,
Expand Down Expand Up @@ -83,6 +84,7 @@ impl Display for Token {
Slash => write!(f, "/"),
Arrow => write!(f, "->"),
Period => write!(f, "."),
Tilde => write!(f, "~"),
Ampersand => write!(f, "&"),
Bar => write!(f, "|"),
Equal => write!(f, "=="),
Expand Down
3 changes: 2 additions & 1 deletion src/parser/expr/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,10 @@ impl Expression for ExprUnary {

expr_type
}
UnOp::Not => Type::Bool,
UnOp::LogicalNot => Type::Bool,
UnOp::Address => Type::Ptr(Box::new(self.expr.type_(scope)?)),
UnOp::Deref => self.expr.type_(scope)?.inner()?,
UnOp::BitwiseNot => self.expr.type_(scope)?,
})
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/parser/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,23 @@ impl TryFrom<&Token> for BinOp {

#[derive(Debug, Clone, PartialEq)]
pub enum UnOp {
Not,
LogicalNot,
Negative,
Address,
Deref,
BitwiseNot,
}

impl TryFrom<&Token> for UnOp {
type Error = OpParseError;

fn try_from(value: &Token) -> Result<Self, Self::Error> {
match value {
Token::Bang => Ok(Self::Not),
Token::Bang => Ok(Self::LogicalNot),
Token::Minus => Ok(Self::Negative),
Token::Ampersand => Ok(Self::Address),
Token::Asterisk => Ok(Self::Deref),
Token::Tilde => Ok(Self::BitwiseNot),
token => Err(OpParseError::Un(token.to_owned())),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl Parser {
(Token::LParen, Self::grouped_expr),
(Token::Ampersand, Self::unary_expr),
(Token::Asterisk, Self::unary_expr),
(Token::Tilde, Self::unary_expr),
(Token::LBracket, Self::array_expr),
]),
infix_fns: HashMap::from([
Expand Down

0 comments on commit 44240f8

Please sign in to comment.