Skip to content

Commit

Permalink
cilkcc: Fix bug. Add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
maekawatoshiki committed Oct 20, 2020
1 parent db4193f commit 4b1c197
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
12 changes: 12 additions & 0 deletions cilkcc/examples/e_counter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "assert.h"

int main() {
char *s = "the quick brown fox jumps over the lazy dog", *p = s;
int i = 0;
while (*p != 0) {
if (*p == 'e') i += 1;
p += 1;
}
assert(i == 3);
return 0;
}
19 changes: 19 additions & 0 deletions cilkcc/examples/strstr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "assert.h"

int find(char *s, char *q) {
int s_i = 0, q_i = 0;
while (s[s_i] != 0) {
if (q[q_i] == 0) return s_i-q_i;
if (s[s_i] == q[q_i]) q_i += 1;
else if (q_i > 0) q_i = 0;
s_i += 1;
}
return 0 - 1;
}

int main() {
char *s = "the quick brown fox jumps over the lazy dog";
int p = find(s, "brown");
assert(p == 10);
return 0;
}
8 changes: 8 additions & 0 deletions cilkcc/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ impl<'a> FunctionCodeGenerator<'a> {
Ok((Value::new_imm_int32(*n as i32), Type::Int(Sign::Signed)))
}
ast::Kind::String(s) => self.generate_string(s),
ast::Kind::Char(c) => Ok((
Value::new_imm_int32(*c as u8 as i32),
Type::Int(Sign::Signed),
)),
ast::Kind::ConstArray(ty, elems) => self.generate_const_array(ty, elems),
ast::Kind::FieldRef(val, name) => self.generate_field_ref(val, name),
ast::Kind::VariableDecl(ty, name, sclass, val) => {
Expand Down Expand Up @@ -587,6 +591,10 @@ impl<'a> FunctionCodeGenerator<'a> {
self.builder.build_icmp(ICmpKind::Eq, lhs, rhs),
Type::Int(Sign::Signed),
)),
ast::BinaryOp::Ne => Ok((
self.builder.build_icmp(ICmpKind::Ne, lhs, rhs),
Type::Int(Sign::Signed),
)),
ast::BinaryOp::Le => Ok((
self.builder.build_icmp(ICmpKind::Le, lhs, rhs),
Type::Int(Sign::Signed),
Expand Down
2 changes: 1 addition & 1 deletion cilkcc/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ impl<'a> Parser<'a> {
let mut lhs = self.read_relation()?;
loop {
if lexer!(self, skip_symbol(Symbol::Eq)) {
let rhs = self.read_primary()?;
let rhs = self.read_relation()?;
lhs = AST::new(
ast::Kind::BinaryOp(ast::BinaryOp::Eq, Box::new(lhs), Box::new(rhs)),
self.lexer.loc(),
Expand Down
1 change: 1 addition & 0 deletions src/codegen/common/machine/branch_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ impl BranchFolding {
}

if inst.opcode.is_conditional_jmp() {
println!("{:?}", inst);
let dst = inst.get_jmp_dst().unwrap();
// If we jump to the next block conditionally, flip the jump condition.
if i + 1 < order.len() && dst == order[i + 1] {
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/x64/machine/inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl MachineOpcode {
pub fn flip_conditional_jmp(&self) -> Option<Self> {
match self {
MachineOpcode::JE => Some(Self::JNE),
MachineOpcode::JNE => Some(Self::JE),
MachineOpcode::JL => Some(Self::JGE),
MachineOpcode::JLE => Some(Self::JG),
MachineOpcode::JG => Some(Self::JLE),
Expand All @@ -97,6 +98,7 @@ impl MachineInst {
match self.opcode {
MachineOpcode::JMP
| MachineOpcode::JE
| MachineOpcode::JNE
| MachineOpcode::JL
| MachineOpcode::JLE
| MachineOpcode::JG
Expand Down

0 comments on commit 4b1c197

Please sign in to comment.